blob: d673aaa63c24dc91a336adf79afc1b08eb3132e3 [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;
Willy Tarreau0b3a4112011-03-27 19:16:56 +0200191 s->si[1].conn_retries = 0; /* used for logging too */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200192 s->si[1].err_loc = NULL;
193 s->si[1].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200194 s->si[1].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100195 clear_target(&s->si[1].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200196 s->si[1].shutr = stream_int_shutr;
197 s->si[1].shutw = stream_int_shutw;
198 s->si[1].exp = TICK_ETERNITY;
199 s->si[1].flags = SI_FL_NONE;
200
201 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
202 s->si[1].flags |= SI_FL_INDEP_STR;
203
Willy Tarreau827aee92011-03-10 16:55:02 +0100204 s->srv_conn = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100205 clear_target(&s->target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200206 s->pend_pos = NULL;
207
208 /* init store persistence */
209 s->store_count = 0;
210
211 /* Adjust some socket options */
Willy Tarreaufffe1322010-11-11 09:48:16 +0100212 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200213 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200214
215 txn = &s->txn;
216 /* Those variables will be checked and freed if non-NULL in
217 * session.c:session_free(). It is important that they are
218 * properly initialized.
219 */
220 txn->sessid = NULL;
221 txn->srv_cookie = NULL;
222 txn->cli_cookie = NULL;
223 txn->uri = NULL;
224 txn->req.cap = NULL;
225 txn->rsp.cap = NULL;
226 txn->hdr_idx.v = NULL;
227 txn->hdr_idx.size = txn->hdr_idx.used = 0;
228
229 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
230 goto out_free_task; /* no memory */
231
232 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
233 goto out_free_req; /* no memory */
234
235 /* initialize the request buffer */
236 s->req->size = global.tune.bufsize;
237 buffer_init(s->req);
238 s->req->prod = &s->si[0];
239 s->req->cons = &s->si[1];
240 s->si[0].ib = s->si[1].ob = s->req;
241 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
242
243 /* activate default analysers enabled for this listener */
244 s->req->analysers = l->analysers;
245
246 s->req->wto = TICK_ETERNITY;
247 s->req->rto = TICK_ETERNITY;
248 s->req->rex = TICK_ETERNITY;
249 s->req->wex = TICK_ETERNITY;
250 s->req->analyse_exp = TICK_ETERNITY;
251
252 /* initialize response buffer */
253 s->rep->size = global.tune.bufsize;
254 buffer_init(s->rep);
255 s->rep->prod = &s->si[1];
256 s->rep->cons = &s->si[0];
257 s->si[0].ob = s->si[1].ib = s->rep;
258 s->rep->analysers = 0;
259
260 s->rep->rto = TICK_ETERNITY;
261 s->rep->wto = TICK_ETERNITY;
262 s->rep->rex = TICK_ETERNITY;
263 s->rep->wex = TICK_ETERNITY;
264 s->rep->analyse_exp = TICK_ETERNITY;
265
266 /* finish initialization of the accepted file descriptor */
267 fd_insert(cfd);
268 fdtab[cfd].owner = &s->si[0];
269 fdtab[cfd].state = FD_STREADY;
270 fdtab[cfd].flags = 0;
271 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
272 fdtab[cfd].cb[DIR_RD].b = s->req;
273 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
274 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreau957c0a52011-03-03 17:42:23 +0100275 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->si[0].addr.c.from;
276 fdinfo[cfd].peerlen = sizeof(s->si[0].addr.c.from);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200277 EV_FD_SET(cfd, DIR_RD);
278
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100279 if (p->accept && (ret = p->accept(s)) <= 0) {
280 /* Either we had an unrecoverable error (<0) or work is
281 * finished (=0, eg: monitoring), in both situations,
282 * we can release everything and close.
283 */
284 goto out_free_rep;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200285 }
286
287 /* it is important not to call the wakeup function directly but to
288 * pass through task_wakeup(), because this one knows how to apply
289 * priorities to tasks.
290 */
291 task_wakeup(t, TASK_WOKEN_INIT);
292 return 1;
293
294 /* Error unrolling */
295 out_free_rep:
296 pool_free2(pool2_buffer, s->rep);
297 out_free_req:
298 pool_free2(pool2_buffer, s->req);
299 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200300 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200301 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200302 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200303 task_free(t);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200304 LIST_DEL(&s->list);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100305 out_free_session:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200306 pool_free2(pool2_session, s);
307 out_close:
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100308 if (fdtab[cfd].state != FD_STCLOSE)
309 fd_delete(cfd);
310 else
311 close(cfd);
312 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200313}
314
Willy Tarreaubaaee002006-06-26 02:48:02 +0200315/*
316 * frees the context associated to a session. It must have been removed first.
317 */
318void session_free(struct session *s)
319{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100320 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200321 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100322 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200323 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100324
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325 if (s->pend_pos)
326 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100327
Willy Tarreau827aee92011-03-10 16:55:02 +0100328 if (target_srv(&s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100329 if (s->flags & SN_CURR_SESS) {
330 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100331 target_srv(&s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100332 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100333 if (may_dequeue_tasks(target_srv(&s->target), s->be))
334 process_srv_queue(target_srv(&s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100335 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100336
Willy Tarreau7c669d72008-06-20 15:04:11 +0200337 if (unlikely(s->srv_conn)) {
338 /* the session still has a reserved slot on a server, but
339 * it should normally be only the same as the one above,
340 * so this should not happen in fact.
341 */
342 sess_change_server(s, NULL);
343 }
344
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100345 if (s->req->pipe)
346 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100347
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100348 if (s->rep->pipe)
349 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100350
Willy Tarreau48d63db2008-08-03 17:41:33 +0200351 pool_free2(pool2_buffer, s->req);
352 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353
Willy Tarreau46023632010-01-07 22:51:47 +0100354 http_end_txn(s);
355
Willy Tarreaua4cda672010-06-06 18:28:49 +0200356 for (i = 0; i < s->store_count; i++) {
357 if (!s->store[i].ts)
358 continue;
359 stksess_free(s->store[i].table, s->store[i].ts);
360 s->store[i].ts = NULL;
361 }
362
Willy Tarreau92fb9832007-10-16 17:34:28 +0200363 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200364 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100365 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
366 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100368
Willy Tarreau56123282010-08-06 19:06:56 +0200369 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200370 session_store_counters(s);
371
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100372 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100373 /* we have to unlink all watchers. We must not relink them if
374 * this session was the last one in the list.
375 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100376 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100377 LIST_INIT(&bref->users);
378 if (s->list.n != &sessions)
379 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100380 bref->ref = s->list.n;
381 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100382 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200383 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200384
385 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200386 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200387 pool_flush2(pool2_buffer);
388 pool_flush2(fe->hdr_idx_pool);
389 pool_flush2(pool2_requri);
390 pool_flush2(pool2_capture);
391 pool_flush2(pool2_session);
392 pool_flush2(fe->req_cap_pool);
393 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200394 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200395}
396
397
398/* perform minimal intializations, report 0 in case of error, 1 if OK. */
399int init_session()
400{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100401 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200402 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
403 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404}
405
Willy Tarreau30e71012007-11-26 20:15:35 +0100406void session_process_counters(struct session *s)
407{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100408 unsigned long long bytes;
409
Willy Tarreau30e71012007-11-26 20:15:35 +0100410 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100411 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100412 s->logs.bytes_in = s->req->total;
413 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100414 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100415
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100416 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100417
Willy Tarreau827aee92011-03-10 16:55:02 +0100418 if (target_srv(&s->target))
419 target_srv(&s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200420
421 if (s->listener->counters)
422 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200423
Willy Tarreau56123282010-08-06 19:06:56 +0200424 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200425 void *ptr;
426
Willy Tarreau56123282010-08-06 19:06:56 +0200427 ptr = stktable_data_ptr(s->stkctr2_table,
428 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200429 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200430 if (ptr)
431 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200432
Willy Tarreau56123282010-08-06 19:06:56 +0200433 ptr = stktable_data_ptr(s->stkctr2_table,
434 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200435 STKTABLE_DT_BYTES_IN_RATE);
436 if (ptr)
437 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200438 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200439 }
440
Willy Tarreau56123282010-08-06 19:06:56 +0200441 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200442 void *ptr;
443
Willy Tarreau56123282010-08-06 19:06:56 +0200444 ptr = stktable_data_ptr(s->stkctr1_table,
445 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200446 STKTABLE_DT_BYTES_IN_CNT);
447 if (ptr)
448 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
449
Willy Tarreau56123282010-08-06 19:06:56 +0200450 ptr = stktable_data_ptr(s->stkctr1_table,
451 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200452 STKTABLE_DT_BYTES_IN_RATE);
453 if (ptr)
454 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200455 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200456 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100457 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100458 }
459
Willy Tarreau30e71012007-11-26 20:15:35 +0100460 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100461 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100462 s->logs.bytes_out = s->rep->total;
463 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100464 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100465
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100466 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100467
Willy Tarreau827aee92011-03-10 16:55:02 +0100468 if (target_srv(&s->target))
469 target_srv(&s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200470
471 if (s->listener->counters)
472 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200473
Willy Tarreau56123282010-08-06 19:06:56 +0200474 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200475 void *ptr;
476
Willy Tarreau56123282010-08-06 19:06:56 +0200477 ptr = stktable_data_ptr(s->stkctr2_table,
478 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200479 STKTABLE_DT_BYTES_OUT_CNT);
480 if (ptr)
481 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
482
Willy Tarreau56123282010-08-06 19:06:56 +0200483 ptr = stktable_data_ptr(s->stkctr2_table,
484 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200485 STKTABLE_DT_BYTES_OUT_RATE);
486 if (ptr)
487 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200488 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200489 }
490
Willy Tarreau56123282010-08-06 19:06:56 +0200491 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200492 void *ptr;
493
Willy Tarreau56123282010-08-06 19:06:56 +0200494 ptr = stktable_data_ptr(s->stkctr1_table,
495 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200496 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200497 if (ptr)
498 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200499
Willy Tarreau56123282010-08-06 19:06:56 +0200500 ptr = stktable_data_ptr(s->stkctr1_table,
501 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200502 STKTABLE_DT_BYTES_OUT_RATE);
503 if (ptr)
504 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200505 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200506 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100507 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100508 }
509}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200510
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100511/* This function is called with (si->state == SI_ST_CON) meaning that a
512 * connection was attempted and that the file descriptor is already allocated.
513 * We must check for establishment, error and abort. Possible output states
514 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
515 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
516 * otherwise 1.
517 */
518int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
519{
520 struct buffer *req = si->ob;
521 struct buffer *rep = si->ib;
522
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100523 /* If we got an error, or if nothing happened and the connection timed
524 * out, we must give up. The CER state handler will take care of retry
525 * attempts and error reports.
526 */
527 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100528 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100529 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200530 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100531 fd_delete(si->fd);
532
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200533 if (si->release)
534 si->release(si);
535
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100536 if (si->err_type)
537 return 0;
538
Willy Tarreau827aee92011-03-10 16:55:02 +0100539 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100540 if (si->flags & SI_FL_ERR)
541 si->err_type = SI_ET_CONN_ERR;
542 else
543 si->err_type = SI_ET_CONN_TO;
544 return 0;
545 }
546
547 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200548 if (unlikely((rep->flags & BF_SHUTW) ||
549 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200550 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100551 s->be->options & PR_O_ABRT_CLOSE)))) {
552 /* give up */
553 si->shutw(si);
554 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau827aee92011-03-10 16:55:02 +0100555 si->err_loc = target_srv(&s->target);
Willy Tarreaudc340a92009-06-28 23:10:19 +0200556 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100557 if (s->srv_error)
558 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100559 return 1;
560 }
561
562 /* we need to wait a bit more if there was no activity either */
563 if (!(req->flags & BF_WRITE_ACTIVITY))
564 return 1;
565
566 /* OK, this means that a connection succeeded. The caller will be
567 * responsible for handling the transition from CON to EST.
568 */
569 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100570 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100571 si->state = SI_ST_EST;
572 si->err_type = SI_ET_NONE;
573 si->err_loc = NULL;
574 return 1;
575}
576
577/* This function is called with (si->state == SI_ST_CER) meaning that a
578 * previous connection attempt has failed and that the file descriptor
579 * has already been released. Possible causes include asynchronous error
580 * notification and time out. Possible output states are SI_ST_CLO when
581 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
582 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
583 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
584 * marked as in error state. It returns 0.
585 */
586int sess_update_st_cer(struct session *s, struct stream_interface *si)
587{
588 /* we probably have to release last session from the server */
Willy Tarreau827aee92011-03-10 16:55:02 +0100589 if (target_srv(&s->target)) {
590 health_adjust(target_srv(&s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100591
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100592 if (s->flags & SN_CURR_SESS) {
593 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100594 target_srv(&s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100595 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100596 }
597
598 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200599 si->conn_retries--;
600 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100601 if (!si->err_type) {
602 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +0100603 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100604 }
605
Willy Tarreau827aee92011-03-10 16:55:02 +0100606 if (target_srv(&s->target))
607 target_srv(&s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100608 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100609 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100610 if (may_dequeue_tasks(target_srv(&s->target), s->be))
611 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100612
613 /* shutw is enough so stop a connecting socket */
614 si->shutw(si);
615 si->ob->flags |= BF_WRITE_ERROR;
616 si->ib->flags |= BF_READ_ERROR;
617
618 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100619 if (s->srv_error)
620 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100621 return 0;
622 }
623
624 /* If the "redispatch" option is set on the backend, we are allowed to
625 * retry on another server for the last retry. In order to achieve this,
626 * we must mark the session unassigned, and eventually clear the DIRECT
627 * bit to ignore any persistence cookie. We won't count a retry nor a
628 * redispatch yet, because this will depend on what server is selected.
629 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100630 if (target_srv(&s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100631 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100632 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100633 if (may_dequeue_tasks(target_srv(&s->target), s->be))
634 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100635
636 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100637 si->state = SI_ST_REQ;
638 } else {
Willy Tarreau827aee92011-03-10 16:55:02 +0100639 if (target_srv(&s->target))
640 target_srv(&s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100641 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100642 si->state = SI_ST_ASS;
643 }
644
645 if (si->flags & SI_FL_ERR) {
646 /* The error was an asynchronous connection error, and we will
647 * likely have to retry connecting to the same server, most
648 * likely leading to the same result. To avoid this, we wait
649 * one second before retrying.
650 */
651
652 if (!si->err_type)
653 si->err_type = SI_ET_CONN_ERR;
654
655 si->state = SI_ST_TAR;
656 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
657 return 0;
658 }
659 return 0;
660}
661
662/*
663 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200664 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
665 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100666 */
667void sess_establish(struct session *s, struct stream_interface *si)
668{
669 struct buffer *req = si->ob;
670 struct buffer *rep = si->ib;
671
Willy Tarreau827aee92011-03-10 16:55:02 +0100672 if (target_srv(&s->target))
673 health_adjust(target_srv(&s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100674
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100675 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100676 /* if the user wants to log as soon as possible, without counting
677 * bytes from the server, then this is the right moment. */
678 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
679 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100680 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100682 }
683 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100684 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
685 /* reset hdr_idx which was already initialized by the request.
686 * right now, the http parser does it.
687 * hdr_idx_init(&s->txn.hdr_idx);
688 */
689 }
690
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200691 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100692 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200693 if (si->connect) {
694 /* real connections have timeouts */
695 req->wto = s->be->timeout.server;
696 rep->rto = s->be->timeout.server;
697 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100698 req->wex = TICK_ETERNITY;
699}
700
701/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
702 * Other input states are simply ignored.
703 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
704 * Flags must have previously been updated for timeouts and other conditions.
705 */
706void sess_update_stream_int(struct session *s, struct stream_interface *si)
707{
Willy Tarreau827aee92011-03-10 16:55:02 +0100708 struct server *srv = target_srv(&s->target);
709
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100710 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",
711 now_ms, __FUNCTION__,
712 s,
713 s->req, s->rep,
714 s->req->rex, s->rep->wex,
715 s->req->flags, s->rep->flags,
716 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
717
718 if (si->state == SI_ST_ASS) {
719 /* Server assigned to connection request, we have to try to connect now */
720 int conn_err;
721
722 conn_err = connect_server(s);
Willy Tarreau827aee92011-03-10 16:55:02 +0100723 srv = target_srv(&s->target);
724
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100725 if (conn_err == SN_ERR_NONE) {
726 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100727 if (srv)
728 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100729 return;
730 }
731
732 /* We have received a synchronous error. We might have to
733 * abort, retry immediately or redispatch.
734 */
735 if (conn_err == SN_ERR_INTERNAL) {
736 if (!si->err_type) {
737 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +0100738 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739 }
740
Willy Tarreau827aee92011-03-10 16:55:02 +0100741 if (srv)
742 srv_inc_sess_ctr(srv);
743 if (srv)
744 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100745 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100746
747 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100748 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100749 if (may_dequeue_tasks(srv, s->be))
750 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751
752 /* Failed and not retryable. */
753 si->shutr(si);
754 si->shutw(si);
755 si->ob->flags |= BF_WRITE_ERROR;
756
757 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
758
759 /* no session was ever accounted for this server */
760 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100761 if (s->srv_error)
762 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100763 return;
764 }
765
766 /* We are facing a retryable error, but we don't want to run a
767 * turn-around now, as the problem is likely a source port
768 * allocation problem, so we want to retry now.
769 */
770 si->state = SI_ST_CER;
771 si->flags &= ~SI_FL_ERR;
772 sess_update_st_cer(s, si);
773 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
774 return;
775 }
776 else if (si->state == SI_ST_QUE) {
777 /* connection request was queued, check for any update */
778 if (!s->pend_pos) {
779 /* The connection is not in the queue anymore. Either
780 * we have a server connection slot available and we
781 * go directly to the assigned state, or we need to
782 * load-balance first and go to the INI state.
783 */
784 si->exp = TICK_ETERNITY;
785 if (unlikely(!(s->flags & SN_ASSIGNED)))
786 si->state = SI_ST_REQ;
787 else {
788 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
789 si->state = SI_ST_ASS;
790 }
791 return;
792 }
793
794 /* Connection request still in queue... */
795 if (si->flags & SI_FL_EXP) {
796 /* ... and timeout expired */
797 si->exp = TICK_ETERNITY;
798 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100799 if (srv)
800 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100801 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100802 si->shutr(si);
803 si->shutw(si);
804 si->ob->flags |= BF_WRITE_TIMEOUT;
805 if (!si->err_type)
806 si->err_type = SI_ET_QUEUE_TO;
807 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100808 if (s->srv_error)
809 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100810 return;
811 }
812
813 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200814 if ((si->ob->flags & (BF_READ_ERROR)) ||
815 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200816 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100817 /* give up */
818 si->exp = TICK_ETERNITY;
819 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
820 si->shutr(si);
821 si->shutw(si);
822 si->err_type |= SI_ET_QUEUE_ABRT;
823 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100824 if (s->srv_error)
825 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100826 return;
827 }
828
829 /* Nothing changed */
830 return;
831 }
832 else if (si->state == SI_ST_TAR) {
833 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200834 if ((si->ob->flags & (BF_READ_ERROR)) ||
835 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200836 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100837 /* give up */
838 si->exp = TICK_ETERNITY;
839 si->shutr(si);
840 si->shutw(si);
841 si->err_type |= SI_ET_CONN_ABRT;
842 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100843 if (s->srv_error)
844 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100845 return;
846 }
847
848 if (!(si->flags & SI_FL_EXP))
849 return; /* still in turn-around */
850
851 si->exp = TICK_ETERNITY;
852
853 /* we keep trying on the same server as long as the session is
854 * marked "assigned".
855 * FIXME: Should we force a redispatch attempt when the server is down ?
856 */
857 if (s->flags & SN_ASSIGNED)
858 si->state = SI_ST_ASS;
859 else
860 si->state = SI_ST_REQ;
861 return;
862 }
863}
864
865/* This function initiates a server connection request on a stream interface
866 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
867 * indicating that a server has been assigned. It may also return SI_ST_QUE,
868 * or SI_ST_CLO upon error.
869 */
870static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
871 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",
872 now_ms, __FUNCTION__,
873 s,
874 s->req, s->rep,
875 s->req->rex, s->rep->wex,
876 s->req->flags, s->rep->flags,
877 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
878
879 if (si->state != SI_ST_REQ)
880 return;
881
882 /* Try to assign a server */
883 if (srv_redispatch_connect(s) != 0) {
884 /* We did not get a server. Either we queued the
885 * connection request, or we encountered an error.
886 */
887 if (si->state == SI_ST_QUE)
888 return;
889
890 /* we did not get any server, let's check the cause */
891 si->shutr(si);
892 si->shutw(si);
893 si->ob->flags |= BF_WRITE_ERROR;
894 if (!si->err_type)
895 si->err_type = SI_ET_CONN_OTHER;
896 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100897 if (s->srv_error)
898 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100899 return;
900 }
901
902 /* The server is assigned */
903 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
904 si->state = SI_ST_ASS;
905}
906
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200907/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100908 * if appropriate. The default_backend rule is also considered, then the
909 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200910 * It returns 1 if the processing can continue on next analysers, or zero if it
911 * either needs more data or wants to immediately abort the request.
912 */
913int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
914{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200915 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100916
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200917 req->analysers &= ~an_bit;
918 req->analyse_exp = TICK_ETERNITY;
919
920 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
921 now_ms, __FUNCTION__,
922 s,
923 req,
924 req->rex, req->wex,
925 req->flags,
926 req->l,
927 req->analysers);
928
929 /* now check whether we have some switching rules for this request */
930 if (!(s->flags & SN_BE_ASSIGNED)) {
931 struct switching_rule *rule;
932
933 list_for_each_entry(rule, &s->fe->switching_rules, list) {
934 int ret;
935
936 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
937 ret = acl_pass(ret);
938 if (rule->cond->pol == ACL_COND_UNLESS)
939 ret = !ret;
940
941 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200942 if (!session_set_backend(s, rule->be.backend))
943 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200944 break;
945 }
946 }
947
948 /* To ensure correct connection accounting on the backend, we
949 * have to assign one if it was not set (eg: a listen). This
950 * measure also takes care of correctly setting the default
951 * backend if any.
952 */
953 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200954 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
955 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200956 }
957
Willy Tarreaufb356202010-08-03 14:02:05 +0200958 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
959 if (s->fe == s->be) {
960 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200961 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200962 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200963
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200964 /* 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 +0100965 * persistence rule, and report that in the session.
966 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200967 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100968 int ret = 1;
969
970 if (prst_rule->cond) {
971 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
972 ret = acl_pass(ret);
973 if (prst_rule->cond->pol == ACL_COND_UNLESS)
974 ret = !ret;
975 }
976
977 if (ret) {
978 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200979 if (prst_rule->type == PERSIST_TYPE_FORCE) {
980 s->flags |= SN_FORCE_PRST;
981 } else {
982 s->flags |= SN_IGNORE_PRST;
983 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100984 break;
985 }
986 }
987
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200988 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200989
990 sw_failed:
991 /* immediately abort this request in case of allocation failure */
992 buffer_abort(s->req);
993 buffer_abort(s->rep);
994
995 if (!(s->flags & SN_ERR_MASK))
996 s->flags |= SN_ERR_RESOURCE;
997 if (!(s->flags & SN_FINST_MASK))
998 s->flags |= SN_FINST_R;
999
1000 s->txn.status = 500;
1001 s->req->analysers = 0;
1002 s->req->analyse_exp = TICK_ETERNITY;
1003 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001004}
1005
Emeric Brun1d33b292010-01-04 15:47:17 +01001006/* This stream analyser works on a request. It applies all sticking rules on
1007 * it then returns 1. The data must already be present in the buffer otherwise
1008 * they won't match. It always returns 1.
1009 */
1010int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
1011{
1012 struct proxy *px = s->be;
1013 struct sticking_rule *rule;
1014
1015 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1016 now_ms, __FUNCTION__,
1017 s,
1018 req,
1019 req->rex, req->wex,
1020 req->flags,
1021 req->l,
1022 req->analysers);
1023
1024 list_for_each_entry(rule, &px->sticking_rules, list) {
1025 int ret = 1 ;
1026 int i;
1027
1028 for (i = 0; i < s->store_count; i++) {
1029 if (rule->table.t == s->store[i].table)
1030 break;
1031 }
1032
1033 if (i != s->store_count)
1034 continue;
1035
1036 if (rule->cond) {
1037 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1038 ret = acl_pass(ret);
1039 if (rule->cond->pol == ACL_COND_UNLESS)
1040 ret = !ret;
1041 }
1042
1043 if (ret) {
1044 struct stktable_key *key;
1045
Emeric Brun485479d2010-09-23 18:02:19 +02001046 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001047 if (!key)
1048 continue;
1049
1050 if (rule->flags & STK_IS_MATCH) {
1051 struct stksess *ts;
1052
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001053 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001054 if (!(s->flags & SN_ASSIGNED)) {
1055 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001056 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001057
1058 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001059 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1060 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001061 if (node) {
1062 struct server *srv;
1063
1064 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001065 if ((srv->state & SRV_RUNNING) ||
1066 (px->options & PR_O_PERSIST) ||
1067 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001068 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001069 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001070 }
1071 }
1072 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001073 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001074 }
1075 }
1076 if (rule->flags & STK_IS_STORE) {
1077 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1078 struct stksess *ts;
1079
1080 ts = stksess_new(rule->table.t, key);
1081 if (ts) {
1082 s->store[s->store_count].table = rule->table.t;
1083 s->store[s->store_count++].ts = ts;
1084 }
1085 }
1086 }
1087 }
1088 }
1089
1090 req->analysers &= ~an_bit;
1091 req->analyse_exp = TICK_ETERNITY;
1092 return 1;
1093}
1094
1095/* This stream analyser works on a response. It applies all store rules on it
1096 * then returns 1. The data must already be present in the buffer otherwise
1097 * they won't match. It always returns 1.
1098 */
1099int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1100{
1101 struct proxy *px = s->be;
1102 struct sticking_rule *rule;
1103 int i;
1104
1105 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1106 now_ms, __FUNCTION__,
1107 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001108 rep,
1109 rep->rex, rep->wex,
1110 rep->flags,
1111 rep->l,
1112 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001113
1114 list_for_each_entry(rule, &px->storersp_rules, list) {
1115 int ret = 1 ;
1116 int storereqidx = -1;
1117
1118 for (i = 0; i < s->store_count; i++) {
1119 if (rule->table.t == s->store[i].table) {
1120 if (!(s->store[i].flags))
1121 storereqidx = i;
1122 break;
1123 }
1124 }
1125
1126 if ((i != s->store_count) && (storereqidx == -1))
1127 continue;
1128
1129 if (rule->cond) {
1130 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1131 ret = acl_pass(ret);
1132 if (rule->cond->pol == ACL_COND_UNLESS)
1133 ret = !ret;
1134 }
1135
1136 if (ret) {
1137 struct stktable_key *key;
1138
Emeric Brun485479d2010-09-23 18:02:19 +02001139 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001140 if (!key)
1141 continue;
1142
1143 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001144 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001145 s->store[storereqidx].flags = 1;
1146 }
1147 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1148 struct stksess *ts;
1149
1150 ts = stksess_new(rule->table.t, key);
1151 if (ts) {
1152 s->store[s->store_count].table = rule->table.t;
1153 s->store[s->store_count].flags = 1;
1154 s->store[s->store_count++].ts = ts;
1155 }
1156 }
1157 }
1158 }
1159
1160 /* process store request and store response */
1161 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001162 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001163 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001164
1165 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1166 if (ts) {
1167 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001168 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001169 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001170 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001171 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001172 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001173
1174 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001175 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001176 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001177 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001178 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001179
1180 rep->analysers &= ~an_bit;
1181 rep->analyse_exp = TICK_ETERNITY;
1182 return 1;
1183}
1184
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001185/* This macro is very specific to the function below. See the comments in
1186 * process_session() below to understand the logic and the tests.
1187 */
1188#define UPDATE_ANALYSERS(real, list, back, flag) { \
1189 list = (((list) & ~(flag)) | ~(back)) & (real); \
1190 back = real; \
1191 if (!(list)) \
1192 break; \
1193 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1194 continue; \
1195}
1196
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001197/* Processes the client, server, request and response jobs of a session task,
1198 * then puts it back to the wait queue in a clean state, or cleans up its
1199 * resources if it must be deleted. Returns in <next> the date the task wants
1200 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1201 * nothing too many times, the request and response buffers flags are monitored
1202 * and each function is called only if at least another function has changed at
1203 * least one flag it is interested in.
1204 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001205struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001206{
Willy Tarreau827aee92011-03-10 16:55:02 +01001207 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001208 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001209 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001210 unsigned int rq_prod_last, rq_cons_last;
1211 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001212 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001213
1214 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1215 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1216
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001217 /* this data may be no longer valid, clear it */
1218 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1219
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001220 /* This flag must explicitly be set every time */
1221 s->req->flags &= ~BF_READ_NOEXP;
1222
1223 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001224 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1225 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001226
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001227 /* we don't want the stream interface functions to recursively wake us up */
1228 if (s->req->prod->owner == t)
1229 s->req->prod->flags |= SI_FL_DONT_WAKE;
1230 if (s->req->cons->owner == t)
1231 s->req->cons->flags |= SI_FL_DONT_WAKE;
1232
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001233 /* 1a: Check for low level timeouts if needed. We just set a flag on
1234 * stream interfaces when their timeouts have expired.
1235 */
1236 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1237 stream_int_check_timeouts(&s->si[0]);
1238 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001239
1240 /* check buffer timeouts, and close the corresponding stream interfaces
1241 * for future reads or writes. Note: this will also concern upper layers
1242 * but we do not touch any other flag. We must be careful and correctly
1243 * detect state changes when calling them.
1244 */
1245
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001246 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001247
Willy Tarreau14641402009-12-29 14:49:56 +01001248 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1249 s->req->cons->flags |= SI_FL_NOLINGER;
1250 s->req->cons->shutw(s->req->cons);
1251 }
1252
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001253 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1254 s->req->prod->shutr(s->req->prod);
1255
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001256 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001257
Willy Tarreau14641402009-12-29 14:49:56 +01001258 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1259 s->rep->cons->flags |= SI_FL_NOLINGER;
1260 s->rep->cons->shutw(s->rep->cons);
1261 }
1262
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001263 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1264 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001265 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001266
1267 /* 1b: check for low-level errors reported at the stream interface.
1268 * First we check if it's a retryable error (in which case we don't
1269 * want to tell the buffer). Otherwise we report the error one level
1270 * upper by setting flags into the buffers. Note that the side towards
1271 * the client cannot have connect (hence retryable) errors. Also, the
1272 * connection setup code must be able to deal with any type of abort.
1273 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001274 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001275 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1276 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1277 s->si[0].shutr(&s->si[0]);
1278 s->si[0].shutw(&s->si[0]);
1279 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001280 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001281 s->be->be_counters.cli_aborts++;
1282 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001283 if (srv)
1284 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001285 if (!(s->flags & SN_ERR_MASK))
1286 s->flags |= SN_ERR_CLICL;
1287 if (!(s->flags & SN_FINST_MASK))
1288 s->flags |= SN_FINST_D;
1289 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001290 }
1291 }
1292
1293 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1294 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1295 s->si[1].shutr(&s->si[1]);
1296 s->si[1].shutw(&s->si[1]);
1297 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001298 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001299 if (srv)
1300 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001301 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001302 s->be->be_counters.srv_aborts++;
1303 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001304 if (srv)
1305 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001306 if (!(s->flags & SN_ERR_MASK))
1307 s->flags |= SN_ERR_SRVCL;
1308 if (!(s->flags & SN_FINST_MASK))
1309 s->flags |= SN_FINST_D;
1310 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001311 }
1312 /* note: maybe we should process connection errors here ? */
1313 }
1314
1315 if (s->si[1].state == SI_ST_CON) {
1316 /* we were trying to establish a connection on the server side,
1317 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1318 */
1319 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1320 sess_update_st_cer(s, &s->si[1]);
1321 else if (s->si[1].state == SI_ST_EST)
1322 sess_establish(s, &s->si[1]);
1323
1324 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1325 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1326 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1327 */
1328 }
1329
Willy Tarreau815a9b22010-07-27 17:15:12 +02001330 rq_prod_last = s->si[0].state;
1331 rq_cons_last = s->si[1].state;
1332 rp_cons_last = s->si[0].state;
1333 rp_prod_last = s->si[1].state;
1334
1335 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001336 /* Check for connection closure */
1337
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001338 DPRINTF(stderr,
1339 "[%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",
1340 now_ms, __FUNCTION__, __LINE__,
1341 t,
1342 s, s->flags,
1343 s->req, s->rep,
1344 s->req->rex, s->rep->wex,
1345 s->req->flags, s->rep->flags,
1346 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1347 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001348 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001349
1350 /* nothing special to be done on client side */
1351 if (unlikely(s->req->prod->state == SI_ST_DIS))
1352 s->req->prod->state = SI_ST_CLO;
1353
1354 /* When a server-side connection is released, we have to count it and
1355 * check for pending connections on this server.
1356 */
1357 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1358 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001359 srv = target_srv(&s->target);
1360 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001361 if (s->flags & SN_CURR_SESS) {
1362 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001363 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001364 }
1365 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001366 if (may_dequeue_tasks(srv, s->be))
1367 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001368 }
1369 }
1370
1371 /*
1372 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1373 * at this point.
1374 */
1375
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001376 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001377 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001378 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001379 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1380 s->si[0].state != rq_prod_last ||
1381 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001382 unsigned int flags = s->req->flags;
1383
1384 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001385 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001386 unsigned int ana_list;
1387 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001388
Willy Tarreau90deb182010-01-07 00:20:41 +01001389 /* it's up to the analysers to stop new connections,
1390 * disable reading or closing. Note: if an analyser
1391 * disables any of these bits, it is responsible for
1392 * enabling them again when it disables itself, so
1393 * that other analysers are called in similar conditions.
1394 */
1395 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001396 buffer_auto_connect(s->req);
1397 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001398
1399 /* We will call all analysers for which a bit is set in
1400 * s->req->analysers, following the bit order from LSB
1401 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001402 * the list when not needed. Any analyser may return 0
1403 * to break out of the loop, either because of missing
1404 * data to take a decision, or because it decides to
1405 * kill the session. We loop at least once through each
1406 * analyser, and we may loop again if other analysers
1407 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001408 *
1409 * We build a list of analysers to run. We evaluate all
1410 * of these analysers in the order of the lower bit to
1411 * the higher bit. This ordering is very important.
1412 * An analyser will often add/remove other analysers,
1413 * including itself. Any changes to itself have no effect
1414 * on the loop. If it removes any other analysers, we
1415 * want those analysers not to be called anymore during
1416 * this loop. If it adds an analyser that is located
1417 * after itself, we want it to be scheduled for being
1418 * processed during the loop. If it adds an analyser
1419 * which is located before it, we want it to switch to
1420 * it immediately, even if it has already been called
1421 * once but removed since.
1422 *
1423 * In order to achieve this, we compare the analyser
1424 * list after the call with a copy of it before the
1425 * call. The work list is fed with analyser bits that
1426 * appeared during the call. Then we compare previous
1427 * work list with the new one, and check the bits that
1428 * appeared. If the lowest of these bits is lower than
1429 * the current bit, it means we have enabled a previous
1430 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001431 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001432
1433 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001434 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001435 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001436
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001437 if (ana_list & AN_REQ_DECODE_PROXY) {
1438 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1439 break;
1440 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1441 }
1442
Willy Tarreaufb356202010-08-03 14:02:05 +02001443 if (ana_list & AN_REQ_INSPECT_FE) {
1444 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001445 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001446 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001447 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001448
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001449 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001450 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001451 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001452 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001453 }
1454
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001455 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001456 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1457 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001458 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001459 }
1460
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001461 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001462 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1463 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001464 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001465 }
1466
Willy Tarreaufb356202010-08-03 14:02:05 +02001467 if (ana_list & AN_REQ_INSPECT_BE) {
1468 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1469 break;
1470 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1471 }
1472
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001473 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001474 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1475 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001476 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001477 }
1478
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001479 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001480 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001481 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001482 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001483 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001484
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001485 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001486 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1487 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001488 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001489 }
1490
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001491 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001492 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001493 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001494 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001495 }
Emeric Brun647caf12009-06-30 17:57:00 +02001496
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001497 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001498 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1499 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001500 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001501 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001502
Emeric Brun1d33b292010-01-04 15:47:17 +01001503 if (ana_list & AN_REQ_STICKING_RULES) {
1504 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1505 break;
1506 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1507 }
1508
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001509 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001510 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1511 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001512 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001513 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001514 break;
1515 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001516 }
Willy Tarreau84455332009-03-15 22:34:05 +01001517
Willy Tarreau815a9b22010-07-27 17:15:12 +02001518 rq_prod_last = s->si[0].state;
1519 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001520 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001521 rqf_last = s->req->flags;
1522
1523 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001524 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001525 }
1526
Willy Tarreau576507f2010-01-07 00:09:04 +01001527 /* we'll monitor the request analysers while parsing the response,
1528 * because some response analysers may indirectly enable new request
1529 * analysers (eg: HTTP keep-alive).
1530 */
1531 req_ana_back = s->req->analysers;
1532
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001533 resync_response:
1534 /* Analyse response */
1535
1536 if (unlikely(s->rep->flags & BF_HIJACK)) {
1537 /* In inject mode, we wake up everytime something has
1538 * happened on the write side of the buffer.
1539 */
1540 unsigned int flags = s->rep->flags;
1541
1542 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1543 !(s->rep->flags & BF_FULL)) {
1544 s->rep->hijacker(s, s->rep);
1545 }
1546
1547 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1548 rpf_last = s->rep->flags;
1549 goto resync_response;
1550 }
1551 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001552 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001553 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1554 s->si[0].state != rp_cons_last ||
1555 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001556 unsigned int flags = s->rep->flags;
1557
Willy Tarreau0499e352010-12-17 07:13:42 +01001558 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1559 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1560 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1561 * for some free space in the response buffer, so we might need to call
1562 * it when something changes in the response buffer, but still we pass
1563 * it the request buffer. Note that the SI state might very well still
1564 * be zero due to us returning a flow of redirects!
1565 */
1566 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1567 s->req->flags |= BF_WAKE_ONCE;
1568 }
1569
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001570 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001571 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001572 unsigned int ana_list;
1573 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001574
Willy Tarreau90deb182010-01-07 00:20:41 +01001575 /* it's up to the analysers to stop disable reading or
1576 * closing. Note: if an analyser disables any of these
1577 * bits, it is responsible for enabling them again when
1578 * it disables itself, so that other analysers are called
1579 * in similar conditions.
1580 */
1581 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001582 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001583
1584 /* We will call all analysers for which a bit is set in
1585 * s->rep->analysers, following the bit order from LSB
1586 * to MSB. The analysers must remove themselves from
1587 * the list when not needed. Any analyser may return 0
1588 * to break out of the loop, either because of missing
1589 * data to take a decision, or because it decides to
1590 * kill the session. We loop at least once through each
1591 * analyser, and we may loop again if other analysers
1592 * are added in the middle.
1593 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001594
1595 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001596 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001597 /* Warning! ensure that analysers are always placed in ascending order! */
1598
Emeric Brun97679e72010-09-23 17:56:44 +02001599 if (ana_list & AN_RES_INSPECT) {
1600 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1601 break;
1602 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1603 }
1604
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001605 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001606 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1607 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001608 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001609 }
1610
Emeric Brun1d33b292010-01-04 15:47:17 +01001611 if (ana_list & AN_RES_STORE_RULES) {
1612 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1613 break;
1614 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1615 }
1616
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001617 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001618 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1619 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001620 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001621 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001622
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001623 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001624 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1625 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001626 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001627 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001628 break;
1629 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001630 }
1631
Willy Tarreau815a9b22010-07-27 17:15:12 +02001632 rp_cons_last = s->si[0].state;
1633 rp_prod_last = s->si[1].state;
1634 rpf_last = s->rep->flags;
1635
1636 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001637 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001638 }
1639
Willy Tarreau576507f2010-01-07 00:09:04 +01001640 /* maybe someone has added some request analysers, so we must check and loop */
1641 if (s->req->analysers & ~req_ana_back)
1642 goto resync_request;
1643
Willy Tarreau0499e352010-12-17 07:13:42 +01001644 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1645 goto resync_request;
1646
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001647 /* FIXME: here we should call protocol handlers which rely on
1648 * both buffers.
1649 */
1650
1651
1652 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001653 * Now we propagate unhandled errors to the session. Normally
1654 * we're just in a data phase here since it means we have not
1655 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001656 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001657 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001658 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001659 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1660 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001661 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001662 if (s->req->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001663 s->be->be_counters.cli_aborts++;
1664 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001665 if (srv)
1666 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001667 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001668 }
1669 else if (s->req->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001670 s->be->be_counters.cli_aborts++;
1671 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001672 if (srv)
1673 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001674 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001675 }
1676 else if (s->req->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001677 s->be->be_counters.srv_aborts++;
1678 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001679 if (srv)
1680 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001681 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001682 }
1683 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001684 s->be->be_counters.srv_aborts++;
1685 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001686 if (srv)
1687 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001688 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001689 }
Willy Tarreau84455332009-03-15 22:34:05 +01001690 sess_set_term_flags(s);
1691 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001692 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1693 /* Report it if the server got an error or a read timeout expired */
1694 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001695 if (s->rep->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001696 s->be->be_counters.srv_aborts++;
1697 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001698 if (srv)
1699 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001700 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001701 }
1702 else if (s->rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001703 s->be->be_counters.srv_aborts++;
1704 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001705 if (srv)
1706 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001707 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001708 }
1709 else if (s->rep->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001710 s->be->be_counters.cli_aborts++;
1711 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001712 if (srv)
1713 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001714 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001715 }
1716 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001717 s->be->be_counters.cli_aborts++;
1718 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001719 if (srv)
1720 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001721 s->flags |= SN_ERR_CLITO;
1722 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001723 sess_set_term_flags(s);
1724 }
Willy Tarreau84455332009-03-15 22:34:05 +01001725 }
1726
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001727 /*
1728 * Here we take care of forwarding unhandled data. This also includes
1729 * connection establishments and shutdown requests.
1730 */
1731
1732
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001733 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001734 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001735 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1736 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001737 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001738 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001739 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001740 (s->req->prod->state >= SI_ST_EST) &&
1741 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001742 /* This buffer is freewheeling, there's no analyser nor hijacker
1743 * attached to it. If any data are left in, we'll permit them to
1744 * move.
1745 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001746 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001747 buffer_auto_connect(s->req);
1748 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001749 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001750
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001751 /* We'll let data flow between the producer (if still connected)
1752 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001753 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001754 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001755 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001756 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001757
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001758 /* check if it is wise to enable kernel splicing to forward request data */
1759 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1760 s->req->to_forward &&
1761 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001762 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001763 (pipes_used < global.maxpipes) &&
1764 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1765 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1766 (s->req->flags & BF_STREAMER_FAST)))) {
1767 s->req->flags |= BF_KERN_SPLICING;
1768 }
1769
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001770 /* reflect what the L7 analysers have seen last */
1771 rqf_last = s->req->flags;
1772
1773 /*
1774 * Now forward all shutdown requests between both sides of the buffer
1775 */
1776
Willy Tarreau520d95e2009-09-19 21:04:57 +02001777 /* first, let's check if the request buffer needs to shutdown(write), which may
1778 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001779 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001780 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001781 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001782 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001783 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001784
1785 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001786 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 +01001787 s->req->cons->shutw(s->req->cons);
1788
1789 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001790 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1791 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001792 buffer_shutr_now(s->req);
1793
1794 /* shutdown(read) pending */
1795 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1796 s->req->prod->shutr(s->req->prod);
1797
Willy Tarreau520d95e2009-09-19 21:04:57 +02001798 /* it's possible that an upper layer has requested a connection setup or abort.
1799 * There are 2 situations where we decide to establish a new connection :
1800 * - there are data scheduled for emission in the buffer
1801 * - the BF_AUTO_CONNECT flag is set (active connection)
1802 */
1803 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001804 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001805 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001806 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001807 * switch to the connected state, otherwise we perform a connection
1808 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001809 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001810 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001811 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001812 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001813 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001814 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1815 s->req->wex = TICK_ETERNITY;
1816 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001817 }
Willy Tarreau73201222009-08-16 18:27:24 +02001818 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001819 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001820 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001821 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1822 buffer_shutr_now(s->rep);
1823 }
Willy Tarreau92795622009-03-06 12:51:23 +01001824 }
1825
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001826
1827 /* we may have a pending connection request, or a connection waiting
1828 * for completion.
1829 */
1830 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1831 do {
1832 /* nb: step 1 might switch from QUE to ASS, but we first want
1833 * to give a chance to step 2 to perform a redirect if needed.
1834 */
1835 if (s->si[1].state != SI_ST_REQ)
1836 sess_update_stream_int(s, &s->si[1]);
1837 if (s->si[1].state == SI_ST_REQ)
1838 sess_prepare_conn_req(s, &s->si[1]);
1839
Willy Tarreau827aee92011-03-10 16:55:02 +01001840 srv = target_srv(&s->target);
1841 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001842 perform_http_redirect(s, &s->si[1]);
1843 } while (s->si[1].state == SI_ST_ASS);
1844 }
1845
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001846 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001847 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001848 goto resync_stream_interface;
1849
Willy Tarreau815a9b22010-07-27 17:15:12 +02001850 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001851 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001852 goto resync_request;
1853
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001854 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001855
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001856 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001857 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001858 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1859 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001860 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001861 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001862 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001863 (s->rep->prod->state >= SI_ST_EST) &&
1864 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001865 /* This buffer is freewheeling, there's no analyser nor hijacker
1866 * attached to it. If any data are left in, we'll permit them to
1867 * move.
1868 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001869 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001870 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001871 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001872
1873 /* We'll let data flow between the producer (if still connected)
1874 * to the consumer.
1875 */
1876 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001877 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001878 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001879
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001880 /* check if it is wise to enable kernel splicing to forward response data */
1881 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1882 s->rep->to_forward &&
1883 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001884 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001885 (pipes_used < global.maxpipes) &&
1886 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1887 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1888 (s->rep->flags & BF_STREAMER_FAST)))) {
1889 s->rep->flags |= BF_KERN_SPLICING;
1890 }
1891
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001892 /* reflect what the L7 analysers have seen last */
1893 rpf_last = s->rep->flags;
1894
1895 /*
1896 * Now forward all shutdown requests between both sides of the buffer
1897 */
1898
1899 /*
1900 * FIXME: this is probably where we should produce error responses.
1901 */
1902
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001903 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001904 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1905 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001906 buffer_shutw_now(s->rep);
1907
1908 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001909 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 +01001910 s->rep->cons->shutw(s->rep->cons);
1911
1912 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001913 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1914 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001915 buffer_shutr_now(s->rep);
1916
1917 /* shutdown(read) pending */
1918 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1919 s->rep->prod->shutr(s->rep->prod);
1920
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001921 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001922 goto resync_stream_interface;
1923
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001924 if (s->req->flags != rqf_last)
1925 goto resync_request;
1926
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001927 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001928 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001929
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001930 /* we're interested in getting wakeups again */
1931 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1932 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1933
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001934 /* This is needed only when debugging is enabled, to indicate
1935 * client-side or server-side close. Please note that in the unlikely
1936 * event where both sides would close at once, the sequence is reported
1937 * on the server side first.
1938 */
1939 if (unlikely((global.mode & MODE_DEBUG) &&
1940 (!(global.mode & MODE_QUIET) ||
1941 (global.mode & MODE_VERBOSE)))) {
1942 int len;
1943
1944 if (s->si[1].state == SI_ST_CLO &&
1945 s->si[1].prev_state == SI_ST_EST) {
1946 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1947 s->uniq_id, s->be->id,
1948 (unsigned short)s->si[0].fd,
1949 (unsigned short)s->si[1].fd);
1950 write(1, trash, len);
1951 }
1952
1953 if (s->si[0].state == SI_ST_CLO &&
1954 s->si[0].prev_state == SI_ST_EST) {
1955 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1956 s->uniq_id, s->be->id,
1957 (unsigned short)s->si[0].fd,
1958 (unsigned short)s->si[1].fd);
1959 write(1, trash, len);
1960 }
1961 }
1962
1963 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1964 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1965
1966 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1967 session_process_counters(s);
1968
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001969 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001970 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001971
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001972 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001973 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001974
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001975 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1976 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 +01001977 s->si[0].prev_state = s->si[0].state;
1978 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001979 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1980 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001981
1982 /* Trick: if a request is being waiting for the server to respond,
1983 * and if we know the server can timeout, we don't want the timeout
1984 * to expire on the client side first, but we're still interested
1985 * in passing data from the client to the server (eg: POST). Thus,
1986 * we can cancel the client's request timeout if the server's
1987 * request timeout is set and the server has not yet sent a response.
1988 */
1989
Willy Tarreau520d95e2009-09-19 21:04:57 +02001990 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001991 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1992 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001993 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001994 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001995
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001996 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001997 * Note that this one may wake the task up again.
1998 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001999 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
2000 s->rep->cons->target.type == TARG_TYPE_APPLET) {
2001 if (s->req->cons->target.type == TARG_TYPE_APPLET)
2002 s->req->cons->target.ptr.a->fct(s->req->cons);
2003 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
2004 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002005 if (task_in_rq(t)) {
2006 /* If we woke up, we don't want to requeue the
2007 * task to the wait queue, but rather requeue
2008 * it into the runqueue ASAP.
2009 */
2010 t->expire = TICK_ETERNITY;
2011 return t;
2012 }
2013 }
2014
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002015 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2016 tick_first(s->rep->rex, s->rep->wex));
2017 if (s->req->analysers)
2018 t->expire = tick_first(t->expire, s->req->analyse_exp);
2019
2020 if (s->si[0].exp)
2021 t->expire = tick_first(t->expire, s->si[0].exp);
2022
2023 if (s->si[1].exp)
2024 t->expire = tick_first(t->expire, s->si[1].exp);
2025
2026#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002027 fprintf(stderr,
2028 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2029 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2030 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2031 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 +01002032#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002033
2034#ifdef DEBUG_DEV
2035 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002036 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002037 ABORT_NOW();
2038#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002039 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002040 }
2041
2042 s->fe->feconn--;
2043 if (s->flags & SN_BE_ASSIGNED)
2044 s->be->beconn--;
2045 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002046 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002047 s->listener->nbconn--;
2048 if (s->listener->state == LI_FULL &&
2049 s->listener->nbconn < s->listener->maxconn) {
2050 /* we should reactivate the listener */
2051 EV_FD_SET(s->listener->fd, DIR_RD);
2052 s->listener->state = LI_READY;
2053 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002054
2055 if (unlikely((global.mode & MODE_DEBUG) &&
2056 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2057 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002058 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002059 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002060 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002061 write(1, trash, len);
2062 }
2063
2064 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2065 session_process_counters(s);
2066
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002067 if (s->txn.status) {
2068 int n;
2069
2070 n = s->txn.status / 100;
2071 if (n < 1 || n > 5)
2072 n = 0;
2073
2074 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002075 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002076
Willy Tarreau24657792010-02-26 10:30:28 +01002077 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002078 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002079 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002080 }
2081
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002082 /* let's do a final log if we need it */
2083 if (s->logs.logwait &&
2084 !(s->flags & SN_MONITOR) &&
2085 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002086 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002087 }
2088
2089 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002090 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002091 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002092 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002093 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002094}
2095
Willy Tarreau7c669d72008-06-20 15:04:11 +02002096/*
2097 * This function adjusts sess->srv_conn and maintains the previous and new
2098 * server's served session counts. Setting newsrv to NULL is enough to release
2099 * current connection slot. This function also notifies any LB algo which might
2100 * expect to be informed about any change in the number of active sessions on a
2101 * server.
2102 */
2103void sess_change_server(struct session *sess, struct server *newsrv)
2104{
2105 if (sess->srv_conn == newsrv)
2106 return;
2107
2108 if (sess->srv_conn) {
2109 sess->srv_conn->served--;
2110 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2111 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
2112 sess->srv_conn = NULL;
2113 }
2114
2115 if (newsrv) {
2116 newsrv->served++;
2117 if (newsrv->proxy->lbprm.server_take_conn)
2118 newsrv->proxy->lbprm.server_take_conn(newsrv);
2119 sess->srv_conn = newsrv;
2120 }
2121}
2122
Willy Tarreau84455332009-03-15 22:34:05 +01002123/* Set correct session termination flags in case no analyser has done it. It
2124 * also counts a failed request if the server state has not reached the request
2125 * stage.
2126 */
2127void sess_set_term_flags(struct session *s)
2128{
2129 if (!(s->flags & SN_FINST_MASK)) {
2130 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002131
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002132 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002133 if (s->listener->counters)
2134 s->listener->counters->failed_req++;
2135
Willy Tarreau84455332009-03-15 22:34:05 +01002136 s->flags |= SN_FINST_R;
2137 }
2138 else if (s->si[1].state == SI_ST_QUE)
2139 s->flags |= SN_FINST_Q;
2140 else if (s->si[1].state < SI_ST_EST)
2141 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002142 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002143 s->flags |= SN_FINST_D;
2144 else
2145 s->flags |= SN_FINST_L;
2146 }
2147}
2148
2149/* Handle server-side errors for default protocols. It is called whenever a a
2150 * connection setup is aborted or a request is aborted in queue. It sets the
2151 * session termination flags so that the caller does not have to worry about
2152 * them. It's installed as ->srv_error for the server-side stream_interface.
2153 */
2154void default_srv_error(struct session *s, struct stream_interface *si)
2155{
2156 int err_type = si->err_type;
2157 int err = 0, fin = 0;
2158
2159 if (err_type & SI_ET_QUEUE_ABRT) {
2160 err = SN_ERR_CLICL;
2161 fin = SN_FINST_Q;
2162 }
2163 else if (err_type & SI_ET_CONN_ABRT) {
2164 err = SN_ERR_CLICL;
2165 fin = SN_FINST_C;
2166 }
2167 else if (err_type & SI_ET_QUEUE_TO) {
2168 err = SN_ERR_SRVTO;
2169 fin = SN_FINST_Q;
2170 }
2171 else if (err_type & SI_ET_QUEUE_ERR) {
2172 err = SN_ERR_SRVCL;
2173 fin = SN_FINST_Q;
2174 }
2175 else if (err_type & SI_ET_CONN_TO) {
2176 err = SN_ERR_SRVTO;
2177 fin = SN_FINST_C;
2178 }
2179 else if (err_type & SI_ET_CONN_ERR) {
2180 err = SN_ERR_SRVCL;
2181 fin = SN_FINST_C;
2182 }
2183 else /* SI_ET_CONN_OTHER and others */ {
2184 err = SN_ERR_INTERNAL;
2185 fin = SN_FINST_C;
2186 }
2187
2188 if (!(s->flags & SN_ERR_MASK))
2189 s->flags |= err;
2190 if (!(s->flags & SN_FINST_MASK))
2191 s->flags |= fin;
2192}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002193
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002194
Willy Tarreau8b22a712010-06-18 17:46:06 +02002195/************************************************************************/
2196/* All supported ACL keywords must be declared here. */
2197/************************************************************************/
2198
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002199/* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */
2200static int
2201acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2202{
2203 test->flags = ACL_TEST_F_VOL_TEST;
2204 test->i = 0;
2205 if (ts != NULL) {
2206 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2207 if (!ptr)
2208 return 0; /* parameter not stored */
2209 test->i = stktable_data_cast(ptr, gpc0);
2210 }
2211 return 1;
2212}
2213
2214/* set test->i to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002215 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002216 */
2217static int
Willy Tarreau56123282010-08-06 19:06:56 +02002218acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2219 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002220{
Willy Tarreau56123282010-08-06 19:06:56 +02002221 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002222 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002223 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002224}
2225
2226/* set test->i to the General Purpose Counter 0 value from the session's tracked
2227 * backend counters.
2228 */
2229static int
Willy Tarreau56123282010-08-06 19:06:56 +02002230acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2231 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002232{
Willy Tarreau56123282010-08-06 19:06:56 +02002233 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002234 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002235 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002236}
2237
2238/* set test->i to the General Purpose Counter 0 value from the session's source
2239 * address in the table pointed to by expr.
2240 */
2241static int
2242acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2243 struct acl_expr *expr, struct acl_test *test)
2244{
2245 struct stktable_key *key;
2246
2247 key = tcpv4_src_to_stktable_key(l4);
2248 if (!key)
2249 return 0; /* only TCPv4 is supported right now */
2250
2251 if (expr->arg_len)
2252 px = find_stktable(expr->arg.str);
2253
2254 if (!px)
2255 return 0; /* table not found */
2256
2257 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2258}
2259
2260/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
2261 * return it into test->i.
2262 */
2263static int
2264acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2265{
2266 test->flags = ACL_TEST_F_VOL_TEST;
2267 test->i = 0;
2268 if (ts != NULL) {
2269 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2270 if (!ptr)
2271 return 0; /* parameter not stored */
2272 test->i = ++stktable_data_cast(ptr, gpc0);
2273 }
2274 return 1;
2275}
2276
2277/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002278 * frontend counters and return it into test->i.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002279 */
2280static int
Willy Tarreau56123282010-08-06 19:06:56 +02002281acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2282 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002283{
Willy Tarreau56123282010-08-06 19:06:56 +02002284 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002285 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002286 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002287}
2288
2289/* Increment the General Purpose Counter 0 value from the session's tracked
2290 * backend counters and return it into test->i.
2291 */
2292static int
Willy Tarreau56123282010-08-06 19:06:56 +02002293acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2294 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002295{
Willy Tarreau56123282010-08-06 19:06:56 +02002296 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002297 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002298 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002299}
2300
2301/* Increment the General Purpose Counter 0 value from the session's source
2302 * address in the table pointed to by expr, and return it into test->i.
2303 */
2304static int
2305acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2306 struct acl_expr *expr, struct acl_test *test)
2307{
2308 struct stktable_key *key;
2309
2310 key = tcpv4_src_to_stktable_key(l4);
2311 if (!key)
2312 return 0; /* only TCPv4 is supported right now */
2313
2314 if (expr->arg_len)
2315 px = find_stktable(expr->arg.str);
2316
2317 if (!px)
2318 return 0; /* table not found */
2319
2320 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2321}
2322
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002323/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2324static int
2325acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2326{
2327 test->flags = ACL_TEST_F_VOL_TEST;
2328 test->i = 0;
2329 if (ts != NULL) {
2330 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2331 if (!ptr)
2332 return 0; /* parameter not stored */
2333 test->i = stktable_data_cast(ptr, conn_cnt);
2334 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002335 return 1;
2336}
2337
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002338/* set test->i to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002339static int
Willy Tarreau56123282010-08-06 19:06:56 +02002340acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2341 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002342{
Willy Tarreau56123282010-08-06 19:06:56 +02002343 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002344 return 0;
2345
Willy Tarreau56123282010-08-06 19:06:56 +02002346 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002347}
2348
2349/* set test->i to the cumulated number of connections from the session's tracked BE counters */
2350static int
Willy Tarreau56123282010-08-06 19:06:56 +02002351acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2352 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002353{
Willy Tarreau56123282010-08-06 19:06:56 +02002354 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002355 return 0;
2356
Willy Tarreau56123282010-08-06 19:06:56 +02002357 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002358}
2359
2360/* set test->i to the cumulated number of connections from the session's source
2361 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002362 */
2363static int
2364acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2365 struct acl_expr *expr, struct acl_test *test)
2366{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002367 struct stktable_key *key;
2368
2369 key = tcpv4_src_to_stktable_key(l4);
2370 if (!key)
2371 return 0; /* only TCPv4 is supported right now */
2372
2373 if (expr->arg_len)
2374 px = find_stktable(expr->arg.str);
2375
2376 if (!px)
2377 return 0; /* table not found */
2378
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002379 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002380}
2381
Willy Tarreau91c43d72010-06-20 11:19:22 +02002382/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2383static int
2384acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2385{
2386 test->flags = ACL_TEST_F_VOL_TEST;
2387 test->i = 0;
2388 if (ts != NULL) {
2389 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2390 if (!ptr)
2391 return 0; /* parameter not stored */
2392 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2393 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2394 }
2395 return 1;
2396}
2397
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002398/* set test->i to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002399 * the configured period.
2400 */
2401static int
Willy Tarreau56123282010-08-06 19:06:56 +02002402acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2403 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002404{
Willy Tarreau56123282010-08-06 19:06:56 +02002405 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002406 return 0;
2407
Willy Tarreau56123282010-08-06 19:06:56 +02002408 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002409}
2410
2411/* set test->i to the connection rate from the session's tracked BE counters over
2412 * the configured period.
2413 */
2414static int
Willy Tarreau56123282010-08-06 19:06:56 +02002415acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2416 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002417{
Willy Tarreau56123282010-08-06 19:06:56 +02002418 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002419 return 0;
2420
Willy Tarreau56123282010-08-06 19:06:56 +02002421 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002422}
2423
2424/* set test->i to the connection rate from the session's source address in the
2425 * table pointed to by expr, over the configured period.
2426 */
2427static int
2428acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2429 struct acl_expr *expr, struct acl_test *test)
2430{
2431 struct stktable_key *key;
2432
2433 key = tcpv4_src_to_stktable_key(l4);
2434 if (!key)
2435 return 0; /* only TCPv4 is supported right now */
2436
2437 if (expr->arg_len)
2438 px = find_stktable(expr->arg.str);
2439
2440 if (!px)
2441 return 0; /* table not found */
2442
2443 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2444}
2445
Willy Tarreau8b22a712010-06-18 17:46:06 +02002446/* set test->i to the number of connections from the session's source address
2447 * in the table pointed to by expr, after updating it.
2448 */
2449static int
2450acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2451 struct acl_expr *expr, struct acl_test *test)
2452{
2453 struct stksess *ts;
2454 struct stktable_key *key;
2455 void *ptr;
2456
2457 key = tcpv4_src_to_stktable_key(l4);
2458 if (!key)
2459 return 0; /* only TCPv4 is supported right now */
2460
2461 if (expr->arg_len)
2462 px = find_stktable(expr->arg.str);
2463
2464 if (!px)
2465 return 0; /* table not found */
2466
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002467 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2468 /* entry does not exist and could not be created */
2469 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002470
2471 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2472 if (!ptr)
2473 return 0; /* parameter not stored in this table */
2474
2475 test->i = ++stktable_data_cast(ptr, conn_cnt);
2476 test->flags = ACL_TEST_F_VOL_TEST;
2477 return 1;
2478}
2479
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002480/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2481static int
2482acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2483{
2484 test->flags = ACL_TEST_F_VOL_TEST;
2485 test->i = 0;
2486
2487 if (ts != NULL) {
2488 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2489 if (!ptr)
2490 return 0; /* parameter not stored */
2491 test->i = stktable_data_cast(ptr, conn_cur);
2492 }
2493 return 1;
2494}
2495
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002496/* set test->i to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002497static int
Willy Tarreau56123282010-08-06 19:06:56 +02002498acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2499 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002500{
Willy Tarreau56123282010-08-06 19:06:56 +02002501 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002502 return 0;
2503
Willy Tarreau56123282010-08-06 19:06:56 +02002504 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002505}
2506
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002507/* set test->i to the number of concurrent connections from the session's tracked BE counters */
2508static int
Willy Tarreau56123282010-08-06 19:06:56 +02002509acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2510 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002511{
Willy Tarreau56123282010-08-06 19:06:56 +02002512 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002513 return 0;
2514
Willy Tarreau56123282010-08-06 19:06:56 +02002515 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002516}
2517
Willy Tarreau38285c12010-06-18 16:35:43 +02002518/* set test->i to the number of concurrent connections from the session's source
2519 * address in the table pointed to by expr.
2520 */
2521static int
2522acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2523 struct acl_expr *expr, struct acl_test *test)
2524{
Willy Tarreau38285c12010-06-18 16:35:43 +02002525 struct stktable_key *key;
2526
2527 key = tcpv4_src_to_stktable_key(l4);
2528 if (!key)
2529 return 0; /* only TCPv4 is supported right now */
2530
2531 if (expr->arg_len)
2532 px = find_stktable(expr->arg.str);
2533
2534 if (!px)
2535 return 0; /* table not found */
2536
Willy Tarreau1b6e6082011-03-16 06:55:50 +01002537 return acl_fetch_conn_cur(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002538}
2539
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002540/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2541static int
2542acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2543{
2544 test->flags = ACL_TEST_F_VOL_TEST;
2545 test->i = 0;
2546 if (ts != NULL) {
2547 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2548 if (!ptr)
2549 return 0; /* parameter not stored */
2550 test->i = stktable_data_cast(ptr, sess_cnt);
2551 }
2552 return 1;
2553}
2554
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002555/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002556static int
Willy Tarreau56123282010-08-06 19:06:56 +02002557acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2558 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002559{
Willy Tarreau56123282010-08-06 19:06:56 +02002560 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002561 return 0;
2562
Willy Tarreau56123282010-08-06 19:06:56 +02002563 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002564}
2565
2566/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2567static int
Willy Tarreau56123282010-08-06 19:06:56 +02002568acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2569 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002570{
Willy Tarreau56123282010-08-06 19:06:56 +02002571 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002572 return 0;
2573
Willy Tarreau56123282010-08-06 19:06:56 +02002574 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002575}
2576
2577/* set test->i to the cumulated number of session from the session's source
2578 * address in the table pointed to by expr.
2579 */
2580static int
2581acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2582 struct acl_expr *expr, struct acl_test *test)
2583{
2584 struct stktable_key *key;
2585
2586 key = tcpv4_src_to_stktable_key(l4);
2587 if (!key)
2588 return 0; /* only TCPv4 is supported right now */
2589
2590 if (expr->arg_len)
2591 px = find_stktable(expr->arg.str);
2592
2593 if (!px)
2594 return 0; /* table not found */
2595
2596 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2597}
2598
Willy Tarreau91c43d72010-06-20 11:19:22 +02002599/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2600static int
2601acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2602{
2603 test->flags = ACL_TEST_F_VOL_TEST;
2604 test->i = 0;
2605 if (ts != NULL) {
2606 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2607 if (!ptr)
2608 return 0; /* parameter not stored */
2609 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2610 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2611 }
2612 return 1;
2613}
2614
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002615/* set test->i to the session rate from the session's tracked FE counters over
2616 * the configured period.
2617 */
2618static int
Willy Tarreau56123282010-08-06 19:06:56 +02002619acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2620 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002621{
Willy Tarreau56123282010-08-06 19:06:56 +02002622 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002623 return 0;
2624
Willy Tarreau56123282010-08-06 19:06:56 +02002625 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002626}
2627
2628/* set test->i to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002629 * the configured period.
2630 */
2631static int
Willy Tarreau56123282010-08-06 19:06:56 +02002632acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2633 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002634{
Willy Tarreau56123282010-08-06 19:06:56 +02002635 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002636 return 0;
2637
Willy Tarreau56123282010-08-06 19:06:56 +02002638 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002639}
2640
2641/* set test->i to the session rate from the session's source address in the
2642 * table pointed to by expr, over the configured period.
2643 */
2644static int
2645acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2646 struct acl_expr *expr, struct acl_test *test)
2647{
2648 struct stktable_key *key;
2649
2650 key = tcpv4_src_to_stktable_key(l4);
2651 if (!key)
2652 return 0; /* only TCPv4 is supported right now */
2653
2654 if (expr->arg_len)
2655 px = find_stktable(expr->arg.str);
2656
2657 if (!px)
2658 return 0; /* table not found */
2659
2660 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2661}
2662
Willy Tarreauda7ff642010-06-23 11:44:09 +02002663/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2664static int
2665acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2666{
2667 test->flags = ACL_TEST_F_VOL_TEST;
2668 test->i = 0;
2669 if (ts != NULL) {
2670 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2671 if (!ptr)
2672 return 0; /* parameter not stored */
2673 test->i = stktable_data_cast(ptr, http_req_cnt);
2674 }
2675 return 1;
2676}
2677
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002678/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002679static int
Willy Tarreau56123282010-08-06 19:06:56 +02002680acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2681 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002682{
Willy Tarreau56123282010-08-06 19:06:56 +02002683 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002684 return 0;
2685
Willy Tarreau56123282010-08-06 19:06:56 +02002686 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002687}
2688
2689/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2690static int
Willy Tarreau56123282010-08-06 19:06:56 +02002691acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2692 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002693{
Willy Tarreau56123282010-08-06 19:06:56 +02002694 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002695 return 0;
2696
Willy Tarreau56123282010-08-06 19:06:56 +02002697 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002698}
2699
2700/* set test->i to the cumulated number of session from the session's source
2701 * address in the table pointed to by expr.
2702 */
2703static int
2704acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002705 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002706{
2707 struct stktable_key *key;
2708
2709 key = tcpv4_src_to_stktable_key(l4);
2710 if (!key)
2711 return 0; /* only TCPv4 is supported right now */
2712
2713 if (expr->arg_len)
2714 px = find_stktable(expr->arg.str);
2715
2716 if (!px)
2717 return 0; /* table not found */
2718
2719 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2720}
2721
2722/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2723static int
2724acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2725{
2726 test->flags = ACL_TEST_F_VOL_TEST;
2727 test->i = 0;
2728 if (ts != NULL) {
2729 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2730 if (!ptr)
2731 return 0; /* parameter not stored */
2732 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2733 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2734 }
2735 return 1;
2736}
2737
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002738/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002739 * the configured period.
2740 */
2741static int
Willy Tarreau56123282010-08-06 19:06:56 +02002742acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2743 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002744{
Willy Tarreau56123282010-08-06 19:06:56 +02002745 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002746 return 0;
2747
Willy Tarreau56123282010-08-06 19:06:56 +02002748 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002749}
2750
2751/* set test->i to the session rate from the session's tracked BE counters over
2752 * the configured period.
2753 */
2754static int
Willy Tarreau56123282010-08-06 19:06:56 +02002755acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2756 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002757{
Willy Tarreau56123282010-08-06 19:06:56 +02002758 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002759 return 0;
2760
Willy Tarreau56123282010-08-06 19:06:56 +02002761 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002762}
2763
2764/* set test->i to the session rate from the session's source address in the
2765 * table pointed to by expr, over the configured period.
2766 */
2767static int
2768acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002769 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002770{
2771 struct stktable_key *key;
2772
2773 key = tcpv4_src_to_stktable_key(l4);
2774 if (!key)
2775 return 0; /* only TCPv4 is supported right now */
2776
2777 if (expr->arg_len)
2778 px = find_stktable(expr->arg.str);
2779
2780 if (!px)
2781 return 0; /* table not found */
2782
2783 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2784}
2785
2786/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2787static int
2788acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2789{
2790 test->flags = ACL_TEST_F_VOL_TEST;
2791 test->i = 0;
2792 if (ts != NULL) {
2793 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2794 if (!ptr)
2795 return 0; /* parameter not stored */
2796 test->i = stktable_data_cast(ptr, http_err_cnt);
2797 }
2798 return 1;
2799}
2800
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002801/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002802static int
Willy Tarreau56123282010-08-06 19:06:56 +02002803acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2804 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002805{
Willy Tarreau56123282010-08-06 19:06:56 +02002806 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002807 return 0;
2808
Willy Tarreau56123282010-08-06 19:06:56 +02002809 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002810}
2811
2812/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2813static int
Willy Tarreau56123282010-08-06 19:06:56 +02002814acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2815 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002816{
Willy Tarreau56123282010-08-06 19:06:56 +02002817 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002818 return 0;
2819
Willy Tarreau56123282010-08-06 19:06:56 +02002820 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002821}
2822
2823/* set test->i to the cumulated number of session from the session's source
2824 * address in the table pointed to by expr.
2825 */
2826static int
2827acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002828 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002829{
2830 struct stktable_key *key;
2831
2832 key = tcpv4_src_to_stktable_key(l4);
2833 if (!key)
2834 return 0; /* only TCPv4 is supported right now */
2835
2836 if (expr->arg_len)
2837 px = find_stktable(expr->arg.str);
2838
2839 if (!px)
2840 return 0; /* table not found */
2841
2842 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2843}
2844
2845/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2846static int
2847acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2848{
2849 test->flags = ACL_TEST_F_VOL_TEST;
2850 test->i = 0;
2851 if (ts != NULL) {
2852 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2853 if (!ptr)
2854 return 0; /* parameter not stored */
2855 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2856 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2857 }
2858 return 1;
2859}
2860
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002861/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002862 * the configured period.
2863 */
2864static int
Willy Tarreau56123282010-08-06 19:06:56 +02002865acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2866 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002867{
Willy Tarreau56123282010-08-06 19:06:56 +02002868 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002869 return 0;
2870
Willy Tarreau56123282010-08-06 19:06:56 +02002871 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002872}
2873
2874/* set test->i to the session rate from the session's tracked BE counters over
2875 * the configured period.
2876 */
2877static int
Willy Tarreau56123282010-08-06 19:06:56 +02002878acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2879 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002880{
Willy Tarreau56123282010-08-06 19:06:56 +02002881 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002882 return 0;
2883
Willy Tarreau56123282010-08-06 19:06:56 +02002884 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002885}
2886
2887/* set test->i to the session rate from the session's source address in the
2888 * table pointed to by expr, over the configured period.
2889 */
2890static int
2891acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2892 struct acl_expr *expr, struct acl_test *test)
2893{
2894 struct stktable_key *key;
2895
2896 key = tcpv4_src_to_stktable_key(l4);
2897 if (!key)
2898 return 0; /* only TCPv4 is supported right now */
2899
2900 if (expr->arg_len)
2901 px = find_stktable(expr->arg.str);
2902
2903 if (!px)
2904 return 0; /* table not found */
2905
2906 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2907}
2908
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002909/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2910static int
2911acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2912{
2913 test->flags = ACL_TEST_F_VOL_TEST;
2914 test->i = 0;
2915
2916 if (ts != NULL) {
2917 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2918 if (!ptr)
2919 return 0; /* parameter not stored */
2920 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2921 }
2922 return 1;
2923}
2924
2925/* set test->i to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002926 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002927 */
2928static int
Willy Tarreau56123282010-08-06 19:06:56 +02002929acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2930 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002931{
Willy Tarreau56123282010-08-06 19:06:56 +02002932 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002933 return 0;
2934
Willy Tarreau56123282010-08-06 19:06:56 +02002935 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002936}
2937
2938/* set test->i to the number of kbytes received from clients according to the
2939 * session's tracked BE counters.
2940 */
2941static int
Willy Tarreau56123282010-08-06 19:06:56 +02002942acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2943 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002944{
Willy Tarreau56123282010-08-06 19:06:56 +02002945 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002946 return 0;
2947
Willy Tarreau56123282010-08-06 19:06:56 +02002948 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002949}
2950
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002951/* set test->i to the number of kbytes received from the session's source
2952 * address in the table pointed to by expr.
2953 */
2954static int
2955acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2956 struct acl_expr *expr, struct acl_test *test)
2957{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002958 struct stktable_key *key;
2959
2960 key = tcpv4_src_to_stktable_key(l4);
2961 if (!key)
2962 return 0; /* only TCPv4 is supported right now */
2963
2964 if (expr->arg_len)
2965 px = find_stktable(expr->arg.str);
2966
2967 if (!px)
2968 return 0; /* table not found */
2969
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002970 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2971}
2972
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002973/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2974 * configured period.
2975 */
2976static int
2977acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2978{
2979 test->flags = ACL_TEST_F_VOL_TEST;
2980 test->i = 0;
2981 if (ts != NULL) {
2982 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
2983 if (!ptr)
2984 return 0; /* parameter not stored */
2985 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2986 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2987 }
2988 return 1;
2989}
2990
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002991/* set test->i to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002992 * counters over the configured period.
2993 */
2994static int
Willy Tarreau56123282010-08-06 19:06:56 +02002995acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2996 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002997{
Willy Tarreau56123282010-08-06 19:06:56 +02002998 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002999 return 0;
3000
Willy Tarreau56123282010-08-06 19:06:56 +02003001 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003002}
3003
3004/* set test->i to the bytes rate from clients from the session's tracked BE
3005 * counters over the configured period.
3006 */
3007static int
Willy Tarreau56123282010-08-06 19:06:56 +02003008acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3009 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003010{
Willy Tarreau56123282010-08-06 19:06:56 +02003011 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003012 return 0;
3013
Willy Tarreau56123282010-08-06 19:06:56 +02003014 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003015}
3016
3017/* set test->i to the bytes rate from clients from the session's source address
3018 * in the table pointed to by expr, over the configured period.
3019 */
3020static int
3021acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003022 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003023{
3024 struct stktable_key *key;
3025
3026 key = tcpv4_src_to_stktable_key(l4);
3027 if (!key)
3028 return 0; /* only TCPv4 is supported right now */
3029
3030 if (expr->arg_len)
3031 px = find_stktable(expr->arg.str);
3032
3033 if (!px)
3034 return 0; /* table not found */
3035
3036 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3037}
3038
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003039/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
3040static int
3041acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3042{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003043 test->flags = ACL_TEST_F_VOL_TEST;
3044 test->i = 0;
3045
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003046 if (ts != NULL) {
3047 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003048 if (!ptr)
3049 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003050 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003051 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003052 return 1;
3053}
3054
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003055/* set test->i to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003056 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003057 */
3058static int
Willy Tarreau56123282010-08-06 19:06:56 +02003059acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3060 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003061{
Willy Tarreau56123282010-08-06 19:06:56 +02003062 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003063 return 0;
3064
Willy Tarreau56123282010-08-06 19:06:56 +02003065 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003066}
3067
3068/* set test->i to the number of kbytes sent to clients according to the session's
3069 * tracked BE counters.
3070 */
3071static int
Willy Tarreau56123282010-08-06 19:06:56 +02003072acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3073 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003074{
Willy Tarreau56123282010-08-06 19:06:56 +02003075 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003076 return 0;
3077
Willy Tarreau56123282010-08-06 19:06:56 +02003078 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003079}
3080
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003081/* set test->i to the number of kbytes sent to the session's source address in
3082 * the table pointed to by expr.
3083 */
3084static int
3085acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3086 struct acl_expr *expr, struct acl_test *test)
3087{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003088 struct stktable_key *key;
3089
3090 key = tcpv4_src_to_stktable_key(l4);
3091 if (!key)
3092 return 0; /* only TCPv4 is supported right now */
3093
3094 if (expr->arg_len)
3095 px = find_stktable(expr->arg.str);
3096
3097 if (!px)
3098 return 0; /* table not found */
3099
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003100 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003101}
3102
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003103/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
3104 * configured period.
3105 */
3106static int
3107acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3108{
3109 test->flags = ACL_TEST_F_VOL_TEST;
3110 test->i = 0;
3111 if (ts != NULL) {
3112 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3113 if (!ptr)
3114 return 0; /* parameter not stored */
3115 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3116 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3117 }
3118 return 1;
3119}
3120
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003121/* set test->i to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003122 * over the configured period.
3123 */
3124static int
Willy Tarreau56123282010-08-06 19:06:56 +02003125acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3126 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003127{
Willy Tarreau56123282010-08-06 19:06:56 +02003128 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003129 return 0;
3130
Willy Tarreau56123282010-08-06 19:06:56 +02003131 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003132}
3133
3134/* set test->i to the bytes rate to clients from the session's tracked BE counters
3135 * over the configured period.
3136 */
3137static int
Willy Tarreau56123282010-08-06 19:06:56 +02003138acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3139 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003140{
Willy Tarreau56123282010-08-06 19:06:56 +02003141 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003142 return 0;
3143
Willy Tarreau56123282010-08-06 19:06:56 +02003144 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003145}
3146
3147/* set test->i to the bytes rate to client from the session's source address in
3148 * the table pointed to by expr, over the configured period.
3149 */
3150static int
3151acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003152 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003153{
3154 struct stktable_key *key;
3155
3156 key = tcpv4_src_to_stktable_key(l4);
3157 if (!key)
3158 return 0; /* only TCPv4 is supported right now */
3159
3160 if (expr->arg_len)
3161 px = find_stktable(expr->arg.str);
3162
3163 if (!px)
3164 return 0; /* table not found */
3165
3166 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3167}
3168
Willy Tarreau8b22a712010-06-18 17:46:06 +02003169
3170/* Note: must not be declared <const> as its list will be overwritten */
3171static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003172 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3173 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3174 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3175 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3176 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3177 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3178 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3179 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3180 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3181 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3182 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3183 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3184 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3185 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3186 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3187 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3188 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3189 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3190 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3191 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3192 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3193 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3194 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3195 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3196 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3197 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3198 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3199 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3200 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3201 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3202 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3203 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3204 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3205 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3206 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3207 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3208 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3209 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3210 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3211 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3212 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3213 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3214 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3215 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3216 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3217 { "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 +02003218 { NULL, NULL, NULL, NULL },
3219}};
3220
3221
Willy Tarreau56123282010-08-06 19:06:56 +02003222/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003223 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3224 * <prm> struct is fed with the table name if any. If unspecified, the caller
3225 * will assume that the current proxy's table is used.
3226 */
3227int parse_track_counters(char **args, int *arg,
3228 int section_type, struct proxy *curpx,
3229 struct track_ctr_prm *prm,
3230 struct proxy *defpx, char *err, int errlen)
3231{
3232 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003233 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003234
Willy Tarreau56123282010-08-06 19:06:56 +02003235 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003236 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003237 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003238 */
3239 while (args[*arg]) {
3240 if (strcmp(args[*arg], "src") == 0) {
3241 prm->type = STKTABLE_TYPE_IP;
3242 pattern_type = 1;
3243 }
3244 else if (strcmp(args[*arg], "table") == 0) {
3245 if (!args[*arg + 1]) {
3246 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003247 "missing table for %s in %s '%s'.",
3248 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003249 return -1;
3250 }
3251 /* we copy the table name for now, it will be resolved later */
3252 prm->table.n = strdup(args[*arg + 1]);
3253 (*arg)++;
3254 }
3255 else {
3256 /* unhandled keywords are handled by the caller */
3257 break;
3258 }
3259 (*arg)++;
3260 }
3261
3262 if (!pattern_type) {
3263 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003264 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3265 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003266 return -1;
3267 }
3268
3269 return 0;
3270}
3271
Willy Tarreau8b22a712010-06-18 17:46:06 +02003272__attribute__((constructor))
3273static void __session_init(void)
3274{
3275 acl_register_keywords(&acl_kws);
3276}
3277
Willy Tarreaubaaee002006-06-26 02:48:02 +02003278/*
3279 * Local variables:
3280 * c-indent-level: 8
3281 * c-basic-offset: 8
3282 * End:
3283 */