blob: 37ded37634b1f848a7487e77ba8a77aadef6defc [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 Tarreau8d5d7f22007-01-21 19:16:41 +010030#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020031#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010033#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010034#include <proto/proto_http.h>
35#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020036#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010038#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010039#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010040#include <proto/stream_interface.h>
41#include <proto/stream_sock.h>
42#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020044struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010045struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau81f9aa32010-06-01 17:45:26 +020047/* This function is called from the protocol layer accept() in order to instanciate
48 * a new session on behalf of a given listener and frontend. It returns a positive
49 * value upon success, 0 if the connection needs to be closed and ignored, or a
50 * negative value upon critical failure.
51 */
52int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
53{
54 struct proxy *p = l->frontend;
55 struct session *s;
56 struct http_txn *txn;
57 struct task *t;
58
59 if (unlikely((s = pool_alloc2(pool2_session)) == NULL)) {
60 Alert("out of memory in event_accept().\n");
61 goto out_close;
62 }
63
64 /* minimum session initialization required for monitor mode below */
65 s->flags = 0;
66 s->logs.logwait = p->to_log;
Willy Tarreauf059a0f2010-08-03 16:29:52 +020067 s->fe_tracked_counters = NULL;
68 s->be_tracked_counters = NULL;
69 s->fe_tracked_table = NULL;
70 s->be_tracked_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020071
72 /* if this session comes from a known monitoring system, we want to ignore
73 * it as soon as possible, which means closing it immediately for TCP, but
74 * cleanly.
75 */
76 if (unlikely((l->options & LI_O_CHK_MONNET) &&
77 addr->ss_family == AF_INET &&
78 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
79 if (p->mode == PR_MODE_TCP) {
80 pool_free2(pool2_session, s);
81 return 0;
82 }
83 s->flags |= SN_MONITOR;
84 s->logs.logwait = 0;
85 }
86
87 /* OK, we're keeping the session, so let's properly initialize the session */
88 LIST_ADDQ(&sessions, &s->list);
89 LIST_INIT(&s->back_refs);
90
91 if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */
92 Alert("out of memory in event_accept().\n");
93 goto out_free_session;
94 }
95
96 s->term_trace = 0;
97 s->cli_addr = *addr;
98 s->logs.accept_date = date; /* user-visible date for logging */
99 s->logs.tv_accept = now; /* corrected date for internal use */
100 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200101 p->feconn++; /* beconn will be increased once assigned */
102
Willy Tarreaub36b4242010-06-04 20:59:39 +0200103 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200104
105 t->process = l->handler;
106 t->context = s;
107 t->nice = l->nice;
108 t->expire = TICK_ETERNITY;
109
110 s->task = t;
111 s->listener = l;
112
113 /* Note: initially, the session's backend points to the frontend.
114 * This changes later when switching rules are executed or
115 * when the default backend is assigned.
116 */
117 s->be = s->fe = p;
118 s->req = s->rep = NULL; /* will be allocated later */
119
120 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
121 * to abort right here as soon as possible, we check the rules before
122 * even initializing the stream interfaces.
123 */
124 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200125 if (s->fe_tracked_counters || s->be_tracked_counters)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200126 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200127 task_free(t);
128 LIST_DEL(&s->list);
129 pool_free2(pool2_session, s);
130 /* let's do a no-linger now to close with a single RST. */
131 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200132 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200133 return 0;
134 }
135
Willy Tarreaub36b4242010-06-04 20:59:39 +0200136 /* This session was accepted, count it now */
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200137 if (p->feconn > p->counters.feconn_max)
138 p->counters.feconn_max = p->feconn;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200139 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200140 if (s->fe_tracked_counters) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200141 void *ptr;
142
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200143 ptr = stktable_data_ptr(s->fe_tracked_table, s->fe_tracked_counters, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200144 if (ptr)
145 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200146
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200147 ptr = stktable_data_ptr(s->fe_tracked_table, s->fe_tracked_counters, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200148 if (ptr)
149 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200150 s->fe_tracked_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200151 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200152
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200153 if (s->be_tracked_counters) {
154 void *ptr;
155
156 ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_SESS_CNT);
157 if (ptr)
158 stktable_data_cast(ptr, sess_cnt)++;
159
160 ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_SESS_RATE);
161 if (ptr)
162 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
163 s->be_tracked_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
164 }
165
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200166 /* this part should be common with other protocols */
167 s->si[0].fd = cfd;
168 s->si[0].owner = t;
169 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
170 s->si[0].err_type = SI_ET_NONE;
171 s->si[0].err_loc = NULL;
172 s->si[0].connect = NULL;
173 s->si[0].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200174 s->si[0].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200175 s->si[0].exp = TICK_ETERNITY;
176 s->si[0].flags = SI_FL_NONE;
177
178 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
179 s->si[0].flags |= SI_FL_INDEP_STR;
180
181 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
182 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
183
184 /* add the various callbacks */
185 stream_sock_prepare_interface(&s->si[0]);
186
187 /* pre-initialize the other side's stream interface to an INIT state. The
188 * callbacks will be initialized before attempting to connect.
189 */
190 s->si[1].fd = -1; /* just to help with debugging */
191 s->si[1].owner = t;
192 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
193 s->si[1].err_type = SI_ET_NONE;
194 s->si[1].err_loc = NULL;
195 s->si[1].connect = NULL;
196 s->si[1].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200197 s->si[1].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200198 s->si[1].shutr = stream_int_shutr;
199 s->si[1].shutw = stream_int_shutw;
200 s->si[1].exp = TICK_ETERNITY;
201 s->si[1].flags = SI_FL_NONE;
202
203 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
204 s->si[1].flags |= SI_FL_INDEP_STR;
205
206 s->srv = s->prev_srv = s->srv_conn = NULL;
207 s->pend_pos = NULL;
208
209 /* init store persistence */
210 s->store_count = 0;
211
212 /* Adjust some socket options */
213 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) {
214 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
215 goto out_free_task;
216 }
217
218 txn = &s->txn;
219 /* Those variables will be checked and freed if non-NULL in
220 * session.c:session_free(). It is important that they are
221 * properly initialized.
222 */
223 txn->sessid = NULL;
224 txn->srv_cookie = NULL;
225 txn->cli_cookie = NULL;
226 txn->uri = NULL;
227 txn->req.cap = NULL;
228 txn->rsp.cap = NULL;
229 txn->hdr_idx.v = NULL;
230 txn->hdr_idx.size = txn->hdr_idx.used = 0;
231
232 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
233 goto out_free_task; /* no memory */
234
235 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
236 goto out_free_req; /* no memory */
237
238 /* initialize the request buffer */
239 s->req->size = global.tune.bufsize;
240 buffer_init(s->req);
241 s->req->prod = &s->si[0];
242 s->req->cons = &s->si[1];
243 s->si[0].ib = s->si[1].ob = s->req;
244 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
245
246 /* activate default analysers enabled for this listener */
247 s->req->analysers = l->analysers;
248
249 s->req->wto = TICK_ETERNITY;
250 s->req->rto = TICK_ETERNITY;
251 s->req->rex = TICK_ETERNITY;
252 s->req->wex = TICK_ETERNITY;
253 s->req->analyse_exp = TICK_ETERNITY;
254
255 /* initialize response buffer */
256 s->rep->size = global.tune.bufsize;
257 buffer_init(s->rep);
258 s->rep->prod = &s->si[1];
259 s->rep->cons = &s->si[0];
260 s->si[0].ob = s->si[1].ib = s->rep;
261 s->rep->analysers = 0;
262
263 s->rep->rto = TICK_ETERNITY;
264 s->rep->wto = TICK_ETERNITY;
265 s->rep->rex = TICK_ETERNITY;
266 s->rep->wex = TICK_ETERNITY;
267 s->rep->analyse_exp = TICK_ETERNITY;
268
269 /* finish initialization of the accepted file descriptor */
270 fd_insert(cfd);
271 fdtab[cfd].owner = &s->si[0];
272 fdtab[cfd].state = FD_STREADY;
273 fdtab[cfd].flags = 0;
274 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
275 fdtab[cfd].cb[DIR_RD].b = s->req;
276 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
277 fdtab[cfd].cb[DIR_WR].b = s->rep;
278 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
279 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
280 EV_FD_SET(cfd, DIR_RD);
281
282 if (p->accept) {
283 int ret = p->accept(s);
284 if (unlikely(ret < 0))
285 goto out_free_rep;
286
287 if (unlikely(ret == 0)) {
288 /* work is finished, we can release everything (eg: monitoring) */
289 pool_free2(pool2_buffer, s->rep);
290 pool_free2(pool2_buffer, s->req);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200291 if (s->fe_tracked_counters || s->be_tracked_counters)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200292 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200293 task_free(t);
294 LIST_DEL(&s->list);
295 pool_free2(pool2_session, s);
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200296 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200297 return 0;
298 }
299 }
300
301 /* it is important not to call the wakeup function directly but to
302 * pass through task_wakeup(), because this one knows how to apply
303 * priorities to tasks.
304 */
305 task_wakeup(t, TASK_WOKEN_INIT);
306 return 1;
307
308 /* Error unrolling */
309 out_free_rep:
310 pool_free2(pool2_buffer, s->rep);
311 out_free_req:
312 pool_free2(pool2_buffer, s->req);
313 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200314 p->feconn--;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200315 if (s->fe_tracked_counters || s->be_tracked_counters)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200316 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200317 task_free(t);
318 out_free_session:
319 LIST_DEL(&s->list);
320 pool_free2(pool2_session, s);
321 out_close:
322 return -1;
323}
324
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325/*
326 * frees the context associated to a session. It must have been removed first.
327 */
328void session_free(struct session *s)
329{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100330 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200331 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100332 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200333 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100334
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335 if (s->pend_pos)
336 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100337
Willy Tarreau1e62de62008-11-11 20:20:02 +0100338 if (s->srv) { /* there may be requests left pending in queue */
339 if (s->flags & SN_CURR_SESS) {
340 s->flags &= ~SN_CURR_SESS;
341 s->srv->cur_sess--;
342 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100343 if (may_dequeue_tasks(s->srv, s->be))
344 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100345 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100346
Willy Tarreau7c669d72008-06-20 15:04:11 +0200347 if (unlikely(s->srv_conn)) {
348 /* the session still has a reserved slot on a server, but
349 * it should normally be only the same as the one above,
350 * so this should not happen in fact.
351 */
352 sess_change_server(s, NULL);
353 }
354
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100355 if (s->req->pipe)
356 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100357
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100358 if (s->rep->pipe)
359 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100360
Willy Tarreau48d63db2008-08-03 17:41:33 +0200361 pool_free2(pool2_buffer, s->req);
362 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363
Willy Tarreau46023632010-01-07 22:51:47 +0100364 http_end_txn(s);
365
Willy Tarreaua4cda672010-06-06 18:28:49 +0200366 for (i = 0; i < s->store_count; i++) {
367 if (!s->store[i].ts)
368 continue;
369 stksess_free(s->store[i].table, s->store[i].ts);
370 s->store[i].ts = NULL;
371 }
372
Willy Tarreau92fb9832007-10-16 17:34:28 +0200373 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200374 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100375 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
376 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200377 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100378
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200379 if (s->fe_tracked_counters || s->be_tracked_counters)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200380 session_store_counters(s);
381
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100382 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100383 /* we have to unlink all watchers. We must not relink them if
384 * this session was the last one in the list.
385 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100386 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100387 LIST_INIT(&bref->users);
388 if (s->list.n != &sessions)
389 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100390 bref->ref = s->list.n;
391 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100392 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200393 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200394
395 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200396 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200397 pool_flush2(pool2_buffer);
398 pool_flush2(fe->hdr_idx_pool);
399 pool_flush2(pool2_requri);
400 pool_flush2(pool2_capture);
401 pool_flush2(pool2_session);
402 pool_flush2(fe->req_cap_pool);
403 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200404 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200405}
406
407
408/* perform minimal intializations, report 0 in case of error, 1 if OK. */
409int init_session()
410{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100411 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200412 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
413 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414}
415
Willy Tarreau30e71012007-11-26 20:15:35 +0100416void session_process_counters(struct session *s)
417{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100418 unsigned long long bytes;
419
Willy Tarreau30e71012007-11-26 20:15:35 +0100420 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100421 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100422 s->logs.bytes_in = s->req->total;
423 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200424 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100425
Willy Tarreau30e71012007-11-26 20:15:35 +0100426 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200427 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100428
Willy Tarreau30e71012007-11-26 20:15:35 +0100429 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200430 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200431
432 if (s->listener->counters)
433 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200434
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200435 if (s->be_tracked_counters) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200436 void *ptr;
437
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200438 ptr = stktable_data_ptr(s->be_tracked_table,
439 s->be_tracked_counters,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200440 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200441 if (ptr)
442 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200443
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200444 ptr = stktable_data_ptr(s->be_tracked_table,
445 s->be_tracked_counters,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200446 STKTABLE_DT_BYTES_IN_RATE);
447 if (ptr)
448 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200449 s->be_tracked_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
450 }
451
452 if (s->fe_tracked_counters) {
453 void *ptr;
454
455 ptr = stktable_data_ptr(s->fe_tracked_table,
456 s->fe_tracked_counters,
457 STKTABLE_DT_BYTES_IN_CNT);
458 if (ptr)
459 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
460
461 ptr = stktable_data_ptr(s->fe_tracked_table,
462 s->fe_tracked_counters,
463 STKTABLE_DT_BYTES_IN_RATE);
464 if (ptr)
465 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
466 s->fe_tracked_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200467 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100468 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100469 }
470
Willy Tarreau30e71012007-11-26 20:15:35 +0100471 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100472 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100473 s->logs.bytes_out = s->rep->total;
474 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200475 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100476
Willy Tarreau30e71012007-11-26 20:15:35 +0100477 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200478 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100479
Willy Tarreau30e71012007-11-26 20:15:35 +0100480 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200481 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200482
483 if (s->listener->counters)
484 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200485
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200486 if (s->be_tracked_counters) {
487 void *ptr;
488
489 ptr = stktable_data_ptr(s->be_tracked_table,
490 s->be_tracked_counters,
491 STKTABLE_DT_BYTES_OUT_CNT);
492 if (ptr)
493 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
494
495 ptr = stktable_data_ptr(s->be_tracked_table,
496 s->be_tracked_counters,
497 STKTABLE_DT_BYTES_OUT_RATE);
498 if (ptr)
499 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
500 s->be_tracked_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
501 }
502
503 if (s->fe_tracked_counters) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200504 void *ptr;
505
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200506 ptr = stktable_data_ptr(s->fe_tracked_table,
507 s->fe_tracked_counters,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200508 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200509 if (ptr)
510 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200511
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200512 ptr = stktable_data_ptr(s->fe_tracked_table,
513 s->fe_tracked_counters,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200514 STKTABLE_DT_BYTES_OUT_RATE);
515 if (ptr)
516 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200517 s->fe_tracked_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200518 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100519 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100520 }
521}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100523/* This function is called with (si->state == SI_ST_CON) meaning that a
524 * connection was attempted and that the file descriptor is already allocated.
525 * We must check for establishment, error and abort. Possible output states
526 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
527 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
528 * otherwise 1.
529 */
530int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
531{
532 struct buffer *req = si->ob;
533 struct buffer *rep = si->ib;
534
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100535 /* If we got an error, or if nothing happened and the connection timed
536 * out, we must give up. The CER state handler will take care of retry
537 * attempts and error reports.
538 */
539 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100540 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200542 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100543 fd_delete(si->fd);
544
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200545 if (si->release)
546 si->release(si);
547
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100548 if (si->err_type)
549 return 0;
550
551 si->err_loc = s->srv;
552 if (si->flags & SI_FL_ERR)
553 si->err_type = SI_ET_CONN_ERR;
554 else
555 si->err_type = SI_ET_CONN_TO;
556 return 0;
557 }
558
559 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200560 if (unlikely((rep->flags & BF_SHUTW) ||
561 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200562 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100563 s->be->options & PR_O_ABRT_CLOSE)))) {
564 /* give up */
565 si->shutw(si);
566 si->err_type |= SI_ET_CONN_ABRT;
567 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200568 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100569 if (s->srv_error)
570 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100571 return 1;
572 }
573
574 /* we need to wait a bit more if there was no activity either */
575 if (!(req->flags & BF_WRITE_ACTIVITY))
576 return 1;
577
578 /* OK, this means that a connection succeeded. The caller will be
579 * responsible for handling the transition from CON to EST.
580 */
581 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100582 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100583 si->state = SI_ST_EST;
584 si->err_type = SI_ET_NONE;
585 si->err_loc = NULL;
586 return 1;
587}
588
589/* This function is called with (si->state == SI_ST_CER) meaning that a
590 * previous connection attempt has failed and that the file descriptor
591 * has already been released. Possible causes include asynchronous error
592 * notification and time out. Possible output states are SI_ST_CLO when
593 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
594 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
595 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
596 * marked as in error state. It returns 0.
597 */
598int sess_update_st_cer(struct session *s, struct stream_interface *si)
599{
600 /* we probably have to release last session from the server */
601 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100602 health_adjust(s->srv, HANA_STATUS_L4_ERR);
603
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100604 if (s->flags & SN_CURR_SESS) {
605 s->flags &= ~SN_CURR_SESS;
606 s->srv->cur_sess--;
607 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100608 }
609
610 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200611 si->conn_retries--;
612 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100613 if (!si->err_type) {
614 si->err_type = SI_ET_CONN_ERR;
615 si->err_loc = s->srv;
616 }
617
618 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200619 s->srv->counters.failed_conns++;
620 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100621 if (may_dequeue_tasks(s->srv, s->be))
622 process_srv_queue(s->srv);
623
624 /* shutw is enough so stop a connecting socket */
625 si->shutw(si);
626 si->ob->flags |= BF_WRITE_ERROR;
627 si->ib->flags |= BF_READ_ERROR;
628
629 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100630 if (s->srv_error)
631 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100632 return 0;
633 }
634
635 /* If the "redispatch" option is set on the backend, we are allowed to
636 * retry on another server for the last retry. In order to achieve this,
637 * we must mark the session unassigned, and eventually clear the DIRECT
638 * bit to ignore any persistence cookie. We won't count a retry nor a
639 * redispatch yet, because this will depend on what server is selected.
640 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200641 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100642 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100643 if (may_dequeue_tasks(s->srv, s->be))
644 process_srv_queue(s->srv);
645
646 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
647 s->prev_srv = s->srv;
648 si->state = SI_ST_REQ;
649 } else {
650 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200651 s->srv->counters.retries++;
652 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100653 si->state = SI_ST_ASS;
654 }
655
656 if (si->flags & SI_FL_ERR) {
657 /* The error was an asynchronous connection error, and we will
658 * likely have to retry connecting to the same server, most
659 * likely leading to the same result. To avoid this, we wait
660 * one second before retrying.
661 */
662
663 if (!si->err_type)
664 si->err_type = SI_ET_CONN_ERR;
665
666 si->state = SI_ST_TAR;
667 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
668 return 0;
669 }
670 return 0;
671}
672
673/*
674 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200675 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
676 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100677 */
678void sess_establish(struct session *s, struct stream_interface *si)
679{
680 struct buffer *req = si->ob;
681 struct buffer *rep = si->ib;
682
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100683 if (s->srv)
684 health_adjust(s->srv, HANA_STATUS_L4_OK);
685
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100686 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687 /* if the user wants to log as soon as possible, without counting
688 * bytes from the server, then this is the right moment. */
689 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
690 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100691 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100692 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100693 }
694 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100695 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
696 /* reset hdr_idx which was already initialized by the request.
697 * right now, the http parser does it.
698 * hdr_idx_init(&s->txn.hdr_idx);
699 */
700 }
701
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200702 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200704 if (si->connect) {
705 /* real connections have timeouts */
706 req->wto = s->be->timeout.server;
707 rep->rto = s->be->timeout.server;
708 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100709 req->wex = TICK_ETERNITY;
710}
711
712/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
713 * Other input states are simply ignored.
714 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
715 * Flags must have previously been updated for timeouts and other conditions.
716 */
717void sess_update_stream_int(struct session *s, struct stream_interface *si)
718{
719 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",
720 now_ms, __FUNCTION__,
721 s,
722 s->req, s->rep,
723 s->req->rex, s->rep->wex,
724 s->req->flags, s->rep->flags,
725 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
726
727 if (si->state == SI_ST_ASS) {
728 /* Server assigned to connection request, we have to try to connect now */
729 int conn_err;
730
731 conn_err = connect_server(s);
732 if (conn_err == SN_ERR_NONE) {
733 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100734 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100735 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736 return;
737 }
738
739 /* We have received a synchronous error. We might have to
740 * abort, retry immediately or redispatch.
741 */
742 if (conn_err == SN_ERR_INTERNAL) {
743 if (!si->err_type) {
744 si->err_type = SI_ET_CONN_OTHER;
745 si->err_loc = s->srv;
746 }
747
748 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100749 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100750 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200751 s->srv->counters.failed_conns++;
752 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100753
754 /* release other sessions waiting for this server */
755 if (may_dequeue_tasks(s->srv, s->be))
756 process_srv_queue(s->srv);
757
758 /* Failed and not retryable. */
759 si->shutr(si);
760 si->shutw(si);
761 si->ob->flags |= BF_WRITE_ERROR;
762
763 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
764
765 /* no session was ever accounted for this server */
766 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100767 if (s->srv_error)
768 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100769 return;
770 }
771
772 /* We are facing a retryable error, but we don't want to run a
773 * turn-around now, as the problem is likely a source port
774 * allocation problem, so we want to retry now.
775 */
776 si->state = SI_ST_CER;
777 si->flags &= ~SI_FL_ERR;
778 sess_update_st_cer(s, si);
779 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
780 return;
781 }
782 else if (si->state == SI_ST_QUE) {
783 /* connection request was queued, check for any update */
784 if (!s->pend_pos) {
785 /* The connection is not in the queue anymore. Either
786 * we have a server connection slot available and we
787 * go directly to the assigned state, or we need to
788 * load-balance first and go to the INI state.
789 */
790 si->exp = TICK_ETERNITY;
791 if (unlikely(!(s->flags & SN_ASSIGNED)))
792 si->state = SI_ST_REQ;
793 else {
794 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
795 si->state = SI_ST_ASS;
796 }
797 return;
798 }
799
800 /* Connection request still in queue... */
801 if (si->flags & SI_FL_EXP) {
802 /* ... and timeout expired */
803 si->exp = TICK_ETERNITY;
804 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
805 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200806 s->srv->counters.failed_conns++;
807 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100808 si->shutr(si);
809 si->shutw(si);
810 si->ob->flags |= BF_WRITE_TIMEOUT;
811 if (!si->err_type)
812 si->err_type = SI_ET_QUEUE_TO;
813 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100814 if (s->srv_error)
815 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100816 return;
817 }
818
819 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200820 if ((si->ob->flags & (BF_READ_ERROR)) ||
821 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200822 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100823 /* give up */
824 si->exp = TICK_ETERNITY;
825 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
826 si->shutr(si);
827 si->shutw(si);
828 si->err_type |= SI_ET_QUEUE_ABRT;
829 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100830 if (s->srv_error)
831 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100832 return;
833 }
834
835 /* Nothing changed */
836 return;
837 }
838 else if (si->state == SI_ST_TAR) {
839 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200840 if ((si->ob->flags & (BF_READ_ERROR)) ||
841 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200842 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100843 /* give up */
844 si->exp = TICK_ETERNITY;
845 si->shutr(si);
846 si->shutw(si);
847 si->err_type |= SI_ET_CONN_ABRT;
848 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100849 if (s->srv_error)
850 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100851 return;
852 }
853
854 if (!(si->flags & SI_FL_EXP))
855 return; /* still in turn-around */
856
857 si->exp = TICK_ETERNITY;
858
859 /* we keep trying on the same server as long as the session is
860 * marked "assigned".
861 * FIXME: Should we force a redispatch attempt when the server is down ?
862 */
863 if (s->flags & SN_ASSIGNED)
864 si->state = SI_ST_ASS;
865 else
866 si->state = SI_ST_REQ;
867 return;
868 }
869}
870
871/* This function initiates a server connection request on a stream interface
872 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
873 * indicating that a server has been assigned. It may also return SI_ST_QUE,
874 * or SI_ST_CLO upon error.
875 */
876static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
877 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",
878 now_ms, __FUNCTION__,
879 s,
880 s->req, s->rep,
881 s->req->rex, s->rep->wex,
882 s->req->flags, s->rep->flags,
883 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
884
885 if (si->state != SI_ST_REQ)
886 return;
887
888 /* Try to assign a server */
889 if (srv_redispatch_connect(s) != 0) {
890 /* We did not get a server. Either we queued the
891 * connection request, or we encountered an error.
892 */
893 if (si->state == SI_ST_QUE)
894 return;
895
896 /* we did not get any server, let's check the cause */
897 si->shutr(si);
898 si->shutw(si);
899 si->ob->flags |= BF_WRITE_ERROR;
900 if (!si->err_type)
901 si->err_type = SI_ET_CONN_OTHER;
902 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100903 if (s->srv_error)
904 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100905 return;
906 }
907
908 /* The server is assigned */
909 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
910 si->state = SI_ST_ASS;
911}
912
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200913/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100914 * if appropriate. The default_backend rule is also considered, then the
915 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200916 * It returns 1 if the processing can continue on next analysers, or zero if it
917 * either needs more data or wants to immediately abort the request.
918 */
919int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
920{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200921 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100922
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200923 req->analysers &= ~an_bit;
924 req->analyse_exp = TICK_ETERNITY;
925
926 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
927 now_ms, __FUNCTION__,
928 s,
929 req,
930 req->rex, req->wex,
931 req->flags,
932 req->l,
933 req->analysers);
934
935 /* now check whether we have some switching rules for this request */
936 if (!(s->flags & SN_BE_ASSIGNED)) {
937 struct switching_rule *rule;
938
939 list_for_each_entry(rule, &s->fe->switching_rules, list) {
940 int ret;
941
942 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
943 ret = acl_pass(ret);
944 if (rule->cond->pol == ACL_COND_UNLESS)
945 ret = !ret;
946
947 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200948 if (!session_set_backend(s, rule->be.backend))
949 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200950 break;
951 }
952 }
953
954 /* To ensure correct connection accounting on the backend, we
955 * have to assign one if it was not set (eg: a listen). This
956 * measure also takes care of correctly setting the default
957 * backend if any.
958 */
959 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200960 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
961 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200962 }
963
Willy Tarreaufb356202010-08-03 14:02:05 +0200964 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
965 if (s->fe == s->be) {
966 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200967 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200968 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200969
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200970 /* 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 +0100971 * persistence rule, and report that in the session.
972 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200973 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100974 int ret = 1;
975
976 if (prst_rule->cond) {
977 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
978 ret = acl_pass(ret);
979 if (prst_rule->cond->pol == ACL_COND_UNLESS)
980 ret = !ret;
981 }
982
983 if (ret) {
984 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200985 if (prst_rule->type == PERSIST_TYPE_FORCE) {
986 s->flags |= SN_FORCE_PRST;
987 } else {
988 s->flags |= SN_IGNORE_PRST;
989 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100990 break;
991 }
992 }
993
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200994 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200995
996 sw_failed:
997 /* immediately abort this request in case of allocation failure */
998 buffer_abort(s->req);
999 buffer_abort(s->rep);
1000
1001 if (!(s->flags & SN_ERR_MASK))
1002 s->flags |= SN_ERR_RESOURCE;
1003 if (!(s->flags & SN_FINST_MASK))
1004 s->flags |= SN_FINST_R;
1005
1006 s->txn.status = 500;
1007 s->req->analysers = 0;
1008 s->req->analyse_exp = TICK_ETERNITY;
1009 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001010}
1011
Emeric Brun1d33b292010-01-04 15:47:17 +01001012/* This stream analyser works on a request. It applies all sticking rules on
1013 * it then returns 1. The data must already be present in the buffer otherwise
1014 * they won't match. It always returns 1.
1015 */
1016int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
1017{
1018 struct proxy *px = s->be;
1019 struct sticking_rule *rule;
1020
1021 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1022 now_ms, __FUNCTION__,
1023 s,
1024 req,
1025 req->rex, req->wex,
1026 req->flags,
1027 req->l,
1028 req->analysers);
1029
1030 list_for_each_entry(rule, &px->sticking_rules, list) {
1031 int ret = 1 ;
1032 int i;
1033
1034 for (i = 0; i < s->store_count; i++) {
1035 if (rule->table.t == s->store[i].table)
1036 break;
1037 }
1038
1039 if (i != s->store_count)
1040 continue;
1041
1042 if (rule->cond) {
1043 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1044 ret = acl_pass(ret);
1045 if (rule->cond->pol == ACL_COND_UNLESS)
1046 ret = !ret;
1047 }
1048
1049 if (ret) {
1050 struct stktable_key *key;
1051
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001052 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001053 if (!key)
1054 continue;
1055
1056 if (rule->flags & STK_IS_MATCH) {
1057 struct stksess *ts;
1058
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001059 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001060 if (!(s->flags & SN_ASSIGNED)) {
1061 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001062 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001063
1064 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001065 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1066 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001067 if (node) {
1068 struct server *srv;
1069
1070 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001071 if ((srv->state & SRV_RUNNING) ||
1072 (px->options & PR_O_PERSIST) ||
1073 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001074 s->flags |= SN_DIRECT | SN_ASSIGNED;
1075 s->srv = srv;
1076 }
1077 }
1078 }
Willy Tarreaue8f63382010-07-13 15:20:24 +02001079 stktable_touch(rule->table.t, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001080 }
1081 }
1082 if (rule->flags & STK_IS_STORE) {
1083 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1084 struct stksess *ts;
1085
1086 ts = stksess_new(rule->table.t, key);
1087 if (ts) {
1088 s->store[s->store_count].table = rule->table.t;
1089 s->store[s->store_count++].ts = ts;
1090 }
1091 }
1092 }
1093 }
1094 }
1095
1096 req->analysers &= ~an_bit;
1097 req->analyse_exp = TICK_ETERNITY;
1098 return 1;
1099}
1100
1101/* This stream analyser works on a response. It applies all store rules on it
1102 * then returns 1. The data must already be present in the buffer otherwise
1103 * they won't match. It always returns 1.
1104 */
1105int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1106{
1107 struct proxy *px = s->be;
1108 struct sticking_rule *rule;
1109 int i;
1110
1111 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1112 now_ms, __FUNCTION__,
1113 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001114 rep,
1115 rep->rex, rep->wex,
1116 rep->flags,
1117 rep->l,
1118 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001119
1120 list_for_each_entry(rule, &px->storersp_rules, list) {
1121 int ret = 1 ;
1122 int storereqidx = -1;
1123
1124 for (i = 0; i < s->store_count; i++) {
1125 if (rule->table.t == s->store[i].table) {
1126 if (!(s->store[i].flags))
1127 storereqidx = i;
1128 break;
1129 }
1130 }
1131
1132 if ((i != s->store_count) && (storereqidx == -1))
1133 continue;
1134
1135 if (rule->cond) {
1136 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1137 ret = acl_pass(ret);
1138 if (rule->cond->pol == ACL_COND_UNLESS)
1139 ret = !ret;
1140 }
1141
1142 if (ret) {
1143 struct stktable_key *key;
1144
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001145 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001146 if (!key)
1147 continue;
1148
1149 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001150 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001151 s->store[storereqidx].flags = 1;
1152 }
1153 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1154 struct stksess *ts;
1155
1156 ts = stksess_new(rule->table.t, key);
1157 if (ts) {
1158 s->store[s->store_count].table = rule->table.t;
1159 s->store[s->store_count].flags = 1;
1160 s->store[s->store_count++].ts = ts;
1161 }
1162 }
1163 }
1164 }
1165
1166 /* process store request and store response */
1167 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001168 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001169 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001170
1171 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1172 if (ts) {
1173 /* the entry already existed, we can free ours */
Willy Tarreaue8f63382010-07-13 15:20:24 +02001174 stktable_touch(s->store[i].table, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001175 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001176 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001177 else
1178 ts = stktable_store(s->store[i].table, s->store[i].ts);
1179
1180 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001181 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1182 stktable_data_cast(ptr, server_id) = s->srv->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001183 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001184 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001185
1186 rep->analysers &= ~an_bit;
1187 rep->analyse_exp = TICK_ETERNITY;
1188 return 1;
1189}
1190
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001191/* This macro is very specific to the function below. See the comments in
1192 * process_session() below to understand the logic and the tests.
1193 */
1194#define UPDATE_ANALYSERS(real, list, back, flag) { \
1195 list = (((list) & ~(flag)) | ~(back)) & (real); \
1196 back = real; \
1197 if (!(list)) \
1198 break; \
1199 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1200 continue; \
1201}
1202
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001203/* Processes the client, server, request and response jobs of a session task,
1204 * then puts it back to the wait queue in a clean state, or cleans up its
1205 * resources if it must be deleted. Returns in <next> the date the task wants
1206 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1207 * nothing too many times, the request and response buffers flags are monitored
1208 * and each function is called only if at least another function has changed at
1209 * least one flag it is interested in.
1210 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001211struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001212{
1213 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001214 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001215 unsigned int rq_prod_last, rq_cons_last;
1216 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001217 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001218
1219 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1220 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1221
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001222 /* this data may be no longer valid, clear it */
1223 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1224
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001225 /* This flag must explicitly be set every time */
1226 s->req->flags &= ~BF_READ_NOEXP;
1227
1228 /* Keep a copy of req/rep flags so that we can detect shutdowns */
1229 rqf_last = s->req->flags;
1230 rpf_last = s->rep->flags;
1231
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001232 /* we don't want the stream interface functions to recursively wake us up */
1233 if (s->req->prod->owner == t)
1234 s->req->prod->flags |= SI_FL_DONT_WAKE;
1235 if (s->req->cons->owner == t)
1236 s->req->cons->flags |= SI_FL_DONT_WAKE;
1237
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001238 /* 1a: Check for low level timeouts if needed. We just set a flag on
1239 * stream interfaces when their timeouts have expired.
1240 */
1241 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1242 stream_int_check_timeouts(&s->si[0]);
1243 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001244
1245 /* check buffer timeouts, and close the corresponding stream interfaces
1246 * for future reads or writes. Note: this will also concern upper layers
1247 * but we do not touch any other flag. We must be careful and correctly
1248 * detect state changes when calling them.
1249 */
1250
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001251 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001252
Willy Tarreau14641402009-12-29 14:49:56 +01001253 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1254 s->req->cons->flags |= SI_FL_NOLINGER;
1255 s->req->cons->shutw(s->req->cons);
1256 }
1257
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001258 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1259 s->req->prod->shutr(s->req->prod);
1260
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001261 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001262
Willy Tarreau14641402009-12-29 14:49:56 +01001263 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1264 s->rep->cons->flags |= SI_FL_NOLINGER;
1265 s->rep->cons->shutw(s->rep->cons);
1266 }
1267
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001268 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1269 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001270 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001271
1272 /* 1b: check for low-level errors reported at the stream interface.
1273 * First we check if it's a retryable error (in which case we don't
1274 * want to tell the buffer). Otherwise we report the error one level
1275 * upper by setting flags into the buffers. Note that the side towards
1276 * the client cannot have connect (hence retryable) errors. Also, the
1277 * connection setup code must be able to deal with any type of abort.
1278 */
1279 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1280 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1281 s->si[0].shutr(&s->si[0]);
1282 s->si[0].shutw(&s->si[0]);
1283 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001284 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001285 s->be->counters.cli_aborts++;
1286 if (s->srv)
1287 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001288 if (!(s->flags & SN_ERR_MASK))
1289 s->flags |= SN_ERR_CLICL;
1290 if (!(s->flags & SN_FINST_MASK))
1291 s->flags |= SN_FINST_D;
1292 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001293 }
1294 }
1295
1296 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1297 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1298 s->si[1].shutr(&s->si[1]);
1299 s->si[1].shutw(&s->si[1]);
1300 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001301 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001302 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001303 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001304 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001305 s->be->counters.srv_aborts++;
1306 if (s->srv)
1307 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001308 if (!(s->flags & SN_ERR_MASK))
1309 s->flags |= SN_ERR_SRVCL;
1310 if (!(s->flags & SN_FINST_MASK))
1311 s->flags |= SN_FINST_D;
1312 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001313 }
1314 /* note: maybe we should process connection errors here ? */
1315 }
1316
1317 if (s->si[1].state == SI_ST_CON) {
1318 /* we were trying to establish a connection on the server side,
1319 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1320 */
1321 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1322 sess_update_st_cer(s, &s->si[1]);
1323 else if (s->si[1].state == SI_ST_EST)
1324 sess_establish(s, &s->si[1]);
1325
1326 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1327 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1328 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1329 */
1330 }
1331
Willy Tarreau815a9b22010-07-27 17:15:12 +02001332 rq_prod_last = s->si[0].state;
1333 rq_cons_last = s->si[1].state;
1334 rp_cons_last = s->si[0].state;
1335 rp_prod_last = s->si[1].state;
1336
1337 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001338 /* Check for connection closure */
1339
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001340 DPRINTF(stderr,
1341 "[%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",
1342 now_ms, __FUNCTION__, __LINE__,
1343 t,
1344 s, s->flags,
1345 s->req, s->rep,
1346 s->req->rex, s->rep->wex,
1347 s->req->flags, s->rep->flags,
1348 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1349 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001350 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001351
1352 /* nothing special to be done on client side */
1353 if (unlikely(s->req->prod->state == SI_ST_DIS))
1354 s->req->prod->state = SI_ST_CLO;
1355
1356 /* When a server-side connection is released, we have to count it and
1357 * check for pending connections on this server.
1358 */
1359 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1360 s->req->cons->state = SI_ST_CLO;
1361 if (s->srv) {
1362 if (s->flags & SN_CURR_SESS) {
1363 s->flags &= ~SN_CURR_SESS;
1364 s->srv->cur_sess--;
1365 }
1366 sess_change_server(s, NULL);
1367 if (may_dequeue_tasks(s->srv, s->be))
1368 process_srv_queue(s->srv);
1369 }
1370 }
1371
1372 /*
1373 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1374 * at this point.
1375 */
1376
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001377 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001378 /* Analyse request */
1379 if ((s->req->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001380 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1381 s->si[0].state != rq_prod_last ||
1382 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001383 unsigned int flags = s->req->flags;
1384
1385 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001386 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001387 unsigned int ana_list;
1388 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001389
Willy Tarreau90deb182010-01-07 00:20:41 +01001390 /* it's up to the analysers to stop new connections,
1391 * disable reading or closing. Note: if an analyser
1392 * disables any of these bits, it is responsible for
1393 * enabling them again when it disables itself, so
1394 * that other analysers are called in similar conditions.
1395 */
1396 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001397 buffer_auto_connect(s->req);
1398 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001399
1400 /* We will call all analysers for which a bit is set in
1401 * s->req->analysers, following the bit order from LSB
1402 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001403 * the list when not needed. Any analyser may return 0
1404 * to break out of the loop, either because of missing
1405 * data to take a decision, or because it decides to
1406 * kill the session. We loop at least once through each
1407 * analyser, and we may loop again if other analysers
1408 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001409 *
1410 * We build a list of analysers to run. We evaluate all
1411 * of these analysers in the order of the lower bit to
1412 * the higher bit. This ordering is very important.
1413 * An analyser will often add/remove other analysers,
1414 * including itself. Any changes to itself have no effect
1415 * on the loop. If it removes any other analysers, we
1416 * want those analysers not to be called anymore during
1417 * this loop. If it adds an analyser that is located
1418 * after itself, we want it to be scheduled for being
1419 * processed during the loop. If it adds an analyser
1420 * which is located before it, we want it to switch to
1421 * it immediately, even if it has already been called
1422 * once but removed since.
1423 *
1424 * In order to achieve this, we compare the analyser
1425 * list after the call with a copy of it before the
1426 * call. The work list is fed with analyser bits that
1427 * appeared during the call. Then we compare previous
1428 * work list with the new one, and check the bits that
1429 * appeared. If the lowest of these bits is lower than
1430 * the current bit, it means we have enabled a previous
1431 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001432 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001433
1434 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001435 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001436 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001437
Willy Tarreaufb356202010-08-03 14:02:05 +02001438 if (ana_list & AN_REQ_INSPECT_FE) {
1439 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001440 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001441 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001442 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001443
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001444 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001445 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001446 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001447 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001448 }
1449
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001450 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001451 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1452 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001453 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001454 }
1455
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001456 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001457 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1458 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001459 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001460 }
1461
Willy Tarreaufb356202010-08-03 14:02:05 +02001462 if (ana_list & AN_REQ_INSPECT_BE) {
1463 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1464 break;
1465 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1466 }
1467
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001468 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001469 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1470 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001471 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001472 }
1473
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001474 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001475 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001476 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001477 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001478 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001479
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001480 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001481 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1482 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001483 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001484 }
1485
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001486 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001487 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001488 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001489 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001490 }
Emeric Brun647caf12009-06-30 17:57:00 +02001491
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001492 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001493 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1494 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001495 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001496 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001497
Emeric Brun1d33b292010-01-04 15:47:17 +01001498 if (ana_list & AN_REQ_STICKING_RULES) {
1499 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1500 break;
1501 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1502 }
1503
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001504 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001505 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1506 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001507 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001508 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001509 break;
1510 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001511 }
Willy Tarreau84455332009-03-15 22:34:05 +01001512
Willy Tarreau815a9b22010-07-27 17:15:12 +02001513 rq_prod_last = s->si[0].state;
1514 rq_cons_last = s->si[1].state;
1515 rqf_last = s->req->flags;
1516
1517 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001518 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001519 }
1520
Willy Tarreau576507f2010-01-07 00:09:04 +01001521 /* we'll monitor the request analysers while parsing the response,
1522 * because some response analysers may indirectly enable new request
1523 * analysers (eg: HTTP keep-alive).
1524 */
1525 req_ana_back = s->req->analysers;
1526
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001527 resync_response:
1528 /* Analyse response */
1529
1530 if (unlikely(s->rep->flags & BF_HIJACK)) {
1531 /* In inject mode, we wake up everytime something has
1532 * happened on the write side of the buffer.
1533 */
1534 unsigned int flags = s->rep->flags;
1535
1536 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1537 !(s->rep->flags & BF_FULL)) {
1538 s->rep->hijacker(s, s->rep);
1539 }
1540
1541 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1542 rpf_last = s->rep->flags;
1543 goto resync_response;
1544 }
1545 }
1546 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001547 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1548 s->si[0].state != rp_cons_last ||
1549 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001550 unsigned int flags = s->rep->flags;
1551
1552 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001553 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001554 unsigned int ana_list;
1555 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001556
Willy Tarreau90deb182010-01-07 00:20:41 +01001557 /* it's up to the analysers to stop disable reading or
1558 * closing. Note: if an analyser disables any of these
1559 * bits, it is responsible for enabling them again when
1560 * it disables itself, so that other analysers are called
1561 * in similar conditions.
1562 */
1563 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001564 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001565
1566 /* We will call all analysers for which a bit is set in
1567 * s->rep->analysers, following the bit order from LSB
1568 * to MSB. The analysers must remove themselves from
1569 * the list when not needed. Any analyser may return 0
1570 * to break out of the loop, either because of missing
1571 * data to take a decision, or because it decides to
1572 * kill the session. We loop at least once through each
1573 * analyser, and we may loop again if other analysers
1574 * are added in the middle.
1575 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001576
1577 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001578 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001579 /* Warning! ensure that analysers are always placed in ascending order! */
1580
1581 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001582 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1583 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001584 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001585 }
1586
Emeric Brun1d33b292010-01-04 15:47:17 +01001587 if (ana_list & AN_RES_STORE_RULES) {
1588 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1589 break;
1590 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1591 }
1592
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001593 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001594 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1595 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001596 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001597 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001598
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001599 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001600 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1601 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001602 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001603 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001604 break;
1605 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001606 }
1607
Willy Tarreau815a9b22010-07-27 17:15:12 +02001608 rp_cons_last = s->si[0].state;
1609 rp_prod_last = s->si[1].state;
1610 rpf_last = s->rep->flags;
1611
1612 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001613 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001614 }
1615
Willy Tarreau576507f2010-01-07 00:09:04 +01001616 /* maybe someone has added some request analysers, so we must check and loop */
1617 if (s->req->analysers & ~req_ana_back)
1618 goto resync_request;
1619
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001620 /* FIXME: here we should call protocol handlers which rely on
1621 * both buffers.
1622 */
1623
1624
1625 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001626 * Now we propagate unhandled errors to the session. Normally
1627 * we're just in a data phase here since it means we have not
1628 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001629 */
1630 if (!(s->flags & SN_ERR_MASK)) {
1631 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1632 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001633 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001634 if (s->req->flags & BF_READ_ERROR) {
1635 s->be->counters.cli_aborts++;
1636 if (s->srv)
1637 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001638 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001639 }
1640 else if (s->req->flags & BF_READ_TIMEOUT) {
1641 s->be->counters.cli_aborts++;
1642 if (s->srv)
1643 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001644 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001645 }
1646 else if (s->req->flags & BF_WRITE_ERROR) {
1647 s->be->counters.srv_aborts++;
1648 if (s->srv)
1649 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001650 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001651 }
1652 else {
1653 s->be->counters.srv_aborts++;
1654 if (s->srv)
1655 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001656 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001657 }
Willy Tarreau84455332009-03-15 22:34:05 +01001658 sess_set_term_flags(s);
1659 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001660 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1661 /* Report it if the server got an error or a read timeout expired */
1662 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001663 if (s->rep->flags & BF_READ_ERROR) {
1664 s->be->counters.srv_aborts++;
1665 if (s->srv)
1666 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001667 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001668 }
1669 else if (s->rep->flags & BF_READ_TIMEOUT) {
1670 s->be->counters.srv_aborts++;
1671 if (s->srv)
1672 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001673 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001674 }
1675 else if (s->rep->flags & BF_WRITE_ERROR) {
1676 s->be->counters.cli_aborts++;
1677 if (s->srv)
1678 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001679 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001680 }
1681 else {
1682 s->be->counters.cli_aborts++;
1683 if (s->srv)
1684 s->srv->counters.cli_aborts++;
1685 s->flags |= SN_ERR_CLITO;
1686 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001687 sess_set_term_flags(s);
1688 }
Willy Tarreau84455332009-03-15 22:34:05 +01001689 }
1690
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001691 /*
1692 * Here we take care of forwarding unhandled data. This also includes
1693 * connection establishments and shutdown requests.
1694 */
1695
1696
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001697 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001698 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001699 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001700 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001701 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001702 (s->req->prod->state >= SI_ST_EST) &&
1703 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001704 /* This buffer is freewheeling, there's no analyser nor hijacker
1705 * attached to it. If any data are left in, we'll permit them to
1706 * move.
1707 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001708 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001709 buffer_auto_connect(s->req);
1710 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001711 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001712
Willy Tarreau31971e52009-09-20 12:07:52 +02001713 /* If the producer is still connected, we'll enable data to flow
1714 * from the producer to the consumer (which might possibly not be
1715 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001716 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001717 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1718 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001719 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001720
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001721 /* check if it is wise to enable kernel splicing to forward request data */
1722 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1723 s->req->to_forward &&
1724 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001725 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001726 (pipes_used < global.maxpipes) &&
1727 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1728 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1729 (s->req->flags & BF_STREAMER_FAST)))) {
1730 s->req->flags |= BF_KERN_SPLICING;
1731 }
1732
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001733 /* reflect what the L7 analysers have seen last */
1734 rqf_last = s->req->flags;
1735
1736 /*
1737 * Now forward all shutdown requests between both sides of the buffer
1738 */
1739
Willy Tarreau520d95e2009-09-19 21:04:57 +02001740 /* first, let's check if the request buffer needs to shutdown(write), which may
1741 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001742 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001743 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001744 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001745 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001746 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001747
1748 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001749 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 +01001750 s->req->cons->shutw(s->req->cons);
1751
1752 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001753 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1754 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001755 buffer_shutr_now(s->req);
1756
1757 /* shutdown(read) pending */
1758 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1759 s->req->prod->shutr(s->req->prod);
1760
Willy Tarreau520d95e2009-09-19 21:04:57 +02001761 /* it's possible that an upper layer has requested a connection setup or abort.
1762 * There are 2 situations where we decide to establish a new connection :
1763 * - there are data scheduled for emission in the buffer
1764 * - the BF_AUTO_CONNECT flag is set (active connection)
1765 */
1766 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001767 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001768 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau85e7d002010-05-31 11:57:51 +02001769 /* If we have an iohandler without a connect method, we immediately
1770 * switch to the connected state, otherwise we perform a connection
1771 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001772 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001773 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001774 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau85e7d002010-05-31 11:57:51 +02001775 if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001776 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001777 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1778 s->req->wex = TICK_ETERNITY;
1779 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001780 }
Willy Tarreau73201222009-08-16 18:27:24 +02001781 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001782 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001783 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001784 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1785 buffer_shutr_now(s->rep);
1786 }
Willy Tarreau92795622009-03-06 12:51:23 +01001787 }
1788
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001789
1790 /* we may have a pending connection request, or a connection waiting
1791 * for completion.
1792 */
1793 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1794 do {
1795 /* nb: step 1 might switch from QUE to ASS, but we first want
1796 * to give a chance to step 2 to perform a redirect if needed.
1797 */
1798 if (s->si[1].state != SI_ST_REQ)
1799 sess_update_stream_int(s, &s->si[1]);
1800 if (s->si[1].state == SI_ST_REQ)
1801 sess_prepare_conn_req(s, &s->si[1]);
1802
1803 if (s->si[1].state == SI_ST_ASS && s->srv &&
1804 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1805 perform_http_redirect(s, &s->si[1]);
1806 } while (s->si[1].state == SI_ST_ASS);
1807 }
1808
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001809 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001810 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001811 goto resync_stream_interface;
1812
Willy Tarreau815a9b22010-07-27 17:15:12 +02001813 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001814 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001815 goto resync_request;
1816
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001817 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001818
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001819 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001820 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001821 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001822 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001823 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001824 (s->rep->prod->state >= SI_ST_EST) &&
1825 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001826 /* This buffer is freewheeling, there's no analyser nor hijacker
1827 * attached to it. If any data are left in, we'll permit them to
1828 * move.
1829 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001830 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001831 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001832 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001833 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1834 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001835 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001836
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001837 /* check if it is wise to enable kernel splicing to forward response data */
1838 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1839 s->rep->to_forward &&
1840 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001841 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001842 (pipes_used < global.maxpipes) &&
1843 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1844 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1845 (s->rep->flags & BF_STREAMER_FAST)))) {
1846 s->rep->flags |= BF_KERN_SPLICING;
1847 }
1848
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001849 /* reflect what the L7 analysers have seen last */
1850 rpf_last = s->rep->flags;
1851
1852 /*
1853 * Now forward all shutdown requests between both sides of the buffer
1854 */
1855
1856 /*
1857 * FIXME: this is probably where we should produce error responses.
1858 */
1859
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001860 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001861 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1862 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001863 buffer_shutw_now(s->rep);
1864
1865 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001866 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 +01001867 s->rep->cons->shutw(s->rep->cons);
1868
1869 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001870 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1871 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001872 buffer_shutr_now(s->rep);
1873
1874 /* shutdown(read) pending */
1875 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1876 s->rep->prod->shutr(s->rep->prod);
1877
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001878 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001879 goto resync_stream_interface;
1880
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001881 if (s->req->flags != rqf_last)
1882 goto resync_request;
1883
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001884 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001885 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001886
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001887 /* we're interested in getting wakeups again */
1888 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1889 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1890
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001891 /* This is needed only when debugging is enabled, to indicate
1892 * client-side or server-side close. Please note that in the unlikely
1893 * event where both sides would close at once, the sequence is reported
1894 * on the server side first.
1895 */
1896 if (unlikely((global.mode & MODE_DEBUG) &&
1897 (!(global.mode & MODE_QUIET) ||
1898 (global.mode & MODE_VERBOSE)))) {
1899 int len;
1900
1901 if (s->si[1].state == SI_ST_CLO &&
1902 s->si[1].prev_state == SI_ST_EST) {
1903 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1904 s->uniq_id, s->be->id,
1905 (unsigned short)s->si[0].fd,
1906 (unsigned short)s->si[1].fd);
1907 write(1, trash, len);
1908 }
1909
1910 if (s->si[0].state == SI_ST_CLO &&
1911 s->si[0].prev_state == SI_ST_EST) {
1912 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1913 s->uniq_id, s->be->id,
1914 (unsigned short)s->si[0].fd,
1915 (unsigned short)s->si[1].fd);
1916 write(1, trash, len);
1917 }
1918 }
1919
1920 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1921 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1922
1923 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1924 session_process_counters(s);
1925
Willy Tarreau1accfc02009-09-05 20:57:35 +02001926 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001927 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001928
Willy Tarreau1accfc02009-09-05 20:57:35 +02001929 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001930 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001931
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001932 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1933 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 +01001934 s->si[0].prev_state = s->si[0].state;
1935 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001936 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1937 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001938
1939 /* Trick: if a request is being waiting for the server to respond,
1940 * and if we know the server can timeout, we don't want the timeout
1941 * to expire on the client side first, but we're still interested
1942 * in passing data from the client to the server (eg: POST). Thus,
1943 * we can cancel the client's request timeout if the server's
1944 * request timeout is set and the server has not yet sent a response.
1945 */
1946
Willy Tarreau520d95e2009-09-19 21:04:57 +02001947 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001948 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1949 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001950 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001951 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001952
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001953 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001954 * Note that this one may wake the task up again.
1955 */
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001956 if (s->req->cons->iohandler || s->rep->cons->iohandler) {
1957 if (s->req->cons->iohandler)
1958 s->req->cons->iohandler(s->req->cons);
1959 if (s->rep->cons->iohandler)
1960 s->rep->cons->iohandler(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02001961 if (task_in_rq(t)) {
1962 /* If we woke up, we don't want to requeue the
1963 * task to the wait queue, but rather requeue
1964 * it into the runqueue ASAP.
1965 */
1966 t->expire = TICK_ETERNITY;
1967 return t;
1968 }
1969 }
1970
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001971 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1972 tick_first(s->rep->rex, s->rep->wex));
1973 if (s->req->analysers)
1974 t->expire = tick_first(t->expire, s->req->analyse_exp);
1975
1976 if (s->si[0].exp)
1977 t->expire = tick_first(t->expire, s->si[0].exp);
1978
1979 if (s->si[1].exp)
1980 t->expire = tick_first(t->expire, s->si[1].exp);
1981
1982#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001983 fprintf(stderr,
1984 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1985 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1986 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1987 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 +01001988#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001989
1990#ifdef DEBUG_DEV
1991 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001992 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001993 ABORT_NOW();
1994#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001995 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001996 }
1997
1998 s->fe->feconn--;
1999 if (s->flags & SN_BE_ASSIGNED)
2000 s->be->beconn--;
2001 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002002 s->listener->nbconn--;
2003 if (s->listener->state == LI_FULL &&
2004 s->listener->nbconn < s->listener->maxconn) {
2005 /* we should reactivate the listener */
2006 EV_FD_SET(s->listener->fd, DIR_RD);
2007 s->listener->state = LI_READY;
2008 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002009
2010 if (unlikely((global.mode & MODE_DEBUG) &&
2011 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2012 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002013 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002014 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002015 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002016 write(1, trash, len);
2017 }
2018
2019 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2020 session_process_counters(s);
2021
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002022 if (s->txn.status) {
2023 int n;
2024
2025 n = s->txn.status / 100;
2026 if (n < 1 || n > 5)
2027 n = 0;
2028
2029 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01002030 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002031
Willy Tarreau24657792010-02-26 10:30:28 +01002032 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002033 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01002034 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002035 }
2036
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002037 /* let's do a final log if we need it */
2038 if (s->logs.logwait &&
2039 !(s->flags & SN_MONITOR) &&
2040 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002041 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002042 }
2043
2044 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002045 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002046 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002047 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002048 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002049}
2050
Willy Tarreau7c669d72008-06-20 15:04:11 +02002051/*
2052 * This function adjusts sess->srv_conn and maintains the previous and new
2053 * server's served session counts. Setting newsrv to NULL is enough to release
2054 * current connection slot. This function also notifies any LB algo which might
2055 * expect to be informed about any change in the number of active sessions on a
2056 * server.
2057 */
2058void sess_change_server(struct session *sess, struct server *newsrv)
2059{
2060 if (sess->srv_conn == newsrv)
2061 return;
2062
2063 if (sess->srv_conn) {
2064 sess->srv_conn->served--;
2065 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2066 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
2067 sess->srv_conn = NULL;
2068 }
2069
2070 if (newsrv) {
2071 newsrv->served++;
2072 if (newsrv->proxy->lbprm.server_take_conn)
2073 newsrv->proxy->lbprm.server_take_conn(newsrv);
2074 sess->srv_conn = newsrv;
2075 }
2076}
2077
Willy Tarreau84455332009-03-15 22:34:05 +01002078/* Set correct session termination flags in case no analyser has done it. It
2079 * also counts a failed request if the server state has not reached the request
2080 * stage.
2081 */
2082void sess_set_term_flags(struct session *s)
2083{
2084 if (!(s->flags & SN_FINST_MASK)) {
2085 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002086
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002087 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002088 if (s->listener->counters)
2089 s->listener->counters->failed_req++;
2090
Willy Tarreau84455332009-03-15 22:34:05 +01002091 s->flags |= SN_FINST_R;
2092 }
2093 else if (s->si[1].state == SI_ST_QUE)
2094 s->flags |= SN_FINST_Q;
2095 else if (s->si[1].state < SI_ST_EST)
2096 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002097 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002098 s->flags |= SN_FINST_D;
2099 else
2100 s->flags |= SN_FINST_L;
2101 }
2102}
2103
2104/* Handle server-side errors for default protocols. It is called whenever a a
2105 * connection setup is aborted or a request is aborted in queue. It sets the
2106 * session termination flags so that the caller does not have to worry about
2107 * them. It's installed as ->srv_error for the server-side stream_interface.
2108 */
2109void default_srv_error(struct session *s, struct stream_interface *si)
2110{
2111 int err_type = si->err_type;
2112 int err = 0, fin = 0;
2113
2114 if (err_type & SI_ET_QUEUE_ABRT) {
2115 err = SN_ERR_CLICL;
2116 fin = SN_FINST_Q;
2117 }
2118 else if (err_type & SI_ET_CONN_ABRT) {
2119 err = SN_ERR_CLICL;
2120 fin = SN_FINST_C;
2121 }
2122 else if (err_type & SI_ET_QUEUE_TO) {
2123 err = SN_ERR_SRVTO;
2124 fin = SN_FINST_Q;
2125 }
2126 else if (err_type & SI_ET_QUEUE_ERR) {
2127 err = SN_ERR_SRVCL;
2128 fin = SN_FINST_Q;
2129 }
2130 else if (err_type & SI_ET_CONN_TO) {
2131 err = SN_ERR_SRVTO;
2132 fin = SN_FINST_C;
2133 }
2134 else if (err_type & SI_ET_CONN_ERR) {
2135 err = SN_ERR_SRVCL;
2136 fin = SN_FINST_C;
2137 }
2138 else /* SI_ET_CONN_OTHER and others */ {
2139 err = SN_ERR_INTERNAL;
2140 fin = SN_FINST_C;
2141 }
2142
2143 if (!(s->flags & SN_ERR_MASK))
2144 s->flags |= err;
2145 if (!(s->flags & SN_FINST_MASK))
2146 s->flags |= fin;
2147}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002148
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002149
Willy Tarreau8b22a712010-06-18 17:46:06 +02002150/************************************************************************/
2151/* All supported ACL keywords must be declared here. */
2152/************************************************************************/
2153
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002154/* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */
2155static int
2156acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2157{
2158 test->flags = ACL_TEST_F_VOL_TEST;
2159 test->i = 0;
2160 if (ts != NULL) {
2161 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2162 if (!ptr)
2163 return 0; /* parameter not stored */
2164 test->i = stktable_data_cast(ptr, gpc0);
2165 }
2166 return 1;
2167}
2168
2169/* set test->i to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002170 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002171 */
2172static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002173acl_fetch_trkfe_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2174 struct acl_expr *expr, struct acl_test *test)
2175{
2176 if (!l4->fe_tracked_counters)
2177 return 0;
2178 return acl_fetch_get_gpc0(l4->fe_tracked_table, test, l4->fe_tracked_counters);
2179}
2180
2181/* set test->i to the General Purpose Counter 0 value from the session's tracked
2182 * backend counters.
2183 */
2184static int
2185acl_fetch_trkbe_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2186 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002187{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002188 if (!l4->be_tracked_counters)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002189 return 0;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002190 return acl_fetch_get_gpc0(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002191}
2192
2193/* set test->i to the General Purpose Counter 0 value from the session's source
2194 * address in the table pointed to by expr.
2195 */
2196static int
2197acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2198 struct acl_expr *expr, struct acl_test *test)
2199{
2200 struct stktable_key *key;
2201
2202 key = tcpv4_src_to_stktable_key(l4);
2203 if (!key)
2204 return 0; /* only TCPv4 is supported right now */
2205
2206 if (expr->arg_len)
2207 px = find_stktable(expr->arg.str);
2208
2209 if (!px)
2210 return 0; /* table not found */
2211
2212 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2213}
2214
2215/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
2216 * return it into test->i.
2217 */
2218static int
2219acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2220{
2221 test->flags = ACL_TEST_F_VOL_TEST;
2222 test->i = 0;
2223 if (ts != NULL) {
2224 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2225 if (!ptr)
2226 return 0; /* parameter not stored */
2227 test->i = ++stktable_data_cast(ptr, gpc0);
2228 }
2229 return 1;
2230}
2231
2232/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002233 * frontend counters and return it into test->i.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002234 */
2235static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002236acl_fetch_trkfe_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2237 struct acl_expr *expr, struct acl_test *test)
2238{
2239 if (!l4->fe_tracked_counters)
2240 return 0;
2241 return acl_fetch_inc_gpc0(l4->fe_tracked_table, test, l4->fe_tracked_counters);
2242}
2243
2244/* Increment the General Purpose Counter 0 value from the session's tracked
2245 * backend counters and return it into test->i.
2246 */
2247static int
2248acl_fetch_trkbe_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2249 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002250{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002251 if (!l4->be_tracked_counters)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002252 return 0;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002253 return acl_fetch_inc_gpc0(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002254}
2255
2256/* Increment the General Purpose Counter 0 value from the session's source
2257 * address in the table pointed to by expr, and return it into test->i.
2258 */
2259static int
2260acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2261 struct acl_expr *expr, struct acl_test *test)
2262{
2263 struct stktable_key *key;
2264
2265 key = tcpv4_src_to_stktable_key(l4);
2266 if (!key)
2267 return 0; /* only TCPv4 is supported right now */
2268
2269 if (expr->arg_len)
2270 px = find_stktable(expr->arg.str);
2271
2272 if (!px)
2273 return 0; /* table not found */
2274
2275 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2276}
2277
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002278/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2279static int
2280acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2281{
2282 test->flags = ACL_TEST_F_VOL_TEST;
2283 test->i = 0;
2284 if (ts != NULL) {
2285 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2286 if (!ptr)
2287 return 0; /* parameter not stored */
2288 test->i = stktable_data_cast(ptr, conn_cnt);
2289 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002290 return 1;
2291}
2292
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002293/* set test->i to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002294static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002295acl_fetch_trkfe_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2296 struct acl_expr *expr, struct acl_test *test)
2297{
2298 if (!l4->fe_tracked_counters)
2299 return 0;
2300
2301 return acl_fetch_conn_cnt(l4->fe_tracked_table, test, l4->fe_tracked_counters);
2302}
2303
2304/* set test->i to the cumulated number of connections from the session's tracked BE counters */
2305static int
2306acl_fetch_trkbe_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2307 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002308{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002309 if (!l4->be_tracked_counters)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002310 return 0;
2311
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002312 return acl_fetch_conn_cnt(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002313}
2314
2315/* set test->i to the cumulated number of connections from the session's source
2316 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002317 */
2318static int
2319acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2320 struct acl_expr *expr, struct acl_test *test)
2321{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002322 struct stktable_key *key;
2323
2324 key = tcpv4_src_to_stktable_key(l4);
2325 if (!key)
2326 return 0; /* only TCPv4 is supported right now */
2327
2328 if (expr->arg_len)
2329 px = find_stktable(expr->arg.str);
2330
2331 if (!px)
2332 return 0; /* table not found */
2333
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002334 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002335}
2336
Willy Tarreau91c43d72010-06-20 11:19:22 +02002337/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2338static int
2339acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2340{
2341 test->flags = ACL_TEST_F_VOL_TEST;
2342 test->i = 0;
2343 if (ts != NULL) {
2344 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2345 if (!ptr)
2346 return 0; /* parameter not stored */
2347 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2348 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2349 }
2350 return 1;
2351}
2352
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002353/* set test->i to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002354 * the configured period.
2355 */
2356static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002357acl_fetch_trkfe_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2358 struct acl_expr *expr, struct acl_test *test)
2359{
2360 if (!l4->fe_tracked_counters)
2361 return 0;
2362
2363 return acl_fetch_conn_rate(l4->fe_tracked_table, test, l4->fe_tracked_counters);
2364}
2365
2366/* set test->i to the connection rate from the session's tracked BE counters over
2367 * the configured period.
2368 */
2369static int
2370acl_fetch_trkbe_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2371 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002372{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002373 if (!l4->be_tracked_counters)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002374 return 0;
2375
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002376 return acl_fetch_conn_rate(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002377}
2378
2379/* set test->i to the connection rate from the session's source address in the
2380 * table pointed to by expr, over the configured period.
2381 */
2382static int
2383acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2384 struct acl_expr *expr, struct acl_test *test)
2385{
2386 struct stktable_key *key;
2387
2388 key = tcpv4_src_to_stktable_key(l4);
2389 if (!key)
2390 return 0; /* only TCPv4 is supported right now */
2391
2392 if (expr->arg_len)
2393 px = find_stktable(expr->arg.str);
2394
2395 if (!px)
2396 return 0; /* table not found */
2397
2398 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2399}
2400
Willy Tarreau8b22a712010-06-18 17:46:06 +02002401/* set test->i to the number of connections from the session's source address
2402 * in the table pointed to by expr, after updating it.
2403 */
2404static int
2405acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2406 struct acl_expr *expr, struct acl_test *test)
2407{
2408 struct stksess *ts;
2409 struct stktable_key *key;
2410 void *ptr;
2411
2412 key = tcpv4_src_to_stktable_key(l4);
2413 if (!key)
2414 return 0; /* only TCPv4 is supported right now */
2415
2416 if (expr->arg_len)
2417 px = find_stktable(expr->arg.str);
2418
2419 if (!px)
2420 return 0; /* table not found */
2421
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002422 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2423 /* entry does not exist and could not be created */
2424 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002425
2426 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2427 if (!ptr)
2428 return 0; /* parameter not stored in this table */
2429
2430 test->i = ++stktable_data_cast(ptr, conn_cnt);
2431 test->flags = ACL_TEST_F_VOL_TEST;
2432 return 1;
2433}
2434
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002435/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2436static int
2437acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2438{
2439 test->flags = ACL_TEST_F_VOL_TEST;
2440 test->i = 0;
2441
2442 if (ts != NULL) {
2443 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2444 if (!ptr)
2445 return 0; /* parameter not stored */
2446 test->i = stktable_data_cast(ptr, conn_cur);
2447 }
2448 return 1;
2449}
2450
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002451/* set test->i to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002452static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002453acl_fetch_trkfe_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2454 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002455{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002456 if (!l4->fe_tracked_counters)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002457 return 0;
2458
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002459 return acl_fetch_conn_cur(l4->fe_tracked_table, test, l4->fe_tracked_counters);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002460}
2461
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002462/* set test->i to the number of concurrent connections from the session's tracked BE counters */
2463static int
2464acl_fetch_trkbe_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2465 struct acl_expr *expr, struct acl_test *test)
2466{
2467 if (!l4->be_tracked_counters)
2468 return 0;
2469
2470 return acl_fetch_conn_cur(l4->be_tracked_table, test, l4->be_tracked_counters);
2471}
2472
Willy Tarreau38285c12010-06-18 16:35:43 +02002473/* set test->i to the number of concurrent connections from the session's source
2474 * address in the table pointed to by expr.
2475 */
2476static int
2477acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2478 struct acl_expr *expr, struct acl_test *test)
2479{
Willy Tarreau38285c12010-06-18 16:35:43 +02002480 struct stktable_key *key;
2481
2482 key = tcpv4_src_to_stktable_key(l4);
2483 if (!key)
2484 return 0; /* only TCPv4 is supported right now */
2485
2486 if (expr->arg_len)
2487 px = find_stktable(expr->arg.str);
2488
2489 if (!px)
2490 return 0; /* table not found */
2491
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002492 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002493}
2494
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002495/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2496static int
2497acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2498{
2499 test->flags = ACL_TEST_F_VOL_TEST;
2500 test->i = 0;
2501 if (ts != NULL) {
2502 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2503 if (!ptr)
2504 return 0; /* parameter not stored */
2505 test->i = stktable_data_cast(ptr, sess_cnt);
2506 }
2507 return 1;
2508}
2509
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002510/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002511static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002512acl_fetch_trkfe_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2513 struct acl_expr *expr, struct acl_test *test)
2514{
2515 if (!l4->fe_tracked_counters)
2516 return 0;
2517
2518 return acl_fetch_sess_cnt(l4->fe_tracked_table, test, l4->fe_tracked_counters);
2519}
2520
2521/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2522static int
2523acl_fetch_trkbe_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2524 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002525{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002526 if (!l4->be_tracked_counters)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002527 return 0;
2528
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002529 return acl_fetch_sess_cnt(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002530}
2531
2532/* set test->i to the cumulated number of session from the session's source
2533 * address in the table pointed to by expr.
2534 */
2535static int
2536acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2537 struct acl_expr *expr, struct acl_test *test)
2538{
2539 struct stktable_key *key;
2540
2541 key = tcpv4_src_to_stktable_key(l4);
2542 if (!key)
2543 return 0; /* only TCPv4 is supported right now */
2544
2545 if (expr->arg_len)
2546 px = find_stktable(expr->arg.str);
2547
2548 if (!px)
2549 return 0; /* table not found */
2550
2551 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2552}
2553
Willy Tarreau91c43d72010-06-20 11:19:22 +02002554/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2555static int
2556acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2557{
2558 test->flags = ACL_TEST_F_VOL_TEST;
2559 test->i = 0;
2560 if (ts != NULL) {
2561 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2562 if (!ptr)
2563 return 0; /* parameter not stored */
2564 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2565 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2566 }
2567 return 1;
2568}
2569
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002570/* set test->i to the session rate from the session's tracked FE counters over
2571 * the configured period.
2572 */
2573static int
2574acl_fetch_trkfe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2575 struct acl_expr *expr, struct acl_test *test)
2576{
2577 if (!l4->fe_tracked_counters)
2578 return 0;
2579
2580 return acl_fetch_sess_rate(l4->fe_tracked_table, test, l4->fe_tracked_counters);
2581}
2582
2583/* set test->i to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002584 * the configured period.
2585 */
2586static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002587acl_fetch_trkbe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau91c43d72010-06-20 11:19:22 +02002588 struct acl_expr *expr, struct acl_test *test)
2589{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002590 if (!l4->be_tracked_counters)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002591 return 0;
2592
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002593 return acl_fetch_sess_rate(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002594}
2595
2596/* set test->i to the session rate from the session's source address in the
2597 * table pointed to by expr, over the configured period.
2598 */
2599static int
2600acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2601 struct acl_expr *expr, struct acl_test *test)
2602{
2603 struct stktable_key *key;
2604
2605 key = tcpv4_src_to_stktable_key(l4);
2606 if (!key)
2607 return 0; /* only TCPv4 is supported right now */
2608
2609 if (expr->arg_len)
2610 px = find_stktable(expr->arg.str);
2611
2612 if (!px)
2613 return 0; /* table not found */
2614
2615 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2616}
2617
Willy Tarreauda7ff642010-06-23 11:44:09 +02002618/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2619static int
2620acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2621{
2622 test->flags = ACL_TEST_F_VOL_TEST;
2623 test->i = 0;
2624 if (ts != NULL) {
2625 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2626 if (!ptr)
2627 return 0; /* parameter not stored */
2628 test->i = stktable_data_cast(ptr, http_req_cnt);
2629 }
2630 return 1;
2631}
2632
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002633/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002634static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002635acl_fetch_trkfe_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2636 struct acl_expr *expr, struct acl_test *test)
2637{
2638 if (!l4->fe_tracked_counters)
2639 return 0;
2640
2641 return acl_fetch_http_req_cnt(l4->fe_tracked_table, test, l4->fe_tracked_counters);
2642}
2643
2644/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2645static int
2646acl_fetch_trkbe_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2647 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002648{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002649 if (!l4->be_tracked_counters)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002650 return 0;
2651
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002652 return acl_fetch_http_req_cnt(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002653}
2654
2655/* set test->i to the cumulated number of session from the session's source
2656 * address in the table pointed to by expr.
2657 */
2658static int
2659acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2660 struct acl_expr *expr, struct acl_test *test)
2661{
2662 struct stktable_key *key;
2663
2664 key = tcpv4_src_to_stktable_key(l4);
2665 if (!key)
2666 return 0; /* only TCPv4 is supported right now */
2667
2668 if (expr->arg_len)
2669 px = find_stktable(expr->arg.str);
2670
2671 if (!px)
2672 return 0; /* table not found */
2673
2674 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2675}
2676
2677/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2678static int
2679acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2680{
2681 test->flags = ACL_TEST_F_VOL_TEST;
2682 test->i = 0;
2683 if (ts != NULL) {
2684 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2685 if (!ptr)
2686 return 0; /* parameter not stored */
2687 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2688 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2689 }
2690 return 1;
2691}
2692
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002693/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002694 * the configured period.
2695 */
2696static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002697acl_fetch_trkfe_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2698 struct acl_expr *expr, struct acl_test *test)
2699{
2700 if (!l4->fe_tracked_counters)
2701 return 0;
2702
2703 return acl_fetch_http_req_rate(l4->fe_tracked_table, test, l4->fe_tracked_counters);
2704}
2705
2706/* set test->i to the session rate from the session's tracked BE counters over
2707 * the configured period.
2708 */
2709static int
2710acl_fetch_trkbe_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2711 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002712{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002713 if (!l4->be_tracked_counters)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002714 return 0;
2715
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002716 return acl_fetch_http_req_rate(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002717}
2718
2719/* set test->i to the session rate from the session's source address in the
2720 * table pointed to by expr, over the configured period.
2721 */
2722static int
2723acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2724 struct acl_expr *expr, struct acl_test *test)
2725{
2726 struct stktable_key *key;
2727
2728 key = tcpv4_src_to_stktable_key(l4);
2729 if (!key)
2730 return 0; /* only TCPv4 is supported right now */
2731
2732 if (expr->arg_len)
2733 px = find_stktable(expr->arg.str);
2734
2735 if (!px)
2736 return 0; /* table not found */
2737
2738 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2739}
2740
2741/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2742static int
2743acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2744{
2745 test->flags = ACL_TEST_F_VOL_TEST;
2746 test->i = 0;
2747 if (ts != NULL) {
2748 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2749 if (!ptr)
2750 return 0; /* parameter not stored */
2751 test->i = stktable_data_cast(ptr, http_err_cnt);
2752 }
2753 return 1;
2754}
2755
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002756/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002757static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002758acl_fetch_trkfe_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2759 struct acl_expr *expr, struct acl_test *test)
2760{
2761 if (!l4->fe_tracked_counters)
2762 return 0;
2763
2764 return acl_fetch_http_err_cnt(l4->fe_tracked_table, test, l4->fe_tracked_counters);
2765}
2766
2767/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2768static int
2769acl_fetch_trkbe_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2770 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002771{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002772 if (!l4->be_tracked_counters)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002773 return 0;
2774
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002775 return acl_fetch_http_err_cnt(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002776}
2777
2778/* set test->i to the cumulated number of session from the session's source
2779 * address in the table pointed to by expr.
2780 */
2781static int
2782acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2783 struct acl_expr *expr, struct acl_test *test)
2784{
2785 struct stktable_key *key;
2786
2787 key = tcpv4_src_to_stktable_key(l4);
2788 if (!key)
2789 return 0; /* only TCPv4 is supported right now */
2790
2791 if (expr->arg_len)
2792 px = find_stktable(expr->arg.str);
2793
2794 if (!px)
2795 return 0; /* table not found */
2796
2797 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2798}
2799
2800/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2801static int
2802acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2803{
2804 test->flags = ACL_TEST_F_VOL_TEST;
2805 test->i = 0;
2806 if (ts != NULL) {
2807 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2808 if (!ptr)
2809 return 0; /* parameter not stored */
2810 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2811 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2812 }
2813 return 1;
2814}
2815
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002816/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002817 * the configured period.
2818 */
2819static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002820acl_fetch_trkfe_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2821 struct acl_expr *expr, struct acl_test *test)
2822{
2823 if (!l4->fe_tracked_counters)
2824 return 0;
2825
2826 return acl_fetch_http_err_rate(l4->fe_tracked_table, test, l4->fe_tracked_counters);
2827}
2828
2829/* set test->i to the session rate from the session's tracked BE counters over
2830 * the configured period.
2831 */
2832static int
2833acl_fetch_trkbe_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2834 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002835{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002836 if (!l4->be_tracked_counters)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002837 return 0;
2838
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002839 return acl_fetch_http_err_rate(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002840}
2841
2842/* set test->i to the session rate from the session's source address in the
2843 * table pointed to by expr, over the configured period.
2844 */
2845static int
2846acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2847 struct acl_expr *expr, struct acl_test *test)
2848{
2849 struct stktable_key *key;
2850
2851 key = tcpv4_src_to_stktable_key(l4);
2852 if (!key)
2853 return 0; /* only TCPv4 is supported right now */
2854
2855 if (expr->arg_len)
2856 px = find_stktable(expr->arg.str);
2857
2858 if (!px)
2859 return 0; /* table not found */
2860
2861 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2862}
2863
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002864/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2865static int
2866acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2867{
2868 test->flags = ACL_TEST_F_VOL_TEST;
2869 test->i = 0;
2870
2871 if (ts != NULL) {
2872 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2873 if (!ptr)
2874 return 0; /* parameter not stored */
2875 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2876 }
2877 return 1;
2878}
2879
2880/* set test->i to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002881 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002882 */
2883static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002884acl_fetch_trkfe_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2885 struct acl_expr *expr, struct acl_test *test)
2886{
2887 if (!l4->fe_tracked_counters)
2888 return 0;
2889
2890 return acl_fetch_kbytes_in(l4->fe_tracked_table, test, l4->fe_tracked_counters);
2891}
2892
2893/* set test->i to the number of kbytes received from clients according to the
2894 * session's tracked BE counters.
2895 */
2896static int
2897acl_fetch_trkbe_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2898 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002899{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002900 if (!l4->be_tracked_counters)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002901 return 0;
2902
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002903 return acl_fetch_kbytes_in(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002904}
2905
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002906/* set test->i to the number of kbytes received from the session's source
2907 * address in the table pointed to by expr.
2908 */
2909static int
2910acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2911 struct acl_expr *expr, struct acl_test *test)
2912{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002913 struct stktable_key *key;
2914
2915 key = tcpv4_src_to_stktable_key(l4);
2916 if (!key)
2917 return 0; /* only TCPv4 is supported right now */
2918
2919 if (expr->arg_len)
2920 px = find_stktable(expr->arg.str);
2921
2922 if (!px)
2923 return 0; /* table not found */
2924
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002925 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2926}
2927
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002928/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2929 * configured period.
2930 */
2931static int
2932acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2933{
2934 test->flags = ACL_TEST_F_VOL_TEST;
2935 test->i = 0;
2936 if (ts != NULL) {
2937 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
2938 if (!ptr)
2939 return 0; /* parameter not stored */
2940 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2941 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2942 }
2943 return 1;
2944}
2945
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002946/* set test->i to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002947 * counters over the configured period.
2948 */
2949static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002950acl_fetch_trkfe_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2951 struct acl_expr *expr, struct acl_test *test)
2952{
2953 if (!l4->fe_tracked_counters)
2954 return 0;
2955
2956 return acl_fetch_bytes_in_rate(l4->fe_tracked_table, test, l4->fe_tracked_counters);
2957}
2958
2959/* set test->i to the bytes rate from clients from the session's tracked BE
2960 * counters over the configured period.
2961 */
2962static int
2963acl_fetch_trkbe_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2964 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002965{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002966 if (!l4->be_tracked_counters)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002967 return 0;
2968
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002969 return acl_fetch_bytes_in_rate(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002970}
2971
2972/* set test->i to the bytes rate from clients from the session's source address
2973 * in the table pointed to by expr, over the configured period.
2974 */
2975static int
2976acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2977 struct acl_expr *expr, struct acl_test *test)
2978{
2979 struct stktable_key *key;
2980
2981 key = tcpv4_src_to_stktable_key(l4);
2982 if (!key)
2983 return 0; /* only TCPv4 is supported right now */
2984
2985 if (expr->arg_len)
2986 px = find_stktable(expr->arg.str);
2987
2988 if (!px)
2989 return 0; /* table not found */
2990
2991 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2992}
2993
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002994/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
2995static int
2996acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
2997{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002998 test->flags = ACL_TEST_F_VOL_TEST;
2999 test->i = 0;
3000
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003001 if (ts != NULL) {
3002 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003003 if (!ptr)
3004 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003005 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003006 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003007 return 1;
3008}
3009
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003010/* set test->i to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003011 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003012 */
3013static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003014acl_fetch_trkfe_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3015 struct acl_expr *expr, struct acl_test *test)
3016{
3017 if (!l4->fe_tracked_counters)
3018 return 0;
3019
3020 return acl_fetch_kbytes_out(l4->fe_tracked_table, test, l4->fe_tracked_counters);
3021}
3022
3023/* set test->i to the number of kbytes sent to clients according to the session's
3024 * tracked BE counters.
3025 */
3026static int
3027acl_fetch_trkbe_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3028 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003029{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003030 if (!l4->be_tracked_counters)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003031 return 0;
3032
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003033 return acl_fetch_kbytes_out(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003034}
3035
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003036/* set test->i to the number of kbytes sent to the session's source address in
3037 * the table pointed to by expr.
3038 */
3039static int
3040acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3041 struct acl_expr *expr, struct acl_test *test)
3042{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003043 struct stktable_key *key;
3044
3045 key = tcpv4_src_to_stktable_key(l4);
3046 if (!key)
3047 return 0; /* only TCPv4 is supported right now */
3048
3049 if (expr->arg_len)
3050 px = find_stktable(expr->arg.str);
3051
3052 if (!px)
3053 return 0; /* table not found */
3054
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003055 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003056}
3057
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003058/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
3059 * configured period.
3060 */
3061static int
3062acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3063{
3064 test->flags = ACL_TEST_F_VOL_TEST;
3065 test->i = 0;
3066 if (ts != NULL) {
3067 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3068 if (!ptr)
3069 return 0; /* parameter not stored */
3070 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3071 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3072 }
3073 return 1;
3074}
3075
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003076/* set test->i to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003077 * over the configured period.
3078 */
3079static int
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003080acl_fetch_trkfe_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3081 struct acl_expr *expr, struct acl_test *test)
3082{
3083 if (!l4->fe_tracked_counters)
3084 return 0;
3085
3086 return acl_fetch_bytes_out_rate(l4->fe_tracked_table, test, l4->fe_tracked_counters);
3087}
3088
3089/* set test->i to the bytes rate to clients from the session's tracked BE counters
3090 * over the configured period.
3091 */
3092static int
3093acl_fetch_trkbe_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3094 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003095{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003096 if (!l4->be_tracked_counters)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003097 return 0;
3098
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003099 return acl_fetch_bytes_out_rate(l4->be_tracked_table, test, l4->be_tracked_counters);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003100}
3101
3102/* set test->i to the bytes rate to client from the session's source address in
3103 * the table pointed to by expr, over the configured period.
3104 */
3105static int
3106acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3107 struct acl_expr *expr, struct acl_test *test)
3108{
3109 struct stktable_key *key;
3110
3111 key = tcpv4_src_to_stktable_key(l4);
3112 if (!key)
3113 return 0; /* only TCPv4 is supported right now */
3114
3115 if (expr->arg_len)
3116 px = find_stktable(expr->arg.str);
3117
3118 if (!px)
3119 return 0; /* table not found */
3120
3121 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3122}
3123
Willy Tarreau8b22a712010-06-18 17:46:06 +02003124
3125/* Note: must not be declared <const> as its list will be overwritten */
3126static struct acl_kw_list acl_kws = {{ },{
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003127 { "trkfe_get_gpc0", acl_parse_int, acl_fetch_trkfe_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3128 { "trkbe_get_gpc0", acl_parse_int, acl_fetch_trkbe_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3129 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3130 { "trkfe_inc_gpc0", acl_parse_int, acl_fetch_trkfe_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3131 { "trkbe_inc_gpc0", acl_parse_int, acl_fetch_trkbe_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3132 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3133 { "trkfe_conn_cnt", acl_parse_int, acl_fetch_trkfe_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3134 { "trkbe_conn_cnt", acl_parse_int, acl_fetch_trkbe_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3135 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3136 { "trkfe_conn_rate", acl_parse_int, acl_fetch_trkfe_conn_rate, acl_match_int, ACL_USE_NOTHING },
3137 { "trkbe_conn_rate", acl_parse_int, acl_fetch_trkbe_conn_rate, acl_match_int, ACL_USE_NOTHING },
3138 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3139 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3140 { "trkfe_conn_cur", acl_parse_int, acl_fetch_trkfe_conn_cur, acl_match_int, ACL_USE_NOTHING },
3141 { "trkbe_conn_cur", acl_parse_int, acl_fetch_trkbe_conn_cur, acl_match_int, ACL_USE_NOTHING },
3142 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3143 { "trkfe_sess_cnt", acl_parse_int, acl_fetch_trkfe_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3144 { "trkbe_sess_cnt", acl_parse_int, acl_fetch_trkbe_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3145 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3146 { "trkfe_sess_rate", acl_parse_int, acl_fetch_trkfe_sess_rate, acl_match_int, ACL_USE_NOTHING },
3147 { "trkbe_sess_rate", acl_parse_int, acl_fetch_trkbe_sess_rate, acl_match_int, ACL_USE_NOTHING },
3148 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3149 { "trkfe_http_req_cnt", acl_parse_int, acl_fetch_trkfe_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3150 { "trkbe_http_req_cnt", acl_parse_int, acl_fetch_trkbe_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3151 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3152 { "trkfe_http_req_rate", acl_parse_int, acl_fetch_trkfe_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3153 { "trkbe_http_req_rate", acl_parse_int, acl_fetch_trkbe_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3154 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3155 { "trkfe_http_err_cnt", acl_parse_int, acl_fetch_trkfe_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3156 { "trkbe_http_err_cnt", acl_parse_int, acl_fetch_trkbe_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3157 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3158 { "trkfe_http_err_rate", acl_parse_int, acl_fetch_trkfe_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3159 { "trkbe_http_err_rate", acl_parse_int, acl_fetch_trkbe_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3160 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3161 { "trkfe_kbytes_in", acl_parse_int, acl_fetch_trkfe_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3162 { "trkbe_kbytes_in", acl_parse_int, acl_fetch_trkbe_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3163 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3164 { "trkfe_bytes_in_rate", acl_parse_int, acl_fetch_trkfe_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3165 { "trkbe_bytes_in_rate", acl_parse_int, acl_fetch_trkbe_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3166 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3167 { "trkfe_kbytes_out", acl_parse_int, acl_fetch_trkfe_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3168 { "trkbe_kbytes_out", acl_parse_int, acl_fetch_trkbe_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3169 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3170 { "trkfe_bytes_out_rate", acl_parse_int, acl_fetch_trkfe_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3171 { "trkbe_bytes_out_rate", acl_parse_int, acl_fetch_trkbe_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3172 { "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 +02003173 { NULL, NULL, NULL, NULL },
3174}};
3175
3176
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003177/* Parse a "track-counters" line starting with "track-counters" in args[arg-1].
3178 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3179 * <prm> struct is fed with the table name if any. If unspecified, the caller
3180 * will assume that the current proxy's table is used.
3181 */
3182int parse_track_counters(char **args, int *arg,
3183 int section_type, struct proxy *curpx,
3184 struct track_ctr_prm *prm,
3185 struct proxy *defpx, char *err, int errlen)
3186{
3187 int pattern_type = 0;
3188
3189 /* parse the arguments of "track-counters" before the condition in the
3190 * following form :
3191 * track-counters src [ table xxx ] [ if|unless ... ]
3192 */
3193 while (args[*arg]) {
3194 if (strcmp(args[*arg], "src") == 0) {
3195 prm->type = STKTABLE_TYPE_IP;
3196 pattern_type = 1;
3197 }
3198 else if (strcmp(args[*arg], "table") == 0) {
3199 if (!args[*arg + 1]) {
3200 snprintf(err, errlen,
3201 "missing table for track-counter in %s '%s'.",
3202 proxy_type_str(curpx), curpx->id);
3203 return -1;
3204 }
3205 /* we copy the table name for now, it will be resolved later */
3206 prm->table.n = strdup(args[*arg + 1]);
3207 (*arg)++;
3208 }
3209 else {
3210 /* unhandled keywords are handled by the caller */
3211 break;
3212 }
3213 (*arg)++;
3214 }
3215
3216 if (!pattern_type) {
3217 snprintf(err, errlen,
3218 "track-counter key not specified in %s '%s' (found %s, only 'src' is supported).",
3219 proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
3220 return -1;
3221 }
3222
3223 return 0;
3224}
3225
Willy Tarreau8b22a712010-06-18 17:46:06 +02003226__attribute__((constructor))
3227static void __session_init(void)
3228{
3229 acl_register_keywords(&acl_kws);
3230}
3231
Willy Tarreaubaaee002006-06-26 02:48:02 +02003232/*
3233 * Local variables:
3234 * c-indent-level: 8
3235 * c-basic-offset: 8
3236 * End:
3237 */