blob: 9ecb6edcc4923da4b9b391a6e4d204e9dc083c19 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau81f9aa32010-06-01 17:45:26 +02002 * Session management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreau81f9aa32010-06-01 17:45:26 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <stdlib.h>
Willy Tarreau81f9aa32010-06-01 17:45:26 +020014#include <unistd.h>
15#include <fcntl.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020018#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010022#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020024#include <proto/acl.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010025#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020026#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010027#include <proto/checks.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020028#include <proto/dumpstats.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020029#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020030#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010031#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020032#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010034#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010035#include <proto/proto_http.h>
36#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020037#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010039#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010040#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010041#include <proto/stream_interface.h>
42#include <proto/stream_sock.h>
43#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020045struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010046struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau81f9aa32010-06-01 17:45:26 +020048/* This function is called from the protocol layer accept() in order to instanciate
49 * a new session on behalf of a given listener and frontend. It returns a positive
Willy Tarreauabe8ea52010-11-11 10:56:04 +010050 * value upon success, 0 if the connection can be ignored, or a negative value upon
51 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020052 */
53int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
54{
55 struct proxy *p = l->frontend;
56 struct session *s;
57 struct http_txn *txn;
58 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010059 int ret;
60
61
62 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020063
Willy Tarreaufffe1322010-11-11 09:48:16 +010064 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020065 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020066
67 /* minimum session initialization required for monitor mode below */
68 s->flags = 0;
69 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020070 s->stkctr1_entry = NULL;
71 s->stkctr2_entry = NULL;
72 s->stkctr1_table = NULL;
73 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020074
75 /* if this session comes from a known monitoring system, we want to ignore
76 * it as soon as possible, which means closing it immediately for TCP, but
77 * cleanly.
78 */
79 if (unlikely((l->options & LI_O_CHK_MONNET) &&
80 addr->ss_family == AF_INET &&
81 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
82 if (p->mode == PR_MODE_TCP) {
Willy Tarreauabe8ea52010-11-11 10:56:04 +010083 ret = 0; /* successful termination */
84 goto out_free_session;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020085 }
86 s->flags |= SN_MONITOR;
87 s->logs.logwait = 0;
88 }
89
Willy Tarreauabe8ea52010-11-11 10:56:04 +010090 if (unlikely((t = task_new()) == NULL))
91 goto out_free_session;
92
Willy Tarreau81f9aa32010-06-01 17:45:26 +020093 /* OK, we're keeping the session, so let's properly initialize the session */
94 LIST_ADDQ(&sessions, &s->list);
95 LIST_INIT(&s->back_refs);
96
Willy Tarreau81f9aa32010-06-01 17:45:26 +020097 s->term_trace = 0;
Willy Tarreau957c0a52011-03-03 17:42:23 +010098 s->si[0].addr.c.from = *addr;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020099 s->logs.accept_date = date; /* user-visible date for logging */
100 s->logs.tv_accept = now; /* corrected date for internal use */
101 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200102 p->feconn++; /* beconn will be increased once assigned */
103
Willy Tarreaub36b4242010-06-04 20:59:39 +0200104 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200105
106 t->process = l->handler;
107 t->context = s;
108 t->nice = l->nice;
109 t->expire = TICK_ETERNITY;
110
111 s->task = t;
112 s->listener = l;
113
114 /* Note: initially, the session's backend points to the frontend.
115 * This changes later when switching rules are executed or
116 * when the default backend is assigned.
117 */
118 s->be = s->fe = p;
119 s->req = s->rep = NULL; /* will be allocated later */
120
121 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
122 * to abort right here as soon as possible, we check the rules before
123 * even initializing the stream interfaces.
124 */
125 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200126 /* let's do a no-linger now to close with a single RST. */
127 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100128 ret = 0; /* successful termination */
129 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200130 }
131
Willy Tarreaub36b4242010-06-04 20:59:39 +0200132 /* This session was accepted, count it now */
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200133 if (p->feconn > p->counters.feconn_max)
134 p->counters.feconn_max = p->feconn;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100135
Willy Tarreaub36b4242010-06-04 20:59:39 +0200136 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200137 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200138 void *ptr;
139
Willy Tarreau56123282010-08-06 19:06:56 +0200140 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200141 if (ptr)
142 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200143
Willy Tarreau56123282010-08-06 19:06:56 +0200144 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200145 if (ptr)
146 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200147 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200148 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200149
Willy Tarreau56123282010-08-06 19:06:56 +0200150 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200151 void *ptr;
152
Willy Tarreau56123282010-08-06 19:06:56 +0200153 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200154 if (ptr)
155 stktable_data_cast(ptr, sess_cnt)++;
156
Willy Tarreau56123282010-08-06 19:06:56 +0200157 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200158 if (ptr)
159 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200160 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200161 }
162
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200163 /* this part should be common with other protocols */
164 s->si[0].fd = cfd;
165 s->si[0].owner = t;
166 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
167 s->si[0].err_type = SI_ET_NONE;
168 s->si[0].err_loc = NULL;
169 s->si[0].connect = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200170 s->si[0].release = NULL;
Willy Tarreauac825402011-03-04 22:04:29 +0100171 s->si[0].target.type = TARG_TYPE_NONE;
172 s->si[0].target.ptr.v = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200173 s->si[0].exp = TICK_ETERNITY;
174 s->si[0].flags = SI_FL_NONE;
175
176 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
177 s->si[0].flags |= SI_FL_INDEP_STR;
178
179 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
180 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
181
182 /* add the various callbacks */
183 stream_sock_prepare_interface(&s->si[0]);
184
185 /* pre-initialize the other side's stream interface to an INIT state. The
186 * callbacks will be initialized before attempting to connect.
187 */
188 s->si[1].fd = -1; /* just to help with debugging */
189 s->si[1].owner = t;
190 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
191 s->si[1].err_type = SI_ET_NONE;
192 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 Tarreauac825402011-03-04 22:04:29 +0100195 s->si[1].target.type = TARG_TYPE_NONE;
196 s->si[1].target.ptr.v = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200197 s->si[1].shutr = stream_int_shutr;
198 s->si[1].shutw = stream_int_shutw;
199 s->si[1].exp = TICK_ETERNITY;
200 s->si[1].flags = SI_FL_NONE;
201
202 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
203 s->si[1].flags |= SI_FL_INDEP_STR;
204
205 s->srv = s->prev_srv = s->srv_conn = NULL;
Willy Tarreau664beb82011-03-10 11:38:29 +0100206 s->target.type = TARG_TYPE_NONE;
207 s->target.ptr.v = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200208 s->pend_pos = NULL;
209
210 /* init store persistence */
211 s->store_count = 0;
212
213 /* Adjust some socket options */
Willy Tarreaufffe1322010-11-11 09:48:16 +0100214 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200215 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200216
217 txn = &s->txn;
218 /* Those variables will be checked and freed if non-NULL in
219 * session.c:session_free(). It is important that they are
220 * properly initialized.
221 */
222 txn->sessid = NULL;
223 txn->srv_cookie = NULL;
224 txn->cli_cookie = NULL;
225 txn->uri = NULL;
226 txn->req.cap = NULL;
227 txn->rsp.cap = NULL;
228 txn->hdr_idx.v = NULL;
229 txn->hdr_idx.size = txn->hdr_idx.used = 0;
230
231 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
232 goto out_free_task; /* no memory */
233
234 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
235 goto out_free_req; /* no memory */
236
237 /* initialize the request buffer */
238 s->req->size = global.tune.bufsize;
239 buffer_init(s->req);
240 s->req->prod = &s->si[0];
241 s->req->cons = &s->si[1];
242 s->si[0].ib = s->si[1].ob = s->req;
243 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
244
245 /* activate default analysers enabled for this listener */
246 s->req->analysers = l->analysers;
247
248 s->req->wto = TICK_ETERNITY;
249 s->req->rto = TICK_ETERNITY;
250 s->req->rex = TICK_ETERNITY;
251 s->req->wex = TICK_ETERNITY;
252 s->req->analyse_exp = TICK_ETERNITY;
253
254 /* initialize response buffer */
255 s->rep->size = global.tune.bufsize;
256 buffer_init(s->rep);
257 s->rep->prod = &s->si[1];
258 s->rep->cons = &s->si[0];
259 s->si[0].ob = s->si[1].ib = s->rep;
260 s->rep->analysers = 0;
261
262 s->rep->rto = TICK_ETERNITY;
263 s->rep->wto = TICK_ETERNITY;
264 s->rep->rex = TICK_ETERNITY;
265 s->rep->wex = TICK_ETERNITY;
266 s->rep->analyse_exp = TICK_ETERNITY;
267
268 /* finish initialization of the accepted file descriptor */
269 fd_insert(cfd);
270 fdtab[cfd].owner = &s->si[0];
271 fdtab[cfd].state = FD_STREADY;
272 fdtab[cfd].flags = 0;
273 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
274 fdtab[cfd].cb[DIR_RD].b = s->req;
275 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
276 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreau957c0a52011-03-03 17:42:23 +0100277 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->si[0].addr.c.from;
278 fdinfo[cfd].peerlen = sizeof(s->si[0].addr.c.from);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200279 EV_FD_SET(cfd, DIR_RD);
280
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100281 if (p->accept && (ret = p->accept(s)) <= 0) {
282 /* Either we had an unrecoverable error (<0) or work is
283 * finished (=0, eg: monitoring), in both situations,
284 * we can release everything and close.
285 */
286 goto out_free_rep;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200287 }
288
289 /* it is important not to call the wakeup function directly but to
290 * pass through task_wakeup(), because this one knows how to apply
291 * priorities to tasks.
292 */
293 task_wakeup(t, TASK_WOKEN_INIT);
294 return 1;
295
296 /* Error unrolling */
297 out_free_rep:
298 pool_free2(pool2_buffer, s->rep);
299 out_free_req:
300 pool_free2(pool2_buffer, s->req);
301 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200302 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200303 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200304 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200305 task_free(t);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200306 LIST_DEL(&s->list);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100307 out_free_session:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200308 pool_free2(pool2_session, s);
309 out_close:
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100310 if (fdtab[cfd].state != FD_STCLOSE)
311 fd_delete(cfd);
312 else
313 close(cfd);
314 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200315}
316
Willy Tarreaubaaee002006-06-26 02:48:02 +0200317/*
318 * frees the context associated to a session. It must have been removed first.
319 */
320void session_free(struct session *s)
321{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100322 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200323 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100324 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200325 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100326
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327 if (s->pend_pos)
328 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100329
Willy Tarreau1e62de62008-11-11 20:20:02 +0100330 if (s->srv) { /* there may be requests left pending in queue */
331 if (s->flags & SN_CURR_SESS) {
332 s->flags &= ~SN_CURR_SESS;
333 s->srv->cur_sess--;
334 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100335 if (may_dequeue_tasks(s->srv, s->be))
336 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100337 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100338
Willy Tarreau7c669d72008-06-20 15:04:11 +0200339 if (unlikely(s->srv_conn)) {
340 /* the session still has a reserved slot on a server, but
341 * it should normally be only the same as the one above,
342 * so this should not happen in fact.
343 */
344 sess_change_server(s, NULL);
345 }
346
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100347 if (s->req->pipe)
348 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100349
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100350 if (s->rep->pipe)
351 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100352
Willy Tarreau48d63db2008-08-03 17:41:33 +0200353 pool_free2(pool2_buffer, s->req);
354 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200355
Willy Tarreau46023632010-01-07 22:51:47 +0100356 http_end_txn(s);
357
Willy Tarreaua4cda672010-06-06 18:28:49 +0200358 for (i = 0; i < s->store_count; i++) {
359 if (!s->store[i].ts)
360 continue;
361 stksess_free(s->store[i].table, s->store[i].ts);
362 s->store[i].ts = NULL;
363 }
364
Willy Tarreau92fb9832007-10-16 17:34:28 +0200365 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200366 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100367 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
368 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100370
Willy Tarreau56123282010-08-06 19:06:56 +0200371 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200372 session_store_counters(s);
373
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100374 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100375 /* we have to unlink all watchers. We must not relink them if
376 * this session was the last one in the list.
377 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100378 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100379 LIST_INIT(&bref->users);
380 if (s->list.n != &sessions)
381 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100382 bref->ref = s->list.n;
383 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100384 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200385 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200386
387 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200388 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200389 pool_flush2(pool2_buffer);
390 pool_flush2(fe->hdr_idx_pool);
391 pool_flush2(pool2_requri);
392 pool_flush2(pool2_capture);
393 pool_flush2(pool2_session);
394 pool_flush2(fe->req_cap_pool);
395 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200396 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200397}
398
399
400/* perform minimal intializations, report 0 in case of error, 1 if OK. */
401int init_session()
402{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100403 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200404 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
405 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200406}
407
Willy Tarreau30e71012007-11-26 20:15:35 +0100408void session_process_counters(struct session *s)
409{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100410 unsigned long long bytes;
411
Willy Tarreau30e71012007-11-26 20:15:35 +0100412 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100413 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100414 s->logs.bytes_in = s->req->total;
415 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200416 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100417
Willy Tarreau30e71012007-11-26 20:15:35 +0100418 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200419 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100420
Willy Tarreau30e71012007-11-26 20:15:35 +0100421 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200422 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200423
424 if (s->listener->counters)
425 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200426
Willy Tarreau56123282010-08-06 19:06:56 +0200427 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200428 void *ptr;
429
Willy Tarreau56123282010-08-06 19:06:56 +0200430 ptr = stktable_data_ptr(s->stkctr2_table,
431 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200432 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200433 if (ptr)
434 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200435
Willy Tarreau56123282010-08-06 19:06:56 +0200436 ptr = stktable_data_ptr(s->stkctr2_table,
437 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200438 STKTABLE_DT_BYTES_IN_RATE);
439 if (ptr)
440 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200441 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200442 }
443
Willy Tarreau56123282010-08-06 19:06:56 +0200444 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200445 void *ptr;
446
Willy Tarreau56123282010-08-06 19:06:56 +0200447 ptr = stktable_data_ptr(s->stkctr1_table,
448 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200449 STKTABLE_DT_BYTES_IN_CNT);
450 if (ptr)
451 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
452
Willy Tarreau56123282010-08-06 19:06:56 +0200453 ptr = stktable_data_ptr(s->stkctr1_table,
454 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200455 STKTABLE_DT_BYTES_IN_RATE);
456 if (ptr)
457 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200458 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200459 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100460 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100461 }
462
Willy Tarreau30e71012007-11-26 20:15:35 +0100463 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100464 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100465 s->logs.bytes_out = s->rep->total;
466 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200467 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100468
Willy Tarreau30e71012007-11-26 20:15:35 +0100469 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200470 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100471
Willy Tarreau30e71012007-11-26 20:15:35 +0100472 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200473 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200474
475 if (s->listener->counters)
476 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200477
Willy Tarreau56123282010-08-06 19:06:56 +0200478 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200479 void *ptr;
480
Willy Tarreau56123282010-08-06 19:06:56 +0200481 ptr = stktable_data_ptr(s->stkctr2_table,
482 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200483 STKTABLE_DT_BYTES_OUT_CNT);
484 if (ptr)
485 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
486
Willy Tarreau56123282010-08-06 19:06:56 +0200487 ptr = stktable_data_ptr(s->stkctr2_table,
488 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200489 STKTABLE_DT_BYTES_OUT_RATE);
490 if (ptr)
491 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200492 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200493 }
494
Willy Tarreau56123282010-08-06 19:06:56 +0200495 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200496 void *ptr;
497
Willy Tarreau56123282010-08-06 19:06:56 +0200498 ptr = stktable_data_ptr(s->stkctr1_table,
499 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200500 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200501 if (ptr)
502 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200503
Willy Tarreau56123282010-08-06 19:06:56 +0200504 ptr = stktable_data_ptr(s->stkctr1_table,
505 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200506 STKTABLE_DT_BYTES_OUT_RATE);
507 if (ptr)
508 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200509 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200510 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100511 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100512 }
513}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200514
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100515/* This function is called with (si->state == SI_ST_CON) meaning that a
516 * connection was attempted and that the file descriptor is already allocated.
517 * We must check for establishment, error and abort. Possible output states
518 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
519 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
520 * otherwise 1.
521 */
522int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
523{
524 struct buffer *req = si->ob;
525 struct buffer *rep = si->ib;
526
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100527 /* If we got an error, or if nothing happened and the connection timed
528 * out, we must give up. The CER state handler will take care of retry
529 * attempts and error reports.
530 */
531 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100532 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100533 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200534 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100535 fd_delete(si->fd);
536
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200537 if (si->release)
538 si->release(si);
539
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100540 if (si->err_type)
541 return 0;
542
543 si->err_loc = s->srv;
544 if (si->flags & SI_FL_ERR)
545 si->err_type = SI_ET_CONN_ERR;
546 else
547 si->err_type = SI_ET_CONN_TO;
548 return 0;
549 }
550
551 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200552 if (unlikely((rep->flags & BF_SHUTW) ||
553 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200554 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100555 s->be->options & PR_O_ABRT_CLOSE)))) {
556 /* give up */
557 si->shutw(si);
558 si->err_type |= SI_ET_CONN_ABRT;
559 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200560 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100561 if (s->srv_error)
562 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100563 return 1;
564 }
565
566 /* we need to wait a bit more if there was no activity either */
567 if (!(req->flags & BF_WRITE_ACTIVITY))
568 return 1;
569
570 /* OK, this means that a connection succeeded. The caller will be
571 * responsible for handling the transition from CON to EST.
572 */
573 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100574 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100575 si->state = SI_ST_EST;
576 si->err_type = SI_ET_NONE;
577 si->err_loc = NULL;
578 return 1;
579}
580
581/* This function is called with (si->state == SI_ST_CER) meaning that a
582 * previous connection attempt has failed and that the file descriptor
583 * has already been released. Possible causes include asynchronous error
584 * notification and time out. Possible output states are SI_ST_CLO when
585 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
586 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
587 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
588 * marked as in error state. It returns 0.
589 */
590int sess_update_st_cer(struct session *s, struct stream_interface *si)
591{
592 /* we probably have to release last session from the server */
593 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100594 health_adjust(s->srv, HANA_STATUS_L4_ERR);
595
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100596 if (s->flags & SN_CURR_SESS) {
597 s->flags &= ~SN_CURR_SESS;
598 s->srv->cur_sess--;
599 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100600 }
601
602 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200603 si->conn_retries--;
604 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100605 if (!si->err_type) {
606 si->err_type = SI_ET_CONN_ERR;
607 si->err_loc = s->srv;
608 }
609
610 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200611 s->srv->counters.failed_conns++;
612 s->be->counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100613 sess_change_server(s, NULL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100614 if (may_dequeue_tasks(s->srv, s->be))
615 process_srv_queue(s->srv);
616
617 /* shutw is enough so stop a connecting socket */
618 si->shutw(si);
619 si->ob->flags |= BF_WRITE_ERROR;
620 si->ib->flags |= BF_READ_ERROR;
621
622 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100623 if (s->srv_error)
624 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100625 return 0;
626 }
627
628 /* If the "redispatch" option is set on the backend, we are allowed to
629 * retry on another server for the last retry. In order to achieve this,
630 * we must mark the session unassigned, and eventually clear the DIRECT
631 * bit to ignore any persistence cookie. We won't count a retry nor a
632 * redispatch yet, because this will depend on what server is selected.
633 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200634 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100635 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100636 sess_change_server(s, NULL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100637 if (may_dequeue_tasks(s->srv, s->be))
638 process_srv_queue(s->srv);
639
640 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
641 s->prev_srv = s->srv;
642 si->state = SI_ST_REQ;
643 } else {
644 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200645 s->srv->counters.retries++;
646 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100647 si->state = SI_ST_ASS;
648 }
649
650 if (si->flags & SI_FL_ERR) {
651 /* The error was an asynchronous connection error, and we will
652 * likely have to retry connecting to the same server, most
653 * likely leading to the same result. To avoid this, we wait
654 * one second before retrying.
655 */
656
657 if (!si->err_type)
658 si->err_type = SI_ET_CONN_ERR;
659
660 si->state = SI_ST_TAR;
661 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
662 return 0;
663 }
664 return 0;
665}
666
667/*
668 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200669 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
670 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100671 */
672void sess_establish(struct session *s, struct stream_interface *si)
673{
674 struct buffer *req = si->ob;
675 struct buffer *rep = si->ib;
676
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100677 if (s->srv)
678 health_adjust(s->srv, HANA_STATUS_L4_OK);
679
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100680 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681 /* if the user wants to log as soon as possible, without counting
682 * bytes from the server, then this is the right moment. */
683 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
684 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100685 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100686 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687 }
688 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100689 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
690 /* reset hdr_idx which was already initialized by the request.
691 * right now, the http parser does it.
692 * hdr_idx_init(&s->txn.hdr_idx);
693 */
694 }
695
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200696 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100697 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200698 if (si->connect) {
699 /* real connections have timeouts */
700 req->wto = s->be->timeout.server;
701 rep->rto = s->be->timeout.server;
702 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 req->wex = TICK_ETERNITY;
704}
705
706/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
707 * Other input states are simply ignored.
708 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
709 * Flags must have previously been updated for timeouts and other conditions.
710 */
711void sess_update_stream_int(struct session *s, struct stream_interface *si)
712{
713 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",
714 now_ms, __FUNCTION__,
715 s,
716 s->req, s->rep,
717 s->req->rex, s->rep->wex,
718 s->req->flags, s->rep->flags,
719 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
720
721 if (si->state == SI_ST_ASS) {
722 /* Server assigned to connection request, we have to try to connect now */
723 int conn_err;
724
725 conn_err = connect_server(s);
726 if (conn_err == SN_ERR_NONE) {
727 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100728 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100729 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100730 return;
731 }
732
733 /* We have received a synchronous error. We might have to
734 * abort, retry immediately or redispatch.
735 */
736 if (conn_err == SN_ERR_INTERNAL) {
737 if (!si->err_type) {
738 si->err_type = SI_ET_CONN_OTHER;
739 si->err_loc = s->srv;
740 }
741
742 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100743 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200745 s->srv->counters.failed_conns++;
746 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100747
748 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100749 sess_change_server(s, NULL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100750 if (may_dequeue_tasks(s->srv, s->be))
751 process_srv_queue(s->srv);
752
753 /* Failed and not retryable. */
754 si->shutr(si);
755 si->shutw(si);
756 si->ob->flags |= BF_WRITE_ERROR;
757
758 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
759
760 /* no session was ever accounted for this server */
761 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100762 if (s->srv_error)
763 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100764 return;
765 }
766
767 /* We are facing a retryable error, but we don't want to run a
768 * turn-around now, as the problem is likely a source port
769 * allocation problem, so we want to retry now.
770 */
771 si->state = SI_ST_CER;
772 si->flags &= ~SI_FL_ERR;
773 sess_update_st_cer(s, si);
774 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
775 return;
776 }
777 else if (si->state == SI_ST_QUE) {
778 /* connection request was queued, check for any update */
779 if (!s->pend_pos) {
780 /* The connection is not in the queue anymore. Either
781 * we have a server connection slot available and we
782 * go directly to the assigned state, or we need to
783 * load-balance first and go to the INI state.
784 */
785 si->exp = TICK_ETERNITY;
786 if (unlikely(!(s->flags & SN_ASSIGNED)))
787 si->state = SI_ST_REQ;
788 else {
789 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
790 si->state = SI_ST_ASS;
791 }
792 return;
793 }
794
795 /* Connection request still in queue... */
796 if (si->flags & SI_FL_EXP) {
797 /* ... and timeout expired */
798 si->exp = TICK_ETERNITY;
799 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
800 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200801 s->srv->counters.failed_conns++;
802 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100803 si->shutr(si);
804 si->shutw(si);
805 si->ob->flags |= BF_WRITE_TIMEOUT;
806 if (!si->err_type)
807 si->err_type = SI_ET_QUEUE_TO;
808 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100809 if (s->srv_error)
810 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100811 return;
812 }
813
814 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200815 if ((si->ob->flags & (BF_READ_ERROR)) ||
816 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200817 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100818 /* give up */
819 si->exp = TICK_ETERNITY;
820 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
821 si->shutr(si);
822 si->shutw(si);
823 si->err_type |= SI_ET_QUEUE_ABRT;
824 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100825 if (s->srv_error)
826 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100827 return;
828 }
829
830 /* Nothing changed */
831 return;
832 }
833 else if (si->state == SI_ST_TAR) {
834 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200835 if ((si->ob->flags & (BF_READ_ERROR)) ||
836 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200837 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100838 /* give up */
839 si->exp = TICK_ETERNITY;
840 si->shutr(si);
841 si->shutw(si);
842 si->err_type |= SI_ET_CONN_ABRT;
843 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100844 if (s->srv_error)
845 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100846 return;
847 }
848
849 if (!(si->flags & SI_FL_EXP))
850 return; /* still in turn-around */
851
852 si->exp = TICK_ETERNITY;
853
854 /* we keep trying on the same server as long as the session is
855 * marked "assigned".
856 * FIXME: Should we force a redispatch attempt when the server is down ?
857 */
858 if (s->flags & SN_ASSIGNED)
859 si->state = SI_ST_ASS;
860 else
861 si->state = SI_ST_REQ;
862 return;
863 }
864}
865
866/* This function initiates a server connection request on a stream interface
867 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
868 * indicating that a server has been assigned. It may also return SI_ST_QUE,
869 * or SI_ST_CLO upon error.
870 */
871static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
872 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n",
873 now_ms, __FUNCTION__,
874 s,
875 s->req, s->rep,
876 s->req->rex, s->rep->wex,
877 s->req->flags, s->rep->flags,
878 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
879
880 if (si->state != SI_ST_REQ)
881 return;
882
883 /* Try to assign a server */
884 if (srv_redispatch_connect(s) != 0) {
885 /* We did not get a server. Either we queued the
886 * connection request, or we encountered an error.
887 */
888 if (si->state == SI_ST_QUE)
889 return;
890
891 /* we did not get any server, let's check the cause */
892 si->shutr(si);
893 si->shutw(si);
894 si->ob->flags |= BF_WRITE_ERROR;
895 if (!si->err_type)
896 si->err_type = SI_ET_CONN_OTHER;
897 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100898 if (s->srv_error)
899 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100900 return;
901 }
902
903 /* The server is assigned */
904 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
905 si->state = SI_ST_ASS;
906}
907
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200908/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100909 * if appropriate. The default_backend rule is also considered, then the
910 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200911 * It returns 1 if the processing can continue on next analysers, or zero if it
912 * either needs more data or wants to immediately abort the request.
913 */
914int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
915{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200916 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100917
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200918 req->analysers &= ~an_bit;
919 req->analyse_exp = TICK_ETERNITY;
920
921 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
922 now_ms, __FUNCTION__,
923 s,
924 req,
925 req->rex, req->wex,
926 req->flags,
927 req->l,
928 req->analysers);
929
930 /* now check whether we have some switching rules for this request */
931 if (!(s->flags & SN_BE_ASSIGNED)) {
932 struct switching_rule *rule;
933
934 list_for_each_entry(rule, &s->fe->switching_rules, list) {
935 int ret;
936
937 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
938 ret = acl_pass(ret);
939 if (rule->cond->pol == ACL_COND_UNLESS)
940 ret = !ret;
941
942 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200943 if (!session_set_backend(s, rule->be.backend))
944 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200945 break;
946 }
947 }
948
949 /* To ensure correct connection accounting on the backend, we
950 * have to assign one if it was not set (eg: a listen). This
951 * measure also takes care of correctly setting the default
952 * backend if any.
953 */
954 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200955 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
956 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200957 }
958
Willy Tarreaufb356202010-08-03 14:02:05 +0200959 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
960 if (s->fe == s->be) {
961 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200962 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200963 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200964
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200965 /* as soon as we know the backend, we must check if we have a matching forced or ignored
Willy Tarreau4de91492010-01-22 19:10:05 +0100966 * persistence rule, and report that in the session.
967 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200968 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100969 int ret = 1;
970
971 if (prst_rule->cond) {
972 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
973 ret = acl_pass(ret);
974 if (prst_rule->cond->pol == ACL_COND_UNLESS)
975 ret = !ret;
976 }
977
978 if (ret) {
979 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200980 if (prst_rule->type == PERSIST_TYPE_FORCE) {
981 s->flags |= SN_FORCE_PRST;
982 } else {
983 s->flags |= SN_IGNORE_PRST;
984 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100985 break;
986 }
987 }
988
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200989 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200990
991 sw_failed:
992 /* immediately abort this request in case of allocation failure */
993 buffer_abort(s->req);
994 buffer_abort(s->rep);
995
996 if (!(s->flags & SN_ERR_MASK))
997 s->flags |= SN_ERR_RESOURCE;
998 if (!(s->flags & SN_FINST_MASK))
999 s->flags |= SN_FINST_R;
1000
1001 s->txn.status = 500;
1002 s->req->analysers = 0;
1003 s->req->analyse_exp = TICK_ETERNITY;
1004 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001005}
1006
Emeric Brun1d33b292010-01-04 15:47:17 +01001007/* This stream analyser works on a request. It applies all sticking rules on
1008 * it then returns 1. The data must already be present in the buffer otherwise
1009 * they won't match. It always returns 1.
1010 */
1011int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
1012{
1013 struct proxy *px = s->be;
1014 struct sticking_rule *rule;
1015
1016 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1017 now_ms, __FUNCTION__,
1018 s,
1019 req,
1020 req->rex, req->wex,
1021 req->flags,
1022 req->l,
1023 req->analysers);
1024
1025 list_for_each_entry(rule, &px->sticking_rules, list) {
1026 int ret = 1 ;
1027 int i;
1028
1029 for (i = 0; i < s->store_count; i++) {
1030 if (rule->table.t == s->store[i].table)
1031 break;
1032 }
1033
1034 if (i != s->store_count)
1035 continue;
1036
1037 if (rule->cond) {
1038 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1039 ret = acl_pass(ret);
1040 if (rule->cond->pol == ACL_COND_UNLESS)
1041 ret = !ret;
1042 }
1043
1044 if (ret) {
1045 struct stktable_key *key;
1046
Emeric Brun485479d2010-09-23 18:02:19 +02001047 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001048 if (!key)
1049 continue;
1050
1051 if (rule->flags & STK_IS_MATCH) {
1052 struct stksess *ts;
1053
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001054 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001055 if (!(s->flags & SN_ASSIGNED)) {
1056 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001057 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001058
1059 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001060 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1061 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001062 if (node) {
1063 struct server *srv;
1064
1065 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001066 if ((srv->state & SRV_RUNNING) ||
1067 (px->options & PR_O_PERSIST) ||
1068 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001069 s->flags |= SN_DIRECT | SN_ASSIGNED;
1070 s->srv = srv;
Willy Tarreau664beb82011-03-10 11:38:29 +01001071 s->target.type = TARG_TYPE_SERVER;
1072 s->target.ptr.s = srv;
Emeric Brun1d33b292010-01-04 15:47:17 +01001073 }
1074 }
1075 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001076 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001077 }
1078 }
1079 if (rule->flags & STK_IS_STORE) {
1080 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1081 struct stksess *ts;
1082
1083 ts = stksess_new(rule->table.t, key);
1084 if (ts) {
1085 s->store[s->store_count].table = rule->table.t;
1086 s->store[s->store_count++].ts = ts;
1087 }
1088 }
1089 }
1090 }
1091 }
1092
1093 req->analysers &= ~an_bit;
1094 req->analyse_exp = TICK_ETERNITY;
1095 return 1;
1096}
1097
1098/* This stream analyser works on a response. It applies all store rules on it
1099 * then returns 1. The data must already be present in the buffer otherwise
1100 * they won't match. It always returns 1.
1101 */
1102int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1103{
1104 struct proxy *px = s->be;
1105 struct sticking_rule *rule;
1106 int i;
1107
1108 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1109 now_ms, __FUNCTION__,
1110 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001111 rep,
1112 rep->rex, rep->wex,
1113 rep->flags,
1114 rep->l,
1115 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001116
1117 list_for_each_entry(rule, &px->storersp_rules, list) {
1118 int ret = 1 ;
1119 int storereqidx = -1;
1120
1121 for (i = 0; i < s->store_count; i++) {
1122 if (rule->table.t == s->store[i].table) {
1123 if (!(s->store[i].flags))
1124 storereqidx = i;
1125 break;
1126 }
1127 }
1128
1129 if ((i != s->store_count) && (storereqidx == -1))
1130 continue;
1131
1132 if (rule->cond) {
1133 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1134 ret = acl_pass(ret);
1135 if (rule->cond->pol == ACL_COND_UNLESS)
1136 ret = !ret;
1137 }
1138
1139 if (ret) {
1140 struct stktable_key *key;
1141
Emeric Brun485479d2010-09-23 18:02:19 +02001142 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001143 if (!key)
1144 continue;
1145
1146 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001147 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001148 s->store[storereqidx].flags = 1;
1149 }
1150 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1151 struct stksess *ts;
1152
1153 ts = stksess_new(rule->table.t, key);
1154 if (ts) {
1155 s->store[s->store_count].table = rule->table.t;
1156 s->store[s->store_count].flags = 1;
1157 s->store[s->store_count++].ts = ts;
1158 }
1159 }
1160 }
1161 }
1162
1163 /* process store request and store response */
1164 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001165 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001166 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001167
1168 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1169 if (ts) {
1170 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001171 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001172 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001173 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001174 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001175 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001176
1177 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001178 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1179 stktable_data_cast(ptr, server_id) = s->srv->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001180 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001181 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001182
1183 rep->analysers &= ~an_bit;
1184 rep->analyse_exp = TICK_ETERNITY;
1185 return 1;
1186}
1187
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001188/* This macro is very specific to the function below. See the comments in
1189 * process_session() below to understand the logic and the tests.
1190 */
1191#define UPDATE_ANALYSERS(real, list, back, flag) { \
1192 list = (((list) & ~(flag)) | ~(back)) & (real); \
1193 back = real; \
1194 if (!(list)) \
1195 break; \
1196 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1197 continue; \
1198}
1199
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001200/* Processes the client, server, request and response jobs of a session task,
1201 * then puts it back to the wait queue in a clean state, or cleans up its
1202 * resources if it must be deleted. Returns in <next> the date the task wants
1203 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1204 * nothing too many times, the request and response buffers flags are monitored
1205 * and each function is called only if at least another function has changed at
1206 * least one flag it is interested in.
1207 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001208struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001209{
1210 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001211 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001212 unsigned int rq_prod_last, rq_cons_last;
1213 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001214 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001215
1216 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1217 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1218
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001219 /* this data may be no longer valid, clear it */
1220 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1221
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001222 /* This flag must explicitly be set every time */
1223 s->req->flags &= ~BF_READ_NOEXP;
1224
1225 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001226 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1227 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001228
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001229 /* we don't want the stream interface functions to recursively wake us up */
1230 if (s->req->prod->owner == t)
1231 s->req->prod->flags |= SI_FL_DONT_WAKE;
1232 if (s->req->cons->owner == t)
1233 s->req->cons->flags |= SI_FL_DONT_WAKE;
1234
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001235 /* 1a: Check for low level timeouts if needed. We just set a flag on
1236 * stream interfaces when their timeouts have expired.
1237 */
1238 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1239 stream_int_check_timeouts(&s->si[0]);
1240 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001241
1242 /* check buffer timeouts, and close the corresponding stream interfaces
1243 * for future reads or writes. Note: this will also concern upper layers
1244 * but we do not touch any other flag. We must be careful and correctly
1245 * detect state changes when calling them.
1246 */
1247
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001248 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001249
Willy Tarreau14641402009-12-29 14:49:56 +01001250 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1251 s->req->cons->flags |= SI_FL_NOLINGER;
1252 s->req->cons->shutw(s->req->cons);
1253 }
1254
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001255 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1256 s->req->prod->shutr(s->req->prod);
1257
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001258 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001259
Willy Tarreau14641402009-12-29 14:49:56 +01001260 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1261 s->rep->cons->flags |= SI_FL_NOLINGER;
1262 s->rep->cons->shutw(s->rep->cons);
1263 }
1264
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001265 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1266 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001267 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001268
1269 /* 1b: check for low-level errors reported at the stream interface.
1270 * First we check if it's a retryable error (in which case we don't
1271 * want to tell the buffer). Otherwise we report the error one level
1272 * upper by setting flags into the buffers. Note that the side towards
1273 * the client cannot have connect (hence retryable) errors. Also, the
1274 * connection setup code must be able to deal with any type of abort.
1275 */
1276 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1277 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1278 s->si[0].shutr(&s->si[0]);
1279 s->si[0].shutw(&s->si[0]);
1280 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001281 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001282 s->be->counters.cli_aborts++;
1283 if (s->srv)
1284 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001285 if (!(s->flags & SN_ERR_MASK))
1286 s->flags |= SN_ERR_CLICL;
1287 if (!(s->flags & SN_FINST_MASK))
1288 s->flags |= SN_FINST_D;
1289 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001290 }
1291 }
1292
1293 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1294 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1295 s->si[1].shutr(&s->si[1]);
1296 s->si[1].shutw(&s->si[1]);
1297 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001298 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001299 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001300 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001301 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001302 s->be->counters.srv_aborts++;
1303 if (s->srv)
1304 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001305 if (!(s->flags & SN_ERR_MASK))
1306 s->flags |= SN_ERR_SRVCL;
1307 if (!(s->flags & SN_FINST_MASK))
1308 s->flags |= SN_FINST_D;
1309 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001310 }
1311 /* note: maybe we should process connection errors here ? */
1312 }
1313
1314 if (s->si[1].state == SI_ST_CON) {
1315 /* we were trying to establish a connection on the server side,
1316 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1317 */
1318 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1319 sess_update_st_cer(s, &s->si[1]);
1320 else if (s->si[1].state == SI_ST_EST)
1321 sess_establish(s, &s->si[1]);
1322
1323 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1324 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1325 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1326 */
1327 }
1328
Willy Tarreau815a9b22010-07-27 17:15:12 +02001329 rq_prod_last = s->si[0].state;
1330 rq_cons_last = s->si[1].state;
1331 rp_cons_last = s->si[0].state;
1332 rp_prod_last = s->si[1].state;
1333
1334 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001335 /* Check for connection closure */
1336
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001337 DPRINTF(stderr,
1338 "[%u] %s:%d: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n",
1339 now_ms, __FUNCTION__, __LINE__,
1340 t,
1341 s, s->flags,
1342 s->req, s->rep,
1343 s->req->rex, s->rep->wex,
1344 s->req->flags, s->rep->flags,
1345 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1346 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001347 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001348
1349 /* nothing special to be done on client side */
1350 if (unlikely(s->req->prod->state == SI_ST_DIS))
1351 s->req->prod->state = SI_ST_CLO;
1352
1353 /* When a server-side connection is released, we have to count it and
1354 * check for pending connections on this server.
1355 */
1356 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1357 s->req->cons->state = SI_ST_CLO;
1358 if (s->srv) {
1359 if (s->flags & SN_CURR_SESS) {
1360 s->flags &= ~SN_CURR_SESS;
1361 s->srv->cur_sess--;
1362 }
1363 sess_change_server(s, NULL);
1364 if (may_dequeue_tasks(s->srv, s->be))
1365 process_srv_queue(s->srv);
1366 }
1367 }
1368
1369 /*
1370 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1371 * at this point.
1372 */
1373
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001374 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001375 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001376 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001377 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1378 s->si[0].state != rq_prod_last ||
1379 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001380 unsigned int flags = s->req->flags;
1381
1382 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001383 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001384 unsigned int ana_list;
1385 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001386
Willy Tarreau90deb182010-01-07 00:20:41 +01001387 /* it's up to the analysers to stop new connections,
1388 * disable reading or closing. Note: if an analyser
1389 * disables any of these bits, it is responsible for
1390 * enabling them again when it disables itself, so
1391 * that other analysers are called in similar conditions.
1392 */
1393 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001394 buffer_auto_connect(s->req);
1395 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001396
1397 /* We will call all analysers for which a bit is set in
1398 * s->req->analysers, following the bit order from LSB
1399 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001400 * the list when not needed. Any analyser may return 0
1401 * to break out of the loop, either because of missing
1402 * data to take a decision, or because it decides to
1403 * kill the session. We loop at least once through each
1404 * analyser, and we may loop again if other analysers
1405 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001406 *
1407 * We build a list of analysers to run. We evaluate all
1408 * of these analysers in the order of the lower bit to
1409 * the higher bit. This ordering is very important.
1410 * An analyser will often add/remove other analysers,
1411 * including itself. Any changes to itself have no effect
1412 * on the loop. If it removes any other analysers, we
1413 * want those analysers not to be called anymore during
1414 * this loop. If it adds an analyser that is located
1415 * after itself, we want it to be scheduled for being
1416 * processed during the loop. If it adds an analyser
1417 * which is located before it, we want it to switch to
1418 * it immediately, even if it has already been called
1419 * once but removed since.
1420 *
1421 * In order to achieve this, we compare the analyser
1422 * list after the call with a copy of it before the
1423 * call. The work list is fed with analyser bits that
1424 * appeared during the call. Then we compare previous
1425 * work list with the new one, and check the bits that
1426 * appeared. If the lowest of these bits is lower than
1427 * the current bit, it means we have enabled a previous
1428 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001429 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001430
1431 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001432 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001433 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001434
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001435 if (ana_list & AN_REQ_DECODE_PROXY) {
1436 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1437 break;
1438 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1439 }
1440
Willy Tarreaufb356202010-08-03 14:02:05 +02001441 if (ana_list & AN_REQ_INSPECT_FE) {
1442 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001443 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001444 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001445 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001446
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001447 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001448 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001449 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001450 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001451 }
1452
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001453 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001454 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1455 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001456 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001457 }
1458
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001459 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001460 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1461 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001462 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001463 }
1464
Willy Tarreaufb356202010-08-03 14:02:05 +02001465 if (ana_list & AN_REQ_INSPECT_BE) {
1466 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1467 break;
1468 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1469 }
1470
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001471 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001472 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1473 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001474 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001475 }
1476
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001477 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001478 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001479 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001480 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001481 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001482
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001483 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001484 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1485 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001486 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001487 }
1488
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001489 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001490 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001491 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001492 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001493 }
Emeric Brun647caf12009-06-30 17:57:00 +02001494
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001495 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001496 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1497 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001498 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001499 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001500
Emeric Brun1d33b292010-01-04 15:47:17 +01001501 if (ana_list & AN_REQ_STICKING_RULES) {
1502 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1503 break;
1504 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1505 }
1506
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001507 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001508 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1509 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001510 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001511 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001512 break;
1513 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001514 }
Willy Tarreau84455332009-03-15 22:34:05 +01001515
Willy Tarreau815a9b22010-07-27 17:15:12 +02001516 rq_prod_last = s->si[0].state;
1517 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001518 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001519 rqf_last = s->req->flags;
1520
1521 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001522 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001523 }
1524
Willy Tarreau576507f2010-01-07 00:09:04 +01001525 /* we'll monitor the request analysers while parsing the response,
1526 * because some response analysers may indirectly enable new request
1527 * analysers (eg: HTTP keep-alive).
1528 */
1529 req_ana_back = s->req->analysers;
1530
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001531 resync_response:
1532 /* Analyse response */
1533
1534 if (unlikely(s->rep->flags & BF_HIJACK)) {
1535 /* In inject mode, we wake up everytime something has
1536 * happened on the write side of the buffer.
1537 */
1538 unsigned int flags = s->rep->flags;
1539
1540 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1541 !(s->rep->flags & BF_FULL)) {
1542 s->rep->hijacker(s, s->rep);
1543 }
1544
1545 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1546 rpf_last = s->rep->flags;
1547 goto resync_response;
1548 }
1549 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001550 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001551 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1552 s->si[0].state != rp_cons_last ||
1553 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001554 unsigned int flags = s->rep->flags;
1555
Willy Tarreau0499e352010-12-17 07:13:42 +01001556 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1557 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1558 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1559 * for some free space in the response buffer, so we might need to call
1560 * it when something changes in the response buffer, but still we pass
1561 * it the request buffer. Note that the SI state might very well still
1562 * be zero due to us returning a flow of redirects!
1563 */
1564 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1565 s->req->flags |= BF_WAKE_ONCE;
1566 }
1567
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001568 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001569 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001570 unsigned int ana_list;
1571 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001572
Willy Tarreau90deb182010-01-07 00:20:41 +01001573 /* it's up to the analysers to stop disable reading or
1574 * closing. Note: if an analyser disables any of these
1575 * bits, it is responsible for enabling them again when
1576 * it disables itself, so that other analysers are called
1577 * in similar conditions.
1578 */
1579 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001580 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001581
1582 /* We will call all analysers for which a bit is set in
1583 * s->rep->analysers, following the bit order from LSB
1584 * to MSB. The analysers must remove themselves from
1585 * the list when not needed. Any analyser may return 0
1586 * to break out of the loop, either because of missing
1587 * data to take a decision, or because it decides to
1588 * kill the session. We loop at least once through each
1589 * analyser, and we may loop again if other analysers
1590 * are added in the middle.
1591 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001592
1593 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001594 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001595 /* Warning! ensure that analysers are always placed in ascending order! */
1596
Emeric Brun97679e72010-09-23 17:56:44 +02001597 if (ana_list & AN_RES_INSPECT) {
1598 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1599 break;
1600 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1601 }
1602
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001603 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001604 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1605 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001606 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001607 }
1608
Emeric Brun1d33b292010-01-04 15:47:17 +01001609 if (ana_list & AN_RES_STORE_RULES) {
1610 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1611 break;
1612 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1613 }
1614
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001615 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001616 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1617 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001618 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001619 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001620
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001621 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001622 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1623 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001624 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001625 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001626 break;
1627 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001628 }
1629
Willy Tarreau815a9b22010-07-27 17:15:12 +02001630 rp_cons_last = s->si[0].state;
1631 rp_prod_last = s->si[1].state;
1632 rpf_last = s->rep->flags;
1633
1634 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001635 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001636 }
1637
Willy Tarreau576507f2010-01-07 00:09:04 +01001638 /* maybe someone has added some request analysers, so we must check and loop */
1639 if (s->req->analysers & ~req_ana_back)
1640 goto resync_request;
1641
Willy Tarreau0499e352010-12-17 07:13:42 +01001642 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1643 goto resync_request;
1644
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001645 /* FIXME: here we should call protocol handlers which rely on
1646 * both buffers.
1647 */
1648
1649
1650 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001651 * Now we propagate unhandled errors to the session. Normally
1652 * we're just in a data phase here since it means we have not
1653 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001654 */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001655 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001656 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1657 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001658 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001659 if (s->req->flags & BF_READ_ERROR) {
1660 s->be->counters.cli_aborts++;
1661 if (s->srv)
1662 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001663 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001664 }
1665 else if (s->req->flags & BF_READ_TIMEOUT) {
1666 s->be->counters.cli_aborts++;
1667 if (s->srv)
1668 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001669 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001670 }
1671 else if (s->req->flags & BF_WRITE_ERROR) {
1672 s->be->counters.srv_aborts++;
1673 if (s->srv)
1674 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001675 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001676 }
1677 else {
1678 s->be->counters.srv_aborts++;
1679 if (s->srv)
1680 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001681 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001682 }
Willy Tarreau84455332009-03-15 22:34:05 +01001683 sess_set_term_flags(s);
1684 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001685 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1686 /* Report it if the server got an error or a read timeout expired */
1687 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001688 if (s->rep->flags & BF_READ_ERROR) {
1689 s->be->counters.srv_aborts++;
1690 if (s->srv)
1691 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001692 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001693 }
1694 else if (s->rep->flags & BF_READ_TIMEOUT) {
1695 s->be->counters.srv_aborts++;
1696 if (s->srv)
1697 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001698 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001699 }
1700 else if (s->rep->flags & BF_WRITE_ERROR) {
1701 s->be->counters.cli_aborts++;
1702 if (s->srv)
1703 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001704 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001705 }
1706 else {
1707 s->be->counters.cli_aborts++;
1708 if (s->srv)
1709 s->srv->counters.cli_aborts++;
1710 s->flags |= SN_ERR_CLITO;
1711 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001712 sess_set_term_flags(s);
1713 }
Willy Tarreau84455332009-03-15 22:34:05 +01001714 }
1715
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001716 /*
1717 * Here we take care of forwarding unhandled data. This also includes
1718 * connection establishments and shutdown requests.
1719 */
1720
1721
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001722 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001723 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001724 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1725 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001726 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001727 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001728 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001729 (s->req->prod->state >= SI_ST_EST) &&
1730 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001731 /* This buffer is freewheeling, there's no analyser nor hijacker
1732 * attached to it. If any data are left in, we'll permit them to
1733 * move.
1734 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001735 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001736 buffer_auto_connect(s->req);
1737 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001738 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001739
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001740 /* We'll let data flow between the producer (if still connected)
1741 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001742 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001743 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001744 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001745 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001746
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001747 /* check if it is wise to enable kernel splicing to forward request data */
1748 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1749 s->req->to_forward &&
1750 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001751 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001752 (pipes_used < global.maxpipes) &&
1753 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1754 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1755 (s->req->flags & BF_STREAMER_FAST)))) {
1756 s->req->flags |= BF_KERN_SPLICING;
1757 }
1758
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001759 /* reflect what the L7 analysers have seen last */
1760 rqf_last = s->req->flags;
1761
1762 /*
1763 * Now forward all shutdown requests between both sides of the buffer
1764 */
1765
Willy Tarreau520d95e2009-09-19 21:04:57 +02001766 /* first, let's check if the request buffer needs to shutdown(write), which may
1767 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001768 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001769 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001770 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001771 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001772 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001773
1774 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001775 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 +01001776 s->req->cons->shutw(s->req->cons);
1777
1778 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001779 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1780 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001781 buffer_shutr_now(s->req);
1782
1783 /* shutdown(read) pending */
1784 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1785 s->req->prod->shutr(s->req->prod);
1786
Willy Tarreau520d95e2009-09-19 21:04:57 +02001787 /* it's possible that an upper layer has requested a connection setup or abort.
1788 * There are 2 situations where we decide to establish a new connection :
1789 * - there are data scheduled for emission in the buffer
1790 * - the BF_AUTO_CONNECT flag is set (active connection)
1791 */
1792 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001793 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001794 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001795 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001796 * switch to the connected state, otherwise we perform a connection
1797 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001798 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001799 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001800 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001801 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001802 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001803 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1804 s->req->wex = TICK_ETERNITY;
1805 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001806 }
Willy Tarreau73201222009-08-16 18:27:24 +02001807 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001808 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001809 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001810 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1811 buffer_shutr_now(s->rep);
1812 }
Willy Tarreau92795622009-03-06 12:51:23 +01001813 }
1814
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001815
1816 /* we may have a pending connection request, or a connection waiting
1817 * for completion.
1818 */
1819 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1820 do {
1821 /* nb: step 1 might switch from QUE to ASS, but we first want
1822 * to give a chance to step 2 to perform a redirect if needed.
1823 */
1824 if (s->si[1].state != SI_ST_REQ)
1825 sess_update_stream_int(s, &s->si[1]);
1826 if (s->si[1].state == SI_ST_REQ)
1827 sess_prepare_conn_req(s, &s->si[1]);
1828
1829 if (s->si[1].state == SI_ST_ASS && s->srv &&
1830 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1831 perform_http_redirect(s, &s->si[1]);
1832 } while (s->si[1].state == SI_ST_ASS);
1833 }
1834
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001835 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001836 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001837 goto resync_stream_interface;
1838
Willy Tarreau815a9b22010-07-27 17:15:12 +02001839 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001840 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001841 goto resync_request;
1842
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001843 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001844
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001845 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001846 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001847 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1848 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001849 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001850 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001851 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001852 (s->rep->prod->state >= SI_ST_EST) &&
1853 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001854 /* This buffer is freewheeling, there's no analyser nor hijacker
1855 * attached to it. If any data are left in, we'll permit them to
1856 * move.
1857 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001858 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001859 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001860 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001861
1862 /* We'll let data flow between the producer (if still connected)
1863 * to the consumer.
1864 */
1865 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001866 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001867 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001868
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001869 /* check if it is wise to enable kernel splicing to forward response data */
1870 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1871 s->rep->to_forward &&
1872 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001873 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001874 (pipes_used < global.maxpipes) &&
1875 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1876 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1877 (s->rep->flags & BF_STREAMER_FAST)))) {
1878 s->rep->flags |= BF_KERN_SPLICING;
1879 }
1880
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001881 /* reflect what the L7 analysers have seen last */
1882 rpf_last = s->rep->flags;
1883
1884 /*
1885 * Now forward all shutdown requests between both sides of the buffer
1886 */
1887
1888 /*
1889 * FIXME: this is probably where we should produce error responses.
1890 */
1891
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001892 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001893 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1894 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001895 buffer_shutw_now(s->rep);
1896
1897 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001898 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 +01001899 s->rep->cons->shutw(s->rep->cons);
1900
1901 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001902 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1903 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001904 buffer_shutr_now(s->rep);
1905
1906 /* shutdown(read) pending */
1907 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1908 s->rep->prod->shutr(s->rep->prod);
1909
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001910 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001911 goto resync_stream_interface;
1912
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001913 if (s->req->flags != rqf_last)
1914 goto resync_request;
1915
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001916 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001917 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001918
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001919 /* we're interested in getting wakeups again */
1920 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1921 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1922
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001923 /* This is needed only when debugging is enabled, to indicate
1924 * client-side or server-side close. Please note that in the unlikely
1925 * event where both sides would close at once, the sequence is reported
1926 * on the server side first.
1927 */
1928 if (unlikely((global.mode & MODE_DEBUG) &&
1929 (!(global.mode & MODE_QUIET) ||
1930 (global.mode & MODE_VERBOSE)))) {
1931 int len;
1932
1933 if (s->si[1].state == SI_ST_CLO &&
1934 s->si[1].prev_state == SI_ST_EST) {
1935 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1936 s->uniq_id, s->be->id,
1937 (unsigned short)s->si[0].fd,
1938 (unsigned short)s->si[1].fd);
1939 write(1, trash, len);
1940 }
1941
1942 if (s->si[0].state == SI_ST_CLO &&
1943 s->si[0].prev_state == SI_ST_EST) {
1944 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1945 s->uniq_id, s->be->id,
1946 (unsigned short)s->si[0].fd,
1947 (unsigned short)s->si[1].fd);
1948 write(1, trash, len);
1949 }
1950 }
1951
1952 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1953 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1954
1955 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1956 session_process_counters(s);
1957
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001958 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001959 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001960
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001961 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001962 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001963
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001964 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1965 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 +01001966 s->si[0].prev_state = s->si[0].state;
1967 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001968 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1969 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001970
1971 /* Trick: if a request is being waiting for the server to respond,
1972 * and if we know the server can timeout, we don't want the timeout
1973 * to expire on the client side first, but we're still interested
1974 * in passing data from the client to the server (eg: POST). Thus,
1975 * we can cancel the client's request timeout if the server's
1976 * request timeout is set and the server has not yet sent a response.
1977 */
1978
Willy Tarreau520d95e2009-09-19 21:04:57 +02001979 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001980 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1981 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001982 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001983 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001984
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001985 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001986 * Note that this one may wake the task up again.
1987 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01001988 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
1989 s->rep->cons->target.type == TARG_TYPE_APPLET) {
1990 if (s->req->cons->target.type == TARG_TYPE_APPLET)
1991 s->req->cons->target.ptr.a->fct(s->req->cons);
1992 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
1993 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02001994 if (task_in_rq(t)) {
1995 /* If we woke up, we don't want to requeue the
1996 * task to the wait queue, but rather requeue
1997 * it into the runqueue ASAP.
1998 */
1999 t->expire = TICK_ETERNITY;
2000 return t;
2001 }
2002 }
2003
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002004 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2005 tick_first(s->rep->rex, s->rep->wex));
2006 if (s->req->analysers)
2007 t->expire = tick_first(t->expire, s->req->analyse_exp);
2008
2009 if (s->si[0].exp)
2010 t->expire = tick_first(t->expire, s->si[0].exp);
2011
2012 if (s->si[1].exp)
2013 t->expire = tick_first(t->expire, s->si[1].exp);
2014
2015#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002016 fprintf(stderr,
2017 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2018 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2019 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2020 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 +01002021#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002022
2023#ifdef DEBUG_DEV
2024 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002025 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002026 ABORT_NOW();
2027#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002028 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002029 }
2030
2031 s->fe->feconn--;
2032 if (s->flags & SN_BE_ASSIGNED)
2033 s->be->beconn--;
2034 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002035 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002036 s->listener->nbconn--;
2037 if (s->listener->state == LI_FULL &&
2038 s->listener->nbconn < s->listener->maxconn) {
2039 /* we should reactivate the listener */
2040 EV_FD_SET(s->listener->fd, DIR_RD);
2041 s->listener->state = LI_READY;
2042 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002043
2044 if (unlikely((global.mode & MODE_DEBUG) &&
2045 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2046 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002047 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002048 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002049 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002050 write(1, trash, len);
2051 }
2052
2053 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2054 session_process_counters(s);
2055
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002056 if (s->txn.status) {
2057 int n;
2058
2059 n = s->txn.status / 100;
2060 if (n < 1 || n > 5)
2061 n = 0;
2062
2063 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01002064 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002065
Willy Tarreau24657792010-02-26 10:30:28 +01002066 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002067 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01002068 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002069 }
2070
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002071 /* let's do a final log if we need it */
2072 if (s->logs.logwait &&
2073 !(s->flags & SN_MONITOR) &&
2074 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002075 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002076 }
2077
2078 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002079 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002080 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002081 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002082 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002083}
2084
Willy Tarreau7c669d72008-06-20 15:04:11 +02002085/*
2086 * This function adjusts sess->srv_conn and maintains the previous and new
2087 * server's served session counts. Setting newsrv to NULL is enough to release
2088 * current connection slot. This function also notifies any LB algo which might
2089 * expect to be informed about any change in the number of active sessions on a
2090 * server.
2091 */
2092void sess_change_server(struct session *sess, struct server *newsrv)
2093{
2094 if (sess->srv_conn == newsrv)
2095 return;
2096
2097 if (sess->srv_conn) {
2098 sess->srv_conn->served--;
2099 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2100 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
2101 sess->srv_conn = NULL;
2102 }
2103
2104 if (newsrv) {
2105 newsrv->served++;
2106 if (newsrv->proxy->lbprm.server_take_conn)
2107 newsrv->proxy->lbprm.server_take_conn(newsrv);
2108 sess->srv_conn = newsrv;
2109 }
2110}
2111
Willy Tarreau84455332009-03-15 22:34:05 +01002112/* Set correct session termination flags in case no analyser has done it. It
2113 * also counts a failed request if the server state has not reached the request
2114 * stage.
2115 */
2116void sess_set_term_flags(struct session *s)
2117{
2118 if (!(s->flags & SN_FINST_MASK)) {
2119 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002120
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002121 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002122 if (s->listener->counters)
2123 s->listener->counters->failed_req++;
2124
Willy Tarreau84455332009-03-15 22:34:05 +01002125 s->flags |= SN_FINST_R;
2126 }
2127 else if (s->si[1].state == SI_ST_QUE)
2128 s->flags |= SN_FINST_Q;
2129 else if (s->si[1].state < SI_ST_EST)
2130 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002131 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002132 s->flags |= SN_FINST_D;
2133 else
2134 s->flags |= SN_FINST_L;
2135 }
2136}
2137
2138/* Handle server-side errors for default protocols. It is called whenever a a
2139 * connection setup is aborted or a request is aborted in queue. It sets the
2140 * session termination flags so that the caller does not have to worry about
2141 * them. It's installed as ->srv_error for the server-side stream_interface.
2142 */
2143void default_srv_error(struct session *s, struct stream_interface *si)
2144{
2145 int err_type = si->err_type;
2146 int err = 0, fin = 0;
2147
2148 if (err_type & SI_ET_QUEUE_ABRT) {
2149 err = SN_ERR_CLICL;
2150 fin = SN_FINST_Q;
2151 }
2152 else if (err_type & SI_ET_CONN_ABRT) {
2153 err = SN_ERR_CLICL;
2154 fin = SN_FINST_C;
2155 }
2156 else if (err_type & SI_ET_QUEUE_TO) {
2157 err = SN_ERR_SRVTO;
2158 fin = SN_FINST_Q;
2159 }
2160 else if (err_type & SI_ET_QUEUE_ERR) {
2161 err = SN_ERR_SRVCL;
2162 fin = SN_FINST_Q;
2163 }
2164 else if (err_type & SI_ET_CONN_TO) {
2165 err = SN_ERR_SRVTO;
2166 fin = SN_FINST_C;
2167 }
2168 else if (err_type & SI_ET_CONN_ERR) {
2169 err = SN_ERR_SRVCL;
2170 fin = SN_FINST_C;
2171 }
2172 else /* SI_ET_CONN_OTHER and others */ {
2173 err = SN_ERR_INTERNAL;
2174 fin = SN_FINST_C;
2175 }
2176
2177 if (!(s->flags & SN_ERR_MASK))
2178 s->flags |= err;
2179 if (!(s->flags & SN_FINST_MASK))
2180 s->flags |= fin;
2181}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002182
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002183
Willy Tarreau8b22a712010-06-18 17:46:06 +02002184/************************************************************************/
2185/* All supported ACL keywords must be declared here. */
2186/************************************************************************/
2187
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002188/* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */
2189static int
2190acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2191{
2192 test->flags = ACL_TEST_F_VOL_TEST;
2193 test->i = 0;
2194 if (ts != NULL) {
2195 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2196 if (!ptr)
2197 return 0; /* parameter not stored */
2198 test->i = stktable_data_cast(ptr, gpc0);
2199 }
2200 return 1;
2201}
2202
2203/* set test->i to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002204 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002205 */
2206static int
Willy Tarreau56123282010-08-06 19:06:56 +02002207acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2208 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002209{
Willy Tarreau56123282010-08-06 19:06:56 +02002210 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002211 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002212 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002213}
2214
2215/* set test->i to the General Purpose Counter 0 value from the session's tracked
2216 * backend counters.
2217 */
2218static int
Willy Tarreau56123282010-08-06 19:06:56 +02002219acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2220 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002221{
Willy Tarreau56123282010-08-06 19:06:56 +02002222 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002223 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002224 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002225}
2226
2227/* set test->i to the General Purpose Counter 0 value from the session's source
2228 * address in the table pointed to by expr.
2229 */
2230static int
2231acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2232 struct acl_expr *expr, struct acl_test *test)
2233{
2234 struct stktable_key *key;
2235
2236 key = tcpv4_src_to_stktable_key(l4);
2237 if (!key)
2238 return 0; /* only TCPv4 is supported right now */
2239
2240 if (expr->arg_len)
2241 px = find_stktable(expr->arg.str);
2242
2243 if (!px)
2244 return 0; /* table not found */
2245
2246 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2247}
2248
2249/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
2250 * return it into test->i.
2251 */
2252static int
2253acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2254{
2255 test->flags = ACL_TEST_F_VOL_TEST;
2256 test->i = 0;
2257 if (ts != NULL) {
2258 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2259 if (!ptr)
2260 return 0; /* parameter not stored */
2261 test->i = ++stktable_data_cast(ptr, gpc0);
2262 }
2263 return 1;
2264}
2265
2266/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002267 * frontend counters and return it into test->i.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002268 */
2269static int
Willy Tarreau56123282010-08-06 19:06:56 +02002270acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2271 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002272{
Willy Tarreau56123282010-08-06 19:06:56 +02002273 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002274 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002275 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002276}
2277
2278/* Increment the General Purpose Counter 0 value from the session's tracked
2279 * backend counters and return it into test->i.
2280 */
2281static int
Willy Tarreau56123282010-08-06 19:06:56 +02002282acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2283 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002284{
Willy Tarreau56123282010-08-06 19:06:56 +02002285 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002286 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002287 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002288}
2289
2290/* Increment the General Purpose Counter 0 value from the session's source
2291 * address in the table pointed to by expr, and return it into test->i.
2292 */
2293static int
2294acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2295 struct acl_expr *expr, struct acl_test *test)
2296{
2297 struct stktable_key *key;
2298
2299 key = tcpv4_src_to_stktable_key(l4);
2300 if (!key)
2301 return 0; /* only TCPv4 is supported right now */
2302
2303 if (expr->arg_len)
2304 px = find_stktable(expr->arg.str);
2305
2306 if (!px)
2307 return 0; /* table not found */
2308
2309 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2310}
2311
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002312/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2313static int
2314acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2315{
2316 test->flags = ACL_TEST_F_VOL_TEST;
2317 test->i = 0;
2318 if (ts != NULL) {
2319 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2320 if (!ptr)
2321 return 0; /* parameter not stored */
2322 test->i = stktable_data_cast(ptr, conn_cnt);
2323 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002324 return 1;
2325}
2326
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002327/* set test->i to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002328static int
Willy Tarreau56123282010-08-06 19:06:56 +02002329acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2330 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002331{
Willy Tarreau56123282010-08-06 19:06:56 +02002332 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002333 return 0;
2334
Willy Tarreau56123282010-08-06 19:06:56 +02002335 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002336}
2337
2338/* set test->i to the cumulated number of connections from the session's tracked BE counters */
2339static int
Willy Tarreau56123282010-08-06 19:06:56 +02002340acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2341 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002342{
Willy Tarreau56123282010-08-06 19:06:56 +02002343 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002344 return 0;
2345
Willy Tarreau56123282010-08-06 19:06:56 +02002346 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002347}
2348
2349/* set test->i to the cumulated number of connections from the session's source
2350 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002351 */
2352static int
2353acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2354 struct acl_expr *expr, struct acl_test *test)
2355{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002356 struct stktable_key *key;
2357
2358 key = tcpv4_src_to_stktable_key(l4);
2359 if (!key)
2360 return 0; /* only TCPv4 is supported right now */
2361
2362 if (expr->arg_len)
2363 px = find_stktable(expr->arg.str);
2364
2365 if (!px)
2366 return 0; /* table not found */
2367
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002368 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002369}
2370
Willy Tarreau91c43d72010-06-20 11:19:22 +02002371/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2372static int
2373acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2374{
2375 test->flags = ACL_TEST_F_VOL_TEST;
2376 test->i = 0;
2377 if (ts != NULL) {
2378 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2379 if (!ptr)
2380 return 0; /* parameter not stored */
2381 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2382 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2383 }
2384 return 1;
2385}
2386
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002387/* set test->i to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002388 * the configured period.
2389 */
2390static int
Willy Tarreau56123282010-08-06 19:06:56 +02002391acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2392 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002393{
Willy Tarreau56123282010-08-06 19:06:56 +02002394 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002395 return 0;
2396
Willy Tarreau56123282010-08-06 19:06:56 +02002397 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002398}
2399
2400/* set test->i to the connection rate from the session's tracked BE counters over
2401 * the configured period.
2402 */
2403static int
Willy Tarreau56123282010-08-06 19:06:56 +02002404acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2405 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002406{
Willy Tarreau56123282010-08-06 19:06:56 +02002407 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002408 return 0;
2409
Willy Tarreau56123282010-08-06 19:06:56 +02002410 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002411}
2412
2413/* set test->i to the connection rate from the session's source address in the
2414 * table pointed to by expr, over the configured period.
2415 */
2416static int
2417acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2418 struct acl_expr *expr, struct acl_test *test)
2419{
2420 struct stktable_key *key;
2421
2422 key = tcpv4_src_to_stktable_key(l4);
2423 if (!key)
2424 return 0; /* only TCPv4 is supported right now */
2425
2426 if (expr->arg_len)
2427 px = find_stktable(expr->arg.str);
2428
2429 if (!px)
2430 return 0; /* table not found */
2431
2432 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2433}
2434
Willy Tarreau8b22a712010-06-18 17:46:06 +02002435/* set test->i to the number of connections from the session's source address
2436 * in the table pointed to by expr, after updating it.
2437 */
2438static int
2439acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2440 struct acl_expr *expr, struct acl_test *test)
2441{
2442 struct stksess *ts;
2443 struct stktable_key *key;
2444 void *ptr;
2445
2446 key = tcpv4_src_to_stktable_key(l4);
2447 if (!key)
2448 return 0; /* only TCPv4 is supported right now */
2449
2450 if (expr->arg_len)
2451 px = find_stktable(expr->arg.str);
2452
2453 if (!px)
2454 return 0; /* table not found */
2455
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002456 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2457 /* entry does not exist and could not be created */
2458 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002459
2460 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2461 if (!ptr)
2462 return 0; /* parameter not stored in this table */
2463
2464 test->i = ++stktable_data_cast(ptr, conn_cnt);
2465 test->flags = ACL_TEST_F_VOL_TEST;
2466 return 1;
2467}
2468
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002469/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2470static int
2471acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2472{
2473 test->flags = ACL_TEST_F_VOL_TEST;
2474 test->i = 0;
2475
2476 if (ts != NULL) {
2477 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2478 if (!ptr)
2479 return 0; /* parameter not stored */
2480 test->i = stktable_data_cast(ptr, conn_cur);
2481 }
2482 return 1;
2483}
2484
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002485/* set test->i to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002486static int
Willy Tarreau56123282010-08-06 19:06:56 +02002487acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2488 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002489{
Willy Tarreau56123282010-08-06 19:06:56 +02002490 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002491 return 0;
2492
Willy Tarreau56123282010-08-06 19:06:56 +02002493 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002494}
2495
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002496/* set test->i to the number of concurrent connections from the session's tracked BE counters */
2497static int
Willy Tarreau56123282010-08-06 19:06:56 +02002498acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2499 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002500{
Willy Tarreau56123282010-08-06 19:06:56 +02002501 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002502 return 0;
2503
Willy Tarreau56123282010-08-06 19:06:56 +02002504 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002505}
2506
Willy Tarreau38285c12010-06-18 16:35:43 +02002507/* set test->i to the number of concurrent connections from the session's source
2508 * address in the table pointed to by expr.
2509 */
2510static int
2511acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2512 struct acl_expr *expr, struct acl_test *test)
2513{
Willy Tarreau38285c12010-06-18 16:35:43 +02002514 struct stktable_key *key;
2515
2516 key = tcpv4_src_to_stktable_key(l4);
2517 if (!key)
2518 return 0; /* only TCPv4 is supported right now */
2519
2520 if (expr->arg_len)
2521 px = find_stktable(expr->arg.str);
2522
2523 if (!px)
2524 return 0; /* table not found */
2525
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002526 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002527}
2528
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002529/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2530static int
2531acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2532{
2533 test->flags = ACL_TEST_F_VOL_TEST;
2534 test->i = 0;
2535 if (ts != NULL) {
2536 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2537 if (!ptr)
2538 return 0; /* parameter not stored */
2539 test->i = stktable_data_cast(ptr, sess_cnt);
2540 }
2541 return 1;
2542}
2543
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002544/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002545static int
Willy Tarreau56123282010-08-06 19:06:56 +02002546acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2547 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002548{
Willy Tarreau56123282010-08-06 19:06:56 +02002549 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002550 return 0;
2551
Willy Tarreau56123282010-08-06 19:06:56 +02002552 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002553}
2554
2555/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2556static int
Willy Tarreau56123282010-08-06 19:06:56 +02002557acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2558 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002559{
Willy Tarreau56123282010-08-06 19:06:56 +02002560 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002561 return 0;
2562
Willy Tarreau56123282010-08-06 19:06:56 +02002563 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002564}
2565
2566/* set test->i to the cumulated number of session from the session's source
2567 * address in the table pointed to by expr.
2568 */
2569static int
2570acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2571 struct acl_expr *expr, struct acl_test *test)
2572{
2573 struct stktable_key *key;
2574
2575 key = tcpv4_src_to_stktable_key(l4);
2576 if (!key)
2577 return 0; /* only TCPv4 is supported right now */
2578
2579 if (expr->arg_len)
2580 px = find_stktable(expr->arg.str);
2581
2582 if (!px)
2583 return 0; /* table not found */
2584
2585 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2586}
2587
Willy Tarreau91c43d72010-06-20 11:19:22 +02002588/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2589static int
2590acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2591{
2592 test->flags = ACL_TEST_F_VOL_TEST;
2593 test->i = 0;
2594 if (ts != NULL) {
2595 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2596 if (!ptr)
2597 return 0; /* parameter not stored */
2598 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2599 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2600 }
2601 return 1;
2602}
2603
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002604/* set test->i to the session rate from the session's tracked FE counters over
2605 * the configured period.
2606 */
2607static int
Willy Tarreau56123282010-08-06 19:06:56 +02002608acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2609 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002610{
Willy Tarreau56123282010-08-06 19:06:56 +02002611 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002612 return 0;
2613
Willy Tarreau56123282010-08-06 19:06:56 +02002614 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002615}
2616
2617/* set test->i to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002618 * the configured period.
2619 */
2620static int
Willy Tarreau56123282010-08-06 19:06:56 +02002621acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2622 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002623{
Willy Tarreau56123282010-08-06 19:06:56 +02002624 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002625 return 0;
2626
Willy Tarreau56123282010-08-06 19:06:56 +02002627 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002628}
2629
2630/* set test->i to the session rate from the session's source address in the
2631 * table pointed to by expr, over the configured period.
2632 */
2633static int
2634acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2635 struct acl_expr *expr, struct acl_test *test)
2636{
2637 struct stktable_key *key;
2638
2639 key = tcpv4_src_to_stktable_key(l4);
2640 if (!key)
2641 return 0; /* only TCPv4 is supported right now */
2642
2643 if (expr->arg_len)
2644 px = find_stktable(expr->arg.str);
2645
2646 if (!px)
2647 return 0; /* table not found */
2648
2649 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2650}
2651
Willy Tarreauda7ff642010-06-23 11:44:09 +02002652/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2653static int
2654acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2655{
2656 test->flags = ACL_TEST_F_VOL_TEST;
2657 test->i = 0;
2658 if (ts != NULL) {
2659 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2660 if (!ptr)
2661 return 0; /* parameter not stored */
2662 test->i = stktable_data_cast(ptr, http_req_cnt);
2663 }
2664 return 1;
2665}
2666
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002667/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002668static int
Willy Tarreau56123282010-08-06 19:06:56 +02002669acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2670 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002671{
Willy Tarreau56123282010-08-06 19:06:56 +02002672 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002673 return 0;
2674
Willy Tarreau56123282010-08-06 19:06:56 +02002675 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002676}
2677
2678/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2679static int
Willy Tarreau56123282010-08-06 19:06:56 +02002680acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2681 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002682{
Willy Tarreau56123282010-08-06 19:06:56 +02002683 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002684 return 0;
2685
Willy Tarreau56123282010-08-06 19:06:56 +02002686 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002687}
2688
2689/* set test->i to the cumulated number of session from the session's source
2690 * address in the table pointed to by expr.
2691 */
2692static int
2693acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002694 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002695{
2696 struct stktable_key *key;
2697
2698 key = tcpv4_src_to_stktable_key(l4);
2699 if (!key)
2700 return 0; /* only TCPv4 is supported right now */
2701
2702 if (expr->arg_len)
2703 px = find_stktable(expr->arg.str);
2704
2705 if (!px)
2706 return 0; /* table not found */
2707
2708 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2709}
2710
2711/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2712static int
2713acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2714{
2715 test->flags = ACL_TEST_F_VOL_TEST;
2716 test->i = 0;
2717 if (ts != NULL) {
2718 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2719 if (!ptr)
2720 return 0; /* parameter not stored */
2721 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2722 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2723 }
2724 return 1;
2725}
2726
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002727/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002728 * the configured period.
2729 */
2730static int
Willy Tarreau56123282010-08-06 19:06:56 +02002731acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2732 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002733{
Willy Tarreau56123282010-08-06 19:06:56 +02002734 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002735 return 0;
2736
Willy Tarreau56123282010-08-06 19:06:56 +02002737 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002738}
2739
2740/* set test->i to the session rate from the session's tracked BE counters over
2741 * the configured period.
2742 */
2743static int
Willy Tarreau56123282010-08-06 19:06:56 +02002744acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2745 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002746{
Willy Tarreau56123282010-08-06 19:06:56 +02002747 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002748 return 0;
2749
Willy Tarreau56123282010-08-06 19:06:56 +02002750 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002751}
2752
2753/* set test->i to the session rate from the session's source address in the
2754 * table pointed to by expr, over the configured period.
2755 */
2756static int
2757acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002758 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002759{
2760 struct stktable_key *key;
2761
2762 key = tcpv4_src_to_stktable_key(l4);
2763 if (!key)
2764 return 0; /* only TCPv4 is supported right now */
2765
2766 if (expr->arg_len)
2767 px = find_stktable(expr->arg.str);
2768
2769 if (!px)
2770 return 0; /* table not found */
2771
2772 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2773}
2774
2775/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2776static int
2777acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2778{
2779 test->flags = ACL_TEST_F_VOL_TEST;
2780 test->i = 0;
2781 if (ts != NULL) {
2782 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2783 if (!ptr)
2784 return 0; /* parameter not stored */
2785 test->i = stktable_data_cast(ptr, http_err_cnt);
2786 }
2787 return 1;
2788}
2789
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002790/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002791static int
Willy Tarreau56123282010-08-06 19:06:56 +02002792acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2793 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002794{
Willy Tarreau56123282010-08-06 19:06:56 +02002795 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002796 return 0;
2797
Willy Tarreau56123282010-08-06 19:06:56 +02002798 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002799}
2800
2801/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2802static int
Willy Tarreau56123282010-08-06 19:06:56 +02002803acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2804 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002805{
Willy Tarreau56123282010-08-06 19:06:56 +02002806 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002807 return 0;
2808
Willy Tarreau56123282010-08-06 19:06:56 +02002809 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002810}
2811
2812/* set test->i to the cumulated number of session from the session's source
2813 * address in the table pointed to by expr.
2814 */
2815static int
2816acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002817 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002818{
2819 struct stktable_key *key;
2820
2821 key = tcpv4_src_to_stktable_key(l4);
2822 if (!key)
2823 return 0; /* only TCPv4 is supported right now */
2824
2825 if (expr->arg_len)
2826 px = find_stktable(expr->arg.str);
2827
2828 if (!px)
2829 return 0; /* table not found */
2830
2831 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2832}
2833
2834/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2835static int
2836acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2837{
2838 test->flags = ACL_TEST_F_VOL_TEST;
2839 test->i = 0;
2840 if (ts != NULL) {
2841 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2842 if (!ptr)
2843 return 0; /* parameter not stored */
2844 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2845 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2846 }
2847 return 1;
2848}
2849
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002850/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002851 * the configured period.
2852 */
2853static int
Willy Tarreau56123282010-08-06 19:06:56 +02002854acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2855 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002856{
Willy Tarreau56123282010-08-06 19:06:56 +02002857 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002858 return 0;
2859
Willy Tarreau56123282010-08-06 19:06:56 +02002860 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002861}
2862
2863/* set test->i to the session rate from the session's tracked BE counters over
2864 * the configured period.
2865 */
2866static int
Willy Tarreau56123282010-08-06 19:06:56 +02002867acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2868 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002869{
Willy Tarreau56123282010-08-06 19:06:56 +02002870 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002871 return 0;
2872
Willy Tarreau56123282010-08-06 19:06:56 +02002873 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002874}
2875
2876/* set test->i to the session rate from the session's source address in the
2877 * table pointed to by expr, over the configured period.
2878 */
2879static int
2880acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2881 struct acl_expr *expr, struct acl_test *test)
2882{
2883 struct stktable_key *key;
2884
2885 key = tcpv4_src_to_stktable_key(l4);
2886 if (!key)
2887 return 0; /* only TCPv4 is supported right now */
2888
2889 if (expr->arg_len)
2890 px = find_stktable(expr->arg.str);
2891
2892 if (!px)
2893 return 0; /* table not found */
2894
2895 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2896}
2897
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002898/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2899static int
2900acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2901{
2902 test->flags = ACL_TEST_F_VOL_TEST;
2903 test->i = 0;
2904
2905 if (ts != NULL) {
2906 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2907 if (!ptr)
2908 return 0; /* parameter not stored */
2909 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2910 }
2911 return 1;
2912}
2913
2914/* set test->i to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002915 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002916 */
2917static int
Willy Tarreau56123282010-08-06 19:06:56 +02002918acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2919 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002920{
Willy Tarreau56123282010-08-06 19:06:56 +02002921 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002922 return 0;
2923
Willy Tarreau56123282010-08-06 19:06:56 +02002924 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002925}
2926
2927/* set test->i to the number of kbytes received from clients according to the
2928 * session's tracked BE counters.
2929 */
2930static int
Willy Tarreau56123282010-08-06 19:06:56 +02002931acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2932 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002933{
Willy Tarreau56123282010-08-06 19:06:56 +02002934 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002935 return 0;
2936
Willy Tarreau56123282010-08-06 19:06:56 +02002937 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002938}
2939
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002940/* set test->i to the number of kbytes received from the session's source
2941 * address in the table pointed to by expr.
2942 */
2943static int
2944acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2945 struct acl_expr *expr, struct acl_test *test)
2946{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002947 struct stktable_key *key;
2948
2949 key = tcpv4_src_to_stktable_key(l4);
2950 if (!key)
2951 return 0; /* only TCPv4 is supported right now */
2952
2953 if (expr->arg_len)
2954 px = find_stktable(expr->arg.str);
2955
2956 if (!px)
2957 return 0; /* table not found */
2958
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002959 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2960}
2961
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002962/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2963 * configured period.
2964 */
2965static int
2966acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2967{
2968 test->flags = ACL_TEST_F_VOL_TEST;
2969 test->i = 0;
2970 if (ts != NULL) {
2971 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
2972 if (!ptr)
2973 return 0; /* parameter not stored */
2974 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2975 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2976 }
2977 return 1;
2978}
2979
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002980/* set test->i to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002981 * counters over the configured period.
2982 */
2983static int
Willy Tarreau56123282010-08-06 19:06:56 +02002984acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2985 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002986{
Willy Tarreau56123282010-08-06 19:06:56 +02002987 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002988 return 0;
2989
Willy Tarreau56123282010-08-06 19:06:56 +02002990 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002991}
2992
2993/* set test->i to the bytes rate from clients from the session's tracked BE
2994 * counters over the configured period.
2995 */
2996static int
Willy Tarreau56123282010-08-06 19:06:56 +02002997acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2998 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002999{
Willy Tarreau56123282010-08-06 19:06:56 +02003000 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003001 return 0;
3002
Willy Tarreau56123282010-08-06 19:06:56 +02003003 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003004}
3005
3006/* set test->i to the bytes rate from clients from the session's source address
3007 * in the table pointed to by expr, over the configured period.
3008 */
3009static int
3010acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003011 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003012{
3013 struct stktable_key *key;
3014
3015 key = tcpv4_src_to_stktable_key(l4);
3016 if (!key)
3017 return 0; /* only TCPv4 is supported right now */
3018
3019 if (expr->arg_len)
3020 px = find_stktable(expr->arg.str);
3021
3022 if (!px)
3023 return 0; /* table not found */
3024
3025 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3026}
3027
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003028/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
3029static int
3030acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3031{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003032 test->flags = ACL_TEST_F_VOL_TEST;
3033 test->i = 0;
3034
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003035 if (ts != NULL) {
3036 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003037 if (!ptr)
3038 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003039 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003040 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003041 return 1;
3042}
3043
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003044/* set test->i to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003045 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003046 */
3047static int
Willy Tarreau56123282010-08-06 19:06:56 +02003048acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3049 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003050{
Willy Tarreau56123282010-08-06 19:06:56 +02003051 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003052 return 0;
3053
Willy Tarreau56123282010-08-06 19:06:56 +02003054 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003055}
3056
3057/* set test->i to the number of kbytes sent to clients according to the session's
3058 * tracked BE counters.
3059 */
3060static int
Willy Tarreau56123282010-08-06 19:06:56 +02003061acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3062 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003063{
Willy Tarreau56123282010-08-06 19:06:56 +02003064 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003065 return 0;
3066
Willy Tarreau56123282010-08-06 19:06:56 +02003067 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003068}
3069
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003070/* set test->i to the number of kbytes sent to the session's source address in
3071 * the table pointed to by expr.
3072 */
3073static int
3074acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3075 struct acl_expr *expr, struct acl_test *test)
3076{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003077 struct stktable_key *key;
3078
3079 key = tcpv4_src_to_stktable_key(l4);
3080 if (!key)
3081 return 0; /* only TCPv4 is supported right now */
3082
3083 if (expr->arg_len)
3084 px = find_stktable(expr->arg.str);
3085
3086 if (!px)
3087 return 0; /* table not found */
3088
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003089 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003090}
3091
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003092/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
3093 * configured period.
3094 */
3095static int
3096acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3097{
3098 test->flags = ACL_TEST_F_VOL_TEST;
3099 test->i = 0;
3100 if (ts != NULL) {
3101 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3102 if (!ptr)
3103 return 0; /* parameter not stored */
3104 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3105 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3106 }
3107 return 1;
3108}
3109
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003110/* set test->i to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003111 * over the configured period.
3112 */
3113static int
Willy Tarreau56123282010-08-06 19:06:56 +02003114acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3115 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003116{
Willy Tarreau56123282010-08-06 19:06:56 +02003117 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003118 return 0;
3119
Willy Tarreau56123282010-08-06 19:06:56 +02003120 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003121}
3122
3123/* set test->i to the bytes rate to clients from the session's tracked BE counters
3124 * over the configured period.
3125 */
3126static int
Willy Tarreau56123282010-08-06 19:06:56 +02003127acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3128 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003129{
Willy Tarreau56123282010-08-06 19:06:56 +02003130 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003131 return 0;
3132
Willy Tarreau56123282010-08-06 19:06:56 +02003133 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003134}
3135
3136/* set test->i to the bytes rate to client from the session's source address in
3137 * the table pointed to by expr, over the configured period.
3138 */
3139static int
3140acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003141 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003142{
3143 struct stktable_key *key;
3144
3145 key = tcpv4_src_to_stktable_key(l4);
3146 if (!key)
3147 return 0; /* only TCPv4 is supported right now */
3148
3149 if (expr->arg_len)
3150 px = find_stktable(expr->arg.str);
3151
3152 if (!px)
3153 return 0; /* table not found */
3154
3155 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3156}
3157
Willy Tarreau8b22a712010-06-18 17:46:06 +02003158
3159/* Note: must not be declared <const> as its list will be overwritten */
3160static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003161 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3162 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3163 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3164 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3165 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3166 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3167 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3168 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3169 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3170 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3171 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3172 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3173 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3174 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3175 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3176 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3177 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3178 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3179 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3180 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3181 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3182 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3183 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3184 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3185 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3186 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3187 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3188 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3189 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3190 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3191 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3192 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3193 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3194 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3195 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3196 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3197 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3198 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3199 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3200 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3201 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3202 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3203 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3204 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3205 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3206 { "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 +02003207 { NULL, NULL, NULL, NULL },
3208}};
3209
3210
Willy Tarreau56123282010-08-06 19:06:56 +02003211/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003212 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3213 * <prm> struct is fed with the table name if any. If unspecified, the caller
3214 * will assume that the current proxy's table is used.
3215 */
3216int parse_track_counters(char **args, int *arg,
3217 int section_type, struct proxy *curpx,
3218 struct track_ctr_prm *prm,
3219 struct proxy *defpx, char *err, int errlen)
3220{
3221 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003222 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003223
Willy Tarreau56123282010-08-06 19:06:56 +02003224 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003225 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003226 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003227 */
3228 while (args[*arg]) {
3229 if (strcmp(args[*arg], "src") == 0) {
3230 prm->type = STKTABLE_TYPE_IP;
3231 pattern_type = 1;
3232 }
3233 else if (strcmp(args[*arg], "table") == 0) {
3234 if (!args[*arg + 1]) {
3235 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003236 "missing table for %s in %s '%s'.",
3237 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003238 return -1;
3239 }
3240 /* we copy the table name for now, it will be resolved later */
3241 prm->table.n = strdup(args[*arg + 1]);
3242 (*arg)++;
3243 }
3244 else {
3245 /* unhandled keywords are handled by the caller */
3246 break;
3247 }
3248 (*arg)++;
3249 }
3250
3251 if (!pattern_type) {
3252 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003253 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3254 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003255 return -1;
3256 }
3257
3258 return 0;
3259}
3260
Willy Tarreau8b22a712010-06-18 17:46:06 +02003261__attribute__((constructor))
3262static void __session_init(void)
3263{
3264 acl_register_keywords(&acl_kws);
3265}
3266
Willy Tarreaubaaee002006-06-26 02:48:02 +02003267/*
3268 * Local variables:
3269 * c-indent-level: 8
3270 * c-basic-offset: 8
3271 * End:
3272 */