blob: af3a4644208cf0b4ca8274ec238f722629aa5ca8 [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 Tarreaud28c3532012-04-19 19:28:33 +02004 * Copyright 2000-2012 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 Tarreau61612d42012-04-19 18:42:05 +020025#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010026#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020027#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010028#include <proto/checks.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020029#include <proto/dumpstats.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020030#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020031#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010032#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020033#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010035#include <proto/pipe.h>
Willy Tarreau62793712011-07-24 19:23:38 +020036#include <proto/protocols.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010037#include <proto/proto_http.h>
38#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020039#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010041#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020042#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010043#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010044#include <proto/stream_interface.h>
45#include <proto/stream_sock.h>
46#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020048struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010049struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Willy Tarreau81f9aa32010-06-01 17:45:26 +020051/* This function is called from the protocol layer accept() in order to instanciate
52 * a new session on behalf of a given listener and frontend. It returns a positive
Willy Tarreauabe8ea52010-11-11 10:56:04 +010053 * value upon success, 0 if the connection can be ignored, or a negative value upon
54 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020055 */
56int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
57{
58 struct proxy *p = l->frontend;
59 struct session *s;
60 struct http_txn *txn;
61 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010062 int ret;
63
64
65 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020066
Willy Tarreaufffe1322010-11-11 09:48:16 +010067 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020068 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020069
70 /* minimum session initialization required for monitor mode below */
71 s->flags = 0;
72 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020073 s->stkctr1_entry = NULL;
74 s->stkctr2_entry = NULL;
75 s->stkctr1_table = NULL;
76 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020077
78 /* if this session comes from a known monitoring system, we want to ignore
79 * it as soon as possible, which means closing it immediately for TCP, but
80 * cleanly.
81 */
82 if (unlikely((l->options & LI_O_CHK_MONNET) &&
83 addr->ss_family == AF_INET &&
84 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
85 if (p->mode == PR_MODE_TCP) {
Willy Tarreauabe8ea52010-11-11 10:56:04 +010086 ret = 0; /* successful termination */
87 goto out_free_session;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020088 }
89 s->flags |= SN_MONITOR;
90 s->logs.logwait = 0;
91 }
92
Willy Tarreauabe8ea52010-11-11 10:56:04 +010093 if (unlikely((t = task_new()) == NULL))
94 goto out_free_session;
95
Willy Tarreau81f9aa32010-06-01 17:45:26 +020096 /* OK, we're keeping the session, so let's properly initialize the session */
97 LIST_ADDQ(&sessions, &s->list);
98 LIST_INIT(&s->back_refs);
99
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200100 s->term_trace = 0;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200101 s->si[0].addr.from = *addr;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200102 s->logs.accept_date = date; /* user-visible date for logging */
103 s->logs.tv_accept = now; /* corrected date for internal use */
104 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200105 p->feconn++; /* beconn will be increased once assigned */
106
Willy Tarreaub36b4242010-06-04 20:59:39 +0200107 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200108
109 t->process = l->handler;
110 t->context = s;
111 t->nice = l->nice;
112 t->expire = TICK_ETERNITY;
113
114 s->task = t;
115 s->listener = l;
116
117 /* Note: initially, the session's backend points to the frontend.
118 * This changes later when switching rules are executed or
119 * when the default backend is assigned.
120 */
121 s->be = s->fe = p;
122 s->req = s->rep = NULL; /* will be allocated later */
123
124 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
125 * to abort right here as soon as possible, we check the rules before
126 * even initializing the stream interfaces.
127 */
128 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200129 /* let's do a no-linger now to close with a single RST. */
130 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100131 ret = 0; /* successful termination */
132 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200133 }
134
Willy Tarreaub36b4242010-06-04 20:59:39 +0200135 /* This session was accepted, count it now */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100136 if (p->feconn > p->fe_counters.conn_max)
137 p->fe_counters.conn_max = p->feconn;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100138
Willy Tarreaub36b4242010-06-04 20:59:39 +0200139 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200140 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200141 void *ptr;
142
Willy Tarreau56123282010-08-06 19:06:56 +0200143 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, 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 Tarreau56123282010-08-06 19:06:56 +0200147 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, 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 Tarreau56123282010-08-06 19:06:56 +0200150 s->stkctr1_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 Tarreau56123282010-08-06 19:06:56 +0200153 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200154 void *ptr;
155
Willy Tarreau56123282010-08-06 19:06:56 +0200156 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200157 if (ptr)
158 stktable_data_cast(ptr, sess_cnt)++;
159
Willy Tarreau56123282010-08-06 19:06:56 +0200160 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200161 if (ptr)
162 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200163 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200164 }
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;
Willy Tarreau26d8c592012-05-07 18:12:14 +0200172 s->si[0].proto = l->proto;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200173 s->si[0].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100174 clear_target(&s->si[0].target);
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 */
Willy Tarreau5c979a92012-05-07 17:15:39 +0200185 stream_interface_prepare(&s->si[0], &stream_sock);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200186
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;
Willy Tarreau0b3a4112011-03-27 19:16:56 +0200194 s->si[1].conn_retries = 0; /* used for logging too */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200195 s->si[1].err_loc = NULL;
Willy Tarreau26d8c592012-05-07 18:12:14 +0200196 s->si[1].proto = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200197 s->si[1].release = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100198 clear_target(&s->si[1].target);
Willy Tarreau060781f2012-05-07 16:50:03 +0200199 s->si[1].sock.shutr= stream_int_shutr;
200 s->si[1].sock.shutw= stream_int_shutw;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200201 s->si[1].exp = TICK_ETERNITY;
202 s->si[1].flags = SI_FL_NONE;
203
204 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
205 s->si[1].flags |= SI_FL_INDEP_STR;
206
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200207 session_init_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +0100208 clear_target(&s->target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200209 s->pend_pos = NULL;
210
211 /* init store persistence */
212 s->store_count = 0;
213
214 /* Adjust some socket options */
Willy Tarreaufffe1322010-11-11 09:48:16 +0100215 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200216 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200217
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200218 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
219 goto out_free_task; /* no memory */
220
221 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
222 goto out_free_req; /* no memory */
223
224 /* initialize the request buffer */
225 s->req->size = global.tune.bufsize;
226 buffer_init(s->req);
227 s->req->prod = &s->si[0];
228 s->req->cons = &s->si[1];
229 s->si[0].ib = s->si[1].ob = s->req;
230 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
231
232 /* activate default analysers enabled for this listener */
233 s->req->analysers = l->analysers;
234
235 s->req->wto = TICK_ETERNITY;
236 s->req->rto = TICK_ETERNITY;
237 s->req->rex = TICK_ETERNITY;
238 s->req->wex = TICK_ETERNITY;
239 s->req->analyse_exp = TICK_ETERNITY;
240
241 /* initialize response buffer */
242 s->rep->size = global.tune.bufsize;
243 buffer_init(s->rep);
244 s->rep->prod = &s->si[1];
245 s->rep->cons = &s->si[0];
246 s->si[0].ob = s->si[1].ib = s->rep;
247 s->rep->analysers = 0;
248
Willy Tarreau96e31212011-05-30 18:10:30 +0200249 if (s->fe->options2 & PR_O2_NODELAY) {
250 s->req->flags |= BF_NEVER_WAIT;
251 s->rep->flags |= BF_NEVER_WAIT;
252 }
253
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200254 s->rep->rto = TICK_ETERNITY;
255 s->rep->wto = TICK_ETERNITY;
256 s->rep->rex = TICK_ETERNITY;
257 s->rep->wex = TICK_ETERNITY;
258 s->rep->analyse_exp = TICK_ETERNITY;
259
Willy Tarreau62f791e2012-03-09 11:32:30 +0100260 txn = &s->txn;
261 /* Those variables will be checked and freed if non-NULL in
262 * session.c:session_free(). It is important that they are
263 * properly initialized.
264 */
265 txn->sessid = NULL;
266 txn->srv_cookie = NULL;
267 txn->cli_cookie = NULL;
268 txn->uri = NULL;
269 txn->req.cap = NULL;
270 txn->rsp.cap = NULL;
271 txn->hdr_idx.v = NULL;
272 txn->hdr_idx.size = txn->hdr_idx.used = 0;
273 txn->req.flags = 0;
274 txn->rsp.flags = 0;
275 /* the HTTP messages need to know what buffer they're associated with */
276 txn->req.buf = s->req;
277 txn->rsp.buf = s->rep;
278
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200279 /* finish initialization of the accepted file descriptor */
280 fd_insert(cfd);
281 fdtab[cfd].owner = &s->si[0];
282 fdtab[cfd].state = FD_STREADY;
283 fdtab[cfd].flags = 0;
Willy Tarreau1b79bde2012-05-07 17:01:39 +0200284 fdtab[cfd].cb[DIR_RD].f = s->si[0].sock.read;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200285 fdtab[cfd].cb[DIR_RD].b = s->req;
Willy Tarreau1b79bde2012-05-07 17:01:39 +0200286 fdtab[cfd].cb[DIR_WR].f = s->si[0].sock.write;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200287 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200288 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->si[0].addr.from;
289 fdinfo[cfd].peerlen = sizeof(s->si[0].addr.from);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200290 EV_FD_SET(cfd, DIR_RD);
291
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100292 if (p->accept && (ret = p->accept(s)) <= 0) {
293 /* Either we had an unrecoverable error (<0) or work is
294 * finished (=0, eg: monitoring), in both situations,
295 * we can release everything and close.
296 */
297 goto out_free_rep;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200298 }
299
300 /* it is important not to call the wakeup function directly but to
301 * pass through task_wakeup(), because this one knows how to apply
302 * priorities to tasks.
303 */
304 task_wakeup(t, TASK_WOKEN_INIT);
305 return 1;
306
307 /* Error unrolling */
308 out_free_rep:
309 pool_free2(pool2_buffer, s->rep);
310 out_free_req:
311 pool_free2(pool2_buffer, s->req);
312 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200313 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200314 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200315 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200316 task_free(t);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200317 LIST_DEL(&s->list);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100318 out_free_session:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200319 pool_free2(pool2_session, s);
320 out_close:
Willy Tarreau2b154922011-07-22 17:36:27 +0200321 if (ret < 0 && s->fe->mode == PR_MODE_HTTP) {
322 /* critical error, no more memory, try to emit a 500 response */
323 struct chunk *err_msg = error_message(s, HTTP_ERR_500);
324 send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
325 }
326
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100327 if (fdtab[cfd].state != FD_STCLOSE)
328 fd_delete(cfd);
329 else
330 close(cfd);
331 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200332}
333
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334/*
335 * frees the context associated to a session. It must have been removed first.
336 */
Simon Hormandec5be42011-06-08 09:19:07 +0900337static void session_free(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200338{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100339 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200340 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100341 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200342 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100343
Willy Tarreaubaaee002006-06-26 02:48:02 +0200344 if (s->pend_pos)
345 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100346
Willy Tarreau827aee92011-03-10 16:55:02 +0100347 if (target_srv(&s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100348 if (s->flags & SN_CURR_SESS) {
349 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100350 target_srv(&s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100351 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100352 if (may_dequeue_tasks(target_srv(&s->target), s->be))
353 process_srv_queue(target_srv(&s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100354 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100355
Willy Tarreau7c669d72008-06-20 15:04:11 +0200356 if (unlikely(s->srv_conn)) {
357 /* the session still has a reserved slot on a server, but
358 * it should normally be only the same as the one above,
359 * so this should not happen in fact.
360 */
361 sess_change_server(s, NULL);
362 }
363
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100364 if (s->req->pipe)
365 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100366
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100367 if (s->rep->pipe)
368 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100369
Willy Tarreau48d63db2008-08-03 17:41:33 +0200370 pool_free2(pool2_buffer, s->req);
371 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372
Willy Tarreau46023632010-01-07 22:51:47 +0100373 http_end_txn(s);
374
Willy Tarreaua4cda672010-06-06 18:28:49 +0200375 for (i = 0; i < s->store_count; i++) {
376 if (!s->store[i].ts)
377 continue;
378 stksess_free(s->store[i].table, s->store[i].ts);
379 s->store[i].ts = NULL;
380 }
381
Willy Tarreau34eb6712011-10-24 18:15:04 +0200382 pool_free2(pool2_hdr_idx, txn->hdr_idx.v);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200383 if (fe) {
Willy Tarreau46023632010-01-07 22:51:47 +0100384 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
385 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100387
Willy Tarreau56123282010-08-06 19:06:56 +0200388 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200389 session_store_counters(s);
390
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100391 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100392 /* we have to unlink all watchers. We must not relink them if
393 * this session was the last one in the list.
394 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100395 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100396 LIST_INIT(&bref->users);
397 if (s->list.n != &sessions)
398 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100399 bref->ref = s->list.n;
400 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100401 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200402 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200403
404 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200405 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200406 pool_flush2(pool2_buffer);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200407 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200408 pool_flush2(pool2_requri);
409 pool_flush2(pool2_capture);
410 pool_flush2(pool2_session);
411 pool_flush2(fe->req_cap_pool);
412 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200413 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200414}
415
416
417/* perform minimal intializations, report 0 in case of error, 1 if OK. */
418int init_session()
419{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100420 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200421 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
422 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200423}
424
Willy Tarreau30e71012007-11-26 20:15:35 +0100425void session_process_counters(struct session *s)
426{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100427 unsigned long long bytes;
428
Willy Tarreau30e71012007-11-26 20:15:35 +0100429 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100430 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100431 s->logs.bytes_in = s->req->total;
432 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100433 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100434
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100435 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100436
Willy Tarreau827aee92011-03-10 16:55:02 +0100437 if (target_srv(&s->target))
438 target_srv(&s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200439
440 if (s->listener->counters)
441 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200442
Willy Tarreau56123282010-08-06 19:06:56 +0200443 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200444 void *ptr;
445
Willy Tarreau56123282010-08-06 19:06:56 +0200446 ptr = stktable_data_ptr(s->stkctr2_table,
447 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200448 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200449 if (ptr)
450 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200451
Willy Tarreau56123282010-08-06 19:06:56 +0200452 ptr = stktable_data_ptr(s->stkctr2_table,
453 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200454 STKTABLE_DT_BYTES_IN_RATE);
455 if (ptr)
456 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200457 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200458 }
459
Willy Tarreau56123282010-08-06 19:06:56 +0200460 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200461 void *ptr;
462
Willy Tarreau56123282010-08-06 19:06:56 +0200463 ptr = stktable_data_ptr(s->stkctr1_table,
464 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200465 STKTABLE_DT_BYTES_IN_CNT);
466 if (ptr)
467 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
468
Willy Tarreau56123282010-08-06 19:06:56 +0200469 ptr = stktable_data_ptr(s->stkctr1_table,
470 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200471 STKTABLE_DT_BYTES_IN_RATE);
472 if (ptr)
473 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200474 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200475 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100476 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100477 }
478
Willy Tarreau30e71012007-11-26 20:15:35 +0100479 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100480 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100481 s->logs.bytes_out = s->rep->total;
482 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100483 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100484
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100485 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100486
Willy Tarreau827aee92011-03-10 16:55:02 +0100487 if (target_srv(&s->target))
488 target_srv(&s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200489
490 if (s->listener->counters)
491 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200492
Willy Tarreau56123282010-08-06 19:06:56 +0200493 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200494 void *ptr;
495
Willy Tarreau56123282010-08-06 19:06:56 +0200496 ptr = stktable_data_ptr(s->stkctr2_table,
497 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200498 STKTABLE_DT_BYTES_OUT_CNT);
499 if (ptr)
500 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
501
Willy Tarreau56123282010-08-06 19:06:56 +0200502 ptr = stktable_data_ptr(s->stkctr2_table,
503 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200504 STKTABLE_DT_BYTES_OUT_RATE);
505 if (ptr)
506 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200507 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200508 }
509
Willy Tarreau56123282010-08-06 19:06:56 +0200510 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200511 void *ptr;
512
Willy Tarreau56123282010-08-06 19:06:56 +0200513 ptr = stktable_data_ptr(s->stkctr1_table,
514 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200515 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200516 if (ptr)
517 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200518
Willy Tarreau56123282010-08-06 19:06:56 +0200519 ptr = stktable_data_ptr(s->stkctr1_table,
520 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200521 STKTABLE_DT_BYTES_OUT_RATE);
522 if (ptr)
523 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200524 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200525 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100526 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100527 }
528}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100530/* This function is called with (si->state == SI_ST_CON) meaning that a
531 * connection was attempted and that the file descriptor is already allocated.
532 * We must check for establishment, error and abort. Possible output states
533 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
534 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
535 * otherwise 1.
536 */
Simon Hormandec5be42011-06-08 09:19:07 +0900537static int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100538{
539 struct buffer *req = si->ob;
540 struct buffer *rep = si->ib;
541
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100542 /* If we got an error, or if nothing happened and the connection timed
543 * out, we must give up. The CER state handler will take care of retry
544 * attempts and error reports.
545 */
546 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100547 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100548 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200549 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100550 fd_delete(si->fd);
551
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200552 if (si->release)
553 si->release(si);
554
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100555 if (si->err_type)
556 return 0;
557
Willy Tarreau827aee92011-03-10 16:55:02 +0100558 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100559 if (si->flags & SI_FL_ERR)
560 si->err_type = SI_ET_CONN_ERR;
561 else
562 si->err_type = SI_ET_CONN_TO;
563 return 0;
564 }
565
566 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200567 if (unlikely((rep->flags & BF_SHUTW) ||
568 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200569 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100570 s->be->options & PR_O_ABRT_CLOSE)))) {
571 /* give up */
Willy Tarreau060781f2012-05-07 16:50:03 +0200572 si->sock.shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100573 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau827aee92011-03-10 16:55:02 +0100574 si->err_loc = target_srv(&s->target);
Willy Tarreaudc340a92009-06-28 23:10:19 +0200575 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100576 if (s->srv_error)
577 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100578 return 1;
579 }
580
581 /* we need to wait a bit more if there was no activity either */
582 if (!(req->flags & BF_WRITE_ACTIVITY))
583 return 1;
584
585 /* OK, this means that a connection succeeded. The caller will be
586 * responsible for handling the transition from CON to EST.
587 */
588 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100589 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100590 si->state = SI_ST_EST;
591 si->err_type = SI_ET_NONE;
592 si->err_loc = NULL;
593 return 1;
594}
595
596/* This function is called with (si->state == SI_ST_CER) meaning that a
597 * previous connection attempt has failed and that the file descriptor
598 * has already been released. Possible causes include asynchronous error
599 * notification and time out. Possible output states are SI_ST_CLO when
600 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
601 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
602 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
603 * marked as in error state. It returns 0.
604 */
Simon Hormandec5be42011-06-08 09:19:07 +0900605static int sess_update_st_cer(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100606{
607 /* we probably have to release last session from the server */
Willy Tarreau827aee92011-03-10 16:55:02 +0100608 if (target_srv(&s->target)) {
609 health_adjust(target_srv(&s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100610
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100611 if (s->flags & SN_CURR_SESS) {
612 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100613 target_srv(&s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100614 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100615 }
616
617 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200618 si->conn_retries--;
619 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100620 if (!si->err_type) {
621 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +0100622 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100623 }
624
Willy Tarreau827aee92011-03-10 16:55:02 +0100625 if (target_srv(&s->target))
626 target_srv(&s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100627 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100628 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100629 if (may_dequeue_tasks(target_srv(&s->target), s->be))
630 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100631
632 /* shutw is enough so stop a connecting socket */
Willy Tarreau060781f2012-05-07 16:50:03 +0200633 si->sock.shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634 si->ob->flags |= BF_WRITE_ERROR;
635 si->ib->flags |= BF_READ_ERROR;
636
637 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100638 if (s->srv_error)
639 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100640 return 0;
641 }
642
643 /* If the "redispatch" option is set on the backend, we are allowed to
644 * retry on another server for the last retry. In order to achieve this,
645 * we must mark the session unassigned, and eventually clear the DIRECT
646 * bit to ignore any persistence cookie. We won't count a retry nor a
647 * redispatch yet, because this will depend on what server is selected.
648 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100649 if (target_srv(&s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100650 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100651 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100652 if (may_dequeue_tasks(target_srv(&s->target), s->be))
653 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100654
655 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100656 si->state = SI_ST_REQ;
657 } else {
Willy Tarreau827aee92011-03-10 16:55:02 +0100658 if (target_srv(&s->target))
659 target_srv(&s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100660 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100661 si->state = SI_ST_ASS;
662 }
663
664 if (si->flags & SI_FL_ERR) {
665 /* The error was an asynchronous connection error, and we will
666 * likely have to retry connecting to the same server, most
667 * likely leading to the same result. To avoid this, we wait
668 * one second before retrying.
669 */
670
671 if (!si->err_type)
672 si->err_type = SI_ET_CONN_ERR;
673
674 si->state = SI_ST_TAR;
675 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
676 return 0;
677 }
678 return 0;
679}
680
681/*
682 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200683 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200684 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100685 */
Simon Hormandec5be42011-06-08 09:19:07 +0900686static void sess_establish(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687{
688 struct buffer *req = si->ob;
689 struct buffer *rep = si->ib;
690
Willy Tarreau827aee92011-03-10 16:55:02 +0100691 if (target_srv(&s->target))
692 health_adjust(target_srv(&s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100693
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100694 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100695 /* if the user wants to log as soon as possible, without counting
696 * bytes from the server, then this is the right moment. */
697 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
698 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100699 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100700 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100701 }
702 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
704 /* reset hdr_idx which was already initialized by the request.
705 * right now, the http parser does it.
706 * hdr_idx_init(&s->txn.hdr_idx);
707 */
708 }
709
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200710 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100711 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau26d8c592012-05-07 18:12:14 +0200712 if (si->proto) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200713 /* real connections have timeouts */
714 req->wto = s->be->timeout.server;
715 rep->rto = s->be->timeout.server;
716 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100717 req->wex = TICK_ETERNITY;
718}
719
720/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
721 * Other input states are simply ignored.
722 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
723 * Flags must have previously been updated for timeouts and other conditions.
724 */
Simon Hormandec5be42011-06-08 09:19:07 +0900725static void sess_update_stream_int(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100726{
Willy Tarreau827aee92011-03-10 16:55:02 +0100727 struct server *srv = target_srv(&s->target);
728
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100729 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100730 now_ms, __FUNCTION__,
731 s,
732 s->req, s->rep,
733 s->req->rex, s->rep->wex,
734 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100735 s->req->i, s->req->o, s->rep->i, s->rep->o, s->rep->cons->state, s->req->cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736
737 if (si->state == SI_ST_ASS) {
738 /* Server assigned to connection request, we have to try to connect now */
739 int conn_err;
740
741 conn_err = connect_server(s);
Willy Tarreau827aee92011-03-10 16:55:02 +0100742 srv = target_srv(&s->target);
743
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744 if (conn_err == SN_ERR_NONE) {
745 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100746 if (srv)
747 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100748 return;
749 }
750
751 /* We have received a synchronous error. We might have to
752 * abort, retry immediately or redispatch.
753 */
754 if (conn_err == SN_ERR_INTERNAL) {
755 if (!si->err_type) {
756 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +0100757 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758 }
759
Willy Tarreau827aee92011-03-10 16:55:02 +0100760 if (srv)
761 srv_inc_sess_ctr(srv);
762 if (srv)
763 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100764 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100765
766 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100767 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100768 if (may_dequeue_tasks(srv, s->be))
769 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100770
771 /* Failed and not retryable. */
Willy Tarreau060781f2012-05-07 16:50:03 +0200772 si->sock.shutr(si);
773 si->sock.shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100774 si->ob->flags |= BF_WRITE_ERROR;
775
776 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
777
778 /* no session was ever accounted for this server */
779 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100780 if (s->srv_error)
781 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100782 return;
783 }
784
785 /* We are facing a retryable error, but we don't want to run a
786 * turn-around now, as the problem is likely a source port
787 * allocation problem, so we want to retry now.
788 */
789 si->state = SI_ST_CER;
790 si->flags &= ~SI_FL_ERR;
791 sess_update_st_cer(s, si);
792 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
793 return;
794 }
795 else if (si->state == SI_ST_QUE) {
796 /* connection request was queued, check for any update */
797 if (!s->pend_pos) {
798 /* The connection is not in the queue anymore. Either
799 * we have a server connection slot available and we
800 * go directly to the assigned state, or we need to
801 * load-balance first and go to the INI state.
802 */
803 si->exp = TICK_ETERNITY;
804 if (unlikely(!(s->flags & SN_ASSIGNED)))
805 si->state = SI_ST_REQ;
806 else {
807 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
808 si->state = SI_ST_ASS;
809 }
810 return;
811 }
812
813 /* Connection request still in queue... */
814 if (si->flags & SI_FL_EXP) {
815 /* ... and timeout expired */
816 si->exp = TICK_ETERNITY;
817 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100818 if (srv)
819 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100820 s->be->be_counters.failed_conns++;
Willy Tarreau060781f2012-05-07 16:50:03 +0200821 si->sock.shutr(si);
822 si->sock.shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100823 si->ob->flags |= BF_WRITE_TIMEOUT;
824 if (!si->err_type)
825 si->err_type = SI_ET_QUEUE_TO;
826 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100827 if (s->srv_error)
828 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100829 return;
830 }
831
832 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200833 if ((si->ob->flags & (BF_READ_ERROR)) ||
834 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200835 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100836 /* give up */
837 si->exp = TICK_ETERNITY;
838 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau060781f2012-05-07 16:50:03 +0200839 si->sock.shutr(si);
840 si->sock.shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100841 si->err_type |= SI_ET_QUEUE_ABRT;
842 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100843 if (s->srv_error)
844 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100845 return;
846 }
847
848 /* Nothing changed */
849 return;
850 }
851 else if (si->state == SI_ST_TAR) {
852 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200853 if ((si->ob->flags & (BF_READ_ERROR)) ||
854 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200855 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100856 /* give up */
857 si->exp = TICK_ETERNITY;
Willy Tarreau060781f2012-05-07 16:50:03 +0200858 si->sock.shutr(si);
859 si->sock.shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100860 si->err_type |= SI_ET_CONN_ABRT;
861 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100862 if (s->srv_error)
863 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100864 return;
865 }
866
867 if (!(si->flags & SI_FL_EXP))
868 return; /* still in turn-around */
869
870 si->exp = TICK_ETERNITY;
871
872 /* we keep trying on the same server as long as the session is
873 * marked "assigned".
874 * FIXME: Should we force a redispatch attempt when the server is down ?
875 */
876 if (s->flags & SN_ASSIGNED)
877 si->state = SI_ST_ASS;
878 else
879 si->state = SI_ST_REQ;
880 return;
881 }
882}
883
Simon Hormandec5be42011-06-08 09:19:07 +0900884/* Set correct session termination flags in case no analyser has done it. It
885 * also counts a failed request if the server state has not reached the request
886 * stage.
887 */
888static void sess_set_term_flags(struct session *s)
889{
890 if (!(s->flags & SN_FINST_MASK)) {
891 if (s->si[1].state < SI_ST_REQ) {
892
893 s->fe->fe_counters.failed_req++;
894 if (s->listener->counters)
895 s->listener->counters->failed_req++;
896
897 s->flags |= SN_FINST_R;
898 }
899 else if (s->si[1].state == SI_ST_QUE)
900 s->flags |= SN_FINST_Q;
901 else if (s->si[1].state < SI_ST_EST)
902 s->flags |= SN_FINST_C;
903 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
904 s->flags |= SN_FINST_D;
905 else
906 s->flags |= SN_FINST_L;
907 }
908}
909
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100910/* This function initiates a server connection request on a stream interface
911 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
912 * indicating that a server has been assigned. It may also return SI_ST_QUE,
913 * or SI_ST_CLO upon error.
914 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100915static void sess_prepare_conn_req(struct session *s, struct stream_interface *si)
916{
917 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100918 now_ms, __FUNCTION__,
919 s,
920 s->req, s->rep,
921 s->req->rex, s->rep->wex,
922 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100923 s->req->i, s->req->o, s->rep->i, s->rep->o, s->rep->cons->state, s->req->cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100924
925 if (si->state != SI_ST_REQ)
926 return;
927
928 /* Try to assign a server */
929 if (srv_redispatch_connect(s) != 0) {
930 /* We did not get a server. Either we queued the
931 * connection request, or we encountered an error.
932 */
933 if (si->state == SI_ST_QUE)
934 return;
935
936 /* we did not get any server, let's check the cause */
Willy Tarreau060781f2012-05-07 16:50:03 +0200937 si->sock.shutr(si);
938 si->sock.shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100939 si->ob->flags |= BF_WRITE_ERROR;
940 if (!si->err_type)
941 si->err_type = SI_ET_CONN_OTHER;
942 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100943 if (s->srv_error)
944 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100945 return;
946 }
947
948 /* The server is assigned */
949 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
950 si->state = SI_ST_ASS;
951}
952
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200953/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100954 * if appropriate. The default_backend rule is also considered, then the
955 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200956 * It returns 1 if the processing can continue on next analysers, or zero if it
957 * either needs more data or wants to immediately abort the request.
958 */
Simon Hormandec5be42011-06-08 09:19:07 +0900959static int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200960{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200961 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100962
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200963 req->analysers &= ~an_bit;
964 req->analyse_exp = TICK_ETERNITY;
965
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100966 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200967 now_ms, __FUNCTION__,
968 s,
969 req,
970 req->rex, req->wex,
971 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100972 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200973 req->analysers);
974
975 /* now check whether we have some switching rules for this request */
976 if (!(s->flags & SN_BE_ASSIGNED)) {
977 struct switching_rule *rule;
978
979 list_for_each_entry(rule, &s->fe->switching_rules, list) {
980 int ret;
981
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200982 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200983 ret = acl_pass(ret);
984 if (rule->cond->pol == ACL_COND_UNLESS)
985 ret = !ret;
986
987 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200988 if (!session_set_backend(s, rule->be.backend))
989 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200990 break;
991 }
992 }
993
994 /* To ensure correct connection accounting on the backend, we
995 * have to assign one if it was not set (eg: a listen). This
996 * measure also takes care of correctly setting the default
997 * backend if any.
998 */
999 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001000 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
1001 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001002 }
1003
Willy Tarreaufb356202010-08-03 14:02:05 +02001004 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
1005 if (s->fe == s->be) {
1006 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001007 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001008 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001009
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001010 /* 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 +01001011 * persistence rule, and report that in the session.
1012 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001013 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001014 int ret = 1;
1015
1016 if (prst_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001017 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4de91492010-01-22 19:10:05 +01001018 ret = acl_pass(ret);
1019 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1020 ret = !ret;
1021 }
1022
1023 if (ret) {
1024 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001025 if (prst_rule->type == PERSIST_TYPE_FORCE) {
1026 s->flags |= SN_FORCE_PRST;
1027 } else {
1028 s->flags |= SN_IGNORE_PRST;
1029 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001030 break;
1031 }
1032 }
1033
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001034 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001035
1036 sw_failed:
1037 /* immediately abort this request in case of allocation failure */
1038 buffer_abort(s->req);
1039 buffer_abort(s->rep);
1040
1041 if (!(s->flags & SN_ERR_MASK))
1042 s->flags |= SN_ERR_RESOURCE;
1043 if (!(s->flags & SN_FINST_MASK))
1044 s->flags |= SN_FINST_R;
1045
1046 s->txn.status = 500;
1047 s->req->analysers = 0;
1048 s->req->analyse_exp = TICK_ETERNITY;
1049 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001050}
1051
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001052/* This stream analyser works on a request. It applies all use-server rules on
1053 * it then returns 1. The data must already be present in the buffer otherwise
1054 * they won't match. It always returns 1.
1055 */
1056static int process_server_rules(struct session *s, struct buffer *req, int an_bit)
1057{
1058 struct proxy *px = s->be;
1059 struct server_rule *rule;
1060
1061 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1062 now_ms, __FUNCTION__,
1063 s,
1064 req,
1065 req->rex, req->wex,
1066 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001067 req->i + req->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001068 req->analysers);
1069
1070 if (!(s->flags & SN_ASSIGNED)) {
1071 list_for_each_entry(rule, &px->server_rules, list) {
1072 int ret;
1073
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001074 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001075 ret = acl_pass(ret);
1076 if (rule->cond->pol == ACL_COND_UNLESS)
1077 ret = !ret;
1078
1079 if (ret) {
1080 struct server *srv = rule->srv.ptr;
1081
1082 if ((srv->state & SRV_RUNNING) ||
1083 (px->options & PR_O_PERSIST) ||
1084 (s->flags & SN_FORCE_PRST)) {
1085 s->flags |= SN_DIRECT | SN_ASSIGNED;
1086 set_target_server(&s->target, srv);
1087 break;
1088 }
1089 /* if the server is not UP, let's go on with next rules
1090 * just in case another one is suited.
1091 */
1092 }
1093 }
1094 }
1095
1096 req->analysers &= ~an_bit;
1097 req->analyse_exp = TICK_ETERNITY;
1098 return 1;
1099}
1100
Emeric Brun1d33b292010-01-04 15:47:17 +01001101/* This stream analyser works on a request. It applies all sticking rules on
1102 * it then returns 1. The data must already be present in the buffer otherwise
1103 * they won't match. It always returns 1.
1104 */
Simon Hormandec5be42011-06-08 09:19:07 +09001105static int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001106{
1107 struct proxy *px = s->be;
1108 struct sticking_rule *rule;
1109
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001110 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001111 now_ms, __FUNCTION__,
1112 s,
1113 req,
1114 req->rex, req->wex,
1115 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001116 req->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001117 req->analysers);
1118
1119 list_for_each_entry(rule, &px->sticking_rules, list) {
1120 int ret = 1 ;
1121 int i;
1122
1123 for (i = 0; i < s->store_count; i++) {
1124 if (rule->table.t == s->store[i].table)
1125 break;
1126 }
1127
1128 if (i != s->store_count)
1129 continue;
1130
1131 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001132 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001133 ret = acl_pass(ret);
1134 if (rule->cond->pol == ACL_COND_UNLESS)
1135 ret = !ret;
1136 }
1137
1138 if (ret) {
1139 struct stktable_key *key;
1140
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001141 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001142 if (!key)
1143 continue;
1144
1145 if (rule->flags & STK_IS_MATCH) {
1146 struct stksess *ts;
1147
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001148 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001149 if (!(s->flags & SN_ASSIGNED)) {
1150 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001151 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001152
1153 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001154 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1155 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001156 if (node) {
1157 struct server *srv;
1158
1159 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001160 if ((srv->state & SRV_RUNNING) ||
1161 (px->options & PR_O_PERSIST) ||
1162 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001163 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001164 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001165 }
1166 }
1167 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001168 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001169 }
1170 }
1171 if (rule->flags & STK_IS_STORE) {
1172 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1173 struct stksess *ts;
1174
1175 ts = stksess_new(rule->table.t, key);
1176 if (ts) {
1177 s->store[s->store_count].table = rule->table.t;
1178 s->store[s->store_count++].ts = ts;
1179 }
1180 }
1181 }
1182 }
1183 }
1184
1185 req->analysers &= ~an_bit;
1186 req->analyse_exp = TICK_ETERNITY;
1187 return 1;
1188}
1189
1190/* This stream analyser works on a response. It applies all store rules on it
1191 * then returns 1. The data must already be present in the buffer otherwise
1192 * they won't match. It always returns 1.
1193 */
Simon Hormandec5be42011-06-08 09:19:07 +09001194static int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001195{
1196 struct proxy *px = s->be;
1197 struct sticking_rule *rule;
1198 int i;
1199
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001200 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001201 now_ms, __FUNCTION__,
1202 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001203 rep,
1204 rep->rex, rep->wex,
1205 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001206 rep->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001207 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001208
1209 list_for_each_entry(rule, &px->storersp_rules, list) {
1210 int ret = 1 ;
1211 int storereqidx = -1;
1212
1213 for (i = 0; i < s->store_count; i++) {
1214 if (rule->table.t == s->store[i].table) {
1215 if (!(s->store[i].flags))
1216 storereqidx = i;
1217 break;
1218 }
1219 }
1220
1221 if ((i != s->store_count) && (storereqidx == -1))
1222 continue;
1223
1224 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001225 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001226 ret = acl_pass(ret);
1227 if (rule->cond->pol == ACL_COND_UNLESS)
1228 ret = !ret;
1229 }
1230
1231 if (ret) {
1232 struct stktable_key *key;
1233
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001234 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001235 if (!key)
1236 continue;
1237
1238 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001239 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001240 s->store[storereqidx].flags = 1;
1241 }
1242 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1243 struct stksess *ts;
1244
1245 ts = stksess_new(rule->table.t, key);
1246 if (ts) {
1247 s->store[s->store_count].table = rule->table.t;
1248 s->store[s->store_count].flags = 1;
1249 s->store[s->store_count++].ts = ts;
1250 }
1251 }
1252 }
1253 }
1254
1255 /* process store request and store response */
1256 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001257 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001258 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001259
Simon Hormanfa461682011-06-25 09:39:49 +09001260 if (target_srv(&s->target) && target_srv(&s->target)->state & SRV_NON_STICK) {
1261 stksess_free(s->store[i].table, s->store[i].ts);
1262 s->store[i].ts = NULL;
1263 continue;
1264 }
1265
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001266 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1267 if (ts) {
1268 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001269 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001270 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001271 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001272 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001273 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001274
1275 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001276 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001277 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001278 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001279 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001280
1281 rep->analysers &= ~an_bit;
1282 rep->analyse_exp = TICK_ETERNITY;
1283 return 1;
1284}
1285
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001286/* This macro is very specific to the function below. See the comments in
1287 * process_session() below to understand the logic and the tests.
1288 */
1289#define UPDATE_ANALYSERS(real, list, back, flag) { \
1290 list = (((list) & ~(flag)) | ~(back)) & (real); \
1291 back = real; \
1292 if (!(list)) \
1293 break; \
1294 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1295 continue; \
1296}
1297
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001298/* Processes the client, server, request and response jobs of a session task,
1299 * then puts it back to the wait queue in a clean state, or cleans up its
1300 * resources if it must be deleted. Returns in <next> the date the task wants
1301 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1302 * nothing too many times, the request and response buffers flags are monitored
1303 * and each function is called only if at least another function has changed at
1304 * least one flag it is interested in.
1305 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001306struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001307{
Willy Tarreau827aee92011-03-10 16:55:02 +01001308 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001309 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001310 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001311 unsigned int rq_prod_last, rq_cons_last;
1312 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001313 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001314
1315 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1316 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1317
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001318 /* this data may be no longer valid, clear it */
1319 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1320
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001321 /* This flag must explicitly be set every time */
1322 s->req->flags &= ~BF_READ_NOEXP;
1323
1324 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001325 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1326 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001327
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001328 /* we don't want the stream interface functions to recursively wake us up */
1329 if (s->req->prod->owner == t)
1330 s->req->prod->flags |= SI_FL_DONT_WAKE;
1331 if (s->req->cons->owner == t)
1332 s->req->cons->flags |= SI_FL_DONT_WAKE;
1333
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001334 /* 1a: Check for low level timeouts if needed. We just set a flag on
1335 * stream interfaces when their timeouts have expired.
1336 */
1337 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1338 stream_int_check_timeouts(&s->si[0]);
1339 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001340
1341 /* check buffer timeouts, and close the corresponding stream interfaces
1342 * for future reads or writes. Note: this will also concern upper layers
1343 * but we do not touch any other flag. We must be careful and correctly
1344 * detect state changes when calling them.
1345 */
1346
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001347 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001348
Willy Tarreau14641402009-12-29 14:49:56 +01001349 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1350 s->req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau060781f2012-05-07 16:50:03 +02001351 s->req->cons->sock.shutw(s->req->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001352 }
1353
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001354 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
Willy Tarreau060781f2012-05-07 16:50:03 +02001355 s->req->prod->sock.shutr(s->req->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001356
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001357 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001358
Willy Tarreau14641402009-12-29 14:49:56 +01001359 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1360 s->rep->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau060781f2012-05-07 16:50:03 +02001361 s->rep->cons->sock.shutw(s->rep->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001362 }
1363
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001364 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
Willy Tarreau060781f2012-05-07 16:50:03 +02001365 s->rep->prod->sock.shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001366 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001367
1368 /* 1b: check for low-level errors reported at the stream interface.
1369 * First we check if it's a retryable error (in which case we don't
1370 * want to tell the buffer). Otherwise we report the error one level
1371 * upper by setting flags into the buffers. Note that the side towards
1372 * the client cannot have connect (hence retryable) errors. Also, the
1373 * connection setup code must be able to deal with any type of abort.
1374 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001375 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001376 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1377 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
Willy Tarreau060781f2012-05-07 16:50:03 +02001378 s->si[0].sock.shutr(&s->si[0]);
1379 s->si[0].sock.shutw(&s->si[0]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001380 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001381 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001382 s->be->be_counters.cli_aborts++;
1383 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001384 if (srv)
1385 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001386 if (!(s->flags & SN_ERR_MASK))
1387 s->flags |= SN_ERR_CLICL;
1388 if (!(s->flags & SN_FINST_MASK))
1389 s->flags |= SN_FINST_D;
1390 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001391 }
1392 }
1393
1394 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1395 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
Willy Tarreau060781f2012-05-07 16:50:03 +02001396 s->si[1].sock.shutr(&s->si[1]);
1397 s->si[1].sock.shutw(&s->si[1]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001398 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001399 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001400 if (srv)
1401 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001402 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001403 s->be->be_counters.srv_aborts++;
1404 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001405 if (srv)
1406 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001407 if (!(s->flags & SN_ERR_MASK))
1408 s->flags |= SN_ERR_SRVCL;
1409 if (!(s->flags & SN_FINST_MASK))
1410 s->flags |= SN_FINST_D;
1411 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001412 }
1413 /* note: maybe we should process connection errors here ? */
1414 }
1415
1416 if (s->si[1].state == SI_ST_CON) {
1417 /* we were trying to establish a connection on the server side,
1418 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1419 */
1420 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1421 sess_update_st_cer(s, &s->si[1]);
1422 else if (s->si[1].state == SI_ST_EST)
1423 sess_establish(s, &s->si[1]);
1424
1425 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1426 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1427 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1428 */
1429 }
1430
Willy Tarreau815a9b22010-07-27 17:15:12 +02001431 rq_prod_last = s->si[0].state;
1432 rq_cons_last = s->si[1].state;
1433 rp_cons_last = s->si[0].state;
1434 rp_prod_last = s->si[1].state;
1435
1436 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001437 /* Check for connection closure */
1438
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001439 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001440 "[%u] %s:%d: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001441 now_ms, __FUNCTION__, __LINE__,
1442 t,
1443 s, s->flags,
1444 s->req, s->rep,
1445 s->req->rex, s->rep->wex,
1446 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001447 s->req->i, s->req->o, s->rep->i, s->rep->o, s->rep->cons->state, s->req->cons->state,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001448 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001449 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001450
1451 /* nothing special to be done on client side */
1452 if (unlikely(s->req->prod->state == SI_ST_DIS))
1453 s->req->prod->state = SI_ST_CLO;
1454
1455 /* When a server-side connection is released, we have to count it and
1456 * check for pending connections on this server.
1457 */
1458 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1459 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001460 srv = target_srv(&s->target);
1461 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001462 if (s->flags & SN_CURR_SESS) {
1463 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001464 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001465 }
1466 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001467 if (may_dequeue_tasks(srv, s->be))
1468 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001469 }
1470 }
1471
1472 /*
1473 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1474 * at this point.
1475 */
1476
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001477 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001478 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001479 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001480 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1481 s->si[0].state != rq_prod_last ||
1482 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001483 unsigned int flags = s->req->flags;
1484
1485 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001486 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001487 unsigned int ana_list;
1488 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001489
Willy Tarreau90deb182010-01-07 00:20:41 +01001490 /* it's up to the analysers to stop new connections,
1491 * disable reading or closing. Note: if an analyser
1492 * disables any of these bits, it is responsible for
1493 * enabling them again when it disables itself, so
1494 * that other analysers are called in similar conditions.
1495 */
1496 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001497 buffer_auto_connect(s->req);
1498 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001499
1500 /* We will call all analysers for which a bit is set in
1501 * s->req->analysers, following the bit order from LSB
1502 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001503 * the list when not needed. Any analyser may return 0
1504 * to break out of the loop, either because of missing
1505 * data to take a decision, or because it decides to
1506 * kill the session. We loop at least once through each
1507 * analyser, and we may loop again if other analysers
1508 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001509 *
1510 * We build a list of analysers to run. We evaluate all
1511 * of these analysers in the order of the lower bit to
1512 * the higher bit. This ordering is very important.
1513 * An analyser will often add/remove other analysers,
1514 * including itself. Any changes to itself have no effect
1515 * on the loop. If it removes any other analysers, we
1516 * want those analysers not to be called anymore during
1517 * this loop. If it adds an analyser that is located
1518 * after itself, we want it to be scheduled for being
1519 * processed during the loop. If it adds an analyser
1520 * which is located before it, we want it to switch to
1521 * it immediately, even if it has already been called
1522 * once but removed since.
1523 *
1524 * In order to achieve this, we compare the analyser
1525 * list after the call with a copy of it before the
1526 * call. The work list is fed with analyser bits that
1527 * appeared during the call. Then we compare previous
1528 * work list with the new one, and check the bits that
1529 * appeared. If the lowest of these bits is lower than
1530 * the current bit, it means we have enabled a previous
1531 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001532 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001533
1534 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001535 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001536 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001537
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001538 if (ana_list & AN_REQ_DECODE_PROXY) {
1539 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1540 break;
1541 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1542 }
1543
Willy Tarreaufb356202010-08-03 14:02:05 +02001544 if (ana_list & AN_REQ_INSPECT_FE) {
1545 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001546 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001547 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001548 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001549
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001550 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001551 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001552 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001553 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001554 }
1555
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001556 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001557 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1558 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001559 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001560 }
1561
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001562 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001563 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1564 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001565 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001566 }
1567
Willy Tarreaufb356202010-08-03 14:02:05 +02001568 if (ana_list & AN_REQ_INSPECT_BE) {
1569 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1570 break;
1571 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1572 }
1573
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001574 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001575 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1576 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001577 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001578 }
1579
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001580 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001581 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001582 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001583 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001584 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001585
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001586 if (ana_list & AN_REQ_SRV_RULES) {
1587 if (!process_server_rules(s, s->req, AN_REQ_SRV_RULES))
1588 break;
1589 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
1590 }
1591
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001592 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001593 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1594 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001595 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001596 }
1597
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001598 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001599 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001600 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001601 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001602 }
Emeric Brun647caf12009-06-30 17:57:00 +02001603
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001604 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001605 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1606 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001607 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001608 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001609
Emeric Brun1d33b292010-01-04 15:47:17 +01001610 if (ana_list & AN_REQ_STICKING_RULES) {
1611 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1612 break;
1613 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1614 }
1615
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001616 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001617 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1618 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001619 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001620 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001621 break;
1622 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001623 }
Willy Tarreau84455332009-03-15 22:34:05 +01001624
Willy Tarreau815a9b22010-07-27 17:15:12 +02001625 rq_prod_last = s->si[0].state;
1626 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001627 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001628 rqf_last = s->req->flags;
1629
1630 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001631 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001632 }
1633
Willy Tarreau576507f2010-01-07 00:09:04 +01001634 /* we'll monitor the request analysers while parsing the response,
1635 * because some response analysers may indirectly enable new request
1636 * analysers (eg: HTTP keep-alive).
1637 */
1638 req_ana_back = s->req->analysers;
1639
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001640 resync_response:
1641 /* Analyse response */
1642
1643 if (unlikely(s->rep->flags & BF_HIJACK)) {
1644 /* In inject mode, we wake up everytime something has
1645 * happened on the write side of the buffer.
1646 */
1647 unsigned int flags = s->rep->flags;
1648
1649 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1650 !(s->rep->flags & BF_FULL)) {
1651 s->rep->hijacker(s, s->rep);
1652 }
1653
1654 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1655 rpf_last = s->rep->flags;
1656 goto resync_response;
1657 }
1658 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001659 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001660 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1661 s->si[0].state != rp_cons_last ||
1662 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001663 unsigned int flags = s->rep->flags;
1664
Willy Tarreau0499e352010-12-17 07:13:42 +01001665 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1666 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1667 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1668 * for some free space in the response buffer, so we might need to call
1669 * it when something changes in the response buffer, but still we pass
1670 * it the request buffer. Note that the SI state might very well still
1671 * be zero due to us returning a flow of redirects!
1672 */
1673 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1674 s->req->flags |= BF_WAKE_ONCE;
1675 }
1676
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001677 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001678 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001679 unsigned int ana_list;
1680 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001681
Willy Tarreau90deb182010-01-07 00:20:41 +01001682 /* it's up to the analysers to stop disable reading or
1683 * closing. Note: if an analyser disables any of these
1684 * bits, it is responsible for enabling them again when
1685 * it disables itself, so that other analysers are called
1686 * in similar conditions.
1687 */
1688 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001689 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001690
1691 /* We will call all analysers for which a bit is set in
1692 * s->rep->analysers, following the bit order from LSB
1693 * to MSB. The analysers must remove themselves from
1694 * the list when not needed. Any analyser may return 0
1695 * to break out of the loop, either because of missing
1696 * data to take a decision, or because it decides to
1697 * kill the session. We loop at least once through each
1698 * analyser, and we may loop again if other analysers
1699 * are added in the middle.
1700 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001701
1702 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001703 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001704 /* Warning! ensure that analysers are always placed in ascending order! */
1705
Emeric Brun97679e72010-09-23 17:56:44 +02001706 if (ana_list & AN_RES_INSPECT) {
1707 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1708 break;
1709 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1710 }
1711
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001712 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001713 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1714 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001715 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001716 }
1717
Emeric Brun1d33b292010-01-04 15:47:17 +01001718 if (ana_list & AN_RES_STORE_RULES) {
1719 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1720 break;
1721 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1722 }
1723
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001724 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001725 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1726 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001727 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001728 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001729
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001730 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001731 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1732 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001733 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001734 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001735 break;
1736 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001737 }
1738
Willy Tarreau815a9b22010-07-27 17:15:12 +02001739 rp_cons_last = s->si[0].state;
1740 rp_prod_last = s->si[1].state;
1741 rpf_last = s->rep->flags;
1742
1743 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001744 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001745 }
1746
Willy Tarreau576507f2010-01-07 00:09:04 +01001747 /* maybe someone has added some request analysers, so we must check and loop */
1748 if (s->req->analysers & ~req_ana_back)
1749 goto resync_request;
1750
Willy Tarreau0499e352010-12-17 07:13:42 +01001751 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1752 goto resync_request;
1753
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001754 /* FIXME: here we should call protocol handlers which rely on
1755 * both buffers.
1756 */
1757
1758
1759 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001760 * Now we propagate unhandled errors to the session. Normally
1761 * we're just in a data phase here since it means we have not
1762 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001763 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001764 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001765 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001766 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1767 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001768 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001769 if (s->req->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001770 s->be->be_counters.cli_aborts++;
1771 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001772 if (srv)
1773 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001774 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001775 }
1776 else if (s->req->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001777 s->be->be_counters.cli_aborts++;
1778 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001779 if (srv)
1780 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001781 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001782 }
1783 else if (s->req->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001784 s->be->be_counters.srv_aborts++;
1785 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001786 if (srv)
1787 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001788 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001789 }
1790 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001791 s->be->be_counters.srv_aborts++;
1792 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001793 if (srv)
1794 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001795 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001796 }
Willy Tarreau84455332009-03-15 22:34:05 +01001797 sess_set_term_flags(s);
1798 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001799 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1800 /* Report it if the server got an error or a read timeout expired */
1801 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001802 if (s->rep->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001803 s->be->be_counters.srv_aborts++;
1804 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001805 if (srv)
1806 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001807 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001808 }
1809 else if (s->rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001810 s->be->be_counters.srv_aborts++;
1811 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001812 if (srv)
1813 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001814 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001815 }
1816 else if (s->rep->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001817 s->be->be_counters.cli_aborts++;
1818 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001819 if (srv)
1820 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001821 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001822 }
1823 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001824 s->be->be_counters.cli_aborts++;
1825 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001826 if (srv)
1827 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001828 s->flags |= SN_ERR_CLITO;
1829 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001830 sess_set_term_flags(s);
1831 }
Willy Tarreau84455332009-03-15 22:34:05 +01001832 }
1833
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001834 /*
1835 * Here we take care of forwarding unhandled data. This also includes
1836 * connection establishments and shutdown requests.
1837 */
1838
1839
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001840 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001841 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001842 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1843 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001844 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001845 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001846 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001847 (s->req->prod->state >= SI_ST_EST) &&
1848 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001849 /* This buffer is freewheeling, there's no analyser nor hijacker
1850 * attached to it. If any data are left in, we'll permit them to
1851 * move.
1852 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001853 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001854 buffer_auto_connect(s->req);
1855 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001856 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001857
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001858 /* We'll let data flow between the producer (if still connected)
1859 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001860 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001861 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001862 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001863 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001864
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001865 /* check if it is wise to enable kernel splicing to forward request data */
1866 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1867 s->req->to_forward &&
1868 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001869 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001870 (pipes_used < global.maxpipes) &&
1871 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1872 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1873 (s->req->flags & BF_STREAMER_FAST)))) {
1874 s->req->flags |= BF_KERN_SPLICING;
1875 }
1876
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001877 /* reflect what the L7 analysers have seen last */
1878 rqf_last = s->req->flags;
1879
1880 /*
1881 * Now forward all shutdown requests between both sides of the buffer
1882 */
1883
Willy Tarreau520d95e2009-09-19 21:04:57 +02001884 /* first, let's check if the request buffer needs to shutdown(write), which may
1885 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001886 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001887 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001888 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001889 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001890 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001891
1892 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001893 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_OUT_EMPTY)) == (BF_SHUTW_NOW|BF_OUT_EMPTY)))
Willy Tarreau060781f2012-05-07 16:50:03 +02001894 s->req->cons->sock.shutw(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001895
1896 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001897 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1898 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001899 buffer_shutr_now(s->req);
1900
1901 /* shutdown(read) pending */
1902 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
Willy Tarreau060781f2012-05-07 16:50:03 +02001903 s->req->prod->sock.shutr(s->req->prod);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001904
Willy Tarreau520d95e2009-09-19 21:04:57 +02001905 /* it's possible that an upper layer has requested a connection setup or abort.
1906 * There are 2 situations where we decide to establish a new connection :
1907 * - there are data scheduled for emission in the buffer
1908 * - the BF_AUTO_CONNECT flag is set (active connection)
1909 */
1910 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001911 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001912 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001913 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001914 * switch to the connected state, otherwise we perform a connection
1915 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001916 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001917 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001918 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau26d8c592012-05-07 18:12:14 +02001919 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET &&
1920 !(s->req->cons->proto && s->req->cons->proto->connect))) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001921 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001922 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1923 s->req->wex = TICK_ETERNITY;
1924 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001925 }
Willy Tarreau73201222009-08-16 18:27:24 +02001926 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001927 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001928 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001929 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1930 buffer_shutr_now(s->rep);
1931 }
Willy Tarreau92795622009-03-06 12:51:23 +01001932 }
1933
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001934
1935 /* we may have a pending connection request, or a connection waiting
1936 * for completion.
1937 */
1938 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1939 do {
1940 /* nb: step 1 might switch from QUE to ASS, but we first want
1941 * to give a chance to step 2 to perform a redirect if needed.
1942 */
1943 if (s->si[1].state != SI_ST_REQ)
1944 sess_update_stream_int(s, &s->si[1]);
1945 if (s->si[1].state == SI_ST_REQ)
1946 sess_prepare_conn_req(s, &s->si[1]);
1947
Willy Tarreau827aee92011-03-10 16:55:02 +01001948 srv = target_srv(&s->target);
1949 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001950 perform_http_redirect(s, &s->si[1]);
1951 } while (s->si[1].state == SI_ST_ASS);
Mark Lamourinec2247f02012-01-04 13:02:01 -05001952
1953 /* Now we can add the server name to a header (if requested) */
1954 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Stathis Voukelatos09a030a2012-01-09 14:27:13 +01001955 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau45c0d982012-03-09 12:11:57 +01001956 (s->be->mode == PR_MODE_HTTP) &&
1957 (s->be->server_id_hdr_name != NULL)) {
1958 http_send_name_header(&s->txn, s->be, target_srv(&s->target)->id);
Mark Lamourinec2247f02012-01-04 13:02:01 -05001959 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001960 }
1961
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001962 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001963 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001964 goto resync_stream_interface;
1965
Willy Tarreau815a9b22010-07-27 17:15:12 +02001966 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001967 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001968 goto resync_request;
1969
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001970 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001971
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001972 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001973 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001974 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1975 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001976 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001977 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001978 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001979 (s->rep->prod->state >= SI_ST_EST) &&
1980 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001981 /* This buffer is freewheeling, there's no analyser nor hijacker
1982 * attached to it. If any data are left in, we'll permit them to
1983 * move.
1984 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001985 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001986 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001987 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001988
1989 /* We'll let data flow between the producer (if still connected)
1990 * to the consumer.
1991 */
1992 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001993 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001994 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001995
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001996 /* check if it is wise to enable kernel splicing to forward response data */
1997 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1998 s->rep->to_forward &&
1999 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02002000 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002001 (pipes_used < global.maxpipes) &&
2002 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2003 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
2004 (s->rep->flags & BF_STREAMER_FAST)))) {
2005 s->rep->flags |= BF_KERN_SPLICING;
2006 }
2007
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002008 /* reflect what the L7 analysers have seen last */
2009 rpf_last = s->rep->flags;
2010
2011 /*
2012 * Now forward all shutdown requests between both sides of the buffer
2013 */
2014
2015 /*
2016 * FIXME: this is probably where we should produce error responses.
2017 */
2018
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002019 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002020 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
2021 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002022 buffer_shutw_now(s->rep);
2023
2024 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02002025 if (unlikely((s->rep->flags & (BF_SHUTW|BF_OUT_EMPTY|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW)))
Willy Tarreau060781f2012-05-07 16:50:03 +02002026 s->rep->cons->sock.shutw(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002027
2028 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002029 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
2030 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002031 buffer_shutr_now(s->rep);
2032
2033 /* shutdown(read) pending */
2034 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
Willy Tarreau060781f2012-05-07 16:50:03 +02002035 s->rep->prod->sock.shutr(s->rep->prod);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002036
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002037 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002038 goto resync_stream_interface;
2039
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002040 if (s->req->flags != rqf_last)
2041 goto resync_request;
2042
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002043 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002044 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002045
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002046 /* we're interested in getting wakeups again */
2047 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
2048 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
2049
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002050 /* This is needed only when debugging is enabled, to indicate
2051 * client-side or server-side close. Please note that in the unlikely
2052 * event where both sides would close at once, the sequence is reported
2053 * on the server side first.
2054 */
2055 if (unlikely((global.mode & MODE_DEBUG) &&
2056 (!(global.mode & MODE_QUIET) ||
2057 (global.mode & MODE_VERBOSE)))) {
2058 int len;
2059
2060 if (s->si[1].state == SI_ST_CLO &&
2061 s->si[1].prev_state == SI_ST_EST) {
2062 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
2063 s->uniq_id, s->be->id,
2064 (unsigned short)s->si[0].fd,
2065 (unsigned short)s->si[1].fd);
Willy Tarreau21337822012-04-29 14:11:38 +02002066 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002067 }
2068
2069 if (s->si[0].state == SI_ST_CLO &&
2070 s->si[0].prev_state == SI_ST_EST) {
2071 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
2072 s->uniq_id, s->be->id,
2073 (unsigned short)s->si[0].fd,
2074 (unsigned short)s->si[1].fd);
Willy Tarreau21337822012-04-29 14:11:38 +02002075 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002076 }
2077 }
2078
2079 if (likely((s->rep->cons->state != SI_ST_CLO) ||
2080 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
2081
2082 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
2083 session_process_counters(s);
2084
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002085 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreau060781f2012-05-07 16:50:03 +02002086 s->rep->cons->sock.update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002087
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002088 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreau060781f2012-05-07 16:50:03 +02002089 s->req->cons->sock.update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002090
Willy Tarreaua6eebb32010-06-04 11:40:20 +02002091 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
2092 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 +01002093 s->si[0].prev_state = s->si[0].state;
2094 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002095 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2096 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002097
2098 /* Trick: if a request is being waiting for the server to respond,
2099 * and if we know the server can timeout, we don't want the timeout
2100 * to expire on the client side first, but we're still interested
2101 * in passing data from the client to the server (eg: POST). Thus,
2102 * we can cancel the client's request timeout if the server's
2103 * request timeout is set and the server has not yet sent a response.
2104 */
2105
Willy Tarreau520d95e2009-09-19 21:04:57 +02002106 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01002107 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
2108 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002109 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01002110 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002111
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002112 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002113 * Note that this one may wake the task up again.
2114 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002115 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
2116 s->rep->cons->target.type == TARG_TYPE_APPLET) {
2117 if (s->req->cons->target.type == TARG_TYPE_APPLET)
2118 s->req->cons->target.ptr.a->fct(s->req->cons);
2119 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
2120 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002121 if (task_in_rq(t)) {
2122 /* If we woke up, we don't want to requeue the
2123 * task to the wait queue, but rather requeue
2124 * it into the runqueue ASAP.
2125 */
2126 t->expire = TICK_ETERNITY;
2127 return t;
2128 }
2129 }
2130
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002131 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2132 tick_first(s->rep->rex, s->rep->wex));
2133 if (s->req->analysers)
2134 t->expire = tick_first(t->expire, s->req->analyse_exp);
2135
2136 if (s->si[0].exp)
2137 t->expire = tick_first(t->expire, s->si[0].exp);
2138
2139 if (s->si[1].exp)
2140 t->expire = tick_first(t->expire, s->si[1].exp);
2141
2142#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002143 fprintf(stderr,
2144 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2145 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2146 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2147 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 +01002148#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002149
2150#ifdef DEBUG_DEV
2151 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002152 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002153 ABORT_NOW();
2154#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002155 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002156 }
2157
2158 s->fe->feconn--;
2159 if (s->flags & SN_BE_ASSIGNED)
2160 s->be->beconn--;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002161 if (!(s->listener->options & LI_O_UNLIMITED))
2162 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002163 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002164 s->listener->nbconn--;
Willy Tarreau62793712011-07-24 19:23:38 +02002165 if (s->listener->state == LI_FULL)
2166 resume_listener(s->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002167
Willy Tarreau08ceb102011-07-24 22:58:00 +02002168 /* Dequeues all of the listeners waiting for a resource */
2169 if (!LIST_ISEMPTY(&global_listener_queue))
2170 dequeue_all_listeners(&global_listener_queue);
2171
Willy Tarreaub32907b2011-07-25 08:37:44 +02002172 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
2173 (!s->fe->fe_sps_lim || freq_ctr_remain(&s->fe->fe_sess_per_sec, s->fe->fe_sps_lim, 0) > 0))
Willy Tarreau07687c12011-07-24 23:55:06 +02002174 dequeue_all_listeners(&s->fe->listener_queue);
2175
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002176 if (unlikely((global.mode & MODE_DEBUG) &&
2177 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2178 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002179 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002180 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002181 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau21337822012-04-29 14:11:38 +02002182 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002183 }
2184
2185 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2186 session_process_counters(s);
2187
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002188 if (s->txn.status) {
2189 int n;
2190
2191 n = s->txn.status / 100;
2192 if (n < 1 || n > 5)
2193 n = 0;
2194
2195 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002196 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002197
Willy Tarreau24657792010-02-26 10:30:28 +01002198 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002199 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002200 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002201 }
2202
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002203 /* let's do a final log if we need it */
2204 if (s->logs.logwait &&
2205 !(s->flags & SN_MONITOR) &&
2206 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002207 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002208 }
2209
2210 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002211 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002212 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002213 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002214 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002215}
2216
Willy Tarreau7c669d72008-06-20 15:04:11 +02002217/*
2218 * This function adjusts sess->srv_conn and maintains the previous and new
2219 * server's served session counts. Setting newsrv to NULL is enough to release
2220 * current connection slot. This function also notifies any LB algo which might
2221 * expect to be informed about any change in the number of active sessions on a
2222 * server.
2223 */
2224void sess_change_server(struct session *sess, struct server *newsrv)
2225{
2226 if (sess->srv_conn == newsrv)
2227 return;
2228
2229 if (sess->srv_conn) {
2230 sess->srv_conn->served--;
2231 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2232 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002233 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002234 }
2235
2236 if (newsrv) {
2237 newsrv->served++;
2238 if (newsrv->proxy->lbprm.server_take_conn)
2239 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002240 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002241 }
2242}
2243
Willy Tarreau84455332009-03-15 22:34:05 +01002244/* Handle server-side errors for default protocols. It is called whenever a a
2245 * connection setup is aborted or a request is aborted in queue. It sets the
2246 * session termination flags so that the caller does not have to worry about
2247 * them. It's installed as ->srv_error for the server-side stream_interface.
2248 */
2249void default_srv_error(struct session *s, struct stream_interface *si)
2250{
2251 int err_type = si->err_type;
2252 int err = 0, fin = 0;
2253
2254 if (err_type & SI_ET_QUEUE_ABRT) {
2255 err = SN_ERR_CLICL;
2256 fin = SN_FINST_Q;
2257 }
2258 else if (err_type & SI_ET_CONN_ABRT) {
2259 err = SN_ERR_CLICL;
2260 fin = SN_FINST_C;
2261 }
2262 else if (err_type & SI_ET_QUEUE_TO) {
2263 err = SN_ERR_SRVTO;
2264 fin = SN_FINST_Q;
2265 }
2266 else if (err_type & SI_ET_QUEUE_ERR) {
2267 err = SN_ERR_SRVCL;
2268 fin = SN_FINST_Q;
2269 }
2270 else if (err_type & SI_ET_CONN_TO) {
2271 err = SN_ERR_SRVTO;
2272 fin = SN_FINST_C;
2273 }
2274 else if (err_type & SI_ET_CONN_ERR) {
2275 err = SN_ERR_SRVCL;
2276 fin = SN_FINST_C;
2277 }
2278 else /* SI_ET_CONN_OTHER and others */ {
2279 err = SN_ERR_INTERNAL;
2280 fin = SN_FINST_C;
2281 }
2282
2283 if (!(s->flags & SN_ERR_MASK))
2284 s->flags |= err;
2285 if (!(s->flags & SN_FINST_MASK))
2286 s->flags |= fin;
2287}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002288
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002289/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
2290void session_shutdown(struct session *session, int why)
2291{
2292 if (session->req->flags & (BF_SHUTW|BF_SHUTW_NOW))
2293 return;
2294
2295 buffer_shutw_now(session->req);
2296 buffer_shutr_now(session->rep);
2297 session->task->nice = 1024;
2298 if (!(session->flags & SN_ERR_MASK))
2299 session->flags |= why;
2300 task_wakeup(session->task, TASK_WOKEN_OTHER);
2301}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002302
Willy Tarreau8b22a712010-06-18 17:46:06 +02002303/************************************************************************/
2304/* All supported ACL keywords must be declared here. */
2305/************************************************************************/
2306
Willy Tarreaua5e37562011-12-16 17:06:15 +01002307/* set temp integer to the General Purpose Counter 0 value in the stksess entry <ts> */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002308static int
Willy Tarreau37406352012-04-23 16:16:37 +02002309acl_fetch_get_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002310{
Willy Tarreau37406352012-04-23 16:16:37 +02002311 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002312 smp->type = SMP_T_UINT;
2313 smp->data.uint = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002314 if (ts != NULL) {
2315 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2316 if (!ptr)
2317 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002318 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002319 }
2320 return 1;
2321}
2322
Willy Tarreaua5e37562011-12-16 17:06:15 +01002323/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002324 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002325 */
2326static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002327acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002328 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002329{
Willy Tarreau56123282010-08-06 19:06:56 +02002330 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002331 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002332 return acl_fetch_get_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002333}
2334
Willy Tarreaua5e37562011-12-16 17:06:15 +01002335/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002336 * backend counters.
2337 */
2338static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002339acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002340 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002341{
Willy Tarreau56123282010-08-06 19:06:56 +02002342 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002343 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002344 return acl_fetch_get_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002345}
2346
Willy Tarreaua5e37562011-12-16 17:06:15 +01002347/* set temp integer to the General Purpose Counter 0 value from the session's source
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002348 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002349 * Accepts exactly 1 argument of type table.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002350 */
2351static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002352acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002353 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002354{
2355 struct stktable_key *key;
2356
David du Colombier4f92d322011-03-24 11:09:31 +01002357 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002358 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002359 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002360
Willy Tarreau24e32d82012-04-23 23:55:44 +02002361 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002362 return acl_fetch_get_gpc0(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002363}
2364
2365/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002366 * return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002367 */
2368static int
Willy Tarreau37406352012-04-23 16:16:37 +02002369acl_fetch_inc_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002370{
Willy Tarreau37406352012-04-23 16:16:37 +02002371 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002372 smp->type = SMP_T_UINT;
2373 smp->data.uint = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002374 if (ts != NULL) {
2375 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2376 if (!ptr)
2377 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002378 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002379 }
2380 return 1;
2381}
2382
2383/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002384 * frontend counters and return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002385 */
2386static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002387acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002388 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002389{
Willy Tarreau56123282010-08-06 19:06:56 +02002390 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002391 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002392 return acl_fetch_inc_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002393}
2394
2395/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002396 * backend counters and return it into temp integer.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002397 */
2398static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002399acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002400 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002401{
Willy Tarreau56123282010-08-06 19:06:56 +02002402 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002403 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002404 return acl_fetch_inc_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002405}
2406
2407/* Increment the General Purpose Counter 0 value from the session's source
Willy Tarreaua5e37562011-12-16 17:06:15 +01002408 * address in the table pointed to by expr, and return it into temp integer.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002409 * Accepts exactly 1 argument of type table.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002410 */
2411static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002412acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002413 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002414{
2415 struct stktable_key *key;
2416
David du Colombier4f92d322011-03-24 11:09:31 +01002417 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002418 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002419 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002420
Willy Tarreau24e32d82012-04-23 23:55:44 +02002421 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002422 return acl_fetch_inc_gpc0(&px->table, smp, stktable_update_key(&px->table, key));
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002423}
2424
Willy Tarreauf73cd112011-08-13 01:45:16 +02002425/* Clear the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002426 * return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002427 */
2428static int
Willy Tarreau37406352012-04-23 16:16:37 +02002429acl_fetch_clr_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002430{
Willy Tarreau37406352012-04-23 16:16:37 +02002431 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002432 smp->type = SMP_T_UINT;
2433 smp->data.uint = 0;
Willy Tarreauf73cd112011-08-13 01:45:16 +02002434 if (ts != NULL) {
2435 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2436 if (!ptr)
2437 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002438 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002439 stktable_data_cast(ptr, gpc0) = 0;
2440 }
2441 return 1;
2442}
2443
2444/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002445 * frontend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002446 */
2447static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002448acl_fetch_sc1_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002449 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002450{
2451 if (!l4->stkctr1_entry)
2452 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002453 return acl_fetch_clr_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002454}
2455
2456/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002457 * backend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002458 */
2459static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002460acl_fetch_sc2_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002461 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002462{
2463 if (!l4->stkctr2_entry)
2464 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002465 return acl_fetch_clr_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002466}
2467
2468/* Clear the General Purpose Counter 0 value from the session's source address
Willy Tarreaua5e37562011-12-16 17:06:15 +01002469 * in the table pointed to by expr, and return its previous value into temp integer.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002470 * Accepts exactly 1 argument of type table.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002471 */
2472static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002473acl_fetch_src_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002474 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002475{
2476 struct stktable_key *key;
2477
2478 key = tcp_src_to_stktable_key(l4);
2479 if (!key)
2480 return 0;
2481
Willy Tarreau24e32d82012-04-23 23:55:44 +02002482 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002483 return acl_fetch_clr_gpc0(&px->table, smp, stktable_update_key(&px->table, key));
Willy Tarreauf73cd112011-08-13 01:45:16 +02002484}
2485
Willy Tarreaua5e37562011-12-16 17:06:15 +01002486/* set temp integer to the cumulated number of connections in the stksess entry <ts> */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002487static int
Willy Tarreau37406352012-04-23 16:16:37 +02002488acl_fetch_conn_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002489{
Willy Tarreau37406352012-04-23 16:16:37 +02002490 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002491 smp->type = SMP_T_UINT;
2492 smp->data.uint = 0;
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002493 if (ts != NULL) {
2494 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2495 if (!ptr)
2496 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002497 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002498 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002499 return 1;
2500}
2501
Willy Tarreaua5e37562011-12-16 17:06:15 +01002502/* set temp integer to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002503static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002504acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002505 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002506{
Willy Tarreau56123282010-08-06 19:06:56 +02002507 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002508 return 0;
2509
Willy Tarreau37406352012-04-23 16:16:37 +02002510 return acl_fetch_conn_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002511}
2512
Willy Tarreaua5e37562011-12-16 17:06:15 +01002513/* set temp integer to the cumulated number of connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002514static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002515acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002516 const struct arg *args, struct sample *smp)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002517{
Willy Tarreau56123282010-08-06 19:06:56 +02002518 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002519 return 0;
2520
Willy Tarreau37406352012-04-23 16:16:37 +02002521 return acl_fetch_conn_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002522}
2523
Willy Tarreaua5e37562011-12-16 17:06:15 +01002524/* set temp integer to the cumulated number of connections from the session's source
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002525 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002526 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002527 */
2528static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002529acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002530 const struct arg *args, struct sample *smp)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002531{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002532 struct stktable_key *key;
2533
David du Colombier4f92d322011-03-24 11:09:31 +01002534 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002535 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002536 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002537
Willy Tarreau24e32d82012-04-23 23:55:44 +02002538 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002539 return acl_fetch_conn_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002540}
2541
Willy Tarreaua5e37562011-12-16 17:06:15 +01002542/* set temp integer to the connection rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002543static int
Willy Tarreau37406352012-04-23 16:16:37 +02002544acl_fetch_conn_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002545{
Willy Tarreau37406352012-04-23 16:16:37 +02002546 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002547 smp->type = SMP_T_UINT;
2548 smp->data.uint = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002549 if (ts != NULL) {
2550 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2551 if (!ptr)
2552 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002553 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002554 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2555 }
2556 return 1;
2557}
2558
Willy Tarreaua5e37562011-12-16 17:06:15 +01002559/* set temp integer to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002560 * the configured period.
2561 */
2562static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002563acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002564 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002565{
Willy Tarreau56123282010-08-06 19:06:56 +02002566 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002567 return 0;
2568
Willy Tarreau37406352012-04-23 16:16:37 +02002569 return acl_fetch_conn_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002570}
2571
Willy Tarreaua5e37562011-12-16 17:06:15 +01002572/* set temp integer to the connection rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002573 * the configured period.
2574 */
2575static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002576acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002577 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002578{
Willy Tarreau56123282010-08-06 19:06:56 +02002579 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002580 return 0;
2581
Willy Tarreau37406352012-04-23 16:16:37 +02002582 return acl_fetch_conn_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002583}
2584
Willy Tarreaua5e37562011-12-16 17:06:15 +01002585/* set temp integer to the connection rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002586 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002587 * Accepts exactly 1 argument of type table.
Willy Tarreau91c43d72010-06-20 11:19:22 +02002588 */
2589static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002590acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002591 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002592{
2593 struct stktable_key *key;
2594
David du Colombier4f92d322011-03-24 11:09:31 +01002595 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002596 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002597 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002598
Willy Tarreau24e32d82012-04-23 23:55:44 +02002599 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002600 return acl_fetch_conn_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau91c43d72010-06-20 11:19:22 +02002601}
2602
Willy Tarreaua5e37562011-12-16 17:06:15 +01002603/* set temp integer to the number of connections from the session's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002604 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002605 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002606 */
2607static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002608acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002609 const struct arg *args, struct sample *smp)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002610{
2611 struct stksess *ts;
2612 struct stktable_key *key;
2613 void *ptr;
2614
David du Colombier4f92d322011-03-24 11:09:31 +01002615 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002616 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002617 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002618
Willy Tarreau24e32d82012-04-23 23:55:44 +02002619 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002620
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002621 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2622 /* entry does not exist and could not be created */
2623 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002624
2625 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2626 if (!ptr)
2627 return 0; /* parameter not stored in this table */
2628
Willy Tarreauf853c462012-04-23 18:53:56 +02002629 smp->type = SMP_T_UINT;
2630 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002631 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002632 return 1;
2633}
2634
Willy Tarreaua5e37562011-12-16 17:06:15 +01002635/* set temp integer to the number of concurrent connections in the stksess entry <ts> */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002636static int
Willy Tarreau37406352012-04-23 16:16:37 +02002637acl_fetch_conn_cur(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002638{
Willy Tarreau37406352012-04-23 16:16:37 +02002639 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002640 smp->type = SMP_T_UINT;
2641 smp->data.uint = 0;
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002642
2643 if (ts != NULL) {
2644 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2645 if (!ptr)
2646 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002647 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002648 }
2649 return 1;
2650}
2651
Willy Tarreaua5e37562011-12-16 17:06:15 +01002652/* set temp integer to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002653static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002654acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002655 const struct arg *args, struct sample *smp)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002656{
Willy Tarreau56123282010-08-06 19:06:56 +02002657 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002658 return 0;
2659
Willy Tarreau37406352012-04-23 16:16:37 +02002660 return acl_fetch_conn_cur(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002661}
2662
Willy Tarreaua5e37562011-12-16 17:06:15 +01002663/* set temp integer to the number of concurrent connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002664static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002665acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002666 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002667{
Willy Tarreau56123282010-08-06 19:06:56 +02002668 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002669 return 0;
2670
Willy Tarreau37406352012-04-23 16:16:37 +02002671 return acl_fetch_conn_cur(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002672}
2673
Willy Tarreaua5e37562011-12-16 17:06:15 +01002674/* set temp integer to the number of concurrent connections from the session's source
Willy Tarreau38285c12010-06-18 16:35:43 +02002675 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002676 * Accepts exactly 1 argument of type table.
Willy Tarreau38285c12010-06-18 16:35:43 +02002677 */
2678static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002679acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002680 const struct arg *args, struct sample *smp)
Willy Tarreau38285c12010-06-18 16:35:43 +02002681{
Willy Tarreau38285c12010-06-18 16:35:43 +02002682 struct stktable_key *key;
2683
David du Colombier4f92d322011-03-24 11:09:31 +01002684 key = tcp_src_to_stktable_key(l4);
Willy Tarreau38285c12010-06-18 16:35:43 +02002685 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002686 return 0;
Willy Tarreau38285c12010-06-18 16:35:43 +02002687
Willy Tarreau24e32d82012-04-23 23:55:44 +02002688 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002689 return acl_fetch_conn_cur(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002690}
2691
Willy Tarreaua5e37562011-12-16 17:06:15 +01002692/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002693static int
Willy Tarreau37406352012-04-23 16:16:37 +02002694acl_fetch_sess_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002695{
Willy Tarreau37406352012-04-23 16:16:37 +02002696 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002697 smp->type = SMP_T_UINT;
2698 smp->data.uint = 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002699 if (ts != NULL) {
2700 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2701 if (!ptr)
2702 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002703 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002704 }
2705 return 1;
2706}
2707
Willy Tarreaua5e37562011-12-16 17:06:15 +01002708/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002709static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002710acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002711 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002712{
Willy Tarreau56123282010-08-06 19:06:56 +02002713 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002714 return 0;
2715
Willy Tarreau37406352012-04-23 16:16:37 +02002716 return acl_fetch_sess_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002717}
2718
Willy Tarreaua5e37562011-12-16 17:06:15 +01002719/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002720static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002721acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002722 const struct arg *args, struct sample *smp)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002723{
Willy Tarreau56123282010-08-06 19:06:56 +02002724 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002725 return 0;
2726
Willy Tarreau37406352012-04-23 16:16:37 +02002727 return acl_fetch_sess_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002728}
2729
Willy Tarreaua5e37562011-12-16 17:06:15 +01002730/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002731 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002732 * Accepts exactly 1 argument of type table.
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002733 */
2734static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002735acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002736 const struct arg *args, struct sample *smp)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002737{
2738 struct stktable_key *key;
2739
David du Colombier4f92d322011-03-24 11:09:31 +01002740 key = tcp_src_to_stktable_key(l4);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002741 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002742 return 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002743
Willy Tarreau24e32d82012-04-23 23:55:44 +02002744 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002745 return acl_fetch_sess_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002746}
2747
Willy Tarreaua5e37562011-12-16 17:06:15 +01002748/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002749static int
Willy Tarreau37406352012-04-23 16:16:37 +02002750acl_fetch_sess_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002751{
Willy Tarreau37406352012-04-23 16:16:37 +02002752 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002753 smp->type = SMP_T_UINT;
2754 smp->data.uint = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002755 if (ts != NULL) {
2756 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2757 if (!ptr)
2758 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002759 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002760 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2761 }
2762 return 1;
2763}
2764
Willy Tarreaua5e37562011-12-16 17:06:15 +01002765/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002766 * the configured period.
2767 */
2768static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002769acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002770 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002771{
Willy Tarreau56123282010-08-06 19:06:56 +02002772 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002773 return 0;
2774
Willy Tarreau37406352012-04-23 16:16:37 +02002775 return acl_fetch_sess_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002776}
2777
Willy Tarreaua5e37562011-12-16 17:06:15 +01002778/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002779 * the configured period.
2780 */
2781static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002782acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002783 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002784{
Willy Tarreau56123282010-08-06 19:06:56 +02002785 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002786 return 0;
2787
Willy Tarreau37406352012-04-23 16:16:37 +02002788 return acl_fetch_sess_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002789}
2790
Willy Tarreaua5e37562011-12-16 17:06:15 +01002791/* set temp integer to the session rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002792 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002793 * Accepts exactly 1 argument of type table.
Willy Tarreau91c43d72010-06-20 11:19:22 +02002794 */
2795static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002796acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002797 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002798{
2799 struct stktable_key *key;
2800
David du Colombier4f92d322011-03-24 11:09:31 +01002801 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002802 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002803 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002804
Willy Tarreau24e32d82012-04-23 23:55:44 +02002805 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002806 return acl_fetch_sess_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau91c43d72010-06-20 11:19:22 +02002807}
2808
Willy Tarreaua5e37562011-12-16 17:06:15 +01002809/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002810static int
Willy Tarreau37406352012-04-23 16:16:37 +02002811acl_fetch_http_req_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002812{
Willy Tarreau37406352012-04-23 16:16:37 +02002813 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002814 smp->type = SMP_T_UINT;
2815 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002816 if (ts != NULL) {
2817 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2818 if (!ptr)
2819 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002820 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002821 }
2822 return 1;
2823}
2824
Willy Tarreaua5e37562011-12-16 17:06:15 +01002825/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002826static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002827acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002828 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002829{
Willy Tarreau56123282010-08-06 19:06:56 +02002830 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002831 return 0;
2832
Willy Tarreau37406352012-04-23 16:16:37 +02002833 return acl_fetch_http_req_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002834}
2835
Willy Tarreaua5e37562011-12-16 17:06:15 +01002836/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002837static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002838acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002839 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002840{
Willy Tarreau56123282010-08-06 19:06:56 +02002841 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002842 return 0;
2843
Willy Tarreau37406352012-04-23 16:16:37 +02002844 return acl_fetch_http_req_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002845}
2846
Willy Tarreaua5e37562011-12-16 17:06:15 +01002847/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02002848 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002849 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002850 */
2851static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002852acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002853 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002854{
2855 struct stktable_key *key;
2856
David du Colombier4f92d322011-03-24 11:09:31 +01002857 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002858 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002859 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002860
Willy Tarreau24e32d82012-04-23 23:55:44 +02002861 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002862 return acl_fetch_http_req_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002863}
2864
Willy Tarreaua5e37562011-12-16 17:06:15 +01002865/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002866static int
Willy Tarreau37406352012-04-23 16:16:37 +02002867acl_fetch_http_req_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002868{
Willy Tarreau37406352012-04-23 16:16:37 +02002869 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002870 smp->type = SMP_T_UINT;
2871 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002872 if (ts != NULL) {
2873 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2874 if (!ptr)
2875 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002876 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02002877 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2878 }
2879 return 1;
2880}
2881
Willy Tarreaua5e37562011-12-16 17:06:15 +01002882/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002883 * the configured period.
2884 */
2885static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002886acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002887 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002888{
Willy Tarreau56123282010-08-06 19:06:56 +02002889 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002890 return 0;
2891
Willy Tarreau37406352012-04-23 16:16:37 +02002892 return acl_fetch_http_req_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002893}
2894
Willy Tarreaua5e37562011-12-16 17:06:15 +01002895/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002896 * the configured period.
2897 */
2898static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002899acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002900 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002901{
Willy Tarreau56123282010-08-06 19:06:56 +02002902 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002903 return 0;
2904
Willy Tarreau37406352012-04-23 16:16:37 +02002905 return acl_fetch_http_req_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002906}
2907
Willy Tarreaua5e37562011-12-16 17:06:15 +01002908/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02002909 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002910 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002911 */
2912static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002913acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002914 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002915{
2916 struct stktable_key *key;
2917
David du Colombier4f92d322011-03-24 11:09:31 +01002918 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002919 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002920 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002921
Willy Tarreau24e32d82012-04-23 23:55:44 +02002922 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002923 return acl_fetch_http_req_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002924}
2925
Willy Tarreaua5e37562011-12-16 17:06:15 +01002926/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002927static int
Willy Tarreau37406352012-04-23 16:16:37 +02002928acl_fetch_http_err_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002929{
Willy Tarreau37406352012-04-23 16:16:37 +02002930 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002931 smp->type = SMP_T_UINT;
2932 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002933 if (ts != NULL) {
2934 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2935 if (!ptr)
2936 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002937 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002938 }
2939 return 1;
2940}
2941
Willy Tarreaua5e37562011-12-16 17:06:15 +01002942/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002943static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002944acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002945 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002946{
Willy Tarreau56123282010-08-06 19:06:56 +02002947 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002948 return 0;
2949
Willy Tarreau37406352012-04-23 16:16:37 +02002950 return acl_fetch_http_err_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002951}
2952
Willy Tarreaua5e37562011-12-16 17:06:15 +01002953/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002954static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002955acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002956 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002957{
Willy Tarreau56123282010-08-06 19:06:56 +02002958 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002959 return 0;
2960
Willy Tarreau37406352012-04-23 16:16:37 +02002961 return acl_fetch_http_err_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002962}
2963
Willy Tarreaua5e37562011-12-16 17:06:15 +01002964/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02002965 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002966 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002967 */
2968static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002969acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002970 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002971{
2972 struct stktable_key *key;
2973
David du Colombier4f92d322011-03-24 11:09:31 +01002974 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002975 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002976 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002977
Willy Tarreau24e32d82012-04-23 23:55:44 +02002978 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002979 return acl_fetch_http_err_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002980}
2981
Willy Tarreaua5e37562011-12-16 17:06:15 +01002982/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002983static int
Willy Tarreau37406352012-04-23 16:16:37 +02002984acl_fetch_http_err_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002985{
Willy Tarreau37406352012-04-23 16:16:37 +02002986 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002987 smp->type = SMP_T_UINT;
2988 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002989 if (ts != NULL) {
2990 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2991 if (!ptr)
2992 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002993 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02002994 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2995 }
2996 return 1;
2997}
2998
Willy Tarreaua5e37562011-12-16 17:06:15 +01002999/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02003000 * the configured period.
3001 */
3002static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003003acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003004 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003005{
Willy Tarreau56123282010-08-06 19:06:56 +02003006 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003007 return 0;
3008
Willy Tarreau37406352012-04-23 16:16:37 +02003009 return acl_fetch_http_err_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003010}
3011
Willy Tarreaua5e37562011-12-16 17:06:15 +01003012/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003013 * the configured period.
3014 */
3015static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003016acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003017 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003018{
Willy Tarreau56123282010-08-06 19:06:56 +02003019 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003020 return 0;
3021
Willy Tarreau37406352012-04-23 16:16:37 +02003022 return acl_fetch_http_err_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003023}
3024
Willy Tarreaua5e37562011-12-16 17:06:15 +01003025/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003026 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003027 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003028 */
3029static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003030acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003031 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003032{
3033 struct stktable_key *key;
3034
David du Colombier4f92d322011-03-24 11:09:31 +01003035 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003036 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003037 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003038
Willy Tarreau24e32d82012-04-23 23:55:44 +02003039 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003040 return acl_fetch_http_err_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003041}
3042
Willy Tarreaua5e37562011-12-16 17:06:15 +01003043/* set temp integer to the number of kbytes received from clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003044static int
Willy Tarreau37406352012-04-23 16:16:37 +02003045acl_fetch_kbytes_in(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003046{
Willy Tarreau37406352012-04-23 16:16:37 +02003047 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003048 smp->type = SMP_T_UINT;
3049 smp->data.uint = 0;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003050
3051 if (ts != NULL) {
3052 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
3053 if (!ptr)
3054 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003055 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003056 }
3057 return 1;
3058}
3059
Willy Tarreaua5e37562011-12-16 17:06:15 +01003060/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003061 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003062 */
3063static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003064acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003065 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003066{
Willy Tarreau56123282010-08-06 19:06:56 +02003067 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003068 return 0;
3069
Willy Tarreau37406352012-04-23 16:16:37 +02003070 return acl_fetch_kbytes_in(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003071}
3072
Willy Tarreaua5e37562011-12-16 17:06:15 +01003073/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003074 * session's tracked BE counters.
3075 */
3076static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003077acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003078 const struct arg *args, struct sample *smp)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003079{
Willy Tarreau56123282010-08-06 19:06:56 +02003080 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003081 return 0;
3082
Willy Tarreau37406352012-04-23 16:16:37 +02003083 return acl_fetch_kbytes_in(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003084}
3085
Willy Tarreaua5e37562011-12-16 17:06:15 +01003086/* set temp integer to the number of kbytes received from the session's source
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003087 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003088 * Accepts exactly 1 argument of type table.
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003089 */
3090static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003091acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003092 const struct arg *args, struct sample *smp)
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003093{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003094 struct stktable_key *key;
3095
David du Colombier4f92d322011-03-24 11:09:31 +01003096 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003097 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003098 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003099
Willy Tarreau24e32d82012-04-23 23:55:44 +02003100 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003101 return acl_fetch_kbytes_in(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003102}
3103
Willy Tarreaua5e37562011-12-16 17:06:15 +01003104/* set temp integer to the bytes rate from clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003105 * configured period.
3106 */
3107static int
Willy Tarreau37406352012-04-23 16:16:37 +02003108acl_fetch_bytes_in_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003109{
Willy Tarreau37406352012-04-23 16:16:37 +02003110 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003111 smp->type = SMP_T_UINT;
3112 smp->data.uint = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003113 if (ts != NULL) {
3114 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
3115 if (!ptr)
3116 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003117 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003118 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
3119 }
3120 return 1;
3121}
3122
Willy Tarreaua5e37562011-12-16 17:06:15 +01003123/* set temp integer to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003124 * counters over the configured period.
3125 */
3126static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003127acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003128 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003129{
Willy Tarreau56123282010-08-06 19:06:56 +02003130 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003131 return 0;
3132
Willy Tarreau37406352012-04-23 16:16:37 +02003133 return acl_fetch_bytes_in_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003134}
3135
Willy Tarreaua5e37562011-12-16 17:06:15 +01003136/* set temp integer to the bytes rate from clients from the session's tracked BE
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003137 * counters over the configured period.
3138 */
3139static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003140acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003141 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003142{
Willy Tarreau56123282010-08-06 19:06:56 +02003143 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003144 return 0;
3145
Willy Tarreau37406352012-04-23 16:16:37 +02003146 return acl_fetch_bytes_in_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003147}
3148
Willy Tarreaua5e37562011-12-16 17:06:15 +01003149/* set temp integer to the bytes rate from clients from the session's source address
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003150 * in the table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003151 * Accepts exactly 1 argument of type table.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003152 */
3153static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003154acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003155 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003156{
3157 struct stktable_key *key;
3158
David du Colombier4f92d322011-03-24 11:09:31 +01003159 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003160 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003161 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003162
Willy Tarreau24e32d82012-04-23 23:55:44 +02003163 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003164 return acl_fetch_bytes_in_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003165}
3166
Willy Tarreaua5e37562011-12-16 17:06:15 +01003167/* set temp integer to the number of kbytes sent to clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003168static int
Willy Tarreau37406352012-04-23 16:16:37 +02003169acl_fetch_kbytes_out(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003170{
Willy Tarreau37406352012-04-23 16:16:37 +02003171 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003172 smp->type = SMP_T_UINT;
3173 smp->data.uint = 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003174
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003175 if (ts != NULL) {
3176 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003177 if (!ptr)
3178 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003179 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003180 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003181 return 1;
3182}
3183
Willy Tarreaua5e37562011-12-16 17:06:15 +01003184/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003185 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003186 */
3187static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003188acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003189 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003190{
Willy Tarreau56123282010-08-06 19:06:56 +02003191 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003192 return 0;
3193
Willy Tarreau37406352012-04-23 16:16:37 +02003194 return acl_fetch_kbytes_out(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003195}
3196
Willy Tarreaua5e37562011-12-16 17:06:15 +01003197/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003198 * tracked BE counters.
3199 */
3200static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003201acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003202 const struct arg *args, struct sample *smp)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003203{
Willy Tarreau56123282010-08-06 19:06:56 +02003204 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003205 return 0;
3206
Willy Tarreau37406352012-04-23 16:16:37 +02003207 return acl_fetch_kbytes_out(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003208}
3209
Willy Tarreaua5e37562011-12-16 17:06:15 +01003210/* set temp integer to the number of kbytes sent to the session's source address in
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003211 * the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003212 * Accepts exactly 1 argument of type table.
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003213 */
3214static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003215acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003216 const struct arg *args, struct sample *smp)
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003217{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003218 struct stktable_key *key;
3219
David du Colombier4f92d322011-03-24 11:09:31 +01003220 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003221 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003222 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003223
Willy Tarreau24e32d82012-04-23 23:55:44 +02003224 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003225 return acl_fetch_kbytes_out(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003226}
3227
Willy Tarreaua5e37562011-12-16 17:06:15 +01003228/* set temp integer to the bytes rate to clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003229 * configured period.
3230 */
3231static int
Willy Tarreau37406352012-04-23 16:16:37 +02003232acl_fetch_bytes_out_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003233{
Willy Tarreau37406352012-04-23 16:16:37 +02003234 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003235 smp->type = SMP_T_UINT;
3236 smp->data.uint = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003237 if (ts != NULL) {
3238 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3239 if (!ptr)
3240 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003241 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003242 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3243 }
3244 return 1;
3245}
3246
Willy Tarreaua5e37562011-12-16 17:06:15 +01003247/* set temp integer to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003248 * over the configured period.
3249 */
3250static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003251acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003252 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003253{
Willy Tarreau56123282010-08-06 19:06:56 +02003254 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003255 return 0;
3256
Willy Tarreau37406352012-04-23 16:16:37 +02003257 return acl_fetch_bytes_out_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003258}
3259
Willy Tarreaua5e37562011-12-16 17:06:15 +01003260/* set temp integer to the bytes rate to clients from the session's tracked BE counters
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003261 * over the configured period.
3262 */
3263static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003264acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003265 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003266{
Willy Tarreau56123282010-08-06 19:06:56 +02003267 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003268 return 0;
3269
Willy Tarreau37406352012-04-23 16:16:37 +02003270 return acl_fetch_bytes_out_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003271}
3272
Willy Tarreaua5e37562011-12-16 17:06:15 +01003273/* set temp integer to the bytes rate to client from the session's source address in
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003274 * the table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003275 * Accepts exactly 1 argument of type table.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003276 */
3277static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003278acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003279 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003280{
3281 struct stktable_key *key;
3282
David du Colombier4f92d322011-03-24 11:09:31 +01003283 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003284 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003285 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003286
Willy Tarreau24e32d82012-04-23 23:55:44 +02003287 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003288 return acl_fetch_bytes_out_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003289}
3290
Willy Tarreau34db1082012-04-19 17:16:54 +02003291/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003292 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003293 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003294static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003295acl_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003296 const struct arg *args, struct sample *smp)
Willy Tarreauc735a072011-03-29 00:57:02 +02003297{
Willy Tarreau37406352012-04-23 16:16:37 +02003298 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003299 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003300 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003301 return 1;
3302}
3303
Willy Tarreau34db1082012-04-19 17:16:54 +02003304/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003305 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003306 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003307static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003308acl_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003309 const struct arg *args, struct sample *smp)
Willy Tarreauc735a072011-03-29 00:57:02 +02003310{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003311 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003312 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003313 smp->type = SMP_T_UINT;
3314 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003315 return 1;
3316}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003317
Willy Tarreau61612d42012-04-19 18:42:05 +02003318/* Note: must not be declared <const> as its list will be overwritten.
3319 * Please take care of keeping this list alphabetically sorted.
3320 */
Willy Tarreau8b22a712010-06-18 17:46:06 +02003321static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02003322 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3323 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3324 { "sc1_clr_gpc0", acl_parse_int, acl_fetch_sc1_clr_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3325 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3326 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING, 0 },
3327 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3328 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3329 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3330 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3331 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3332 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3333 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3334 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3335 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3336 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3337 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3338 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3339 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3340 { "sc2_clr_gpc0", acl_parse_int, acl_fetch_sc2_clr_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3341 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3342 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING, 0 },
3343 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3344 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3345 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3346 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3347 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3348 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3349 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3350 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3351 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3352 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3353 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02003354 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3355 { "src_bytes_out_rate", acl_parse_int, acl_fetch_src_bytes_out_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3356 { "src_clr_gpc0", acl_parse_int, acl_fetch_src_clr_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3357 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3358 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3359 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3360 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3361 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3362 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3363 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3364 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3365 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3366 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3367 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3368 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3369 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3370 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3371 { "table_avl", acl_parse_int, acl_fetch_table_avl, acl_match_int, ACL_USE_NOTHING, ARG1(1,TAB) },
3372 { "table_cnt", acl_parse_int, acl_fetch_table_cnt, acl_match_int, ACL_USE_NOTHING, ARG1(1,TAB) },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003373 { NULL, NULL, NULL, NULL },
3374}};
3375
3376
Willy Tarreau56123282010-08-06 19:06:56 +02003377/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003378 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3379 * <prm> struct is fed with the table name if any. If unspecified, the caller
3380 * will assume that the current proxy's table is used.
3381 */
3382int parse_track_counters(char **args, int *arg,
3383 int section_type, struct proxy *curpx,
3384 struct track_ctr_prm *prm,
3385 struct proxy *defpx, char *err, int errlen)
3386{
Willy Tarreau12785782012-04-27 21:37:17 +02003387 int sample_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003388 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003389
Willy Tarreau56123282010-08-06 19:06:56 +02003390 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003391 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003392 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003393 */
3394 while (args[*arg]) {
3395 if (strcmp(args[*arg], "src") == 0) {
3396 prm->type = STKTABLE_TYPE_IP;
Willy Tarreau12785782012-04-27 21:37:17 +02003397 sample_type = 1;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003398 }
3399 else if (strcmp(args[*arg], "table") == 0) {
3400 if (!args[*arg + 1]) {
3401 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003402 "missing table for %s in %s '%s'.",
3403 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003404 return -1;
3405 }
3406 /* we copy the table name for now, it will be resolved later */
3407 prm->table.n = strdup(args[*arg + 1]);
3408 (*arg)++;
3409 }
3410 else {
3411 /* unhandled keywords are handled by the caller */
3412 break;
3413 }
3414 (*arg)++;
3415 }
3416
Willy Tarreau12785782012-04-27 21:37:17 +02003417 if (!sample_type) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003418 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003419 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3420 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003421 return -1;
3422 }
3423
3424 return 0;
3425}
3426
Willy Tarreau8b22a712010-06-18 17:46:06 +02003427__attribute__((constructor))
3428static void __session_init(void)
3429{
3430 acl_register_keywords(&acl_kws);
3431}
3432
Willy Tarreaubaaee002006-06-26 02:48:02 +02003433/*
3434 * Local variables:
3435 * c-indent-level: 8
3436 * c-basic-offset: 8
3437 * End:
3438 */