blob: 5cd08532c48088888c99fbf1aba4cc2e871eedb7 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau81f9aa32010-06-01 17:45:26 +02002 * Session management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreau81f9aa32010-06-01 17:45:26 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <stdlib.h>
Willy Tarreau81f9aa32010-06-01 17:45:26 +020014#include <unistd.h>
15#include <fcntl.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020018#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010022#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020024#include <proto/acl.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010025#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020026#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010027#include <proto/checks.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020028#include <proto/dumpstats.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020029#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020030#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010031#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020032#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010034#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010035#include <proto/proto_http.h>
36#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020037#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010039#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010040#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010041#include <proto/stream_interface.h>
42#include <proto/stream_sock.h>
43#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020045struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010046struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau81f9aa32010-06-01 17:45:26 +020048/* This function is called from the protocol layer accept() in order to instanciate
49 * a new session on behalf of a given listener and frontend. It returns a positive
Willy Tarreauabe8ea52010-11-11 10:56:04 +010050 * value upon success, 0 if the connection can be ignored, or a negative value upon
51 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020052 */
53int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
54{
55 struct proxy *p = l->frontend;
56 struct session *s;
57 struct http_txn *txn;
58 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010059 int ret;
60
61
62 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020063
Willy Tarreaufffe1322010-11-11 09:48:16 +010064 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020065 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020066
67 /* minimum session initialization required for monitor mode below */
68 s->flags = 0;
69 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020070 s->stkctr1_entry = NULL;
71 s->stkctr2_entry = NULL;
72 s->stkctr1_table = NULL;
73 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020074
75 /* if this session comes from a known monitoring system, we want to ignore
76 * it as soon as possible, which means closing it immediately for TCP, but
77 * cleanly.
78 */
79 if (unlikely((l->options & LI_O_CHK_MONNET) &&
80 addr->ss_family == AF_INET &&
81 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
82 if (p->mode == PR_MODE_TCP) {
Willy Tarreauabe8ea52010-11-11 10:56:04 +010083 ret = 0; /* successful termination */
84 goto out_free_session;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020085 }
86 s->flags |= SN_MONITOR;
87 s->logs.logwait = 0;
88 }
89
Willy Tarreauabe8ea52010-11-11 10:56:04 +010090 if (unlikely((t = task_new()) == NULL))
91 goto out_free_session;
92
Willy Tarreau81f9aa32010-06-01 17:45:26 +020093 /* OK, we're keeping the session, so let's properly initialize the session */
94 LIST_ADDQ(&sessions, &s->list);
95 LIST_INIT(&s->back_refs);
96
Willy Tarreau81f9aa32010-06-01 17:45:26 +020097 s->term_trace = 0;
Willy Tarreau957c0a52011-03-03 17:42:23 +010098 s->si[0].addr.c.from = *addr;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020099 s->logs.accept_date = date; /* user-visible date for logging */
100 s->logs.tv_accept = now; /* corrected date for internal use */
101 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200102 p->feconn++; /* beconn will be increased once assigned */
103
Willy Tarreaub36b4242010-06-04 20:59:39 +0200104 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200105
106 t->process = l->handler;
107 t->context = s;
108 t->nice = l->nice;
109 t->expire = TICK_ETERNITY;
110
111 s->task = t;
112 s->listener = l;
113
114 /* Note: initially, the session's backend points to the frontend.
115 * This changes later when switching rules are executed or
116 * when the default backend is assigned.
117 */
118 s->be = s->fe = p;
119 s->req = s->rep = NULL; /* will be allocated later */
120
121 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
122 * to abort right here as soon as possible, we check the rules before
123 * even initializing the stream interfaces.
124 */
125 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200126 /* let's do a no-linger now to close with a single RST. */
127 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100128 ret = 0; /* successful termination */
129 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200130 }
131
Willy Tarreaub36b4242010-06-04 20:59:39 +0200132 /* This session was accepted, count it now */
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200133 if (p->feconn > p->counters.feconn_max)
134 p->counters.feconn_max = p->feconn;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100135
Willy Tarreaub36b4242010-06-04 20:59:39 +0200136 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200137 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200138 void *ptr;
139
Willy Tarreau56123282010-08-06 19:06:56 +0200140 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200141 if (ptr)
142 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200143
Willy Tarreau56123282010-08-06 19:06:56 +0200144 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200145 if (ptr)
146 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200147 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200148 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200149
Willy Tarreau56123282010-08-06 19:06:56 +0200150 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200151 void *ptr;
152
Willy Tarreau56123282010-08-06 19:06:56 +0200153 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200154 if (ptr)
155 stktable_data_cast(ptr, sess_cnt)++;
156
Willy Tarreau56123282010-08-06 19:06:56 +0200157 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200158 if (ptr)
159 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200160 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200161 }
162
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200163 /* this part should be common with other protocols */
164 s->si[0].fd = cfd;
165 s->si[0].owner = t;
166 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
167 s->si[0].err_type = SI_ET_NONE;
168 s->si[0].err_loc = NULL;
169 s->si[0].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200170 s->si[0].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100171 clear_target(&s->si[0].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200172 s->si[0].exp = TICK_ETERNITY;
173 s->si[0].flags = SI_FL_NONE;
174
175 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
176 s->si[0].flags |= SI_FL_INDEP_STR;
177
178 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
179 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
180
181 /* add the various callbacks */
182 stream_sock_prepare_interface(&s->si[0]);
183
184 /* pre-initialize the other side's stream interface to an INIT state. The
185 * callbacks will be initialized before attempting to connect.
186 */
187 s->si[1].fd = -1; /* just to help with debugging */
188 s->si[1].owner = t;
189 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
190 s->si[1].err_type = SI_ET_NONE;
191 s->si[1].err_loc = NULL;
192 s->si[1].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200193 s->si[1].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100194 clear_target(&s->si[1].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200195 s->si[1].shutr = stream_int_shutr;
196 s->si[1].shutw = stream_int_shutw;
197 s->si[1].exp = TICK_ETERNITY;
198 s->si[1].flags = SI_FL_NONE;
199
200 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
201 s->si[1].flags |= SI_FL_INDEP_STR;
202
Willy Tarreau827aee92011-03-10 16:55:02 +0100203 s->srv_conn = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100204 clear_target(&s->target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200205 s->pend_pos = NULL;
206
207 /* init store persistence */
208 s->store_count = 0;
209
210 /* Adjust some socket options */
Willy Tarreaufffe1322010-11-11 09:48:16 +0100211 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200212 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200213
214 txn = &s->txn;
215 /* Those variables will be checked and freed if non-NULL in
216 * session.c:session_free(). It is important that they are
217 * properly initialized.
218 */
219 txn->sessid = NULL;
220 txn->srv_cookie = NULL;
221 txn->cli_cookie = NULL;
222 txn->uri = NULL;
223 txn->req.cap = NULL;
224 txn->rsp.cap = NULL;
225 txn->hdr_idx.v = NULL;
226 txn->hdr_idx.size = txn->hdr_idx.used = 0;
227
228 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
229 goto out_free_task; /* no memory */
230
231 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
232 goto out_free_req; /* no memory */
233
234 /* initialize the request buffer */
235 s->req->size = global.tune.bufsize;
236 buffer_init(s->req);
237 s->req->prod = &s->si[0];
238 s->req->cons = &s->si[1];
239 s->si[0].ib = s->si[1].ob = s->req;
240 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
241
242 /* activate default analysers enabled for this listener */
243 s->req->analysers = l->analysers;
244
245 s->req->wto = TICK_ETERNITY;
246 s->req->rto = TICK_ETERNITY;
247 s->req->rex = TICK_ETERNITY;
248 s->req->wex = TICK_ETERNITY;
249 s->req->analyse_exp = TICK_ETERNITY;
250
251 /* initialize response buffer */
252 s->rep->size = global.tune.bufsize;
253 buffer_init(s->rep);
254 s->rep->prod = &s->si[1];
255 s->rep->cons = &s->si[0];
256 s->si[0].ob = s->si[1].ib = s->rep;
257 s->rep->analysers = 0;
258
259 s->rep->rto = TICK_ETERNITY;
260 s->rep->wto = TICK_ETERNITY;
261 s->rep->rex = TICK_ETERNITY;
262 s->rep->wex = TICK_ETERNITY;
263 s->rep->analyse_exp = TICK_ETERNITY;
264
265 /* finish initialization of the accepted file descriptor */
266 fd_insert(cfd);
267 fdtab[cfd].owner = &s->si[0];
268 fdtab[cfd].state = FD_STREADY;
269 fdtab[cfd].flags = 0;
270 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
271 fdtab[cfd].cb[DIR_RD].b = s->req;
272 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
273 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreau957c0a52011-03-03 17:42:23 +0100274 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->si[0].addr.c.from;
275 fdinfo[cfd].peerlen = sizeof(s->si[0].addr.c.from);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200276 EV_FD_SET(cfd, DIR_RD);
277
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100278 if (p->accept && (ret = p->accept(s)) <= 0) {
279 /* Either we had an unrecoverable error (<0) or work is
280 * finished (=0, eg: monitoring), in both situations,
281 * we can release everything and close.
282 */
283 goto out_free_rep;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200284 }
285
286 /* it is important not to call the wakeup function directly but to
287 * pass through task_wakeup(), because this one knows how to apply
288 * priorities to tasks.
289 */
290 task_wakeup(t, TASK_WOKEN_INIT);
291 return 1;
292
293 /* Error unrolling */
294 out_free_rep:
295 pool_free2(pool2_buffer, s->rep);
296 out_free_req:
297 pool_free2(pool2_buffer, s->req);
298 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200299 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200300 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200301 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200302 task_free(t);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200303 LIST_DEL(&s->list);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100304 out_free_session:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200305 pool_free2(pool2_session, s);
306 out_close:
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100307 if (fdtab[cfd].state != FD_STCLOSE)
308 fd_delete(cfd);
309 else
310 close(cfd);
311 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200312}
313
Willy Tarreaubaaee002006-06-26 02:48:02 +0200314/*
315 * frees the context associated to a session. It must have been removed first.
316 */
317void session_free(struct session *s)
318{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100319 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200320 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100321 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200322 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100323
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324 if (s->pend_pos)
325 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100326
Willy Tarreau827aee92011-03-10 16:55:02 +0100327 if (target_srv(&s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100328 if (s->flags & SN_CURR_SESS) {
329 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100330 target_srv(&s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100331 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100332 if (may_dequeue_tasks(target_srv(&s->target), s->be))
333 process_srv_queue(target_srv(&s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100334 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100335
Willy Tarreau7c669d72008-06-20 15:04:11 +0200336 if (unlikely(s->srv_conn)) {
337 /* the session still has a reserved slot on a server, but
338 * it should normally be only the same as the one above,
339 * so this should not happen in fact.
340 */
341 sess_change_server(s, NULL);
342 }
343
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100344 if (s->req->pipe)
345 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100346
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100347 if (s->rep->pipe)
348 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100349
Willy Tarreau48d63db2008-08-03 17:41:33 +0200350 pool_free2(pool2_buffer, s->req);
351 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352
Willy Tarreau46023632010-01-07 22:51:47 +0100353 http_end_txn(s);
354
Willy Tarreaua4cda672010-06-06 18:28:49 +0200355 for (i = 0; i < s->store_count; i++) {
356 if (!s->store[i].ts)
357 continue;
358 stksess_free(s->store[i].table, s->store[i].ts);
359 s->store[i].ts = NULL;
360 }
361
Willy Tarreau92fb9832007-10-16 17:34:28 +0200362 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200363 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100364 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
365 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100367
Willy Tarreau56123282010-08-06 19:06:56 +0200368 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200369 session_store_counters(s);
370
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100371 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100372 /* we have to unlink all watchers. We must not relink them if
373 * this session was the last one in the list.
374 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100375 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100376 LIST_INIT(&bref->users);
377 if (s->list.n != &sessions)
378 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100379 bref->ref = s->list.n;
380 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100381 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200382 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200383
384 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200385 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200386 pool_flush2(pool2_buffer);
387 pool_flush2(fe->hdr_idx_pool);
388 pool_flush2(pool2_requri);
389 pool_flush2(pool2_capture);
390 pool_flush2(pool2_session);
391 pool_flush2(fe->req_cap_pool);
392 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200393 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200394}
395
396
397/* perform minimal intializations, report 0 in case of error, 1 if OK. */
398int init_session()
399{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100400 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200401 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
402 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200403}
404
Willy Tarreau30e71012007-11-26 20:15:35 +0100405void session_process_counters(struct session *s)
406{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100407 unsigned long long bytes;
408
Willy Tarreau30e71012007-11-26 20:15:35 +0100409 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100410 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100411 s->logs.bytes_in = s->req->total;
412 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200413 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100414
Willy Tarreau30e71012007-11-26 20:15:35 +0100415 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200416 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100417
Willy Tarreau827aee92011-03-10 16:55:02 +0100418 if (target_srv(&s->target))
419 target_srv(&s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200420
421 if (s->listener->counters)
422 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200423
Willy Tarreau56123282010-08-06 19:06:56 +0200424 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200425 void *ptr;
426
Willy Tarreau56123282010-08-06 19:06:56 +0200427 ptr = stktable_data_ptr(s->stkctr2_table,
428 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200429 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200430 if (ptr)
431 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200432
Willy Tarreau56123282010-08-06 19:06:56 +0200433 ptr = stktable_data_ptr(s->stkctr2_table,
434 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200435 STKTABLE_DT_BYTES_IN_RATE);
436 if (ptr)
437 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200438 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200439 }
440
Willy Tarreau56123282010-08-06 19:06:56 +0200441 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200442 void *ptr;
443
Willy Tarreau56123282010-08-06 19:06:56 +0200444 ptr = stktable_data_ptr(s->stkctr1_table,
445 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200446 STKTABLE_DT_BYTES_IN_CNT);
447 if (ptr)
448 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
449
Willy Tarreau56123282010-08-06 19:06:56 +0200450 ptr = stktable_data_ptr(s->stkctr1_table,
451 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200452 STKTABLE_DT_BYTES_IN_RATE);
453 if (ptr)
454 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200455 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200456 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100457 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100458 }
459
Willy Tarreau30e71012007-11-26 20:15:35 +0100460 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100461 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100462 s->logs.bytes_out = s->rep->total;
463 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200464 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100465
Willy Tarreau30e71012007-11-26 20:15:35 +0100466 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200467 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100468
Willy Tarreau827aee92011-03-10 16:55:02 +0100469 if (target_srv(&s->target))
470 target_srv(&s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200471
472 if (s->listener->counters)
473 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200474
Willy Tarreau56123282010-08-06 19:06:56 +0200475 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200476 void *ptr;
477
Willy Tarreau56123282010-08-06 19:06:56 +0200478 ptr = stktable_data_ptr(s->stkctr2_table,
479 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200480 STKTABLE_DT_BYTES_OUT_CNT);
481 if (ptr)
482 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
483
Willy Tarreau56123282010-08-06 19:06:56 +0200484 ptr = stktable_data_ptr(s->stkctr2_table,
485 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200486 STKTABLE_DT_BYTES_OUT_RATE);
487 if (ptr)
488 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200489 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200490 }
491
Willy Tarreau56123282010-08-06 19:06:56 +0200492 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200493 void *ptr;
494
Willy Tarreau56123282010-08-06 19:06:56 +0200495 ptr = stktable_data_ptr(s->stkctr1_table,
496 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200497 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200498 if (ptr)
499 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200500
Willy Tarreau56123282010-08-06 19:06:56 +0200501 ptr = stktable_data_ptr(s->stkctr1_table,
502 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200503 STKTABLE_DT_BYTES_OUT_RATE);
504 if (ptr)
505 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200506 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200507 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100508 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100509 }
510}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200511
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100512/* This function is called with (si->state == SI_ST_CON) meaning that a
513 * connection was attempted and that the file descriptor is already allocated.
514 * We must check for establishment, error and abort. Possible output states
515 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
516 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
517 * otherwise 1.
518 */
519int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
520{
521 struct buffer *req = si->ob;
522 struct buffer *rep = si->ib;
523
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100524 /* If we got an error, or if nothing happened and the connection timed
525 * out, we must give up. The CER state handler will take care of retry
526 * attempts and error reports.
527 */
528 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100529 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100530 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200531 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100532 fd_delete(si->fd);
533
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200534 if (si->release)
535 si->release(si);
536
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100537 if (si->err_type)
538 return 0;
539
Willy Tarreau827aee92011-03-10 16:55:02 +0100540 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541 if (si->flags & SI_FL_ERR)
542 si->err_type = SI_ET_CONN_ERR;
543 else
544 si->err_type = SI_ET_CONN_TO;
545 return 0;
546 }
547
548 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200549 if (unlikely((rep->flags & BF_SHUTW) ||
550 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200551 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100552 s->be->options & PR_O_ABRT_CLOSE)))) {
553 /* give up */
554 si->shutw(si);
555 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau827aee92011-03-10 16:55:02 +0100556 si->err_loc = target_srv(&s->target);
Willy Tarreaudc340a92009-06-28 23:10:19 +0200557 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100558 if (s->srv_error)
559 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100560 return 1;
561 }
562
563 /* we need to wait a bit more if there was no activity either */
564 if (!(req->flags & BF_WRITE_ACTIVITY))
565 return 1;
566
567 /* OK, this means that a connection succeeded. The caller will be
568 * responsible for handling the transition from CON to EST.
569 */
570 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100571 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100572 si->state = SI_ST_EST;
573 si->err_type = SI_ET_NONE;
574 si->err_loc = NULL;
575 return 1;
576}
577
578/* This function is called with (si->state == SI_ST_CER) meaning that a
579 * previous connection attempt has failed and that the file descriptor
580 * has already been released. Possible causes include asynchronous error
581 * notification and time out. Possible output states are SI_ST_CLO when
582 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
583 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
584 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
585 * marked as in error state. It returns 0.
586 */
587int sess_update_st_cer(struct session *s, struct stream_interface *si)
588{
589 /* we probably have to release last session from the server */
Willy Tarreau827aee92011-03-10 16:55:02 +0100590 if (target_srv(&s->target)) {
591 health_adjust(target_srv(&s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100592
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100593 if (s->flags & SN_CURR_SESS) {
594 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100595 target_srv(&s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100596 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100597 }
598
599 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200600 si->conn_retries--;
601 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100602 if (!si->err_type) {
603 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +0100604 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100605 }
606
Willy Tarreau827aee92011-03-10 16:55:02 +0100607 if (target_srv(&s->target))
608 target_srv(&s->target)->counters.failed_conns++;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200609 s->be->counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100610 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100611 if (may_dequeue_tasks(target_srv(&s->target), s->be))
612 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100613
614 /* shutw is enough so stop a connecting socket */
615 si->shutw(si);
616 si->ob->flags |= BF_WRITE_ERROR;
617 si->ib->flags |= BF_READ_ERROR;
618
619 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100620 if (s->srv_error)
621 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100622 return 0;
623 }
624
625 /* If the "redispatch" option is set on the backend, we are allowed to
626 * retry on another server for the last retry. In order to achieve this,
627 * we must mark the session unassigned, and eventually clear the DIRECT
628 * bit to ignore any persistence cookie. We won't count a retry nor a
629 * redispatch yet, because this will depend on what server is selected.
630 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100631 if (target_srv(&s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100632 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100633 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100634 if (may_dequeue_tasks(target_srv(&s->target), s->be))
635 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100636
637 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100638 si->state = SI_ST_REQ;
639 } else {
Willy Tarreau827aee92011-03-10 16:55:02 +0100640 if (target_srv(&s->target))
641 target_srv(&s->target)->counters.retries++;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200642 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100643 si->state = SI_ST_ASS;
644 }
645
646 if (si->flags & SI_FL_ERR) {
647 /* The error was an asynchronous connection error, and we will
648 * likely have to retry connecting to the same server, most
649 * likely leading to the same result. To avoid this, we wait
650 * one second before retrying.
651 */
652
653 if (!si->err_type)
654 si->err_type = SI_ET_CONN_ERR;
655
656 si->state = SI_ST_TAR;
657 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
658 return 0;
659 }
660 return 0;
661}
662
663/*
664 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200665 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
666 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100667 */
668void sess_establish(struct session *s, struct stream_interface *si)
669{
670 struct buffer *req = si->ob;
671 struct buffer *rep = si->ib;
672
Willy Tarreau827aee92011-03-10 16:55:02 +0100673 if (target_srv(&s->target))
674 health_adjust(target_srv(&s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100675
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100676 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100677 /* if the user wants to log as soon as possible, without counting
678 * bytes from the server, then this is the right moment. */
679 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
680 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100681 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100682 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100683 }
684 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100685 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
686 /* reset hdr_idx which was already initialized by the request.
687 * right now, the http parser does it.
688 * hdr_idx_init(&s->txn.hdr_idx);
689 */
690 }
691
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200692 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100693 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200694 if (si->connect) {
695 /* real connections have timeouts */
696 req->wto = s->be->timeout.server;
697 rep->rto = s->be->timeout.server;
698 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100699 req->wex = TICK_ETERNITY;
700}
701
702/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
703 * Other input states are simply ignored.
704 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
705 * Flags must have previously been updated for timeouts and other conditions.
706 */
707void sess_update_stream_int(struct session *s, struct stream_interface *si)
708{
Willy Tarreau827aee92011-03-10 16:55:02 +0100709 struct server *srv = target_srv(&s->target);
710
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100711 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",
712 now_ms, __FUNCTION__,
713 s,
714 s->req, s->rep,
715 s->req->rex, s->rep->wex,
716 s->req->flags, s->rep->flags,
717 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
718
719 if (si->state == SI_ST_ASS) {
720 /* Server assigned to connection request, we have to try to connect now */
721 int conn_err;
722
723 conn_err = connect_server(s);
Willy Tarreau827aee92011-03-10 16:55:02 +0100724 srv = target_srv(&s->target);
725
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100726 if (conn_err == SN_ERR_NONE) {
727 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100728 if (srv)
729 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100730 return;
731 }
732
733 /* We have received a synchronous error. We might have to
734 * abort, retry immediately or redispatch.
735 */
736 if (conn_err == SN_ERR_INTERNAL) {
737 if (!si->err_type) {
738 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +0100739 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100740 }
741
Willy Tarreau827aee92011-03-10 16:55:02 +0100742 if (srv)
743 srv_inc_sess_ctr(srv);
744 if (srv)
745 srv->counters.failed_conns++;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200746 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100747
748 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100749 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100750 if (may_dequeue_tasks(srv, s->be))
751 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100752
753 /* Failed and not retryable. */
754 si->shutr(si);
755 si->shutw(si);
756 si->ob->flags |= BF_WRITE_ERROR;
757
758 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
759
760 /* no session was ever accounted for this server */
761 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100762 if (s->srv_error)
763 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100764 return;
765 }
766
767 /* We are facing a retryable error, but we don't want to run a
768 * turn-around now, as the problem is likely a source port
769 * allocation problem, so we want to retry now.
770 */
771 si->state = SI_ST_CER;
772 si->flags &= ~SI_FL_ERR;
773 sess_update_st_cer(s, si);
774 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
775 return;
776 }
777 else if (si->state == SI_ST_QUE) {
778 /* connection request was queued, check for any update */
779 if (!s->pend_pos) {
780 /* The connection is not in the queue anymore. Either
781 * we have a server connection slot available and we
782 * go directly to the assigned state, or we need to
783 * load-balance first and go to the INI state.
784 */
785 si->exp = TICK_ETERNITY;
786 if (unlikely(!(s->flags & SN_ASSIGNED)))
787 si->state = SI_ST_REQ;
788 else {
789 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
790 si->state = SI_ST_ASS;
791 }
792 return;
793 }
794
795 /* Connection request still in queue... */
796 if (si->flags & SI_FL_EXP) {
797 /* ... and timeout expired */
798 si->exp = TICK_ETERNITY;
799 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100800 if (srv)
801 srv->counters.failed_conns++;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200802 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100803 si->shutr(si);
804 si->shutw(si);
805 si->ob->flags |= BF_WRITE_TIMEOUT;
806 if (!si->err_type)
807 si->err_type = SI_ET_QUEUE_TO;
808 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100809 if (s->srv_error)
810 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100811 return;
812 }
813
814 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200815 if ((si->ob->flags & (BF_READ_ERROR)) ||
816 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200817 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100818 /* give up */
819 si->exp = TICK_ETERNITY;
820 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
821 si->shutr(si);
822 si->shutw(si);
823 si->err_type |= SI_ET_QUEUE_ABRT;
824 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100825 if (s->srv_error)
826 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100827 return;
828 }
829
830 /* Nothing changed */
831 return;
832 }
833 else if (si->state == SI_ST_TAR) {
834 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200835 if ((si->ob->flags & (BF_READ_ERROR)) ||
836 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200837 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100838 /* give up */
839 si->exp = TICK_ETERNITY;
840 si->shutr(si);
841 si->shutw(si);
842 si->err_type |= SI_ET_CONN_ABRT;
843 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100844 if (s->srv_error)
845 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100846 return;
847 }
848
849 if (!(si->flags & SI_FL_EXP))
850 return; /* still in turn-around */
851
852 si->exp = TICK_ETERNITY;
853
854 /* we keep trying on the same server as long as the session is
855 * marked "assigned".
856 * FIXME: Should we force a redispatch attempt when the server is down ?
857 */
858 if (s->flags & SN_ASSIGNED)
859 si->state = SI_ST_ASS;
860 else
861 si->state = SI_ST_REQ;
862 return;
863 }
864}
865
866/* This function initiates a server connection request on a stream interface
867 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
868 * indicating that a server has been assigned. It may also return SI_ST_QUE,
869 * or SI_ST_CLO upon error.
870 */
871static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
872 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",
873 now_ms, __FUNCTION__,
874 s,
875 s->req, s->rep,
876 s->req->rex, s->rep->wex,
877 s->req->flags, s->rep->flags,
878 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
879
880 if (si->state != SI_ST_REQ)
881 return;
882
883 /* Try to assign a server */
884 if (srv_redispatch_connect(s) != 0) {
885 /* We did not get a server. Either we queued the
886 * connection request, or we encountered an error.
887 */
888 if (si->state == SI_ST_QUE)
889 return;
890
891 /* we did not get any server, let's check the cause */
892 si->shutr(si);
893 si->shutw(si);
894 si->ob->flags |= BF_WRITE_ERROR;
895 if (!si->err_type)
896 si->err_type = SI_ET_CONN_OTHER;
897 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100898 if (s->srv_error)
899 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100900 return;
901 }
902
903 /* The server is assigned */
904 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
905 si->state = SI_ST_ASS;
906}
907
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200908/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100909 * if appropriate. The default_backend rule is also considered, then the
910 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200911 * It returns 1 if the processing can continue on next analysers, or zero if it
912 * either needs more data or wants to immediately abort the request.
913 */
914int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
915{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200916 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100917
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200918 req->analysers &= ~an_bit;
919 req->analyse_exp = TICK_ETERNITY;
920
921 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
922 now_ms, __FUNCTION__,
923 s,
924 req,
925 req->rex, req->wex,
926 req->flags,
927 req->l,
928 req->analysers);
929
930 /* now check whether we have some switching rules for this request */
931 if (!(s->flags & SN_BE_ASSIGNED)) {
932 struct switching_rule *rule;
933
934 list_for_each_entry(rule, &s->fe->switching_rules, list) {
935 int ret;
936
937 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
938 ret = acl_pass(ret);
939 if (rule->cond->pol == ACL_COND_UNLESS)
940 ret = !ret;
941
942 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200943 if (!session_set_backend(s, rule->be.backend))
944 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200945 break;
946 }
947 }
948
949 /* To ensure correct connection accounting on the backend, we
950 * have to assign one if it was not set (eg: a listen). This
951 * measure also takes care of correctly setting the default
952 * backend if any.
953 */
954 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200955 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
956 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200957 }
958
Willy Tarreaufb356202010-08-03 14:02:05 +0200959 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
960 if (s->fe == s->be) {
961 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200962 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200963 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200964
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200965 /* 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 +0100966 * persistence rule, and report that in the session.
967 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200968 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100969 int ret = 1;
970
971 if (prst_rule->cond) {
972 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
973 ret = acl_pass(ret);
974 if (prst_rule->cond->pol == ACL_COND_UNLESS)
975 ret = !ret;
976 }
977
978 if (ret) {
979 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200980 if (prst_rule->type == PERSIST_TYPE_FORCE) {
981 s->flags |= SN_FORCE_PRST;
982 } else {
983 s->flags |= SN_IGNORE_PRST;
984 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100985 break;
986 }
987 }
988
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200989 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200990
991 sw_failed:
992 /* immediately abort this request in case of allocation failure */
993 buffer_abort(s->req);
994 buffer_abort(s->rep);
995
996 if (!(s->flags & SN_ERR_MASK))
997 s->flags |= SN_ERR_RESOURCE;
998 if (!(s->flags & SN_FINST_MASK))
999 s->flags |= SN_FINST_R;
1000
1001 s->txn.status = 500;
1002 s->req->analysers = 0;
1003 s->req->analyse_exp = TICK_ETERNITY;
1004 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001005}
1006
Emeric Brun1d33b292010-01-04 15:47:17 +01001007/* This stream analyser works on a request. It applies all sticking rules on
1008 * it then returns 1. The data must already be present in the buffer otherwise
1009 * they won't match. It always returns 1.
1010 */
1011int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
1012{
1013 struct proxy *px = s->be;
1014 struct sticking_rule *rule;
1015
1016 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1017 now_ms, __FUNCTION__,
1018 s,
1019 req,
1020 req->rex, req->wex,
1021 req->flags,
1022 req->l,
1023 req->analysers);
1024
1025 list_for_each_entry(rule, &px->sticking_rules, list) {
1026 int ret = 1 ;
1027 int i;
1028
1029 for (i = 0; i < s->store_count; i++) {
1030 if (rule->table.t == s->store[i].table)
1031 break;
1032 }
1033
1034 if (i != s->store_count)
1035 continue;
1036
1037 if (rule->cond) {
1038 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1039 ret = acl_pass(ret);
1040 if (rule->cond->pol == ACL_COND_UNLESS)
1041 ret = !ret;
1042 }
1043
1044 if (ret) {
1045 struct stktable_key *key;
1046
Emeric Brun485479d2010-09-23 18:02:19 +02001047 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001048 if (!key)
1049 continue;
1050
1051 if (rule->flags & STK_IS_MATCH) {
1052 struct stksess *ts;
1053
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001054 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001055 if (!(s->flags & SN_ASSIGNED)) {
1056 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001057 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001058
1059 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001060 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1061 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001062 if (node) {
1063 struct server *srv;
1064
1065 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001066 if ((srv->state & SRV_RUNNING) ||
1067 (px->options & PR_O_PERSIST) ||
1068 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001069 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001070 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001071 }
1072 }
1073 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001074 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001075 }
1076 }
1077 if (rule->flags & STK_IS_STORE) {
1078 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1079 struct stksess *ts;
1080
1081 ts = stksess_new(rule->table.t, key);
1082 if (ts) {
1083 s->store[s->store_count].table = rule->table.t;
1084 s->store[s->store_count++].ts = ts;
1085 }
1086 }
1087 }
1088 }
1089 }
1090
1091 req->analysers &= ~an_bit;
1092 req->analyse_exp = TICK_ETERNITY;
1093 return 1;
1094}
1095
1096/* This stream analyser works on a response. It applies all store rules on it
1097 * then returns 1. The data must already be present in the buffer otherwise
1098 * they won't match. It always returns 1.
1099 */
1100int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1101{
1102 struct proxy *px = s->be;
1103 struct sticking_rule *rule;
1104 int i;
1105
1106 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1107 now_ms, __FUNCTION__,
1108 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001109 rep,
1110 rep->rex, rep->wex,
1111 rep->flags,
1112 rep->l,
1113 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001114
1115 list_for_each_entry(rule, &px->storersp_rules, list) {
1116 int ret = 1 ;
1117 int storereqidx = -1;
1118
1119 for (i = 0; i < s->store_count; i++) {
1120 if (rule->table.t == s->store[i].table) {
1121 if (!(s->store[i].flags))
1122 storereqidx = i;
1123 break;
1124 }
1125 }
1126
1127 if ((i != s->store_count) && (storereqidx == -1))
1128 continue;
1129
1130 if (rule->cond) {
1131 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1132 ret = acl_pass(ret);
1133 if (rule->cond->pol == ACL_COND_UNLESS)
1134 ret = !ret;
1135 }
1136
1137 if (ret) {
1138 struct stktable_key *key;
1139
Emeric Brun485479d2010-09-23 18:02:19 +02001140 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001141 if (!key)
1142 continue;
1143
1144 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001145 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001146 s->store[storereqidx].flags = 1;
1147 }
1148 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1149 struct stksess *ts;
1150
1151 ts = stksess_new(rule->table.t, key);
1152 if (ts) {
1153 s->store[s->store_count].table = rule->table.t;
1154 s->store[s->store_count].flags = 1;
1155 s->store[s->store_count++].ts = ts;
1156 }
1157 }
1158 }
1159 }
1160
1161 /* process store request and store response */
1162 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001163 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001164 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001165
1166 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1167 if (ts) {
1168 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001169 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001170 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001171 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001172 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001173 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001174
1175 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001176 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001177 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001178 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001179 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001180
1181 rep->analysers &= ~an_bit;
1182 rep->analyse_exp = TICK_ETERNITY;
1183 return 1;
1184}
1185
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001186/* This macro is very specific to the function below. See the comments in
1187 * process_session() below to understand the logic and the tests.
1188 */
1189#define UPDATE_ANALYSERS(real, list, back, flag) { \
1190 list = (((list) & ~(flag)) | ~(back)) & (real); \
1191 back = real; \
1192 if (!(list)) \
1193 break; \
1194 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1195 continue; \
1196}
1197
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001198/* Processes the client, server, request and response jobs of a session task,
1199 * then puts it back to the wait queue in a clean state, or cleans up its
1200 * resources if it must be deleted. Returns in <next> the date the task wants
1201 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1202 * nothing too many times, the request and response buffers flags are monitored
1203 * and each function is called only if at least another function has changed at
1204 * least one flag it is interested in.
1205 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001206struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001207{
Willy Tarreau827aee92011-03-10 16:55:02 +01001208 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001209 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001210 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001211 unsigned int rq_prod_last, rq_cons_last;
1212 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001213 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001214
1215 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1216 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1217
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001218 /* this data may be no longer valid, clear it */
1219 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1220
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001221 /* This flag must explicitly be set every time */
1222 s->req->flags &= ~BF_READ_NOEXP;
1223
1224 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001225 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1226 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001227
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001228 /* we don't want the stream interface functions to recursively wake us up */
1229 if (s->req->prod->owner == t)
1230 s->req->prod->flags |= SI_FL_DONT_WAKE;
1231 if (s->req->cons->owner == t)
1232 s->req->cons->flags |= SI_FL_DONT_WAKE;
1233
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001234 /* 1a: Check for low level timeouts if needed. We just set a flag on
1235 * stream interfaces when their timeouts have expired.
1236 */
1237 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1238 stream_int_check_timeouts(&s->si[0]);
1239 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001240
1241 /* check buffer timeouts, and close the corresponding stream interfaces
1242 * for future reads or writes. Note: this will also concern upper layers
1243 * but we do not touch any other flag. We must be careful and correctly
1244 * detect state changes when calling them.
1245 */
1246
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001247 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001248
Willy Tarreau14641402009-12-29 14:49:56 +01001249 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1250 s->req->cons->flags |= SI_FL_NOLINGER;
1251 s->req->cons->shutw(s->req->cons);
1252 }
1253
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001254 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1255 s->req->prod->shutr(s->req->prod);
1256
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001257 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001258
Willy Tarreau14641402009-12-29 14:49:56 +01001259 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1260 s->rep->cons->flags |= SI_FL_NOLINGER;
1261 s->rep->cons->shutw(s->rep->cons);
1262 }
1263
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001264 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1265 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001266 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001267
1268 /* 1b: check for low-level errors reported at the stream interface.
1269 * First we check if it's a retryable error (in which case we don't
1270 * want to tell the buffer). Otherwise we report the error one level
1271 * upper by setting flags into the buffers. Note that the side towards
1272 * the client cannot have connect (hence retryable) errors. Also, the
1273 * connection setup code must be able to deal with any type of abort.
1274 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001275 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001276 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1277 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1278 s->si[0].shutr(&s->si[0]);
1279 s->si[0].shutw(&s->si[0]);
1280 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001281 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001282 s->be->counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001283 if (srv)
1284 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001285 if (!(s->flags & SN_ERR_MASK))
1286 s->flags |= SN_ERR_CLICL;
1287 if (!(s->flags & SN_FINST_MASK))
1288 s->flags |= SN_FINST_D;
1289 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001290 }
1291 }
1292
1293 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1294 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1295 s->si[1].shutr(&s->si[1]);
1296 s->si[1].shutw(&s->si[1]);
1297 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001298 s->be->counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001299 if (srv)
1300 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001301 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001302 s->be->counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001303 if (srv)
1304 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001305 if (!(s->flags & SN_ERR_MASK))
1306 s->flags |= SN_ERR_SRVCL;
1307 if (!(s->flags & SN_FINST_MASK))
1308 s->flags |= SN_FINST_D;
1309 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001310 }
1311 /* note: maybe we should process connection errors here ? */
1312 }
1313
1314 if (s->si[1].state == SI_ST_CON) {
1315 /* we were trying to establish a connection on the server side,
1316 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1317 */
1318 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1319 sess_update_st_cer(s, &s->si[1]);
1320 else if (s->si[1].state == SI_ST_EST)
1321 sess_establish(s, &s->si[1]);
1322
1323 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1324 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1325 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1326 */
1327 }
1328
Willy Tarreau815a9b22010-07-27 17:15:12 +02001329 rq_prod_last = s->si[0].state;
1330 rq_cons_last = s->si[1].state;
1331 rp_cons_last = s->si[0].state;
1332 rp_prod_last = s->si[1].state;
1333
1334 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001335 /* Check for connection closure */
1336
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001337 DPRINTF(stderr,
1338 "[%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",
1339 now_ms, __FUNCTION__, __LINE__,
1340 t,
1341 s, s->flags,
1342 s->req, s->rep,
1343 s->req->rex, s->rep->wex,
1344 s->req->flags, s->rep->flags,
1345 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1346 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001347 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001348
1349 /* nothing special to be done on client side */
1350 if (unlikely(s->req->prod->state == SI_ST_DIS))
1351 s->req->prod->state = SI_ST_CLO;
1352
1353 /* When a server-side connection is released, we have to count it and
1354 * check for pending connections on this server.
1355 */
1356 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1357 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001358 srv = target_srv(&s->target);
1359 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001360 if (s->flags & SN_CURR_SESS) {
1361 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001362 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001363 }
1364 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001365 if (may_dequeue_tasks(srv, s->be))
1366 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001367 }
1368 }
1369
1370 /*
1371 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1372 * at this point.
1373 */
1374
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001375 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001376 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001377 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001378 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1379 s->si[0].state != rq_prod_last ||
1380 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001381 unsigned int flags = s->req->flags;
1382
1383 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001384 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001385 unsigned int ana_list;
1386 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001387
Willy Tarreau90deb182010-01-07 00:20:41 +01001388 /* it's up to the analysers to stop new connections,
1389 * disable reading or closing. Note: if an analyser
1390 * disables any of these bits, it is responsible for
1391 * enabling them again when it disables itself, so
1392 * that other analysers are called in similar conditions.
1393 */
1394 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001395 buffer_auto_connect(s->req);
1396 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001397
1398 /* We will call all analysers for which a bit is set in
1399 * s->req->analysers, following the bit order from LSB
1400 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001401 * the list when not needed. Any analyser may return 0
1402 * to break out of the loop, either because of missing
1403 * data to take a decision, or because it decides to
1404 * kill the session. We loop at least once through each
1405 * analyser, and we may loop again if other analysers
1406 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001407 *
1408 * We build a list of analysers to run. We evaluate all
1409 * of these analysers in the order of the lower bit to
1410 * the higher bit. This ordering is very important.
1411 * An analyser will often add/remove other analysers,
1412 * including itself. Any changes to itself have no effect
1413 * on the loop. If it removes any other analysers, we
1414 * want those analysers not to be called anymore during
1415 * this loop. If it adds an analyser that is located
1416 * after itself, we want it to be scheduled for being
1417 * processed during the loop. If it adds an analyser
1418 * which is located before it, we want it to switch to
1419 * it immediately, even if it has already been called
1420 * once but removed since.
1421 *
1422 * In order to achieve this, we compare the analyser
1423 * list after the call with a copy of it before the
1424 * call. The work list is fed with analyser bits that
1425 * appeared during the call. Then we compare previous
1426 * work list with the new one, and check the bits that
1427 * appeared. If the lowest of these bits is lower than
1428 * the current bit, it means we have enabled a previous
1429 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001430 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001431
1432 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001433 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001434 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001435
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001436 if (ana_list & AN_REQ_DECODE_PROXY) {
1437 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1438 break;
1439 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1440 }
1441
Willy Tarreaufb356202010-08-03 14:02:05 +02001442 if (ana_list & AN_REQ_INSPECT_FE) {
1443 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001444 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001445 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001446 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001447
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001448 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001449 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001450 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001451 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001452 }
1453
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001454 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001455 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1456 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001457 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001458 }
1459
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001460 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001461 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1462 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001463 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001464 }
1465
Willy Tarreaufb356202010-08-03 14:02:05 +02001466 if (ana_list & AN_REQ_INSPECT_BE) {
1467 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1468 break;
1469 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1470 }
1471
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001472 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001473 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1474 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001475 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001476 }
1477
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001478 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001479 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001480 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001481 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001482 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001483
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001484 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001485 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1486 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001487 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001488 }
1489
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001490 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001491 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001492 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001493 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001494 }
Emeric Brun647caf12009-06-30 17:57:00 +02001495
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001496 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001497 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1498 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001499 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001500 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001501
Emeric Brun1d33b292010-01-04 15:47:17 +01001502 if (ana_list & AN_REQ_STICKING_RULES) {
1503 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1504 break;
1505 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1506 }
1507
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001508 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001509 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1510 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001511 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001512 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001513 break;
1514 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001515 }
Willy Tarreau84455332009-03-15 22:34:05 +01001516
Willy Tarreau815a9b22010-07-27 17:15:12 +02001517 rq_prod_last = s->si[0].state;
1518 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001519 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001520 rqf_last = s->req->flags;
1521
1522 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001523 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001524 }
1525
Willy Tarreau576507f2010-01-07 00:09:04 +01001526 /* we'll monitor the request analysers while parsing the response,
1527 * because some response analysers may indirectly enable new request
1528 * analysers (eg: HTTP keep-alive).
1529 */
1530 req_ana_back = s->req->analysers;
1531
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001532 resync_response:
1533 /* Analyse response */
1534
1535 if (unlikely(s->rep->flags & BF_HIJACK)) {
1536 /* In inject mode, we wake up everytime something has
1537 * happened on the write side of the buffer.
1538 */
1539 unsigned int flags = s->rep->flags;
1540
1541 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1542 !(s->rep->flags & BF_FULL)) {
1543 s->rep->hijacker(s, s->rep);
1544 }
1545
1546 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1547 rpf_last = s->rep->flags;
1548 goto resync_response;
1549 }
1550 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001551 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001552 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1553 s->si[0].state != rp_cons_last ||
1554 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001555 unsigned int flags = s->rep->flags;
1556
Willy Tarreau0499e352010-12-17 07:13:42 +01001557 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1558 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1559 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1560 * for some free space in the response buffer, so we might need to call
1561 * it when something changes in the response buffer, but still we pass
1562 * it the request buffer. Note that the SI state might very well still
1563 * be zero due to us returning a flow of redirects!
1564 */
1565 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1566 s->req->flags |= BF_WAKE_ONCE;
1567 }
1568
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001569 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001570 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001571 unsigned int ana_list;
1572 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001573
Willy Tarreau90deb182010-01-07 00:20:41 +01001574 /* it's up to the analysers to stop disable reading or
1575 * closing. Note: if an analyser disables any of these
1576 * bits, it is responsible for enabling them again when
1577 * it disables itself, so that other analysers are called
1578 * in similar conditions.
1579 */
1580 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001581 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001582
1583 /* We will call all analysers for which a bit is set in
1584 * s->rep->analysers, following the bit order from LSB
1585 * to MSB. The analysers must remove themselves from
1586 * the list when not needed. Any analyser may return 0
1587 * to break out of the loop, either because of missing
1588 * data to take a decision, or because it decides to
1589 * kill the session. We loop at least once through each
1590 * analyser, and we may loop again if other analysers
1591 * are added in the middle.
1592 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001593
1594 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001595 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001596 /* Warning! ensure that analysers are always placed in ascending order! */
1597
Emeric Brun97679e72010-09-23 17:56:44 +02001598 if (ana_list & AN_RES_INSPECT) {
1599 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1600 break;
1601 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1602 }
1603
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001604 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001605 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1606 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001607 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001608 }
1609
Emeric Brun1d33b292010-01-04 15:47:17 +01001610 if (ana_list & AN_RES_STORE_RULES) {
1611 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1612 break;
1613 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1614 }
1615
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001616 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001617 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1618 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001619 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001620 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001621
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001622 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001623 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1624 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001625 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001626 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001627 break;
1628 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001629 }
1630
Willy Tarreau815a9b22010-07-27 17:15:12 +02001631 rp_cons_last = s->si[0].state;
1632 rp_prod_last = s->si[1].state;
1633 rpf_last = s->rep->flags;
1634
1635 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001636 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001637 }
1638
Willy Tarreau576507f2010-01-07 00:09:04 +01001639 /* maybe someone has added some request analysers, so we must check and loop */
1640 if (s->req->analysers & ~req_ana_back)
1641 goto resync_request;
1642
Willy Tarreau0499e352010-12-17 07:13:42 +01001643 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1644 goto resync_request;
1645
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001646 /* FIXME: here we should call protocol handlers which rely on
1647 * both buffers.
1648 */
1649
1650
1651 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001652 * Now we propagate unhandled errors to the session. Normally
1653 * we're just in a data phase here since it means we have not
1654 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001655 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001656 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001657 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001658 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1659 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001660 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001661 if (s->req->flags & BF_READ_ERROR) {
1662 s->be->counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001663 if (srv)
1664 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001665 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001666 }
1667 else if (s->req->flags & BF_READ_TIMEOUT) {
1668 s->be->counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001669 if (srv)
1670 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001671 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001672 }
1673 else if (s->req->flags & BF_WRITE_ERROR) {
1674 s->be->counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001675 if (srv)
1676 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001677 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001678 }
1679 else {
1680 s->be->counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001681 if (srv)
1682 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001683 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001684 }
Willy Tarreau84455332009-03-15 22:34:05 +01001685 sess_set_term_flags(s);
1686 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001687 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1688 /* Report it if the server got an error or a read timeout expired */
1689 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001690 if (s->rep->flags & BF_READ_ERROR) {
1691 s->be->counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001692 if (srv)
1693 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001694 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001695 }
1696 else if (s->rep->flags & BF_READ_TIMEOUT) {
1697 s->be->counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001698 if (srv)
1699 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001700 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001701 }
1702 else if (s->rep->flags & BF_WRITE_ERROR) {
1703 s->be->counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001704 if (srv)
1705 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001706 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001707 }
1708 else {
1709 s->be->counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001710 if (srv)
1711 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001712 s->flags |= SN_ERR_CLITO;
1713 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001714 sess_set_term_flags(s);
1715 }
Willy Tarreau84455332009-03-15 22:34:05 +01001716 }
1717
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001718 /*
1719 * Here we take care of forwarding unhandled data. This also includes
1720 * connection establishments and shutdown requests.
1721 */
1722
1723
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001724 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001725 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001726 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1727 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001728 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001729 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001730 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001731 (s->req->prod->state >= SI_ST_EST) &&
1732 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001733 /* This buffer is freewheeling, there's no analyser nor hijacker
1734 * attached to it. If any data are left in, we'll permit them to
1735 * move.
1736 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001737 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001738 buffer_auto_connect(s->req);
1739 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001740 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001741
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001742 /* We'll let data flow between the producer (if still connected)
1743 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001744 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001745 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001746 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001747 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001748
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001749 /* check if it is wise to enable kernel splicing to forward request data */
1750 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1751 s->req->to_forward &&
1752 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001753 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001754 (pipes_used < global.maxpipes) &&
1755 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1756 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1757 (s->req->flags & BF_STREAMER_FAST)))) {
1758 s->req->flags |= BF_KERN_SPLICING;
1759 }
1760
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001761 /* reflect what the L7 analysers have seen last */
1762 rqf_last = s->req->flags;
1763
1764 /*
1765 * Now forward all shutdown requests between both sides of the buffer
1766 */
1767
Willy Tarreau520d95e2009-09-19 21:04:57 +02001768 /* first, let's check if the request buffer needs to shutdown(write), which may
1769 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001770 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001771 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001772 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001773 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001774 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001775
1776 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001777 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 +01001778 s->req->cons->shutw(s->req->cons);
1779
1780 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001781 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1782 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001783 buffer_shutr_now(s->req);
1784
1785 /* shutdown(read) pending */
1786 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1787 s->req->prod->shutr(s->req->prod);
1788
Willy Tarreau520d95e2009-09-19 21:04:57 +02001789 /* it's possible that an upper layer has requested a connection setup or abort.
1790 * There are 2 situations where we decide to establish a new connection :
1791 * - there are data scheduled for emission in the buffer
1792 * - the BF_AUTO_CONNECT flag is set (active connection)
1793 */
1794 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001795 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001796 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001797 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001798 * switch to the connected state, otherwise we perform a connection
1799 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001800 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001801 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001802 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001803 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001804 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001805 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1806 s->req->wex = TICK_ETERNITY;
1807 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001808 }
Willy Tarreau73201222009-08-16 18:27:24 +02001809 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001810 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001811 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001812 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1813 buffer_shutr_now(s->rep);
1814 }
Willy Tarreau92795622009-03-06 12:51:23 +01001815 }
1816
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001817
1818 /* we may have a pending connection request, or a connection waiting
1819 * for completion.
1820 */
1821 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1822 do {
1823 /* nb: step 1 might switch from QUE to ASS, but we first want
1824 * to give a chance to step 2 to perform a redirect if needed.
1825 */
1826 if (s->si[1].state != SI_ST_REQ)
1827 sess_update_stream_int(s, &s->si[1]);
1828 if (s->si[1].state == SI_ST_REQ)
1829 sess_prepare_conn_req(s, &s->si[1]);
1830
Willy Tarreau827aee92011-03-10 16:55:02 +01001831 srv = target_srv(&s->target);
1832 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001833 perform_http_redirect(s, &s->si[1]);
1834 } while (s->si[1].state == SI_ST_ASS);
1835 }
1836
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001837 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001838 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001839 goto resync_stream_interface;
1840
Willy Tarreau815a9b22010-07-27 17:15:12 +02001841 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001842 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001843 goto resync_request;
1844
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001845 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001846
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001847 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001848 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001849 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1850 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001851 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001852 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001853 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001854 (s->rep->prod->state >= SI_ST_EST) &&
1855 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001856 /* This buffer is freewheeling, there's no analyser nor hijacker
1857 * attached to it. If any data are left in, we'll permit them to
1858 * move.
1859 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001860 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001861 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001862 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001863
1864 /* We'll let data flow between the producer (if still connected)
1865 * to the consumer.
1866 */
1867 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001868 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001869 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001870
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001871 /* check if it is wise to enable kernel splicing to forward response data */
1872 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1873 s->rep->to_forward &&
1874 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001875 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001876 (pipes_used < global.maxpipes) &&
1877 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1878 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1879 (s->rep->flags & BF_STREAMER_FAST)))) {
1880 s->rep->flags |= BF_KERN_SPLICING;
1881 }
1882
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001883 /* reflect what the L7 analysers have seen last */
1884 rpf_last = s->rep->flags;
1885
1886 /*
1887 * Now forward all shutdown requests between both sides of the buffer
1888 */
1889
1890 /*
1891 * FIXME: this is probably where we should produce error responses.
1892 */
1893
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001894 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001895 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1896 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001897 buffer_shutw_now(s->rep);
1898
1899 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001900 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 +01001901 s->rep->cons->shutw(s->rep->cons);
1902
1903 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001904 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1905 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001906 buffer_shutr_now(s->rep);
1907
1908 /* shutdown(read) pending */
1909 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1910 s->rep->prod->shutr(s->rep->prod);
1911
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001912 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001913 goto resync_stream_interface;
1914
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001915 if (s->req->flags != rqf_last)
1916 goto resync_request;
1917
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001918 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001919 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001920
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001921 /* we're interested in getting wakeups again */
1922 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1923 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1924
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001925 /* This is needed only when debugging is enabled, to indicate
1926 * client-side or server-side close. Please note that in the unlikely
1927 * event where both sides would close at once, the sequence is reported
1928 * on the server side first.
1929 */
1930 if (unlikely((global.mode & MODE_DEBUG) &&
1931 (!(global.mode & MODE_QUIET) ||
1932 (global.mode & MODE_VERBOSE)))) {
1933 int len;
1934
1935 if (s->si[1].state == SI_ST_CLO &&
1936 s->si[1].prev_state == SI_ST_EST) {
1937 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1938 s->uniq_id, s->be->id,
1939 (unsigned short)s->si[0].fd,
1940 (unsigned short)s->si[1].fd);
1941 write(1, trash, len);
1942 }
1943
1944 if (s->si[0].state == SI_ST_CLO &&
1945 s->si[0].prev_state == SI_ST_EST) {
1946 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1947 s->uniq_id, s->be->id,
1948 (unsigned short)s->si[0].fd,
1949 (unsigned short)s->si[1].fd);
1950 write(1, trash, len);
1951 }
1952 }
1953
1954 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1955 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1956
1957 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1958 session_process_counters(s);
1959
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001960 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001961 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001962
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001963 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001964 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001965
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001966 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1967 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 +01001968 s->si[0].prev_state = s->si[0].state;
1969 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001970 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1971 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001972
1973 /* Trick: if a request is being waiting for the server to respond,
1974 * and if we know the server can timeout, we don't want the timeout
1975 * to expire on the client side first, but we're still interested
1976 * in passing data from the client to the server (eg: POST). Thus,
1977 * we can cancel the client's request timeout if the server's
1978 * request timeout is set and the server has not yet sent a response.
1979 */
1980
Willy Tarreau520d95e2009-09-19 21:04:57 +02001981 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001982 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1983 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001984 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001985 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001986
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001987 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001988 * Note that this one may wake the task up again.
1989 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001990 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
1991 s->rep->cons->target.type == TARG_TYPE_APPLET) {
1992 if (s->req->cons->target.type == TARG_TYPE_APPLET)
1993 s->req->cons->target.ptr.a->fct(s->req->cons);
1994 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
1995 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02001996 if (task_in_rq(t)) {
1997 /* If we woke up, we don't want to requeue the
1998 * task to the wait queue, but rather requeue
1999 * it into the runqueue ASAP.
2000 */
2001 t->expire = TICK_ETERNITY;
2002 return t;
2003 }
2004 }
2005
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002006 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2007 tick_first(s->rep->rex, s->rep->wex));
2008 if (s->req->analysers)
2009 t->expire = tick_first(t->expire, s->req->analyse_exp);
2010
2011 if (s->si[0].exp)
2012 t->expire = tick_first(t->expire, s->si[0].exp);
2013
2014 if (s->si[1].exp)
2015 t->expire = tick_first(t->expire, s->si[1].exp);
2016
2017#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002018 fprintf(stderr,
2019 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2020 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2021 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2022 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 +01002023#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002024
2025#ifdef DEBUG_DEV
2026 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002027 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002028 ABORT_NOW();
2029#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002030 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002031 }
2032
2033 s->fe->feconn--;
2034 if (s->flags & SN_BE_ASSIGNED)
2035 s->be->beconn--;
2036 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002037 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002038 s->listener->nbconn--;
2039 if (s->listener->state == LI_FULL &&
2040 s->listener->nbconn < s->listener->maxconn) {
2041 /* we should reactivate the listener */
2042 EV_FD_SET(s->listener->fd, DIR_RD);
2043 s->listener->state = LI_READY;
2044 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002045
2046 if (unlikely((global.mode & MODE_DEBUG) &&
2047 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2048 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002049 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002050 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002051 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002052 write(1, trash, len);
2053 }
2054
2055 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2056 session_process_counters(s);
2057
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002058 if (s->txn.status) {
2059 int n;
2060
2061 n = s->txn.status / 100;
2062 if (n < 1 || n > 5)
2063 n = 0;
2064
2065 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01002066 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002067
Willy Tarreau24657792010-02-26 10:30:28 +01002068 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002069 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01002070 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002071 }
2072
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002073 /* let's do a final log if we need it */
2074 if (s->logs.logwait &&
2075 !(s->flags & SN_MONITOR) &&
2076 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002077 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002078 }
2079
2080 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002081 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002082 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002083 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002084 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002085}
2086
Willy Tarreau7c669d72008-06-20 15:04:11 +02002087/*
2088 * This function adjusts sess->srv_conn and maintains the previous and new
2089 * server's served session counts. Setting newsrv to NULL is enough to release
2090 * current connection slot. This function also notifies any LB algo which might
2091 * expect to be informed about any change in the number of active sessions on a
2092 * server.
2093 */
2094void sess_change_server(struct session *sess, struct server *newsrv)
2095{
2096 if (sess->srv_conn == newsrv)
2097 return;
2098
2099 if (sess->srv_conn) {
2100 sess->srv_conn->served--;
2101 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2102 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
2103 sess->srv_conn = NULL;
2104 }
2105
2106 if (newsrv) {
2107 newsrv->served++;
2108 if (newsrv->proxy->lbprm.server_take_conn)
2109 newsrv->proxy->lbprm.server_take_conn(newsrv);
2110 sess->srv_conn = newsrv;
2111 }
2112}
2113
Willy Tarreau84455332009-03-15 22:34:05 +01002114/* Set correct session termination flags in case no analyser has done it. It
2115 * also counts a failed request if the server state has not reached the request
2116 * stage.
2117 */
2118void sess_set_term_flags(struct session *s)
2119{
2120 if (!(s->flags & SN_FINST_MASK)) {
2121 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002122
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002123 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002124 if (s->listener->counters)
2125 s->listener->counters->failed_req++;
2126
Willy Tarreau84455332009-03-15 22:34:05 +01002127 s->flags |= SN_FINST_R;
2128 }
2129 else if (s->si[1].state == SI_ST_QUE)
2130 s->flags |= SN_FINST_Q;
2131 else if (s->si[1].state < SI_ST_EST)
2132 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002133 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002134 s->flags |= SN_FINST_D;
2135 else
2136 s->flags |= SN_FINST_L;
2137 }
2138}
2139
2140/* Handle server-side errors for default protocols. It is called whenever a a
2141 * connection setup is aborted or a request is aborted in queue. It sets the
2142 * session termination flags so that the caller does not have to worry about
2143 * them. It's installed as ->srv_error for the server-side stream_interface.
2144 */
2145void default_srv_error(struct session *s, struct stream_interface *si)
2146{
2147 int err_type = si->err_type;
2148 int err = 0, fin = 0;
2149
2150 if (err_type & SI_ET_QUEUE_ABRT) {
2151 err = SN_ERR_CLICL;
2152 fin = SN_FINST_Q;
2153 }
2154 else if (err_type & SI_ET_CONN_ABRT) {
2155 err = SN_ERR_CLICL;
2156 fin = SN_FINST_C;
2157 }
2158 else if (err_type & SI_ET_QUEUE_TO) {
2159 err = SN_ERR_SRVTO;
2160 fin = SN_FINST_Q;
2161 }
2162 else if (err_type & SI_ET_QUEUE_ERR) {
2163 err = SN_ERR_SRVCL;
2164 fin = SN_FINST_Q;
2165 }
2166 else if (err_type & SI_ET_CONN_TO) {
2167 err = SN_ERR_SRVTO;
2168 fin = SN_FINST_C;
2169 }
2170 else if (err_type & SI_ET_CONN_ERR) {
2171 err = SN_ERR_SRVCL;
2172 fin = SN_FINST_C;
2173 }
2174 else /* SI_ET_CONN_OTHER and others */ {
2175 err = SN_ERR_INTERNAL;
2176 fin = SN_FINST_C;
2177 }
2178
2179 if (!(s->flags & SN_ERR_MASK))
2180 s->flags |= err;
2181 if (!(s->flags & SN_FINST_MASK))
2182 s->flags |= fin;
2183}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002184
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002185
Willy Tarreau8b22a712010-06-18 17:46:06 +02002186/************************************************************************/
2187/* All supported ACL keywords must be declared here. */
2188/************************************************************************/
2189
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002190/* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */
2191static int
2192acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2193{
2194 test->flags = ACL_TEST_F_VOL_TEST;
2195 test->i = 0;
2196 if (ts != NULL) {
2197 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2198 if (!ptr)
2199 return 0; /* parameter not stored */
2200 test->i = stktable_data_cast(ptr, gpc0);
2201 }
2202 return 1;
2203}
2204
2205/* set test->i to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002206 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002207 */
2208static int
Willy Tarreau56123282010-08-06 19:06:56 +02002209acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2210 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002211{
Willy Tarreau56123282010-08-06 19:06:56 +02002212 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002213 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002214 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002215}
2216
2217/* set test->i to the General Purpose Counter 0 value from the session's tracked
2218 * backend counters.
2219 */
2220static int
Willy Tarreau56123282010-08-06 19:06:56 +02002221acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2222 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002223{
Willy Tarreau56123282010-08-06 19:06:56 +02002224 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002225 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002226 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002227}
2228
2229/* set test->i to the General Purpose Counter 0 value from the session's source
2230 * address in the table pointed to by expr.
2231 */
2232static int
2233acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2234 struct acl_expr *expr, struct acl_test *test)
2235{
2236 struct stktable_key *key;
2237
2238 key = tcpv4_src_to_stktable_key(l4);
2239 if (!key)
2240 return 0; /* only TCPv4 is supported right now */
2241
2242 if (expr->arg_len)
2243 px = find_stktable(expr->arg.str);
2244
2245 if (!px)
2246 return 0; /* table not found */
2247
2248 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2249}
2250
2251/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
2252 * return it into test->i.
2253 */
2254static int
2255acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2256{
2257 test->flags = ACL_TEST_F_VOL_TEST;
2258 test->i = 0;
2259 if (ts != NULL) {
2260 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2261 if (!ptr)
2262 return 0; /* parameter not stored */
2263 test->i = ++stktable_data_cast(ptr, gpc0);
2264 }
2265 return 1;
2266}
2267
2268/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002269 * frontend counters and return it into test->i.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002270 */
2271static int
Willy Tarreau56123282010-08-06 19:06:56 +02002272acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2273 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002274{
Willy Tarreau56123282010-08-06 19:06:56 +02002275 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002276 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002277 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002278}
2279
2280/* Increment the General Purpose Counter 0 value from the session's tracked
2281 * backend counters and return it into test->i.
2282 */
2283static int
Willy Tarreau56123282010-08-06 19:06:56 +02002284acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2285 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002286{
Willy Tarreau56123282010-08-06 19:06:56 +02002287 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002288 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002289 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002290}
2291
2292/* Increment the General Purpose Counter 0 value from the session's source
2293 * address in the table pointed to by expr, and return it into test->i.
2294 */
2295static int
2296acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2297 struct acl_expr *expr, struct acl_test *test)
2298{
2299 struct stktable_key *key;
2300
2301 key = tcpv4_src_to_stktable_key(l4);
2302 if (!key)
2303 return 0; /* only TCPv4 is supported right now */
2304
2305 if (expr->arg_len)
2306 px = find_stktable(expr->arg.str);
2307
2308 if (!px)
2309 return 0; /* table not found */
2310
2311 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2312}
2313
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002314/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2315static int
2316acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2317{
2318 test->flags = ACL_TEST_F_VOL_TEST;
2319 test->i = 0;
2320 if (ts != NULL) {
2321 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2322 if (!ptr)
2323 return 0; /* parameter not stored */
2324 test->i = stktable_data_cast(ptr, conn_cnt);
2325 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002326 return 1;
2327}
2328
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002329/* set test->i to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002330static int
Willy Tarreau56123282010-08-06 19:06:56 +02002331acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2332 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002333{
Willy Tarreau56123282010-08-06 19:06:56 +02002334 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002335 return 0;
2336
Willy Tarreau56123282010-08-06 19:06:56 +02002337 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002338}
2339
2340/* set test->i to the cumulated number of connections from the session's tracked BE counters */
2341static int
Willy Tarreau56123282010-08-06 19:06:56 +02002342acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2343 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002344{
Willy Tarreau56123282010-08-06 19:06:56 +02002345 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002346 return 0;
2347
Willy Tarreau56123282010-08-06 19:06:56 +02002348 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002349}
2350
2351/* set test->i to the cumulated number of connections from the session's source
2352 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002353 */
2354static int
2355acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2356 struct acl_expr *expr, struct acl_test *test)
2357{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002358 struct stktable_key *key;
2359
2360 key = tcpv4_src_to_stktable_key(l4);
2361 if (!key)
2362 return 0; /* only TCPv4 is supported right now */
2363
2364 if (expr->arg_len)
2365 px = find_stktable(expr->arg.str);
2366
2367 if (!px)
2368 return 0; /* table not found */
2369
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002370 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002371}
2372
Willy Tarreau91c43d72010-06-20 11:19:22 +02002373/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2374static int
2375acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2376{
2377 test->flags = ACL_TEST_F_VOL_TEST;
2378 test->i = 0;
2379 if (ts != NULL) {
2380 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2381 if (!ptr)
2382 return 0; /* parameter not stored */
2383 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2384 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2385 }
2386 return 1;
2387}
2388
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002389/* set test->i to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002390 * the configured period.
2391 */
2392static int
Willy Tarreau56123282010-08-06 19:06:56 +02002393acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2394 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002395{
Willy Tarreau56123282010-08-06 19:06:56 +02002396 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002397 return 0;
2398
Willy Tarreau56123282010-08-06 19:06:56 +02002399 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002400}
2401
2402/* set test->i to the connection rate from the session's tracked BE counters over
2403 * the configured period.
2404 */
2405static int
Willy Tarreau56123282010-08-06 19:06:56 +02002406acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2407 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002408{
Willy Tarreau56123282010-08-06 19:06:56 +02002409 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002410 return 0;
2411
Willy Tarreau56123282010-08-06 19:06:56 +02002412 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002413}
2414
2415/* set test->i to the connection rate from the session's source address in the
2416 * table pointed to by expr, over the configured period.
2417 */
2418static int
2419acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2420 struct acl_expr *expr, struct acl_test *test)
2421{
2422 struct stktable_key *key;
2423
2424 key = tcpv4_src_to_stktable_key(l4);
2425 if (!key)
2426 return 0; /* only TCPv4 is supported right now */
2427
2428 if (expr->arg_len)
2429 px = find_stktable(expr->arg.str);
2430
2431 if (!px)
2432 return 0; /* table not found */
2433
2434 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2435}
2436
Willy Tarreau8b22a712010-06-18 17:46:06 +02002437/* set test->i to the number of connections from the session's source address
2438 * in the table pointed to by expr, after updating it.
2439 */
2440static int
2441acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2442 struct acl_expr *expr, struct acl_test *test)
2443{
2444 struct stksess *ts;
2445 struct stktable_key *key;
2446 void *ptr;
2447
2448 key = tcpv4_src_to_stktable_key(l4);
2449 if (!key)
2450 return 0; /* only TCPv4 is supported right now */
2451
2452 if (expr->arg_len)
2453 px = find_stktable(expr->arg.str);
2454
2455 if (!px)
2456 return 0; /* table not found */
2457
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002458 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2459 /* entry does not exist and could not be created */
2460 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002461
2462 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2463 if (!ptr)
2464 return 0; /* parameter not stored in this table */
2465
2466 test->i = ++stktable_data_cast(ptr, conn_cnt);
2467 test->flags = ACL_TEST_F_VOL_TEST;
2468 return 1;
2469}
2470
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002471/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2472static int
2473acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2474{
2475 test->flags = ACL_TEST_F_VOL_TEST;
2476 test->i = 0;
2477
2478 if (ts != NULL) {
2479 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2480 if (!ptr)
2481 return 0; /* parameter not stored */
2482 test->i = stktable_data_cast(ptr, conn_cur);
2483 }
2484 return 1;
2485}
2486
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002487/* set test->i to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002488static int
Willy Tarreau56123282010-08-06 19:06:56 +02002489acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2490 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002491{
Willy Tarreau56123282010-08-06 19:06:56 +02002492 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002493 return 0;
2494
Willy Tarreau56123282010-08-06 19:06:56 +02002495 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002496}
2497
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002498/* set test->i to the number of concurrent connections from the session's tracked BE counters */
2499static int
Willy Tarreau56123282010-08-06 19:06:56 +02002500acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2501 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002502{
Willy Tarreau56123282010-08-06 19:06:56 +02002503 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002504 return 0;
2505
Willy Tarreau56123282010-08-06 19:06:56 +02002506 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002507}
2508
Willy Tarreau38285c12010-06-18 16:35:43 +02002509/* set test->i to the number of concurrent connections from the session's source
2510 * address in the table pointed to by expr.
2511 */
2512static int
2513acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2514 struct acl_expr *expr, struct acl_test *test)
2515{
Willy Tarreau38285c12010-06-18 16:35:43 +02002516 struct stktable_key *key;
2517
2518 key = tcpv4_src_to_stktable_key(l4);
2519 if (!key)
2520 return 0; /* only TCPv4 is supported right now */
2521
2522 if (expr->arg_len)
2523 px = find_stktable(expr->arg.str);
2524
2525 if (!px)
2526 return 0; /* table not found */
2527
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002528 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002529}
2530
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002531/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2532static int
2533acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2534{
2535 test->flags = ACL_TEST_F_VOL_TEST;
2536 test->i = 0;
2537 if (ts != NULL) {
2538 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2539 if (!ptr)
2540 return 0; /* parameter not stored */
2541 test->i = stktable_data_cast(ptr, sess_cnt);
2542 }
2543 return 1;
2544}
2545
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002546/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002547static int
Willy Tarreau56123282010-08-06 19:06:56 +02002548acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2549 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002550{
Willy Tarreau56123282010-08-06 19:06:56 +02002551 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002552 return 0;
2553
Willy Tarreau56123282010-08-06 19:06:56 +02002554 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002555}
2556
2557/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2558static int
Willy Tarreau56123282010-08-06 19:06:56 +02002559acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2560 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002561{
Willy Tarreau56123282010-08-06 19:06:56 +02002562 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002563 return 0;
2564
Willy Tarreau56123282010-08-06 19:06:56 +02002565 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002566}
2567
2568/* set test->i to the cumulated number of session from the session's source
2569 * address in the table pointed to by expr.
2570 */
2571static int
2572acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2573 struct acl_expr *expr, struct acl_test *test)
2574{
2575 struct stktable_key *key;
2576
2577 key = tcpv4_src_to_stktable_key(l4);
2578 if (!key)
2579 return 0; /* only TCPv4 is supported right now */
2580
2581 if (expr->arg_len)
2582 px = find_stktable(expr->arg.str);
2583
2584 if (!px)
2585 return 0; /* table not found */
2586
2587 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2588}
2589
Willy Tarreau91c43d72010-06-20 11:19:22 +02002590/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2591static int
2592acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2593{
2594 test->flags = ACL_TEST_F_VOL_TEST;
2595 test->i = 0;
2596 if (ts != NULL) {
2597 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2598 if (!ptr)
2599 return 0; /* parameter not stored */
2600 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2601 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2602 }
2603 return 1;
2604}
2605
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002606/* set test->i to the session rate from the session's tracked FE counters over
2607 * the configured period.
2608 */
2609static int
Willy Tarreau56123282010-08-06 19:06:56 +02002610acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2611 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002612{
Willy Tarreau56123282010-08-06 19:06:56 +02002613 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002614 return 0;
2615
Willy Tarreau56123282010-08-06 19:06:56 +02002616 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002617}
2618
2619/* set test->i to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002620 * the configured period.
2621 */
2622static int
Willy Tarreau56123282010-08-06 19:06:56 +02002623acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2624 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002625{
Willy Tarreau56123282010-08-06 19:06:56 +02002626 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002627 return 0;
2628
Willy Tarreau56123282010-08-06 19:06:56 +02002629 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002630}
2631
2632/* set test->i to the session rate from the session's source address in the
2633 * table pointed to by expr, over the configured period.
2634 */
2635static int
2636acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2637 struct acl_expr *expr, struct acl_test *test)
2638{
2639 struct stktable_key *key;
2640
2641 key = tcpv4_src_to_stktable_key(l4);
2642 if (!key)
2643 return 0; /* only TCPv4 is supported right now */
2644
2645 if (expr->arg_len)
2646 px = find_stktable(expr->arg.str);
2647
2648 if (!px)
2649 return 0; /* table not found */
2650
2651 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2652}
2653
Willy Tarreauda7ff642010-06-23 11:44:09 +02002654/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2655static int
2656acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2657{
2658 test->flags = ACL_TEST_F_VOL_TEST;
2659 test->i = 0;
2660 if (ts != NULL) {
2661 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2662 if (!ptr)
2663 return 0; /* parameter not stored */
2664 test->i = stktable_data_cast(ptr, http_req_cnt);
2665 }
2666 return 1;
2667}
2668
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002669/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002670static int
Willy Tarreau56123282010-08-06 19:06:56 +02002671acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2672 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002673{
Willy Tarreau56123282010-08-06 19:06:56 +02002674 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002675 return 0;
2676
Willy Tarreau56123282010-08-06 19:06:56 +02002677 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002678}
2679
2680/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2681static int
Willy Tarreau56123282010-08-06 19:06:56 +02002682acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2683 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002684{
Willy Tarreau56123282010-08-06 19:06:56 +02002685 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002686 return 0;
2687
Willy Tarreau56123282010-08-06 19:06:56 +02002688 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002689}
2690
2691/* set test->i to the cumulated number of session from the session's source
2692 * address in the table pointed to by expr.
2693 */
2694static int
2695acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002696 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002697{
2698 struct stktable_key *key;
2699
2700 key = tcpv4_src_to_stktable_key(l4);
2701 if (!key)
2702 return 0; /* only TCPv4 is supported right now */
2703
2704 if (expr->arg_len)
2705 px = find_stktable(expr->arg.str);
2706
2707 if (!px)
2708 return 0; /* table not found */
2709
2710 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2711}
2712
2713/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2714static int
2715acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2716{
2717 test->flags = ACL_TEST_F_VOL_TEST;
2718 test->i = 0;
2719 if (ts != NULL) {
2720 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2721 if (!ptr)
2722 return 0; /* parameter not stored */
2723 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2724 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2725 }
2726 return 1;
2727}
2728
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002729/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002730 * the configured period.
2731 */
2732static int
Willy Tarreau56123282010-08-06 19:06:56 +02002733acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2734 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002735{
Willy Tarreau56123282010-08-06 19:06:56 +02002736 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002737 return 0;
2738
Willy Tarreau56123282010-08-06 19:06:56 +02002739 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002740}
2741
2742/* set test->i to the session rate from the session's tracked BE counters over
2743 * the configured period.
2744 */
2745static int
Willy Tarreau56123282010-08-06 19:06:56 +02002746acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2747 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002748{
Willy Tarreau56123282010-08-06 19:06:56 +02002749 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002750 return 0;
2751
Willy Tarreau56123282010-08-06 19:06:56 +02002752 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002753}
2754
2755/* set test->i to the session rate from the session's source address in the
2756 * table pointed to by expr, over the configured period.
2757 */
2758static int
2759acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002760 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002761{
2762 struct stktable_key *key;
2763
2764 key = tcpv4_src_to_stktable_key(l4);
2765 if (!key)
2766 return 0; /* only TCPv4 is supported right now */
2767
2768 if (expr->arg_len)
2769 px = find_stktable(expr->arg.str);
2770
2771 if (!px)
2772 return 0; /* table not found */
2773
2774 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2775}
2776
2777/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2778static int
2779acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2780{
2781 test->flags = ACL_TEST_F_VOL_TEST;
2782 test->i = 0;
2783 if (ts != NULL) {
2784 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2785 if (!ptr)
2786 return 0; /* parameter not stored */
2787 test->i = stktable_data_cast(ptr, http_err_cnt);
2788 }
2789 return 1;
2790}
2791
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002792/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002793static int
Willy Tarreau56123282010-08-06 19:06:56 +02002794acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2795 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002796{
Willy Tarreau56123282010-08-06 19:06:56 +02002797 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002798 return 0;
2799
Willy Tarreau56123282010-08-06 19:06:56 +02002800 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002801}
2802
2803/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2804static int
Willy Tarreau56123282010-08-06 19:06:56 +02002805acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2806 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002807{
Willy Tarreau56123282010-08-06 19:06:56 +02002808 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002809 return 0;
2810
Willy Tarreau56123282010-08-06 19:06:56 +02002811 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002812}
2813
2814/* set test->i to the cumulated number of session from the session's source
2815 * address in the table pointed to by expr.
2816 */
2817static int
2818acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002819 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002820{
2821 struct stktable_key *key;
2822
2823 key = tcpv4_src_to_stktable_key(l4);
2824 if (!key)
2825 return 0; /* only TCPv4 is supported right now */
2826
2827 if (expr->arg_len)
2828 px = find_stktable(expr->arg.str);
2829
2830 if (!px)
2831 return 0; /* table not found */
2832
2833 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2834}
2835
2836/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2837static int
2838acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2839{
2840 test->flags = ACL_TEST_F_VOL_TEST;
2841 test->i = 0;
2842 if (ts != NULL) {
2843 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2844 if (!ptr)
2845 return 0; /* parameter not stored */
2846 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2847 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2848 }
2849 return 1;
2850}
2851
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002852/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002853 * the configured period.
2854 */
2855static int
Willy Tarreau56123282010-08-06 19:06:56 +02002856acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2857 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002858{
Willy Tarreau56123282010-08-06 19:06:56 +02002859 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002860 return 0;
2861
Willy Tarreau56123282010-08-06 19:06:56 +02002862 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002863}
2864
2865/* set test->i to the session rate from the session's tracked BE counters over
2866 * the configured period.
2867 */
2868static int
Willy Tarreau56123282010-08-06 19:06:56 +02002869acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2870 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002871{
Willy Tarreau56123282010-08-06 19:06:56 +02002872 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002873 return 0;
2874
Willy Tarreau56123282010-08-06 19:06:56 +02002875 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002876}
2877
2878/* set test->i to the session rate from the session's source address in the
2879 * table pointed to by expr, over the configured period.
2880 */
2881static int
2882acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2883 struct acl_expr *expr, struct acl_test *test)
2884{
2885 struct stktable_key *key;
2886
2887 key = tcpv4_src_to_stktable_key(l4);
2888 if (!key)
2889 return 0; /* only TCPv4 is supported right now */
2890
2891 if (expr->arg_len)
2892 px = find_stktable(expr->arg.str);
2893
2894 if (!px)
2895 return 0; /* table not found */
2896
2897 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2898}
2899
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002900/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2901static int
2902acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2903{
2904 test->flags = ACL_TEST_F_VOL_TEST;
2905 test->i = 0;
2906
2907 if (ts != NULL) {
2908 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2909 if (!ptr)
2910 return 0; /* parameter not stored */
2911 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2912 }
2913 return 1;
2914}
2915
2916/* set test->i to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002917 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002918 */
2919static int
Willy Tarreau56123282010-08-06 19:06:56 +02002920acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2921 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002922{
Willy Tarreau56123282010-08-06 19:06:56 +02002923 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002924 return 0;
2925
Willy Tarreau56123282010-08-06 19:06:56 +02002926 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002927}
2928
2929/* set test->i to the number of kbytes received from clients according to the
2930 * session's tracked BE counters.
2931 */
2932static int
Willy Tarreau56123282010-08-06 19:06:56 +02002933acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2934 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002935{
Willy Tarreau56123282010-08-06 19:06:56 +02002936 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002937 return 0;
2938
Willy Tarreau56123282010-08-06 19:06:56 +02002939 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002940}
2941
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002942/* set test->i to the number of kbytes received from the session's source
2943 * address in the table pointed to by expr.
2944 */
2945static int
2946acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2947 struct acl_expr *expr, struct acl_test *test)
2948{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002949 struct stktable_key *key;
2950
2951 key = tcpv4_src_to_stktable_key(l4);
2952 if (!key)
2953 return 0; /* only TCPv4 is supported right now */
2954
2955 if (expr->arg_len)
2956 px = find_stktable(expr->arg.str);
2957
2958 if (!px)
2959 return 0; /* table not found */
2960
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002961 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2962}
2963
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002964/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2965 * configured period.
2966 */
2967static int
2968acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2969{
2970 test->flags = ACL_TEST_F_VOL_TEST;
2971 test->i = 0;
2972 if (ts != NULL) {
2973 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
2974 if (!ptr)
2975 return 0; /* parameter not stored */
2976 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2977 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2978 }
2979 return 1;
2980}
2981
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002982/* set test->i to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002983 * counters over the configured period.
2984 */
2985static int
Willy Tarreau56123282010-08-06 19:06:56 +02002986acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2987 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002988{
Willy Tarreau56123282010-08-06 19:06:56 +02002989 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002990 return 0;
2991
Willy Tarreau56123282010-08-06 19:06:56 +02002992 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002993}
2994
2995/* set test->i to the bytes rate from clients from the session's tracked BE
2996 * counters over the configured period.
2997 */
2998static int
Willy Tarreau56123282010-08-06 19:06:56 +02002999acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3000 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003001{
Willy Tarreau56123282010-08-06 19:06:56 +02003002 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003003 return 0;
3004
Willy Tarreau56123282010-08-06 19:06:56 +02003005 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003006}
3007
3008/* set test->i to the bytes rate from clients from the session's source address
3009 * in the table pointed to by expr, over the configured period.
3010 */
3011static int
3012acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003013 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003014{
3015 struct stktable_key *key;
3016
3017 key = tcpv4_src_to_stktable_key(l4);
3018 if (!key)
3019 return 0; /* only TCPv4 is supported right now */
3020
3021 if (expr->arg_len)
3022 px = find_stktable(expr->arg.str);
3023
3024 if (!px)
3025 return 0; /* table not found */
3026
3027 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3028}
3029
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003030/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
3031static int
3032acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3033{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003034 test->flags = ACL_TEST_F_VOL_TEST;
3035 test->i = 0;
3036
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003037 if (ts != NULL) {
3038 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003039 if (!ptr)
3040 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003041 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003042 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003043 return 1;
3044}
3045
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003046/* set test->i to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003047 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003048 */
3049static int
Willy Tarreau56123282010-08-06 19:06:56 +02003050acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3051 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003052{
Willy Tarreau56123282010-08-06 19:06:56 +02003053 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003054 return 0;
3055
Willy Tarreau56123282010-08-06 19:06:56 +02003056 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003057}
3058
3059/* set test->i to the number of kbytes sent to clients according to the session's
3060 * tracked BE counters.
3061 */
3062static int
Willy Tarreau56123282010-08-06 19:06:56 +02003063acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3064 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003065{
Willy Tarreau56123282010-08-06 19:06:56 +02003066 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003067 return 0;
3068
Willy Tarreau56123282010-08-06 19:06:56 +02003069 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003070}
3071
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003072/* set test->i to the number of kbytes sent to the session's source address in
3073 * the table pointed to by expr.
3074 */
3075static int
3076acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3077 struct acl_expr *expr, struct acl_test *test)
3078{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003079 struct stktable_key *key;
3080
3081 key = tcpv4_src_to_stktable_key(l4);
3082 if (!key)
3083 return 0; /* only TCPv4 is supported right now */
3084
3085 if (expr->arg_len)
3086 px = find_stktable(expr->arg.str);
3087
3088 if (!px)
3089 return 0; /* table not found */
3090
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003091 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003092}
3093
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003094/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
3095 * configured period.
3096 */
3097static int
3098acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3099{
3100 test->flags = ACL_TEST_F_VOL_TEST;
3101 test->i = 0;
3102 if (ts != NULL) {
3103 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3104 if (!ptr)
3105 return 0; /* parameter not stored */
3106 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3107 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3108 }
3109 return 1;
3110}
3111
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003112/* set test->i to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003113 * over the configured period.
3114 */
3115static int
Willy Tarreau56123282010-08-06 19:06:56 +02003116acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3117 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003118{
Willy Tarreau56123282010-08-06 19:06:56 +02003119 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003120 return 0;
3121
Willy Tarreau56123282010-08-06 19:06:56 +02003122 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003123}
3124
3125/* set test->i to the bytes rate to clients from the session's tracked BE counters
3126 * over the configured period.
3127 */
3128static int
Willy Tarreau56123282010-08-06 19:06:56 +02003129acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3130 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003131{
Willy Tarreau56123282010-08-06 19:06:56 +02003132 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003133 return 0;
3134
Willy Tarreau56123282010-08-06 19:06:56 +02003135 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003136}
3137
3138/* set test->i to the bytes rate to client from the session's source address in
3139 * the table pointed to by expr, over the configured period.
3140 */
3141static int
3142acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003143 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003144{
3145 struct stktable_key *key;
3146
3147 key = tcpv4_src_to_stktable_key(l4);
3148 if (!key)
3149 return 0; /* only TCPv4 is supported right now */
3150
3151 if (expr->arg_len)
3152 px = find_stktable(expr->arg.str);
3153
3154 if (!px)
3155 return 0; /* table not found */
3156
3157 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3158}
3159
Willy Tarreau8b22a712010-06-18 17:46:06 +02003160
3161/* Note: must not be declared <const> as its list will be overwritten */
3162static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003163 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3164 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3165 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3166 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3167 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3168 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3169 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3170 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3171 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3172 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3173 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3174 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3175 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3176 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3177 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3178 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3179 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3180 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3181 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3182 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3183 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3184 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3185 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3186 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3187 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3188 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3189 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3190 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3191 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3192 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3193 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3194 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3195 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3196 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3197 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3198 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3199 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3200 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3201 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3202 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3203 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3204 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3205 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3206 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3207 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3208 { "src_bytes_out_rate", acl_parse_int, acl_fetch_src_bytes_out_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003209 { NULL, NULL, NULL, NULL },
3210}};
3211
3212
Willy Tarreau56123282010-08-06 19:06:56 +02003213/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003214 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3215 * <prm> struct is fed with the table name if any. If unspecified, the caller
3216 * will assume that the current proxy's table is used.
3217 */
3218int parse_track_counters(char **args, int *arg,
3219 int section_type, struct proxy *curpx,
3220 struct track_ctr_prm *prm,
3221 struct proxy *defpx, char *err, int errlen)
3222{
3223 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003224 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003225
Willy Tarreau56123282010-08-06 19:06:56 +02003226 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003227 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003228 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003229 */
3230 while (args[*arg]) {
3231 if (strcmp(args[*arg], "src") == 0) {
3232 prm->type = STKTABLE_TYPE_IP;
3233 pattern_type = 1;
3234 }
3235 else if (strcmp(args[*arg], "table") == 0) {
3236 if (!args[*arg + 1]) {
3237 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003238 "missing table for %s in %s '%s'.",
3239 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003240 return -1;
3241 }
3242 /* we copy the table name for now, it will be resolved later */
3243 prm->table.n = strdup(args[*arg + 1]);
3244 (*arg)++;
3245 }
3246 else {
3247 /* unhandled keywords are handled by the caller */
3248 break;
3249 }
3250 (*arg)++;
3251 }
3252
3253 if (!pattern_type) {
3254 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003255 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3256 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003257 return -1;
3258 }
3259
3260 return 0;
3261}
3262
Willy Tarreau8b22a712010-06-18 17:46:06 +02003263__attribute__((constructor))
3264static void __session_init(void)
3265{
3266 acl_register_keywords(&acl_kws);
3267}
3268
Willy Tarreaubaaee002006-06-26 02:48:02 +02003269/*
3270 * Local variables:
3271 * c-indent-level: 8
3272 * c-basic-offset: 8
3273 * End:
3274 */