blob: 24df9365e0972ada8192cbf60c088359759c9537 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau81f9aa32010-06-01 17:45:26 +02002 * Session management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreau81f9aa32010-06-01 17:45:26 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <stdlib.h>
Willy Tarreau81f9aa32010-06-01 17:45:26 +020014#include <unistd.h>
15#include <fcntl.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020018#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010022#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020024#include <proto/acl.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010025#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020026#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010027#include <proto/checks.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020028#include <proto/dumpstats.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020029#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020030#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010031#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020032#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010034#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010035#include <proto/proto_http.h>
36#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020037#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010039#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010040#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010041#include <proto/stream_interface.h>
42#include <proto/stream_sock.h>
43#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020045struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010046struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau81f9aa32010-06-01 17:45:26 +020048/* This function is called from the protocol layer accept() in order to instanciate
49 * a new session on behalf of a given listener and frontend. It returns a positive
Willy Tarreauabe8ea52010-11-11 10:56:04 +010050 * value upon success, 0 if the connection can be ignored, or a negative value upon
51 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020052 */
53int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
54{
55 struct proxy *p = l->frontend;
56 struct session *s;
57 struct http_txn *txn;
58 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010059 int ret;
60
61
62 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020063
Willy Tarreaufffe1322010-11-11 09:48:16 +010064 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020065 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020066
67 /* minimum session initialization required for monitor mode below */
68 s->flags = 0;
69 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020070 s->stkctr1_entry = NULL;
71 s->stkctr2_entry = NULL;
72 s->stkctr1_table = NULL;
73 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020074
75 /* if this session comes from a known monitoring system, we want to ignore
76 * it as soon as possible, which means closing it immediately for TCP, but
77 * cleanly.
78 */
79 if (unlikely((l->options & LI_O_CHK_MONNET) &&
80 addr->ss_family == AF_INET &&
81 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
82 if (p->mode == PR_MODE_TCP) {
Willy Tarreauabe8ea52010-11-11 10:56:04 +010083 ret = 0; /* successful termination */
84 goto out_free_session;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020085 }
86 s->flags |= SN_MONITOR;
87 s->logs.logwait = 0;
88 }
89
Willy Tarreauabe8ea52010-11-11 10:56:04 +010090 if (unlikely((t = task_new()) == NULL))
91 goto out_free_session;
92
Willy Tarreau81f9aa32010-06-01 17:45:26 +020093 /* OK, we're keeping the session, so let's properly initialize the session */
94 LIST_ADDQ(&sessions, &s->list);
95 LIST_INIT(&s->back_refs);
96
Willy Tarreau81f9aa32010-06-01 17:45:26 +020097 s->term_trace = 0;
Willy Tarreau957c0a52011-03-03 17:42:23 +010098 s->si[0].addr.c.from = *addr;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020099 s->logs.accept_date = date; /* user-visible date for logging */
100 s->logs.tv_accept = now; /* corrected date for internal use */
101 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200102 p->feconn++; /* beconn will be increased once assigned */
103
Willy Tarreaub36b4242010-06-04 20:59:39 +0200104 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200105
106 t->process = l->handler;
107 t->context = s;
108 t->nice = l->nice;
109 t->expire = TICK_ETERNITY;
110
111 s->task = t;
112 s->listener = l;
113
114 /* Note: initially, the session's backend points to the frontend.
115 * This changes later when switching rules are executed or
116 * when the default backend is assigned.
117 */
118 s->be = s->fe = p;
119 s->req = s->rep = NULL; /* will be allocated later */
120
121 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
122 * to abort right here as soon as possible, we check the rules before
123 * even initializing the stream interfaces.
124 */
125 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200126 /* let's do a no-linger now to close with a single RST. */
127 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100128 ret = 0; /* successful termination */
129 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200130 }
131
Willy Tarreaub36b4242010-06-04 20:59:39 +0200132 /* This session was accepted, count it now */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100133 if (p->feconn > p->fe_counters.conn_max)
134 p->fe_counters.conn_max = p->feconn;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100135
Willy Tarreaub36b4242010-06-04 20:59:39 +0200136 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200137 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200138 void *ptr;
139
Willy Tarreau56123282010-08-06 19:06:56 +0200140 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200141 if (ptr)
142 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200143
Willy Tarreau56123282010-08-06 19:06:56 +0200144 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200145 if (ptr)
146 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200147 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200148 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200149
Willy Tarreau56123282010-08-06 19:06:56 +0200150 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200151 void *ptr;
152
Willy Tarreau56123282010-08-06 19:06:56 +0200153 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200154 if (ptr)
155 stktable_data_cast(ptr, sess_cnt)++;
156
Willy Tarreau56123282010-08-06 19:06:56 +0200157 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200158 if (ptr)
159 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200160 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200161 }
162
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200163 /* this part should be common with other protocols */
164 s->si[0].fd = cfd;
165 s->si[0].owner = t;
166 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
167 s->si[0].err_type = SI_ET_NONE;
168 s->si[0].err_loc = NULL;
169 s->si[0].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200170 s->si[0].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100171 clear_target(&s->si[0].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200172 s->si[0].exp = TICK_ETERNITY;
173 s->si[0].flags = SI_FL_NONE;
174
175 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
176 s->si[0].flags |= SI_FL_INDEP_STR;
177
178 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
179 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
180
181 /* add the various callbacks */
182 stream_sock_prepare_interface(&s->si[0]);
183
184 /* pre-initialize the other side's stream interface to an INIT state. The
185 * callbacks will be initialized before attempting to connect.
186 */
187 s->si[1].fd = -1; /* just to help with debugging */
188 s->si[1].owner = t;
189 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
190 s->si[1].err_type = SI_ET_NONE;
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) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100413 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100414
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100415 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100416
Willy Tarreau827aee92011-03-10 16:55:02 +0100417 if (target_srv(&s->target))
418 target_srv(&s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200419
420 if (s->listener->counters)
421 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200422
Willy Tarreau56123282010-08-06 19:06:56 +0200423 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200424 void *ptr;
425
Willy Tarreau56123282010-08-06 19:06:56 +0200426 ptr = stktable_data_ptr(s->stkctr2_table,
427 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200428 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200429 if (ptr)
430 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200431
Willy Tarreau56123282010-08-06 19:06:56 +0200432 ptr = stktable_data_ptr(s->stkctr2_table,
433 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200434 STKTABLE_DT_BYTES_IN_RATE);
435 if (ptr)
436 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200437 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200438 }
439
Willy Tarreau56123282010-08-06 19:06:56 +0200440 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200441 void *ptr;
442
Willy Tarreau56123282010-08-06 19:06:56 +0200443 ptr = stktable_data_ptr(s->stkctr1_table,
444 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200445 STKTABLE_DT_BYTES_IN_CNT);
446 if (ptr)
447 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
448
Willy Tarreau56123282010-08-06 19:06:56 +0200449 ptr = stktable_data_ptr(s->stkctr1_table,
450 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200451 STKTABLE_DT_BYTES_IN_RATE);
452 if (ptr)
453 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200454 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200455 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100456 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100457 }
458
Willy Tarreau30e71012007-11-26 20:15:35 +0100459 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100460 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100461 s->logs.bytes_out = s->rep->total;
462 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100463 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100464
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100465 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100466
Willy Tarreau827aee92011-03-10 16:55:02 +0100467 if (target_srv(&s->target))
468 target_srv(&s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200469
470 if (s->listener->counters)
471 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200472
Willy Tarreau56123282010-08-06 19:06:56 +0200473 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200474 void *ptr;
475
Willy Tarreau56123282010-08-06 19:06:56 +0200476 ptr = stktable_data_ptr(s->stkctr2_table,
477 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200478 STKTABLE_DT_BYTES_OUT_CNT);
479 if (ptr)
480 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
481
Willy Tarreau56123282010-08-06 19:06:56 +0200482 ptr = stktable_data_ptr(s->stkctr2_table,
483 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200484 STKTABLE_DT_BYTES_OUT_RATE);
485 if (ptr)
486 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200487 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200488 }
489
Willy Tarreau56123282010-08-06 19:06:56 +0200490 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200491 void *ptr;
492
Willy Tarreau56123282010-08-06 19:06:56 +0200493 ptr = stktable_data_ptr(s->stkctr1_table,
494 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200495 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200496 if (ptr)
497 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200498
Willy Tarreau56123282010-08-06 19:06:56 +0200499 ptr = stktable_data_ptr(s->stkctr1_table,
500 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200501 STKTABLE_DT_BYTES_OUT_RATE);
502 if (ptr)
503 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200504 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200505 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100506 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100507 }
508}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200509
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100510/* This function is called with (si->state == SI_ST_CON) meaning that a
511 * connection was attempted and that the file descriptor is already allocated.
512 * We must check for establishment, error and abort. Possible output states
513 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
514 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
515 * otherwise 1.
516 */
517int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
518{
519 struct buffer *req = si->ob;
520 struct buffer *rep = si->ib;
521
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100522 /* If we got an error, or if nothing happened and the connection timed
523 * out, we must give up. The CER state handler will take care of retry
524 * attempts and error reports.
525 */
526 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100527 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100528 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200529 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100530 fd_delete(si->fd);
531
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200532 if (si->release)
533 si->release(si);
534
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100535 if (si->err_type)
536 return 0;
537
Willy Tarreau827aee92011-03-10 16:55:02 +0100538 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100539 if (si->flags & SI_FL_ERR)
540 si->err_type = SI_ET_CONN_ERR;
541 else
542 si->err_type = SI_ET_CONN_TO;
543 return 0;
544 }
545
546 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200547 if (unlikely((rep->flags & BF_SHUTW) ||
548 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200549 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100550 s->be->options & PR_O_ABRT_CLOSE)))) {
551 /* give up */
552 si->shutw(si);
553 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau827aee92011-03-10 16:55:02 +0100554 si->err_loc = target_srv(&s->target);
Willy Tarreaudc340a92009-06-28 23:10:19 +0200555 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100556 if (s->srv_error)
557 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100558 return 1;
559 }
560
561 /* we need to wait a bit more if there was no activity either */
562 if (!(req->flags & BF_WRITE_ACTIVITY))
563 return 1;
564
565 /* OK, this means that a connection succeeded. The caller will be
566 * responsible for handling the transition from CON to EST.
567 */
568 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100569 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100570 si->state = SI_ST_EST;
571 si->err_type = SI_ET_NONE;
572 si->err_loc = NULL;
573 return 1;
574}
575
576/* This function is called with (si->state == SI_ST_CER) meaning that a
577 * previous connection attempt has failed and that the file descriptor
578 * has already been released. Possible causes include asynchronous error
579 * notification and time out. Possible output states are SI_ST_CLO when
580 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
581 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
582 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
583 * marked as in error state. It returns 0.
584 */
585int sess_update_st_cer(struct session *s, struct stream_interface *si)
586{
587 /* we probably have to release last session from the server */
Willy Tarreau827aee92011-03-10 16:55:02 +0100588 if (target_srv(&s->target)) {
589 health_adjust(target_srv(&s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100590
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100591 if (s->flags & SN_CURR_SESS) {
592 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100593 target_srv(&s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100594 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100595 }
596
597 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200598 si->conn_retries--;
599 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100600 if (!si->err_type) {
601 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +0100602 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100603 }
604
Willy Tarreau827aee92011-03-10 16:55:02 +0100605 if (target_srv(&s->target))
606 target_srv(&s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100607 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100608 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100609 if (may_dequeue_tasks(target_srv(&s->target), s->be))
610 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100611
612 /* shutw is enough so stop a connecting socket */
613 si->shutw(si);
614 si->ob->flags |= BF_WRITE_ERROR;
615 si->ib->flags |= BF_READ_ERROR;
616
617 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100618 if (s->srv_error)
619 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100620 return 0;
621 }
622
623 /* If the "redispatch" option is set on the backend, we are allowed to
624 * retry on another server for the last retry. In order to achieve this,
625 * we must mark the session unassigned, and eventually clear the DIRECT
626 * bit to ignore any persistence cookie. We won't count a retry nor a
627 * redispatch yet, because this will depend on what server is selected.
628 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100629 if (target_srv(&s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100630 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100631 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100632 if (may_dequeue_tasks(target_srv(&s->target), s->be))
633 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634
635 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100636 si->state = SI_ST_REQ;
637 } else {
Willy Tarreau827aee92011-03-10 16:55:02 +0100638 if (target_srv(&s->target))
639 target_srv(&s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100640 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100641 si->state = SI_ST_ASS;
642 }
643
644 if (si->flags & SI_FL_ERR) {
645 /* The error was an asynchronous connection error, and we will
646 * likely have to retry connecting to the same server, most
647 * likely leading to the same result. To avoid this, we wait
648 * one second before retrying.
649 */
650
651 if (!si->err_type)
652 si->err_type = SI_ET_CONN_ERR;
653
654 si->state = SI_ST_TAR;
655 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
656 return 0;
657 }
658 return 0;
659}
660
661/*
662 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200663 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
664 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100665 */
666void sess_establish(struct session *s, struct stream_interface *si)
667{
668 struct buffer *req = si->ob;
669 struct buffer *rep = si->ib;
670
Willy Tarreau827aee92011-03-10 16:55:02 +0100671 if (target_srv(&s->target))
672 health_adjust(target_srv(&s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100673
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100674 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100675 /* if the user wants to log as soon as possible, without counting
676 * bytes from the server, then this is the right moment. */
677 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
678 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100679 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100680 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681 }
682 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100683 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
684 /* reset hdr_idx which was already initialized by the request.
685 * right now, the http parser does it.
686 * hdr_idx_init(&s->txn.hdr_idx);
687 */
688 }
689
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200690 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100691 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200692 if (si->connect) {
693 /* real connections have timeouts */
694 req->wto = s->be->timeout.server;
695 rep->rto = s->be->timeout.server;
696 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100697 req->wex = TICK_ETERNITY;
698}
699
700/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
701 * Other input states are simply ignored.
702 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
703 * Flags must have previously been updated for timeouts and other conditions.
704 */
705void sess_update_stream_int(struct session *s, struct stream_interface *si)
706{
Willy Tarreau827aee92011-03-10 16:55:02 +0100707 struct server *srv = target_srv(&s->target);
708
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100709 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",
710 now_ms, __FUNCTION__,
711 s,
712 s->req, s->rep,
713 s->req->rex, s->rep->wex,
714 s->req->flags, s->rep->flags,
715 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
716
717 if (si->state == SI_ST_ASS) {
718 /* Server assigned to connection request, we have to try to connect now */
719 int conn_err;
720
721 conn_err = connect_server(s);
Willy Tarreau827aee92011-03-10 16:55:02 +0100722 srv = target_srv(&s->target);
723
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100724 if (conn_err == SN_ERR_NONE) {
725 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100726 if (srv)
727 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100728 return;
729 }
730
731 /* We have received a synchronous error. We might have to
732 * abort, retry immediately or redispatch.
733 */
734 if (conn_err == SN_ERR_INTERNAL) {
735 if (!si->err_type) {
736 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +0100737 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100738 }
739
Willy Tarreau827aee92011-03-10 16:55:02 +0100740 if (srv)
741 srv_inc_sess_ctr(srv);
742 if (srv)
743 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100744 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100745
746 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100747 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100748 if (may_dequeue_tasks(srv, s->be))
749 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100750
751 /* Failed and not retryable. */
752 si->shutr(si);
753 si->shutw(si);
754 si->ob->flags |= BF_WRITE_ERROR;
755
756 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
757
758 /* no session was ever accounted for this server */
759 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100760 if (s->srv_error)
761 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100762 return;
763 }
764
765 /* We are facing a retryable error, but we don't want to run a
766 * turn-around now, as the problem is likely a source port
767 * allocation problem, so we want to retry now.
768 */
769 si->state = SI_ST_CER;
770 si->flags &= ~SI_FL_ERR;
771 sess_update_st_cer(s, si);
772 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
773 return;
774 }
775 else if (si->state == SI_ST_QUE) {
776 /* connection request was queued, check for any update */
777 if (!s->pend_pos) {
778 /* The connection is not in the queue anymore. Either
779 * we have a server connection slot available and we
780 * go directly to the assigned state, or we need to
781 * load-balance first and go to the INI state.
782 */
783 si->exp = TICK_ETERNITY;
784 if (unlikely(!(s->flags & SN_ASSIGNED)))
785 si->state = SI_ST_REQ;
786 else {
787 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
788 si->state = SI_ST_ASS;
789 }
790 return;
791 }
792
793 /* Connection request still in queue... */
794 if (si->flags & SI_FL_EXP) {
795 /* ... and timeout expired */
796 si->exp = TICK_ETERNITY;
797 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100798 if (srv)
799 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100800 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100801 si->shutr(si);
802 si->shutw(si);
803 si->ob->flags |= BF_WRITE_TIMEOUT;
804 if (!si->err_type)
805 si->err_type = SI_ET_QUEUE_TO;
806 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100807 if (s->srv_error)
808 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100809 return;
810 }
811
812 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200813 if ((si->ob->flags & (BF_READ_ERROR)) ||
814 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200815 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100816 /* give up */
817 si->exp = TICK_ETERNITY;
818 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
819 si->shutr(si);
820 si->shutw(si);
821 si->err_type |= SI_ET_QUEUE_ABRT;
822 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100823 if (s->srv_error)
824 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100825 return;
826 }
827
828 /* Nothing changed */
829 return;
830 }
831 else if (si->state == SI_ST_TAR) {
832 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200833 if ((si->ob->flags & (BF_READ_ERROR)) ||
834 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200835 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100836 /* give up */
837 si->exp = TICK_ETERNITY;
838 si->shutr(si);
839 si->shutw(si);
840 si->err_type |= SI_ET_CONN_ABRT;
841 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100842 if (s->srv_error)
843 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100844 return;
845 }
846
847 if (!(si->flags & SI_FL_EXP))
848 return; /* still in turn-around */
849
850 si->exp = TICK_ETERNITY;
851
852 /* we keep trying on the same server as long as the session is
853 * marked "assigned".
854 * FIXME: Should we force a redispatch attempt when the server is down ?
855 */
856 if (s->flags & SN_ASSIGNED)
857 si->state = SI_ST_ASS;
858 else
859 si->state = SI_ST_REQ;
860 return;
861 }
862}
863
864/* This function initiates a server connection request on a stream interface
865 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
866 * indicating that a server has been assigned. It may also return SI_ST_QUE,
867 * or SI_ST_CLO upon error.
868 */
869static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
870 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",
871 now_ms, __FUNCTION__,
872 s,
873 s->req, s->rep,
874 s->req->rex, s->rep->wex,
875 s->req->flags, s->rep->flags,
876 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
877
878 if (si->state != SI_ST_REQ)
879 return;
880
881 /* Try to assign a server */
882 if (srv_redispatch_connect(s) != 0) {
883 /* We did not get a server. Either we queued the
884 * connection request, or we encountered an error.
885 */
886 if (si->state == SI_ST_QUE)
887 return;
888
889 /* we did not get any server, let's check the cause */
890 si->shutr(si);
891 si->shutw(si);
892 si->ob->flags |= BF_WRITE_ERROR;
893 if (!si->err_type)
894 si->err_type = SI_ET_CONN_OTHER;
895 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100896 if (s->srv_error)
897 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100898 return;
899 }
900
901 /* The server is assigned */
902 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
903 si->state = SI_ST_ASS;
904}
905
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200906/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100907 * if appropriate. The default_backend rule is also considered, then the
908 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200909 * It returns 1 if the processing can continue on next analysers, or zero if it
910 * either needs more data or wants to immediately abort the request.
911 */
912int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
913{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200914 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100915
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200916 req->analysers &= ~an_bit;
917 req->analyse_exp = TICK_ETERNITY;
918
919 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
920 now_ms, __FUNCTION__,
921 s,
922 req,
923 req->rex, req->wex,
924 req->flags,
925 req->l,
926 req->analysers);
927
928 /* now check whether we have some switching rules for this request */
929 if (!(s->flags & SN_BE_ASSIGNED)) {
930 struct switching_rule *rule;
931
932 list_for_each_entry(rule, &s->fe->switching_rules, list) {
933 int ret;
934
935 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
936 ret = acl_pass(ret);
937 if (rule->cond->pol == ACL_COND_UNLESS)
938 ret = !ret;
939
940 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200941 if (!session_set_backend(s, rule->be.backend))
942 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200943 break;
944 }
945 }
946
947 /* To ensure correct connection accounting on the backend, we
948 * have to assign one if it was not set (eg: a listen). This
949 * measure also takes care of correctly setting the default
950 * backend if any.
951 */
952 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200953 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
954 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200955 }
956
Willy Tarreaufb356202010-08-03 14:02:05 +0200957 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
958 if (s->fe == s->be) {
959 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200960 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200961 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200962
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200963 /* 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 +0100964 * persistence rule, and report that in the session.
965 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200966 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100967 int ret = 1;
968
969 if (prst_rule->cond) {
970 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
971 ret = acl_pass(ret);
972 if (prst_rule->cond->pol == ACL_COND_UNLESS)
973 ret = !ret;
974 }
975
976 if (ret) {
977 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200978 if (prst_rule->type == PERSIST_TYPE_FORCE) {
979 s->flags |= SN_FORCE_PRST;
980 } else {
981 s->flags |= SN_IGNORE_PRST;
982 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100983 break;
984 }
985 }
986
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200987 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200988
989 sw_failed:
990 /* immediately abort this request in case of allocation failure */
991 buffer_abort(s->req);
992 buffer_abort(s->rep);
993
994 if (!(s->flags & SN_ERR_MASK))
995 s->flags |= SN_ERR_RESOURCE;
996 if (!(s->flags & SN_FINST_MASK))
997 s->flags |= SN_FINST_R;
998
999 s->txn.status = 500;
1000 s->req->analysers = 0;
1001 s->req->analyse_exp = TICK_ETERNITY;
1002 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001003}
1004
Emeric Brun1d33b292010-01-04 15:47:17 +01001005/* This stream analyser works on a request. It applies all sticking rules on
1006 * it then returns 1. The data must already be present in the buffer otherwise
1007 * they won't match. It always returns 1.
1008 */
1009int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
1010{
1011 struct proxy *px = s->be;
1012 struct sticking_rule *rule;
1013
1014 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1015 now_ms, __FUNCTION__,
1016 s,
1017 req,
1018 req->rex, req->wex,
1019 req->flags,
1020 req->l,
1021 req->analysers);
1022
1023 list_for_each_entry(rule, &px->sticking_rules, list) {
1024 int ret = 1 ;
1025 int i;
1026
1027 for (i = 0; i < s->store_count; i++) {
1028 if (rule->table.t == s->store[i].table)
1029 break;
1030 }
1031
1032 if (i != s->store_count)
1033 continue;
1034
1035 if (rule->cond) {
1036 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1037 ret = acl_pass(ret);
1038 if (rule->cond->pol == ACL_COND_UNLESS)
1039 ret = !ret;
1040 }
1041
1042 if (ret) {
1043 struct stktable_key *key;
1044
Emeric Brun485479d2010-09-23 18:02:19 +02001045 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001046 if (!key)
1047 continue;
1048
1049 if (rule->flags & STK_IS_MATCH) {
1050 struct stksess *ts;
1051
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001052 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001053 if (!(s->flags & SN_ASSIGNED)) {
1054 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001055 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001056
1057 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001058 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1059 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001060 if (node) {
1061 struct server *srv;
1062
1063 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001064 if ((srv->state & SRV_RUNNING) ||
1065 (px->options & PR_O_PERSIST) ||
1066 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001067 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001068 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001069 }
1070 }
1071 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001072 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001073 }
1074 }
1075 if (rule->flags & STK_IS_STORE) {
1076 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1077 struct stksess *ts;
1078
1079 ts = stksess_new(rule->table.t, key);
1080 if (ts) {
1081 s->store[s->store_count].table = rule->table.t;
1082 s->store[s->store_count++].ts = ts;
1083 }
1084 }
1085 }
1086 }
1087 }
1088
1089 req->analysers &= ~an_bit;
1090 req->analyse_exp = TICK_ETERNITY;
1091 return 1;
1092}
1093
1094/* This stream analyser works on a response. It applies all store rules on it
1095 * then returns 1. The data must already be present in the buffer otherwise
1096 * they won't match. It always returns 1.
1097 */
1098int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1099{
1100 struct proxy *px = s->be;
1101 struct sticking_rule *rule;
1102 int i;
1103
1104 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1105 now_ms, __FUNCTION__,
1106 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001107 rep,
1108 rep->rex, rep->wex,
1109 rep->flags,
1110 rep->l,
1111 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001112
1113 list_for_each_entry(rule, &px->storersp_rules, list) {
1114 int ret = 1 ;
1115 int storereqidx = -1;
1116
1117 for (i = 0; i < s->store_count; i++) {
1118 if (rule->table.t == s->store[i].table) {
1119 if (!(s->store[i].flags))
1120 storereqidx = i;
1121 break;
1122 }
1123 }
1124
1125 if ((i != s->store_count) && (storereqidx == -1))
1126 continue;
1127
1128 if (rule->cond) {
1129 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1130 ret = acl_pass(ret);
1131 if (rule->cond->pol == ACL_COND_UNLESS)
1132 ret = !ret;
1133 }
1134
1135 if (ret) {
1136 struct stktable_key *key;
1137
Emeric Brun485479d2010-09-23 18:02:19 +02001138 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001139 if (!key)
1140 continue;
1141
1142 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001143 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001144 s->store[storereqidx].flags = 1;
1145 }
1146 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1147 struct stksess *ts;
1148
1149 ts = stksess_new(rule->table.t, key);
1150 if (ts) {
1151 s->store[s->store_count].table = rule->table.t;
1152 s->store[s->store_count].flags = 1;
1153 s->store[s->store_count++].ts = ts;
1154 }
1155 }
1156 }
1157 }
1158
1159 /* process store request and store response */
1160 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001161 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001162 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001163
1164 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1165 if (ts) {
1166 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001167 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001168 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001169 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001170 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001171 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001172
1173 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001174 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001175 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001176 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001177 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001178
1179 rep->analysers &= ~an_bit;
1180 rep->analyse_exp = TICK_ETERNITY;
1181 return 1;
1182}
1183
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001184/* This macro is very specific to the function below. See the comments in
1185 * process_session() below to understand the logic and the tests.
1186 */
1187#define UPDATE_ANALYSERS(real, list, back, flag) { \
1188 list = (((list) & ~(flag)) | ~(back)) & (real); \
1189 back = real; \
1190 if (!(list)) \
1191 break; \
1192 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1193 continue; \
1194}
1195
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001196/* Processes the client, server, request and response jobs of a session task,
1197 * then puts it back to the wait queue in a clean state, or cleans up its
1198 * resources if it must be deleted. Returns in <next> the date the task wants
1199 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1200 * nothing too many times, the request and response buffers flags are monitored
1201 * and each function is called only if at least another function has changed at
1202 * least one flag it is interested in.
1203 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001204struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001205{
Willy Tarreau827aee92011-03-10 16:55:02 +01001206 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001207 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001208 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001209 unsigned int rq_prod_last, rq_cons_last;
1210 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001211 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001212
1213 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1214 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1215
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001216 /* this data may be no longer valid, clear it */
1217 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1218
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001219 /* This flag must explicitly be set every time */
1220 s->req->flags &= ~BF_READ_NOEXP;
1221
1222 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001223 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1224 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001225
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001226 /* we don't want the stream interface functions to recursively wake us up */
1227 if (s->req->prod->owner == t)
1228 s->req->prod->flags |= SI_FL_DONT_WAKE;
1229 if (s->req->cons->owner == t)
1230 s->req->cons->flags |= SI_FL_DONT_WAKE;
1231
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001232 /* 1a: Check for low level timeouts if needed. We just set a flag on
1233 * stream interfaces when their timeouts have expired.
1234 */
1235 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1236 stream_int_check_timeouts(&s->si[0]);
1237 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001238
1239 /* check buffer timeouts, and close the corresponding stream interfaces
1240 * for future reads or writes. Note: this will also concern upper layers
1241 * but we do not touch any other flag. We must be careful and correctly
1242 * detect state changes when calling them.
1243 */
1244
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001245 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001246
Willy Tarreau14641402009-12-29 14:49:56 +01001247 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1248 s->req->cons->flags |= SI_FL_NOLINGER;
1249 s->req->cons->shutw(s->req->cons);
1250 }
1251
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001252 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1253 s->req->prod->shutr(s->req->prod);
1254
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001255 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001256
Willy Tarreau14641402009-12-29 14:49:56 +01001257 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1258 s->rep->cons->flags |= SI_FL_NOLINGER;
1259 s->rep->cons->shutw(s->rep->cons);
1260 }
1261
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001262 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1263 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001264 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001265
1266 /* 1b: check for low-level errors reported at the stream interface.
1267 * First we check if it's a retryable error (in which case we don't
1268 * want to tell the buffer). Otherwise we report the error one level
1269 * upper by setting flags into the buffers. Note that the side towards
1270 * the client cannot have connect (hence retryable) errors. Also, the
1271 * connection setup code must be able to deal with any type of abort.
1272 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001273 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001274 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1275 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1276 s->si[0].shutr(&s->si[0]);
1277 s->si[0].shutw(&s->si[0]);
1278 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001279 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001280 s->be->be_counters.cli_aborts++;
1281 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001282 if (srv)
1283 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001284 if (!(s->flags & SN_ERR_MASK))
1285 s->flags |= SN_ERR_CLICL;
1286 if (!(s->flags & SN_FINST_MASK))
1287 s->flags |= SN_FINST_D;
1288 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001289 }
1290 }
1291
1292 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1293 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1294 s->si[1].shutr(&s->si[1]);
1295 s->si[1].shutw(&s->si[1]);
1296 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001297 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001298 if (srv)
1299 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001300 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001301 s->be->be_counters.srv_aborts++;
1302 s->fe->fe_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) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001662 s->be->be_counters.cli_aborts++;
1663 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001664 if (srv)
1665 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001666 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001667 }
1668 else if (s->req->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001669 s->be->be_counters.cli_aborts++;
1670 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001671 if (srv)
1672 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001673 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001674 }
1675 else if (s->req->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001676 s->be->be_counters.srv_aborts++;
1677 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001678 if (srv)
1679 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001680 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001681 }
1682 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001683 s->be->be_counters.srv_aborts++;
1684 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001685 if (srv)
1686 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001687 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001688 }
Willy Tarreau84455332009-03-15 22:34:05 +01001689 sess_set_term_flags(s);
1690 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001691 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1692 /* Report it if the server got an error or a read timeout expired */
1693 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001694 if (s->rep->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001695 s->be->be_counters.srv_aborts++;
1696 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001697 if (srv)
1698 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001699 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001700 }
1701 else if (s->rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001702 s->be->be_counters.srv_aborts++;
1703 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001704 if (srv)
1705 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001706 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001707 }
1708 else if (s->rep->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001709 s->be->be_counters.cli_aborts++;
1710 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001711 if (srv)
1712 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001713 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001714 }
1715 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001716 s->be->be_counters.cli_aborts++;
1717 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001718 if (srv)
1719 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001720 s->flags |= SN_ERR_CLITO;
1721 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001722 sess_set_term_flags(s);
1723 }
Willy Tarreau84455332009-03-15 22:34:05 +01001724 }
1725
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001726 /*
1727 * Here we take care of forwarding unhandled data. This also includes
1728 * connection establishments and shutdown requests.
1729 */
1730
1731
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001732 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001733 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001734 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1735 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001736 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001737 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001738 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001739 (s->req->prod->state >= SI_ST_EST) &&
1740 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001741 /* This buffer is freewheeling, there's no analyser nor hijacker
1742 * attached to it. If any data are left in, we'll permit them to
1743 * move.
1744 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001745 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001746 buffer_auto_connect(s->req);
1747 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001748 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001749
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001750 /* We'll let data flow between the producer (if still connected)
1751 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001752 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001753 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001754 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001755 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001756
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001757 /* check if it is wise to enable kernel splicing to forward request data */
1758 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1759 s->req->to_forward &&
1760 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001761 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001762 (pipes_used < global.maxpipes) &&
1763 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1764 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1765 (s->req->flags & BF_STREAMER_FAST)))) {
1766 s->req->flags |= BF_KERN_SPLICING;
1767 }
1768
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001769 /* reflect what the L7 analysers have seen last */
1770 rqf_last = s->req->flags;
1771
1772 /*
1773 * Now forward all shutdown requests between both sides of the buffer
1774 */
1775
Willy Tarreau520d95e2009-09-19 21:04:57 +02001776 /* first, let's check if the request buffer needs to shutdown(write), which may
1777 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001778 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001779 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001780 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001781 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001782 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001783
1784 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001785 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 +01001786 s->req->cons->shutw(s->req->cons);
1787
1788 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001789 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1790 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001791 buffer_shutr_now(s->req);
1792
1793 /* shutdown(read) pending */
1794 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1795 s->req->prod->shutr(s->req->prod);
1796
Willy Tarreau520d95e2009-09-19 21:04:57 +02001797 /* it's possible that an upper layer has requested a connection setup or abort.
1798 * There are 2 situations where we decide to establish a new connection :
1799 * - there are data scheduled for emission in the buffer
1800 * - the BF_AUTO_CONNECT flag is set (active connection)
1801 */
1802 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001803 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001804 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001805 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001806 * switch to the connected state, otherwise we perform a connection
1807 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001808 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001809 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001810 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001811 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001812 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001813 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1814 s->req->wex = TICK_ETERNITY;
1815 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001816 }
Willy Tarreau73201222009-08-16 18:27:24 +02001817 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001818 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001819 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001820 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1821 buffer_shutr_now(s->rep);
1822 }
Willy Tarreau92795622009-03-06 12:51:23 +01001823 }
1824
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001825
1826 /* we may have a pending connection request, or a connection waiting
1827 * for completion.
1828 */
1829 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1830 do {
1831 /* nb: step 1 might switch from QUE to ASS, but we first want
1832 * to give a chance to step 2 to perform a redirect if needed.
1833 */
1834 if (s->si[1].state != SI_ST_REQ)
1835 sess_update_stream_int(s, &s->si[1]);
1836 if (s->si[1].state == SI_ST_REQ)
1837 sess_prepare_conn_req(s, &s->si[1]);
1838
Willy Tarreau827aee92011-03-10 16:55:02 +01001839 srv = target_srv(&s->target);
1840 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001841 perform_http_redirect(s, &s->si[1]);
1842 } while (s->si[1].state == SI_ST_ASS);
1843 }
1844
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001845 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001846 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001847 goto resync_stream_interface;
1848
Willy Tarreau815a9b22010-07-27 17:15:12 +02001849 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001850 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001851 goto resync_request;
1852
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001853 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001854
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001855 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001856 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001857 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1858 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001859 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001860 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001861 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001862 (s->rep->prod->state >= SI_ST_EST) &&
1863 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001864 /* This buffer is freewheeling, there's no analyser nor hijacker
1865 * attached to it. If any data are left in, we'll permit them to
1866 * move.
1867 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001868 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001869 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001870 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001871
1872 /* We'll let data flow between the producer (if still connected)
1873 * to the consumer.
1874 */
1875 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001876 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001877 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001878
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001879 /* check if it is wise to enable kernel splicing to forward response data */
1880 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1881 s->rep->to_forward &&
1882 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001883 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001884 (pipes_used < global.maxpipes) &&
1885 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1886 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1887 (s->rep->flags & BF_STREAMER_FAST)))) {
1888 s->rep->flags |= BF_KERN_SPLICING;
1889 }
1890
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001891 /* reflect what the L7 analysers have seen last */
1892 rpf_last = s->rep->flags;
1893
1894 /*
1895 * Now forward all shutdown requests between both sides of the buffer
1896 */
1897
1898 /*
1899 * FIXME: this is probably where we should produce error responses.
1900 */
1901
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001902 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001903 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1904 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001905 buffer_shutw_now(s->rep);
1906
1907 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001908 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 +01001909 s->rep->cons->shutw(s->rep->cons);
1910
1911 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001912 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1913 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001914 buffer_shutr_now(s->rep);
1915
1916 /* shutdown(read) pending */
1917 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1918 s->rep->prod->shutr(s->rep->prod);
1919
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001920 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001921 goto resync_stream_interface;
1922
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001923 if (s->req->flags != rqf_last)
1924 goto resync_request;
1925
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001926 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001927 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001928
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001929 /* we're interested in getting wakeups again */
1930 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1931 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1932
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001933 /* This is needed only when debugging is enabled, to indicate
1934 * client-side or server-side close. Please note that in the unlikely
1935 * event where both sides would close at once, the sequence is reported
1936 * on the server side first.
1937 */
1938 if (unlikely((global.mode & MODE_DEBUG) &&
1939 (!(global.mode & MODE_QUIET) ||
1940 (global.mode & MODE_VERBOSE)))) {
1941 int len;
1942
1943 if (s->si[1].state == SI_ST_CLO &&
1944 s->si[1].prev_state == SI_ST_EST) {
1945 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1946 s->uniq_id, s->be->id,
1947 (unsigned short)s->si[0].fd,
1948 (unsigned short)s->si[1].fd);
1949 write(1, trash, len);
1950 }
1951
1952 if (s->si[0].state == SI_ST_CLO &&
1953 s->si[0].prev_state == SI_ST_EST) {
1954 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1955 s->uniq_id, s->be->id,
1956 (unsigned short)s->si[0].fd,
1957 (unsigned short)s->si[1].fd);
1958 write(1, trash, len);
1959 }
1960 }
1961
1962 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1963 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1964
1965 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1966 session_process_counters(s);
1967
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001968 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001969 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001970
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001971 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001972 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001973
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001974 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1975 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 +01001976 s->si[0].prev_state = s->si[0].state;
1977 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001978 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1979 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001980
1981 /* Trick: if a request is being waiting for the server to respond,
1982 * and if we know the server can timeout, we don't want the timeout
1983 * to expire on the client side first, but we're still interested
1984 * in passing data from the client to the server (eg: POST). Thus,
1985 * we can cancel the client's request timeout if the server's
1986 * request timeout is set and the server has not yet sent a response.
1987 */
1988
Willy Tarreau520d95e2009-09-19 21:04:57 +02001989 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001990 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1991 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001992 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001993 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001994
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001995 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001996 * Note that this one may wake the task up again.
1997 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001998 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
1999 s->rep->cons->target.type == TARG_TYPE_APPLET) {
2000 if (s->req->cons->target.type == TARG_TYPE_APPLET)
2001 s->req->cons->target.ptr.a->fct(s->req->cons);
2002 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
2003 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002004 if (task_in_rq(t)) {
2005 /* If we woke up, we don't want to requeue the
2006 * task to the wait queue, but rather requeue
2007 * it into the runqueue ASAP.
2008 */
2009 t->expire = TICK_ETERNITY;
2010 return t;
2011 }
2012 }
2013
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002014 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2015 tick_first(s->rep->rex, s->rep->wex));
2016 if (s->req->analysers)
2017 t->expire = tick_first(t->expire, s->req->analyse_exp);
2018
2019 if (s->si[0].exp)
2020 t->expire = tick_first(t->expire, s->si[0].exp);
2021
2022 if (s->si[1].exp)
2023 t->expire = tick_first(t->expire, s->si[1].exp);
2024
2025#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002026 fprintf(stderr,
2027 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2028 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2029 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2030 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 +01002031#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002032
2033#ifdef DEBUG_DEV
2034 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002035 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002036 ABORT_NOW();
2037#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002038 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002039 }
2040
2041 s->fe->feconn--;
2042 if (s->flags & SN_BE_ASSIGNED)
2043 s->be->beconn--;
2044 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002045 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002046 s->listener->nbconn--;
2047 if (s->listener->state == LI_FULL &&
2048 s->listener->nbconn < s->listener->maxconn) {
2049 /* we should reactivate the listener */
2050 EV_FD_SET(s->listener->fd, DIR_RD);
2051 s->listener->state = LI_READY;
2052 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002053
2054 if (unlikely((global.mode & MODE_DEBUG) &&
2055 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2056 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002057 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002058 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002059 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002060 write(1, trash, len);
2061 }
2062
2063 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2064 session_process_counters(s);
2065
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002066 if (s->txn.status) {
2067 int n;
2068
2069 n = s->txn.status / 100;
2070 if (n < 1 || n > 5)
2071 n = 0;
2072
2073 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002074 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002075
Willy Tarreau24657792010-02-26 10:30:28 +01002076 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002077 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002078 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002079 }
2080
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002081 /* let's do a final log if we need it */
2082 if (s->logs.logwait &&
2083 !(s->flags & SN_MONITOR) &&
2084 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002085 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002086 }
2087
2088 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002089 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002090 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002091 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002092 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002093}
2094
Willy Tarreau7c669d72008-06-20 15:04:11 +02002095/*
2096 * This function adjusts sess->srv_conn and maintains the previous and new
2097 * server's served session counts. Setting newsrv to NULL is enough to release
2098 * current connection slot. This function also notifies any LB algo which might
2099 * expect to be informed about any change in the number of active sessions on a
2100 * server.
2101 */
2102void sess_change_server(struct session *sess, struct server *newsrv)
2103{
2104 if (sess->srv_conn == newsrv)
2105 return;
2106
2107 if (sess->srv_conn) {
2108 sess->srv_conn->served--;
2109 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2110 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
2111 sess->srv_conn = NULL;
2112 }
2113
2114 if (newsrv) {
2115 newsrv->served++;
2116 if (newsrv->proxy->lbprm.server_take_conn)
2117 newsrv->proxy->lbprm.server_take_conn(newsrv);
2118 sess->srv_conn = newsrv;
2119 }
2120}
2121
Willy Tarreau84455332009-03-15 22:34:05 +01002122/* Set correct session termination flags in case no analyser has done it. It
2123 * also counts a failed request if the server state has not reached the request
2124 * stage.
2125 */
2126void sess_set_term_flags(struct session *s)
2127{
2128 if (!(s->flags & SN_FINST_MASK)) {
2129 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002130
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002131 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002132 if (s->listener->counters)
2133 s->listener->counters->failed_req++;
2134
Willy Tarreau84455332009-03-15 22:34:05 +01002135 s->flags |= SN_FINST_R;
2136 }
2137 else if (s->si[1].state == SI_ST_QUE)
2138 s->flags |= SN_FINST_Q;
2139 else if (s->si[1].state < SI_ST_EST)
2140 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002141 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002142 s->flags |= SN_FINST_D;
2143 else
2144 s->flags |= SN_FINST_L;
2145 }
2146}
2147
2148/* Handle server-side errors for default protocols. It is called whenever a a
2149 * connection setup is aborted or a request is aborted in queue. It sets the
2150 * session termination flags so that the caller does not have to worry about
2151 * them. It's installed as ->srv_error for the server-side stream_interface.
2152 */
2153void default_srv_error(struct session *s, struct stream_interface *si)
2154{
2155 int err_type = si->err_type;
2156 int err = 0, fin = 0;
2157
2158 if (err_type & SI_ET_QUEUE_ABRT) {
2159 err = SN_ERR_CLICL;
2160 fin = SN_FINST_Q;
2161 }
2162 else if (err_type & SI_ET_CONN_ABRT) {
2163 err = SN_ERR_CLICL;
2164 fin = SN_FINST_C;
2165 }
2166 else if (err_type & SI_ET_QUEUE_TO) {
2167 err = SN_ERR_SRVTO;
2168 fin = SN_FINST_Q;
2169 }
2170 else if (err_type & SI_ET_QUEUE_ERR) {
2171 err = SN_ERR_SRVCL;
2172 fin = SN_FINST_Q;
2173 }
2174 else if (err_type & SI_ET_CONN_TO) {
2175 err = SN_ERR_SRVTO;
2176 fin = SN_FINST_C;
2177 }
2178 else if (err_type & SI_ET_CONN_ERR) {
2179 err = SN_ERR_SRVCL;
2180 fin = SN_FINST_C;
2181 }
2182 else /* SI_ET_CONN_OTHER and others */ {
2183 err = SN_ERR_INTERNAL;
2184 fin = SN_FINST_C;
2185 }
2186
2187 if (!(s->flags & SN_ERR_MASK))
2188 s->flags |= err;
2189 if (!(s->flags & SN_FINST_MASK))
2190 s->flags |= fin;
2191}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002192
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002193
Willy Tarreau8b22a712010-06-18 17:46:06 +02002194/************************************************************************/
2195/* All supported ACL keywords must be declared here. */
2196/************************************************************************/
2197
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002198/* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */
2199static int
2200acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2201{
2202 test->flags = ACL_TEST_F_VOL_TEST;
2203 test->i = 0;
2204 if (ts != NULL) {
2205 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2206 if (!ptr)
2207 return 0; /* parameter not stored */
2208 test->i = stktable_data_cast(ptr, gpc0);
2209 }
2210 return 1;
2211}
2212
2213/* set test->i to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002214 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002215 */
2216static int
Willy Tarreau56123282010-08-06 19:06:56 +02002217acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2218 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002219{
Willy Tarreau56123282010-08-06 19:06:56 +02002220 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002221 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002222 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002223}
2224
2225/* set test->i to the General Purpose Counter 0 value from the session's tracked
2226 * backend counters.
2227 */
2228static int
Willy Tarreau56123282010-08-06 19:06:56 +02002229acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2230 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002231{
Willy Tarreau56123282010-08-06 19:06:56 +02002232 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002233 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002234 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002235}
2236
2237/* set test->i to the General Purpose Counter 0 value from the session's source
2238 * address in the table pointed to by expr.
2239 */
2240static int
2241acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2242 struct acl_expr *expr, struct acl_test *test)
2243{
2244 struct stktable_key *key;
2245
2246 key = tcpv4_src_to_stktable_key(l4);
2247 if (!key)
2248 return 0; /* only TCPv4 is supported right now */
2249
2250 if (expr->arg_len)
2251 px = find_stktable(expr->arg.str);
2252
2253 if (!px)
2254 return 0; /* table not found */
2255
2256 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2257}
2258
2259/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
2260 * return it into test->i.
2261 */
2262static int
2263acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2264{
2265 test->flags = ACL_TEST_F_VOL_TEST;
2266 test->i = 0;
2267 if (ts != NULL) {
2268 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2269 if (!ptr)
2270 return 0; /* parameter not stored */
2271 test->i = ++stktable_data_cast(ptr, gpc0);
2272 }
2273 return 1;
2274}
2275
2276/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002277 * frontend counters and return it into test->i.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002278 */
2279static int
Willy Tarreau56123282010-08-06 19:06:56 +02002280acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2281 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002282{
Willy Tarreau56123282010-08-06 19:06:56 +02002283 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002284 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002285 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002286}
2287
2288/* Increment the General Purpose Counter 0 value from the session's tracked
2289 * backend counters and return it into test->i.
2290 */
2291static int
Willy Tarreau56123282010-08-06 19:06:56 +02002292acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2293 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002294{
Willy Tarreau56123282010-08-06 19:06:56 +02002295 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002296 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002297 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002298}
2299
2300/* Increment the General Purpose Counter 0 value from the session's source
2301 * address in the table pointed to by expr, and return it into test->i.
2302 */
2303static int
2304acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2305 struct acl_expr *expr, struct acl_test *test)
2306{
2307 struct stktable_key *key;
2308
2309 key = tcpv4_src_to_stktable_key(l4);
2310 if (!key)
2311 return 0; /* only TCPv4 is supported right now */
2312
2313 if (expr->arg_len)
2314 px = find_stktable(expr->arg.str);
2315
2316 if (!px)
2317 return 0; /* table not found */
2318
2319 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2320}
2321
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002322/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2323static int
2324acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2325{
2326 test->flags = ACL_TEST_F_VOL_TEST;
2327 test->i = 0;
2328 if (ts != NULL) {
2329 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2330 if (!ptr)
2331 return 0; /* parameter not stored */
2332 test->i = stktable_data_cast(ptr, conn_cnt);
2333 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002334 return 1;
2335}
2336
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002337/* set test->i to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002338static int
Willy Tarreau56123282010-08-06 19:06:56 +02002339acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2340 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002341{
Willy Tarreau56123282010-08-06 19:06:56 +02002342 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002343 return 0;
2344
Willy Tarreau56123282010-08-06 19:06:56 +02002345 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002346}
2347
2348/* set test->i to the cumulated number of connections from the session's tracked BE counters */
2349static int
Willy Tarreau56123282010-08-06 19:06:56 +02002350acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2351 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002352{
Willy Tarreau56123282010-08-06 19:06:56 +02002353 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002354 return 0;
2355
Willy Tarreau56123282010-08-06 19:06:56 +02002356 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002357}
2358
2359/* set test->i to the cumulated number of connections from the session's source
2360 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002361 */
2362static int
2363acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2364 struct acl_expr *expr, struct acl_test *test)
2365{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002366 struct stktable_key *key;
2367
2368 key = tcpv4_src_to_stktable_key(l4);
2369 if (!key)
2370 return 0; /* only TCPv4 is supported right now */
2371
2372 if (expr->arg_len)
2373 px = find_stktable(expr->arg.str);
2374
2375 if (!px)
2376 return 0; /* table not found */
2377
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002378 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002379}
2380
Willy Tarreau91c43d72010-06-20 11:19:22 +02002381/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2382static int
2383acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2384{
2385 test->flags = ACL_TEST_F_VOL_TEST;
2386 test->i = 0;
2387 if (ts != NULL) {
2388 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2389 if (!ptr)
2390 return 0; /* parameter not stored */
2391 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2392 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2393 }
2394 return 1;
2395}
2396
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002397/* set test->i to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002398 * the configured period.
2399 */
2400static int
Willy Tarreau56123282010-08-06 19:06:56 +02002401acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2402 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002403{
Willy Tarreau56123282010-08-06 19:06:56 +02002404 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002405 return 0;
2406
Willy Tarreau56123282010-08-06 19:06:56 +02002407 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002408}
2409
2410/* set test->i to the connection rate from the session's tracked BE counters over
2411 * the configured period.
2412 */
2413static int
Willy Tarreau56123282010-08-06 19:06:56 +02002414acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2415 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002416{
Willy Tarreau56123282010-08-06 19:06:56 +02002417 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002418 return 0;
2419
Willy Tarreau56123282010-08-06 19:06:56 +02002420 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002421}
2422
2423/* set test->i to the connection rate from the session's source address in the
2424 * table pointed to by expr, over the configured period.
2425 */
2426static int
2427acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2428 struct acl_expr *expr, struct acl_test *test)
2429{
2430 struct stktable_key *key;
2431
2432 key = tcpv4_src_to_stktable_key(l4);
2433 if (!key)
2434 return 0; /* only TCPv4 is supported right now */
2435
2436 if (expr->arg_len)
2437 px = find_stktable(expr->arg.str);
2438
2439 if (!px)
2440 return 0; /* table not found */
2441
2442 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2443}
2444
Willy Tarreau8b22a712010-06-18 17:46:06 +02002445/* set test->i to the number of connections from the session's source address
2446 * in the table pointed to by expr, after updating it.
2447 */
2448static int
2449acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2450 struct acl_expr *expr, struct acl_test *test)
2451{
2452 struct stksess *ts;
2453 struct stktable_key *key;
2454 void *ptr;
2455
2456 key = tcpv4_src_to_stktable_key(l4);
2457 if (!key)
2458 return 0; /* only TCPv4 is supported right now */
2459
2460 if (expr->arg_len)
2461 px = find_stktable(expr->arg.str);
2462
2463 if (!px)
2464 return 0; /* table not found */
2465
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002466 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2467 /* entry does not exist and could not be created */
2468 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002469
2470 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2471 if (!ptr)
2472 return 0; /* parameter not stored in this table */
2473
2474 test->i = ++stktable_data_cast(ptr, conn_cnt);
2475 test->flags = ACL_TEST_F_VOL_TEST;
2476 return 1;
2477}
2478
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002479/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2480static int
2481acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2482{
2483 test->flags = ACL_TEST_F_VOL_TEST;
2484 test->i = 0;
2485
2486 if (ts != NULL) {
2487 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2488 if (!ptr)
2489 return 0; /* parameter not stored */
2490 test->i = stktable_data_cast(ptr, conn_cur);
2491 }
2492 return 1;
2493}
2494
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002495/* set test->i to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002496static int
Willy Tarreau56123282010-08-06 19:06:56 +02002497acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2498 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002499{
Willy Tarreau56123282010-08-06 19:06:56 +02002500 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002501 return 0;
2502
Willy Tarreau56123282010-08-06 19:06:56 +02002503 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002504}
2505
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002506/* set test->i to the number of concurrent connections from the session's tracked BE counters */
2507static int
Willy Tarreau56123282010-08-06 19:06:56 +02002508acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2509 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002510{
Willy Tarreau56123282010-08-06 19:06:56 +02002511 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002512 return 0;
2513
Willy Tarreau56123282010-08-06 19:06:56 +02002514 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002515}
2516
Willy Tarreau38285c12010-06-18 16:35:43 +02002517/* set test->i to the number of concurrent connections from the session's source
2518 * address in the table pointed to by expr.
2519 */
2520static int
2521acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2522 struct acl_expr *expr, struct acl_test *test)
2523{
Willy Tarreau38285c12010-06-18 16:35:43 +02002524 struct stktable_key *key;
2525
2526 key = tcpv4_src_to_stktable_key(l4);
2527 if (!key)
2528 return 0; /* only TCPv4 is supported right now */
2529
2530 if (expr->arg_len)
2531 px = find_stktable(expr->arg.str);
2532
2533 if (!px)
2534 return 0; /* table not found */
2535
Willy Tarreau1b6e6082011-03-16 06:55:50 +01002536 return acl_fetch_conn_cur(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002537}
2538
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002539/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2540static int
2541acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2542{
2543 test->flags = ACL_TEST_F_VOL_TEST;
2544 test->i = 0;
2545 if (ts != NULL) {
2546 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2547 if (!ptr)
2548 return 0; /* parameter not stored */
2549 test->i = stktable_data_cast(ptr, sess_cnt);
2550 }
2551 return 1;
2552}
2553
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002554/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002555static int
Willy Tarreau56123282010-08-06 19:06:56 +02002556acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2557 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002558{
Willy Tarreau56123282010-08-06 19:06:56 +02002559 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002560 return 0;
2561
Willy Tarreau56123282010-08-06 19:06:56 +02002562 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002563}
2564
2565/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2566static int
Willy Tarreau56123282010-08-06 19:06:56 +02002567acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2568 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002569{
Willy Tarreau56123282010-08-06 19:06:56 +02002570 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002571 return 0;
2572
Willy Tarreau56123282010-08-06 19:06:56 +02002573 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002574}
2575
2576/* set test->i to the cumulated number of session from the session's source
2577 * address in the table pointed to by expr.
2578 */
2579static int
2580acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2581 struct acl_expr *expr, struct acl_test *test)
2582{
2583 struct stktable_key *key;
2584
2585 key = tcpv4_src_to_stktable_key(l4);
2586 if (!key)
2587 return 0; /* only TCPv4 is supported right now */
2588
2589 if (expr->arg_len)
2590 px = find_stktable(expr->arg.str);
2591
2592 if (!px)
2593 return 0; /* table not found */
2594
2595 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2596}
2597
Willy Tarreau91c43d72010-06-20 11:19:22 +02002598/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2599static int
2600acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2601{
2602 test->flags = ACL_TEST_F_VOL_TEST;
2603 test->i = 0;
2604 if (ts != NULL) {
2605 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2606 if (!ptr)
2607 return 0; /* parameter not stored */
2608 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2609 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2610 }
2611 return 1;
2612}
2613
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002614/* set test->i to the session rate from the session's tracked FE counters over
2615 * the configured period.
2616 */
2617static int
Willy Tarreau56123282010-08-06 19:06:56 +02002618acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2619 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002620{
Willy Tarreau56123282010-08-06 19:06:56 +02002621 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002622 return 0;
2623
Willy Tarreau56123282010-08-06 19:06:56 +02002624 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002625}
2626
2627/* set test->i to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002628 * the configured period.
2629 */
2630static int
Willy Tarreau56123282010-08-06 19:06:56 +02002631acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2632 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002633{
Willy Tarreau56123282010-08-06 19:06:56 +02002634 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002635 return 0;
2636
Willy Tarreau56123282010-08-06 19:06:56 +02002637 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002638}
2639
2640/* set test->i to the session rate from the session's source address in the
2641 * table pointed to by expr, over the configured period.
2642 */
2643static int
2644acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2645 struct acl_expr *expr, struct acl_test *test)
2646{
2647 struct stktable_key *key;
2648
2649 key = tcpv4_src_to_stktable_key(l4);
2650 if (!key)
2651 return 0; /* only TCPv4 is supported right now */
2652
2653 if (expr->arg_len)
2654 px = find_stktable(expr->arg.str);
2655
2656 if (!px)
2657 return 0; /* table not found */
2658
2659 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2660}
2661
Willy Tarreauda7ff642010-06-23 11:44:09 +02002662/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2663static int
2664acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2665{
2666 test->flags = ACL_TEST_F_VOL_TEST;
2667 test->i = 0;
2668 if (ts != NULL) {
2669 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2670 if (!ptr)
2671 return 0; /* parameter not stored */
2672 test->i = stktable_data_cast(ptr, http_req_cnt);
2673 }
2674 return 1;
2675}
2676
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002677/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002678static int
Willy Tarreau56123282010-08-06 19:06:56 +02002679acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2680 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002681{
Willy Tarreau56123282010-08-06 19:06:56 +02002682 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002683 return 0;
2684
Willy Tarreau56123282010-08-06 19:06:56 +02002685 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002686}
2687
2688/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2689static int
Willy Tarreau56123282010-08-06 19:06:56 +02002690acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2691 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002692{
Willy Tarreau56123282010-08-06 19:06:56 +02002693 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002694 return 0;
2695
Willy Tarreau56123282010-08-06 19:06:56 +02002696 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002697}
2698
2699/* set test->i to the cumulated number of session from the session's source
2700 * address in the table pointed to by expr.
2701 */
2702static int
2703acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002704 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002705{
2706 struct stktable_key *key;
2707
2708 key = tcpv4_src_to_stktable_key(l4);
2709 if (!key)
2710 return 0; /* only TCPv4 is supported right now */
2711
2712 if (expr->arg_len)
2713 px = find_stktable(expr->arg.str);
2714
2715 if (!px)
2716 return 0; /* table not found */
2717
2718 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2719}
2720
2721/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2722static int
2723acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2724{
2725 test->flags = ACL_TEST_F_VOL_TEST;
2726 test->i = 0;
2727 if (ts != NULL) {
2728 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2729 if (!ptr)
2730 return 0; /* parameter not stored */
2731 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2732 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2733 }
2734 return 1;
2735}
2736
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002737/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002738 * the configured period.
2739 */
2740static int
Willy Tarreau56123282010-08-06 19:06:56 +02002741acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2742 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002743{
Willy Tarreau56123282010-08-06 19:06:56 +02002744 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002745 return 0;
2746
Willy Tarreau56123282010-08-06 19:06:56 +02002747 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002748}
2749
2750/* set test->i to the session rate from the session's tracked BE counters over
2751 * the configured period.
2752 */
2753static int
Willy Tarreau56123282010-08-06 19:06:56 +02002754acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2755 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002756{
Willy Tarreau56123282010-08-06 19:06:56 +02002757 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002758 return 0;
2759
Willy Tarreau56123282010-08-06 19:06:56 +02002760 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002761}
2762
2763/* set test->i to the session rate from the session's source address in the
2764 * table pointed to by expr, over the configured period.
2765 */
2766static int
2767acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002768 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002769{
2770 struct stktable_key *key;
2771
2772 key = tcpv4_src_to_stktable_key(l4);
2773 if (!key)
2774 return 0; /* only TCPv4 is supported right now */
2775
2776 if (expr->arg_len)
2777 px = find_stktable(expr->arg.str);
2778
2779 if (!px)
2780 return 0; /* table not found */
2781
2782 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2783}
2784
2785/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2786static int
2787acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2788{
2789 test->flags = ACL_TEST_F_VOL_TEST;
2790 test->i = 0;
2791 if (ts != NULL) {
2792 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2793 if (!ptr)
2794 return 0; /* parameter not stored */
2795 test->i = stktable_data_cast(ptr, http_err_cnt);
2796 }
2797 return 1;
2798}
2799
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002800/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002801static int
Willy Tarreau56123282010-08-06 19:06:56 +02002802acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2803 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002804{
Willy Tarreau56123282010-08-06 19:06:56 +02002805 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002806 return 0;
2807
Willy Tarreau56123282010-08-06 19:06:56 +02002808 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002809}
2810
2811/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2812static int
Willy Tarreau56123282010-08-06 19:06:56 +02002813acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2814 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002815{
Willy Tarreau56123282010-08-06 19:06:56 +02002816 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002817 return 0;
2818
Willy Tarreau56123282010-08-06 19:06:56 +02002819 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002820}
2821
2822/* set test->i to the cumulated number of session from the session's source
2823 * address in the table pointed to by expr.
2824 */
2825static int
2826acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002827 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002828{
2829 struct stktable_key *key;
2830
2831 key = tcpv4_src_to_stktable_key(l4);
2832 if (!key)
2833 return 0; /* only TCPv4 is supported right now */
2834
2835 if (expr->arg_len)
2836 px = find_stktable(expr->arg.str);
2837
2838 if (!px)
2839 return 0; /* table not found */
2840
2841 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2842}
2843
2844/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2845static int
2846acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2847{
2848 test->flags = ACL_TEST_F_VOL_TEST;
2849 test->i = 0;
2850 if (ts != NULL) {
2851 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2852 if (!ptr)
2853 return 0; /* parameter not stored */
2854 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2855 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2856 }
2857 return 1;
2858}
2859
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002860/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002861 * the configured period.
2862 */
2863static int
Willy Tarreau56123282010-08-06 19:06:56 +02002864acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2865 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002866{
Willy Tarreau56123282010-08-06 19:06:56 +02002867 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002868 return 0;
2869
Willy Tarreau56123282010-08-06 19:06:56 +02002870 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002871}
2872
2873/* set test->i to the session rate from the session's tracked BE counters over
2874 * the configured period.
2875 */
2876static int
Willy Tarreau56123282010-08-06 19:06:56 +02002877acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2878 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002879{
Willy Tarreau56123282010-08-06 19:06:56 +02002880 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002881 return 0;
2882
Willy Tarreau56123282010-08-06 19:06:56 +02002883 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002884}
2885
2886/* set test->i to the session rate from the session's source address in the
2887 * table pointed to by expr, over the configured period.
2888 */
2889static int
2890acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2891 struct acl_expr *expr, struct acl_test *test)
2892{
2893 struct stktable_key *key;
2894
2895 key = tcpv4_src_to_stktable_key(l4);
2896 if (!key)
2897 return 0; /* only TCPv4 is supported right now */
2898
2899 if (expr->arg_len)
2900 px = find_stktable(expr->arg.str);
2901
2902 if (!px)
2903 return 0; /* table not found */
2904
2905 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2906}
2907
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002908/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2909static int
2910acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2911{
2912 test->flags = ACL_TEST_F_VOL_TEST;
2913 test->i = 0;
2914
2915 if (ts != NULL) {
2916 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2917 if (!ptr)
2918 return 0; /* parameter not stored */
2919 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2920 }
2921 return 1;
2922}
2923
2924/* set test->i to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002925 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002926 */
2927static int
Willy Tarreau56123282010-08-06 19:06:56 +02002928acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2929 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002930{
Willy Tarreau56123282010-08-06 19:06:56 +02002931 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002932 return 0;
2933
Willy Tarreau56123282010-08-06 19:06:56 +02002934 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002935}
2936
2937/* set test->i to the number of kbytes received from clients according to the
2938 * session's tracked BE counters.
2939 */
2940static int
Willy Tarreau56123282010-08-06 19:06:56 +02002941acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2942 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002943{
Willy Tarreau56123282010-08-06 19:06:56 +02002944 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002945 return 0;
2946
Willy Tarreau56123282010-08-06 19:06:56 +02002947 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002948}
2949
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002950/* set test->i to the number of kbytes received from the session's source
2951 * address in the table pointed to by expr.
2952 */
2953static int
2954acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2955 struct acl_expr *expr, struct acl_test *test)
2956{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002957 struct stktable_key *key;
2958
2959 key = tcpv4_src_to_stktable_key(l4);
2960 if (!key)
2961 return 0; /* only TCPv4 is supported right now */
2962
2963 if (expr->arg_len)
2964 px = find_stktable(expr->arg.str);
2965
2966 if (!px)
2967 return 0; /* table not found */
2968
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002969 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2970}
2971
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002972/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2973 * configured period.
2974 */
2975static int
2976acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2977{
2978 test->flags = ACL_TEST_F_VOL_TEST;
2979 test->i = 0;
2980 if (ts != NULL) {
2981 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
2982 if (!ptr)
2983 return 0; /* parameter not stored */
2984 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2985 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2986 }
2987 return 1;
2988}
2989
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002990/* set test->i to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002991 * counters over the configured period.
2992 */
2993static int
Willy Tarreau56123282010-08-06 19:06:56 +02002994acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2995 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002996{
Willy Tarreau56123282010-08-06 19:06:56 +02002997 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002998 return 0;
2999
Willy Tarreau56123282010-08-06 19:06:56 +02003000 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003001}
3002
3003/* set test->i to the bytes rate from clients from the session's tracked BE
3004 * counters over the configured period.
3005 */
3006static int
Willy Tarreau56123282010-08-06 19:06:56 +02003007acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3008 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003009{
Willy Tarreau56123282010-08-06 19:06:56 +02003010 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003011 return 0;
3012
Willy Tarreau56123282010-08-06 19:06:56 +02003013 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003014}
3015
3016/* set test->i to the bytes rate from clients from the session's source address
3017 * in the table pointed to by expr, over the configured period.
3018 */
3019static int
3020acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003021 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003022{
3023 struct stktable_key *key;
3024
3025 key = tcpv4_src_to_stktable_key(l4);
3026 if (!key)
3027 return 0; /* only TCPv4 is supported right now */
3028
3029 if (expr->arg_len)
3030 px = find_stktable(expr->arg.str);
3031
3032 if (!px)
3033 return 0; /* table not found */
3034
3035 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3036}
3037
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003038/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
3039static int
3040acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3041{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003042 test->flags = ACL_TEST_F_VOL_TEST;
3043 test->i = 0;
3044
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003045 if (ts != NULL) {
3046 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003047 if (!ptr)
3048 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003049 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003050 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003051 return 1;
3052}
3053
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003054/* set test->i to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003055 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003056 */
3057static int
Willy Tarreau56123282010-08-06 19:06:56 +02003058acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3059 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003060{
Willy Tarreau56123282010-08-06 19:06:56 +02003061 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003062 return 0;
3063
Willy Tarreau56123282010-08-06 19:06:56 +02003064 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003065}
3066
3067/* set test->i to the number of kbytes sent to clients according to the session's
3068 * tracked BE counters.
3069 */
3070static int
Willy Tarreau56123282010-08-06 19:06:56 +02003071acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3072 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003073{
Willy Tarreau56123282010-08-06 19:06:56 +02003074 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003075 return 0;
3076
Willy Tarreau56123282010-08-06 19:06:56 +02003077 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003078}
3079
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003080/* set test->i to the number of kbytes sent to the session's source address in
3081 * the table pointed to by expr.
3082 */
3083static int
3084acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3085 struct acl_expr *expr, struct acl_test *test)
3086{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003087 struct stktable_key *key;
3088
3089 key = tcpv4_src_to_stktable_key(l4);
3090 if (!key)
3091 return 0; /* only TCPv4 is supported right now */
3092
3093 if (expr->arg_len)
3094 px = find_stktable(expr->arg.str);
3095
3096 if (!px)
3097 return 0; /* table not found */
3098
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003099 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003100}
3101
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003102/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
3103 * configured period.
3104 */
3105static int
3106acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3107{
3108 test->flags = ACL_TEST_F_VOL_TEST;
3109 test->i = 0;
3110 if (ts != NULL) {
3111 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3112 if (!ptr)
3113 return 0; /* parameter not stored */
3114 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3115 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3116 }
3117 return 1;
3118}
3119
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003120/* set test->i to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003121 * over the configured period.
3122 */
3123static int
Willy Tarreau56123282010-08-06 19:06:56 +02003124acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3125 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003126{
Willy Tarreau56123282010-08-06 19:06:56 +02003127 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003128 return 0;
3129
Willy Tarreau56123282010-08-06 19:06:56 +02003130 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003131}
3132
3133/* set test->i to the bytes rate to clients from the session's tracked BE counters
3134 * over the configured period.
3135 */
3136static int
Willy Tarreau56123282010-08-06 19:06:56 +02003137acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3138 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003139{
Willy Tarreau56123282010-08-06 19:06:56 +02003140 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003141 return 0;
3142
Willy Tarreau56123282010-08-06 19:06:56 +02003143 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003144}
3145
3146/* set test->i to the bytes rate to client from the session's source address in
3147 * the table pointed to by expr, over the configured period.
3148 */
3149static int
3150acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003151 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003152{
3153 struct stktable_key *key;
3154
3155 key = tcpv4_src_to_stktable_key(l4);
3156 if (!key)
3157 return 0; /* only TCPv4 is supported right now */
3158
3159 if (expr->arg_len)
3160 px = find_stktable(expr->arg.str);
3161
3162 if (!px)
3163 return 0; /* table not found */
3164
3165 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3166}
3167
Willy Tarreau8b22a712010-06-18 17:46:06 +02003168
3169/* Note: must not be declared <const> as its list will be overwritten */
3170static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003171 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3172 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3173 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3174 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3175 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3176 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3177 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3178 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3179 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3180 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3181 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3182 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3183 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3184 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3185 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3186 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3187 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3188 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3189 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3190 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3191 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3192 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3193 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3194 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3195 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3196 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3197 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3198 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3199 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3200 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3201 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3202 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3203 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3204 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3205 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3206 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3207 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3208 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3209 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3210 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3211 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3212 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3213 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3214 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3215 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3216 { "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 +02003217 { NULL, NULL, NULL, NULL },
3218}};
3219
3220
Willy Tarreau56123282010-08-06 19:06:56 +02003221/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003222 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3223 * <prm> struct is fed with the table name if any. If unspecified, the caller
3224 * will assume that the current proxy's table is used.
3225 */
3226int parse_track_counters(char **args, int *arg,
3227 int section_type, struct proxy *curpx,
3228 struct track_ctr_prm *prm,
3229 struct proxy *defpx, char *err, int errlen)
3230{
3231 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003232 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003233
Willy Tarreau56123282010-08-06 19:06:56 +02003234 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003235 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003236 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003237 */
3238 while (args[*arg]) {
3239 if (strcmp(args[*arg], "src") == 0) {
3240 prm->type = STKTABLE_TYPE_IP;
3241 pattern_type = 1;
3242 }
3243 else if (strcmp(args[*arg], "table") == 0) {
3244 if (!args[*arg + 1]) {
3245 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003246 "missing table for %s in %s '%s'.",
3247 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003248 return -1;
3249 }
3250 /* we copy the table name for now, it will be resolved later */
3251 prm->table.n = strdup(args[*arg + 1]);
3252 (*arg)++;
3253 }
3254 else {
3255 /* unhandled keywords are handled by the caller */
3256 break;
3257 }
3258 (*arg)++;
3259 }
3260
3261 if (!pattern_type) {
3262 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003263 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3264 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003265 return -1;
3266 }
3267
3268 return 0;
3269}
3270
Willy Tarreau8b22a712010-06-18 17:46:06 +02003271__attribute__((constructor))
3272static void __session_init(void)
3273{
3274 acl_register_keywords(&acl_kws);
3275}
3276
Willy Tarreaubaaee002006-06-26 02:48:02 +02003277/*
3278 * Local variables:
3279 * c-indent-level: 8
3280 * c-basic-offset: 8
3281 * End:
3282 */