blob: 4760a350bc7ef120d97080c0cc7342a4e4dbac7f [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>
Willy Tarreauc63190d2012-05-11 14:23:52 +020043#include <proto/sock_raw.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010044#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010045#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010046#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 Tarreaubd833142012-05-08 15:51:44 +0200100 s->unique_id = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200101 s->term_trace = 0;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200102 s->si[0].addr.from = *addr;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200103 s->logs.accept_date = date; /* user-visible date for logging */
104 s->logs.tv_accept = now; /* corrected date for internal use */
105 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200106 p->feconn++; /* beconn will be increased once assigned */
107
Willy Tarreaub36b4242010-06-04 20:59:39 +0200108 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200109
110 t->process = l->handler;
111 t->context = s;
112 t->nice = l->nice;
113 t->expire = TICK_ETERNITY;
114
115 s->task = t;
116 s->listener = l;
117
118 /* Note: initially, the session's backend points to the frontend.
119 * This changes later when switching rules are executed or
120 * when the default backend is assigned.
121 */
122 s->be = s->fe = p;
123 s->req = s->rep = NULL; /* will be allocated later */
124
125 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
126 * to abort right here as soon as possible, we check the rules before
127 * even initializing the stream interfaces.
128 */
129 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200130 /* let's do a no-linger now to close with a single RST. */
131 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100132 ret = 0; /* successful termination */
133 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200134 }
135
Willy Tarreaub36b4242010-06-04 20:59:39 +0200136 /* This session was accepted, count it now */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100137 if (p->feconn > p->fe_counters.conn_max)
138 p->fe_counters.conn_max = p->feconn;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100139
Willy Tarreaub36b4242010-06-04 20:59:39 +0200140 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200141 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200142 void *ptr;
143
Willy Tarreau56123282010-08-06 19:06:56 +0200144 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200145 if (ptr)
146 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200147
Willy Tarreau56123282010-08-06 19:06:56 +0200148 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200149 if (ptr)
150 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200151 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200152 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200153
Willy Tarreau56123282010-08-06 19:06:56 +0200154 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200155 void *ptr;
156
Willy Tarreau56123282010-08-06 19:06:56 +0200157 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200158 if (ptr)
159 stktable_data_cast(ptr, sess_cnt)++;
160
Willy Tarreau56123282010-08-06 19:06:56 +0200161 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200162 if (ptr)
163 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200164 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200165 }
166
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200167 /* this part should be common with other protocols */
168 s->si[0].fd = cfd;
169 s->si[0].owner = t;
170 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
171 s->si[0].err_type = SI_ET_NONE;
172 s->si[0].err_loc = NULL;
Willy Tarreau26d8c592012-05-07 18:12:14 +0200173 s->si[0].proto = l->proto;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200174 s->si[0].release = NULL;
Willy Tarreau63e7fe32012-05-08 15:20:43 +0200175 s->si[0].send_proxy_ofs = 0;
Willy Tarreau1539a012012-05-11 14:47:34 +0200176 set_target_client(&s->si[0].target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200177 s->si[0].exp = TICK_ETERNITY;
178 s->si[0].flags = SI_FL_NONE;
179
180 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
181 s->si[0].flags |= SI_FL_INDEP_STR;
182
183 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
184 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
185
186 /* add the various callbacks */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200187 stream_interface_prepare(&s->si[0], &sock_raw);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200188
189 /* pre-initialize the other side's stream interface to an INIT state. The
190 * callbacks will be initialized before attempting to connect.
191 */
192 s->si[1].fd = -1; /* just to help with debugging */
193 s->si[1].owner = t;
194 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
195 s->si[1].err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +0200196 s->si[1].conn_retries = 0; /* used for logging too */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200197 s->si[1].err_loc = NULL;
Willy Tarreau26d8c592012-05-07 18:12:14 +0200198 s->si[1].proto = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200199 s->si[1].release = NULL;
Willy Tarreau63e7fe32012-05-08 15:20:43 +0200200 s->si[1].send_proxy_ofs = 0;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100201 clear_target(&s->si[1].target);
Willy Tarreauf873d752012-05-11 17:47:17 +0200202 stream_interface_prepare(&s->si[1], &stream_int_embedded);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200203 s->si[1].exp = TICK_ETERNITY;
204 s->si[1].flags = SI_FL_NONE;
205
206 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
207 s->si[1].flags |= SI_FL_INDEP_STR;
208
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200209 session_init_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +0100210 clear_target(&s->target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200211 s->pend_pos = NULL;
212
213 /* init store persistence */
214 s->store_count = 0;
215
216 /* Adjust some socket options */
Willy Tarreaufffe1322010-11-11 09:48:16 +0100217 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200218 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200219
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200220 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
221 goto out_free_task; /* no memory */
222
223 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
224 goto out_free_req; /* no memory */
225
226 /* initialize the request buffer */
227 s->req->size = global.tune.bufsize;
228 buffer_init(s->req);
229 s->req->prod = &s->si[0];
230 s->req->cons = &s->si[1];
231 s->si[0].ib = s->si[1].ob = s->req;
232 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
233
234 /* activate default analysers enabled for this listener */
235 s->req->analysers = l->analysers;
236
237 s->req->wto = TICK_ETERNITY;
238 s->req->rto = TICK_ETERNITY;
239 s->req->rex = TICK_ETERNITY;
240 s->req->wex = TICK_ETERNITY;
241 s->req->analyse_exp = TICK_ETERNITY;
242
243 /* initialize response buffer */
244 s->rep->size = global.tune.bufsize;
245 buffer_init(s->rep);
246 s->rep->prod = &s->si[1];
247 s->rep->cons = &s->si[0];
248 s->si[0].ob = s->si[1].ib = s->rep;
249 s->rep->analysers = 0;
250
Willy Tarreau96e31212011-05-30 18:10:30 +0200251 if (s->fe->options2 & PR_O2_NODELAY) {
252 s->req->flags |= BF_NEVER_WAIT;
253 s->rep->flags |= BF_NEVER_WAIT;
254 }
255
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200256 s->rep->rto = TICK_ETERNITY;
257 s->rep->wto = TICK_ETERNITY;
258 s->rep->rex = TICK_ETERNITY;
259 s->rep->wex = TICK_ETERNITY;
260 s->rep->analyse_exp = TICK_ETERNITY;
261
Willy Tarreau62f791e2012-03-09 11:32:30 +0100262 txn = &s->txn;
263 /* Those variables will be checked and freed if non-NULL in
264 * session.c:session_free(). It is important that they are
265 * properly initialized.
266 */
267 txn->sessid = NULL;
268 txn->srv_cookie = NULL;
269 txn->cli_cookie = NULL;
270 txn->uri = NULL;
271 txn->req.cap = NULL;
272 txn->rsp.cap = NULL;
273 txn->hdr_idx.v = NULL;
274 txn->hdr_idx.size = txn->hdr_idx.used = 0;
275 txn->req.flags = 0;
276 txn->rsp.flags = 0;
277 /* the HTTP messages need to know what buffer they're associated with */
278 txn->req.buf = s->req;
279 txn->rsp.buf = s->rep;
280
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200281 /* finish initialization of the accepted file descriptor */
282 fd_insert(cfd);
283 fdtab[cfd].owner = &s->si[0];
284 fdtab[cfd].state = FD_STREADY;
285 fdtab[cfd].flags = 0;
Willy Tarreau1b79bde2012-05-07 17:01:39 +0200286 fdtab[cfd].cb[DIR_RD].f = s->si[0].sock.read;
Willy Tarreau1b79bde2012-05-07 17:01:39 +0200287 fdtab[cfd].cb[DIR_WR].f = s->si[0].sock.write;
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 Tarreau7bb68ab2012-05-13 14:48:59 +02001354 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT)) {
1355 if (s->req->prod->flags & SI_FL_NOHALF)
1356 s->req->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau060781f2012-05-07 16:50:03 +02001357 s->req->prod->sock.shutr(s->req->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001358 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001359
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001360 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001361
Willy Tarreau14641402009-12-29 14:49:56 +01001362 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1363 s->rep->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau060781f2012-05-07 16:50:03 +02001364 s->rep->cons->sock.shutw(s->rep->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001365 }
1366
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001367 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT)) {
1368 if (s->rep->prod->flags & SI_FL_NOHALF)
1369 s->rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau060781f2012-05-07 16:50:03 +02001370 s->rep->prod->sock.shutr(s->rep->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001371 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001372 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001373
1374 /* 1b: check for low-level errors reported at the stream interface.
1375 * First we check if it's a retryable error (in which case we don't
1376 * want to tell the buffer). Otherwise we report the error one level
1377 * upper by setting flags into the buffers. Note that the side towards
1378 * the client cannot have connect (hence retryable) errors. Also, the
1379 * connection setup code must be able to deal with any type of abort.
1380 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001381 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001382 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1383 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
Willy Tarreau060781f2012-05-07 16:50:03 +02001384 s->si[0].sock.shutr(&s->si[0]);
1385 s->si[0].sock.shutw(&s->si[0]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001386 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001387 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001388 s->be->be_counters.cli_aborts++;
1389 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001390 if (srv)
1391 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001392 if (!(s->flags & SN_ERR_MASK))
1393 s->flags |= SN_ERR_CLICL;
1394 if (!(s->flags & SN_FINST_MASK))
1395 s->flags |= SN_FINST_D;
1396 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001397 }
1398 }
1399
1400 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1401 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
Willy Tarreau060781f2012-05-07 16:50:03 +02001402 s->si[1].sock.shutr(&s->si[1]);
1403 s->si[1].sock.shutw(&s->si[1]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001404 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001405 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001406 if (srv)
1407 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001408 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001409 s->be->be_counters.srv_aborts++;
1410 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001411 if (srv)
1412 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001413 if (!(s->flags & SN_ERR_MASK))
1414 s->flags |= SN_ERR_SRVCL;
1415 if (!(s->flags & SN_FINST_MASK))
1416 s->flags |= SN_FINST_D;
1417 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001418 }
1419 /* note: maybe we should process connection errors here ? */
1420 }
1421
1422 if (s->si[1].state == SI_ST_CON) {
1423 /* we were trying to establish a connection on the server side,
1424 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1425 */
1426 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1427 sess_update_st_cer(s, &s->si[1]);
1428 else if (s->si[1].state == SI_ST_EST)
1429 sess_establish(s, &s->si[1]);
1430
1431 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1432 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1433 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1434 */
1435 }
1436
Willy Tarreau815a9b22010-07-27 17:15:12 +02001437 rq_prod_last = s->si[0].state;
1438 rq_cons_last = s->si[1].state;
1439 rp_cons_last = s->si[0].state;
1440 rp_prod_last = s->si[1].state;
1441
1442 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001443 /* Check for connection closure */
1444
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001445 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001446 "[%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 +01001447 now_ms, __FUNCTION__, __LINE__,
1448 t,
1449 s, s->flags,
1450 s->req, s->rep,
1451 s->req->rex, s->rep->wex,
1452 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001453 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 +01001454 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001455 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001456
1457 /* nothing special to be done on client side */
1458 if (unlikely(s->req->prod->state == SI_ST_DIS))
1459 s->req->prod->state = SI_ST_CLO;
1460
1461 /* When a server-side connection is released, we have to count it and
1462 * check for pending connections on this server.
1463 */
1464 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1465 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001466 srv = target_srv(&s->target);
1467 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001468 if (s->flags & SN_CURR_SESS) {
1469 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001470 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001471 }
1472 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001473 if (may_dequeue_tasks(srv, s->be))
1474 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001475 }
1476 }
1477
1478 /*
1479 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1480 * at this point.
1481 */
1482
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001483 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001484 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001485 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001486 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1487 s->si[0].state != rq_prod_last ||
1488 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001489 unsigned int flags = s->req->flags;
1490
1491 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001492 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001493 unsigned int ana_list;
1494 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001495
Willy Tarreau90deb182010-01-07 00:20:41 +01001496 /* it's up to the analysers to stop new connections,
1497 * disable reading or closing. Note: if an analyser
1498 * disables any of these bits, it is responsible for
1499 * enabling them again when it disables itself, so
1500 * that other analysers are called in similar conditions.
1501 */
1502 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001503 buffer_auto_connect(s->req);
1504 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001505
1506 /* We will call all analysers for which a bit is set in
1507 * s->req->analysers, following the bit order from LSB
1508 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001509 * the list when not needed. Any analyser may return 0
1510 * to break out of the loop, either because of missing
1511 * data to take a decision, or because it decides to
1512 * kill the session. We loop at least once through each
1513 * analyser, and we may loop again if other analysers
1514 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001515 *
1516 * We build a list of analysers to run. We evaluate all
1517 * of these analysers in the order of the lower bit to
1518 * the higher bit. This ordering is very important.
1519 * An analyser will often add/remove other analysers,
1520 * including itself. Any changes to itself have no effect
1521 * on the loop. If it removes any other analysers, we
1522 * want those analysers not to be called anymore during
1523 * this loop. If it adds an analyser that is located
1524 * after itself, we want it to be scheduled for being
1525 * processed during the loop. If it adds an analyser
1526 * which is located before it, we want it to switch to
1527 * it immediately, even if it has already been called
1528 * once but removed since.
1529 *
1530 * In order to achieve this, we compare the analyser
1531 * list after the call with a copy of it before the
1532 * call. The work list is fed with analyser bits that
1533 * appeared during the call. Then we compare previous
1534 * work list with the new one, and check the bits that
1535 * appeared. If the lowest of these bits is lower than
1536 * the current bit, it means we have enabled a previous
1537 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001538 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001539
1540 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001541 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001542 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001543
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001544 if (ana_list & AN_REQ_DECODE_PROXY) {
1545 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1546 break;
1547 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1548 }
1549
Willy Tarreaufb356202010-08-03 14:02:05 +02001550 if (ana_list & AN_REQ_INSPECT_FE) {
1551 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001552 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001553 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001554 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001555
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001556 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001557 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001558 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001559 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001560 }
1561
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001562 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001563 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1564 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001565 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001566 }
1567
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001568 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001569 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1570 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001571 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001572 }
1573
Willy Tarreaufb356202010-08-03 14:02:05 +02001574 if (ana_list & AN_REQ_INSPECT_BE) {
1575 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1576 break;
1577 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1578 }
1579
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001580 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001581 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1582 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001583 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001584 }
1585
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001586 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001587 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001588 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001589 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001590 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001591
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001592 if (ana_list & AN_REQ_SRV_RULES) {
1593 if (!process_server_rules(s, s->req, AN_REQ_SRV_RULES))
1594 break;
1595 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
1596 }
1597
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001598 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001599 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1600 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001601 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001602 }
1603
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001604 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001605 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001606 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001607 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001608 }
Emeric Brun647caf12009-06-30 17:57:00 +02001609
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001610 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001611 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1612 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001613 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001614 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001615
Emeric Brun1d33b292010-01-04 15:47:17 +01001616 if (ana_list & AN_REQ_STICKING_RULES) {
1617 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1618 break;
1619 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1620 }
1621
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001622 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001623 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1624 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001625 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001626 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001627 break;
1628 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001629 }
Willy Tarreau84455332009-03-15 22:34:05 +01001630
Willy Tarreau815a9b22010-07-27 17:15:12 +02001631 rq_prod_last = s->si[0].state;
1632 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001633 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001634 rqf_last = s->req->flags;
1635
1636 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001637 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001638 }
1639
Willy Tarreau576507f2010-01-07 00:09:04 +01001640 /* we'll monitor the request analysers while parsing the response,
1641 * because some response analysers may indirectly enable new request
1642 * analysers (eg: HTTP keep-alive).
1643 */
1644 req_ana_back = s->req->analysers;
1645
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001646 resync_response:
1647 /* Analyse response */
1648
1649 if (unlikely(s->rep->flags & BF_HIJACK)) {
1650 /* In inject mode, we wake up everytime something has
1651 * happened on the write side of the buffer.
1652 */
1653 unsigned int flags = s->rep->flags;
1654
1655 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1656 !(s->rep->flags & BF_FULL)) {
1657 s->rep->hijacker(s, s->rep);
1658 }
1659
1660 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1661 rpf_last = s->rep->flags;
1662 goto resync_response;
1663 }
1664 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001665 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001666 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1667 s->si[0].state != rp_cons_last ||
1668 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001669 unsigned int flags = s->rep->flags;
1670
Willy Tarreau0499e352010-12-17 07:13:42 +01001671 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1672 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1673 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1674 * for some free space in the response buffer, so we might need to call
1675 * it when something changes in the response buffer, but still we pass
1676 * it the request buffer. Note that the SI state might very well still
1677 * be zero due to us returning a flow of redirects!
1678 */
1679 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1680 s->req->flags |= BF_WAKE_ONCE;
1681 }
1682
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001683 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001684 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001685 unsigned int ana_list;
1686 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001687
Willy Tarreau90deb182010-01-07 00:20:41 +01001688 /* it's up to the analysers to stop disable reading or
1689 * closing. Note: if an analyser disables any of these
1690 * bits, it is responsible for enabling them again when
1691 * it disables itself, so that other analysers are called
1692 * in similar conditions.
1693 */
1694 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001695 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001696
1697 /* We will call all analysers for which a bit is set in
1698 * s->rep->analysers, following the bit order from LSB
1699 * to MSB. The analysers must remove themselves from
1700 * the list when not needed. Any analyser may return 0
1701 * to break out of the loop, either because of missing
1702 * data to take a decision, or because it decides to
1703 * kill the session. We loop at least once through each
1704 * analyser, and we may loop again if other analysers
1705 * are added in the middle.
1706 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001707
1708 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001709 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001710 /* Warning! ensure that analysers are always placed in ascending order! */
1711
Emeric Brun97679e72010-09-23 17:56:44 +02001712 if (ana_list & AN_RES_INSPECT) {
1713 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1714 break;
1715 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1716 }
1717
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001718 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001719 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1720 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001721 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001722 }
1723
Emeric Brun1d33b292010-01-04 15:47:17 +01001724 if (ana_list & AN_RES_STORE_RULES) {
1725 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1726 break;
1727 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1728 }
1729
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001730 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001731 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1732 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001733 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001734 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001735
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001736 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001737 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1738 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001739 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001740 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001741 break;
1742 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001743 }
1744
Willy Tarreau815a9b22010-07-27 17:15:12 +02001745 rp_cons_last = s->si[0].state;
1746 rp_prod_last = s->si[1].state;
1747 rpf_last = s->rep->flags;
1748
1749 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001750 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001751 }
1752
Willy Tarreau576507f2010-01-07 00:09:04 +01001753 /* maybe someone has added some request analysers, so we must check and loop */
1754 if (s->req->analysers & ~req_ana_back)
1755 goto resync_request;
1756
Willy Tarreau0499e352010-12-17 07:13:42 +01001757 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1758 goto resync_request;
1759
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001760 /* FIXME: here we should call protocol handlers which rely on
1761 * both buffers.
1762 */
1763
1764
1765 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001766 * Now we propagate unhandled errors to the session. Normally
1767 * we're just in a data phase here since it means we have not
1768 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001769 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001770 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001771 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001772 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1773 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001774 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001775 if (s->req->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001776 s->be->be_counters.cli_aborts++;
1777 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001778 if (srv)
1779 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001780 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001781 }
1782 else if (s->req->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001783 s->be->be_counters.cli_aborts++;
1784 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001785 if (srv)
1786 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001787 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001788 }
1789 else if (s->req->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001790 s->be->be_counters.srv_aborts++;
1791 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001792 if (srv)
1793 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001794 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001795 }
1796 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001797 s->be->be_counters.srv_aborts++;
1798 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001799 if (srv)
1800 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001801 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001802 }
Willy Tarreau84455332009-03-15 22:34:05 +01001803 sess_set_term_flags(s);
1804 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001805 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1806 /* Report it if the server got an error or a read timeout expired */
1807 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001808 if (s->rep->flags & BF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001809 s->be->be_counters.srv_aborts++;
1810 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001811 if (srv)
1812 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001813 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001814 }
1815 else if (s->rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001816 s->be->be_counters.srv_aborts++;
1817 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001818 if (srv)
1819 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001820 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001821 }
1822 else if (s->rep->flags & BF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001823 s->be->be_counters.cli_aborts++;
1824 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001825 if (srv)
1826 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001827 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001828 }
1829 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001830 s->be->be_counters.cli_aborts++;
1831 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001832 if (srv)
1833 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001834 s->flags |= SN_ERR_CLITO;
1835 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001836 sess_set_term_flags(s);
1837 }
Willy Tarreau84455332009-03-15 22:34:05 +01001838 }
1839
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001840 /*
1841 * Here we take care of forwarding unhandled data. This also includes
1842 * connection establishments and shutdown requests.
1843 */
1844
1845
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001846 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001847 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001848 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1849 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001850 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001851 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001852 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001853 (s->req->prod->state >= SI_ST_EST) &&
1854 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001855 /* This buffer is freewheeling, there's no analyser nor hijacker
1856 * attached to it. If any data are left in, we'll permit them to
1857 * move.
1858 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001859 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001860 buffer_auto_connect(s->req);
1861 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001862 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001863
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001864 /* We'll let data flow between the producer (if still connected)
1865 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001866 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001867 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001868 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001869 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001870
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001871 /* check if it is wise to enable kernel splicing to forward request data */
1872 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1873 s->req->to_forward &&
1874 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001875 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001876 (pipes_used < global.maxpipes) &&
1877 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1878 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1879 (s->req->flags & BF_STREAMER_FAST)))) {
1880 s->req->flags |= BF_KERN_SPLICING;
1881 }
1882
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001883 /* reflect what the L7 analysers have seen last */
1884 rqf_last = s->req->flags;
1885
1886 /*
1887 * Now forward all shutdown requests between both sides of the buffer
1888 */
1889
Willy Tarreau520d95e2009-09-19 21:04:57 +02001890 /* first, let's check if the request buffer needs to shutdown(write), which may
1891 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001892 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001893 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001894 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001895 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001896 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001897
1898 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001899 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 +02001900 s->req->cons->sock.shutw(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001901
1902 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001903 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1904 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001905 buffer_shutr_now(s->req);
1906
1907 /* shutdown(read) pending */
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001908 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) {
1909 if (s->req->prod->flags & SI_FL_NOHALF)
1910 s->req->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau060781f2012-05-07 16:50:03 +02001911 s->req->prod->sock.shutr(s->req->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001912 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001913
Willy Tarreau520d95e2009-09-19 21:04:57 +02001914 /* it's possible that an upper layer has requested a connection setup or abort.
1915 * There are 2 situations where we decide to establish a new connection :
1916 * - there are data scheduled for emission in the buffer
1917 * - the BF_AUTO_CONNECT flag is set (active connection)
1918 */
1919 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001920 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001921 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01001922 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02001923 * switch to the connected state, otherwise we perform a connection
1924 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001925 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001926 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001927 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau26d8c592012-05-07 18:12:14 +02001928 if (unlikely(s->req->cons->target.type == TARG_TYPE_APPLET &&
1929 !(s->req->cons->proto && s->req->cons->proto->connect))) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001930 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001931 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1932 s->req->wex = TICK_ETERNITY;
1933 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001934 }
Willy Tarreau73201222009-08-16 18:27:24 +02001935 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001936 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001937 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001938 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1939 buffer_shutr_now(s->rep);
1940 }
Willy Tarreau92795622009-03-06 12:51:23 +01001941 }
1942
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001943
1944 /* we may have a pending connection request, or a connection waiting
1945 * for completion.
1946 */
1947 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1948 do {
1949 /* nb: step 1 might switch from QUE to ASS, but we first want
1950 * to give a chance to step 2 to perform a redirect if needed.
1951 */
1952 if (s->si[1].state != SI_ST_REQ)
1953 sess_update_stream_int(s, &s->si[1]);
1954 if (s->si[1].state == SI_ST_REQ)
1955 sess_prepare_conn_req(s, &s->si[1]);
1956
Willy Tarreau827aee92011-03-10 16:55:02 +01001957 srv = target_srv(&s->target);
1958 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001959 perform_http_redirect(s, &s->si[1]);
1960 } while (s->si[1].state == SI_ST_ASS);
Mark Lamourinec2247f02012-01-04 13:02:01 -05001961
1962 /* Now we can add the server name to a header (if requested) */
1963 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Stathis Voukelatos09a030a2012-01-09 14:27:13 +01001964 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau45c0d982012-03-09 12:11:57 +01001965 (s->be->mode == PR_MODE_HTTP) &&
1966 (s->be->server_id_hdr_name != NULL)) {
1967 http_send_name_header(&s->txn, s->be, target_srv(&s->target)->id);
Mark Lamourinec2247f02012-01-04 13:02:01 -05001968 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001969 }
1970
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001971 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001972 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001973 goto resync_stream_interface;
1974
Willy Tarreau815a9b22010-07-27 17:15:12 +02001975 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001976 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001977 goto resync_request;
1978
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001979 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001980
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001981 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001982 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001983 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1984 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001985 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001986 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001987 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001988 (s->rep->prod->state >= SI_ST_EST) &&
1989 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001990 /* This buffer is freewheeling, there's no analyser nor hijacker
1991 * attached to it. If any data are left in, we'll permit them to
1992 * move.
1993 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001994 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001995 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001996 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001997
1998 /* We'll let data flow between the producer (if still connected)
1999 * to the consumer.
2000 */
2001 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02002002 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002003
2004 /* if we have no analyser anymore in any direction and have a
2005 * tunnel timeout set, use it now.
2006 */
2007 if (!s->req->analysers && s->be->timeout.tunnel) {
2008 s->req->rto = s->req->wto = s->rep->rto = s->rep->wto =
2009 s->be->timeout.tunnel;
2010 s->req->rex = s->req->wex = s->rep->rex = s->rep->wex =
2011 tick_add(now_ms, s->be->timeout.tunnel);
2012 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002013 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002014
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002015 /* check if it is wise to enable kernel splicing to forward response data */
2016 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
2017 s->rep->to_forward &&
2018 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02002019 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002020 (pipes_used < global.maxpipes) &&
2021 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2022 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
2023 (s->rep->flags & BF_STREAMER_FAST)))) {
2024 s->rep->flags |= BF_KERN_SPLICING;
2025 }
2026
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002027 /* reflect what the L7 analysers have seen last */
2028 rpf_last = s->rep->flags;
2029
2030 /*
2031 * Now forward all shutdown requests between both sides of the buffer
2032 */
2033
2034 /*
2035 * FIXME: this is probably where we should produce error responses.
2036 */
2037
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002038 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002039 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
2040 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002041 buffer_shutw_now(s->rep);
2042
2043 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02002044 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 +02002045 s->rep->cons->sock.shutw(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002046
2047 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002048 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
2049 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002050 buffer_shutr_now(s->rep);
2051
2052 /* shutdown(read) pending */
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002053 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) {
2054 if (s->rep->prod->flags & SI_FL_NOHALF)
2055 s->rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau060781f2012-05-07 16:50:03 +02002056 s->rep->prod->sock.shutr(s->rep->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002057 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002058
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002059 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002060 goto resync_stream_interface;
2061
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002062 if (s->req->flags != rqf_last)
2063 goto resync_request;
2064
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002065 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002066 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002067
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002068 /* we're interested in getting wakeups again */
2069 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
2070 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
2071
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002072 /* This is needed only when debugging is enabled, to indicate
2073 * client-side or server-side close. Please note that in the unlikely
2074 * event where both sides would close at once, the sequence is reported
2075 * on the server side first.
2076 */
2077 if (unlikely((global.mode & MODE_DEBUG) &&
2078 (!(global.mode & MODE_QUIET) ||
2079 (global.mode & MODE_VERBOSE)))) {
2080 int len;
2081
2082 if (s->si[1].state == SI_ST_CLO &&
2083 s->si[1].prev_state == SI_ST_EST) {
2084 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
2085 s->uniq_id, s->be->id,
2086 (unsigned short)s->si[0].fd,
2087 (unsigned short)s->si[1].fd);
Willy Tarreau21337822012-04-29 14:11:38 +02002088 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002089 }
2090
2091 if (s->si[0].state == SI_ST_CLO &&
2092 s->si[0].prev_state == SI_ST_EST) {
2093 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
2094 s->uniq_id, s->be->id,
2095 (unsigned short)s->si[0].fd,
2096 (unsigned short)s->si[1].fd);
Willy Tarreau21337822012-04-29 14:11:38 +02002097 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002098 }
2099 }
2100
2101 if (likely((s->rep->cons->state != SI_ST_CLO) ||
2102 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
2103
2104 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
2105 session_process_counters(s);
2106
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002107 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreau060781f2012-05-07 16:50:03 +02002108 s->rep->cons->sock.update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002109
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002110 if (s->req->cons->state == SI_ST_EST && s->req->cons->target.type != TARG_TYPE_APPLET)
Willy Tarreau060781f2012-05-07 16:50:03 +02002111 s->req->cons->sock.update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002112
Willy Tarreaua6eebb32010-06-04 11:40:20 +02002113 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
2114 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 +01002115 s->si[0].prev_state = s->si[0].state;
2116 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002117 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2118 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002119
2120 /* Trick: if a request is being waiting for the server to respond,
2121 * and if we know the server can timeout, we don't want the timeout
2122 * to expire on the client side first, but we're still interested
2123 * in passing data from the client to the server (eg: POST). Thus,
2124 * we can cancel the client's request timeout if the server's
2125 * request timeout is set and the server has not yet sent a response.
2126 */
2127
Willy Tarreau520d95e2009-09-19 21:04:57 +02002128 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01002129 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
2130 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002131 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01002132 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002133
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002134 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002135 * Note that this one may wake the task up again.
2136 */
Willy Tarreau7c0a1512011-03-10 11:17:02 +01002137 if (s->req->cons->target.type == TARG_TYPE_APPLET ||
2138 s->rep->cons->target.type == TARG_TYPE_APPLET) {
2139 if (s->req->cons->target.type == TARG_TYPE_APPLET)
2140 s->req->cons->target.ptr.a->fct(s->req->cons);
2141 if (s->rep->cons->target.type == TARG_TYPE_APPLET)
2142 s->rep->cons->target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002143 if (task_in_rq(t)) {
2144 /* If we woke up, we don't want to requeue the
2145 * task to the wait queue, but rather requeue
2146 * it into the runqueue ASAP.
2147 */
2148 t->expire = TICK_ETERNITY;
2149 return t;
2150 }
2151 }
2152
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002153 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2154 tick_first(s->rep->rex, s->rep->wex));
2155 if (s->req->analysers)
2156 t->expire = tick_first(t->expire, s->req->analyse_exp);
2157
2158 if (s->si[0].exp)
2159 t->expire = tick_first(t->expire, s->si[0].exp);
2160
2161 if (s->si[1].exp)
2162 t->expire = tick_first(t->expire, s->si[1].exp);
2163
2164#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002165 fprintf(stderr,
2166 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2167 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2168 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2169 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 +01002170#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002171
2172#ifdef DEBUG_DEV
2173 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002174 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002175 ABORT_NOW();
2176#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002177 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002178 }
2179
2180 s->fe->feconn--;
2181 if (s->flags & SN_BE_ASSIGNED)
2182 s->be->beconn--;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002183 if (!(s->listener->options & LI_O_UNLIMITED))
2184 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002185 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002186 s->listener->nbconn--;
Willy Tarreau62793712011-07-24 19:23:38 +02002187 if (s->listener->state == LI_FULL)
2188 resume_listener(s->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002189
Willy Tarreau08ceb102011-07-24 22:58:00 +02002190 /* Dequeues all of the listeners waiting for a resource */
2191 if (!LIST_ISEMPTY(&global_listener_queue))
2192 dequeue_all_listeners(&global_listener_queue);
2193
Willy Tarreaub32907b2011-07-25 08:37:44 +02002194 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
2195 (!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 +02002196 dequeue_all_listeners(&s->fe->listener_queue);
2197
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002198 if (unlikely((global.mode & MODE_DEBUG) &&
2199 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2200 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002201 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002202 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002203 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau21337822012-04-29 14:11:38 +02002204 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002205 }
2206
2207 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2208 session_process_counters(s);
2209
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002210 if (s->txn.status) {
2211 int n;
2212
2213 n = s->txn.status / 100;
2214 if (n < 1 || n > 5)
2215 n = 0;
2216
2217 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002218 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002219
Willy Tarreau24657792010-02-26 10:30:28 +01002220 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002221 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002222 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002223 }
2224
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002225 /* let's do a final log if we need it */
2226 if (s->logs.logwait &&
2227 !(s->flags & SN_MONITOR) &&
2228 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002229 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002230 }
2231
2232 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002233 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002234 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002235 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002236 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002237}
2238
Willy Tarreau7c669d72008-06-20 15:04:11 +02002239/*
2240 * This function adjusts sess->srv_conn and maintains the previous and new
2241 * server's served session counts. Setting newsrv to NULL is enough to release
2242 * current connection slot. This function also notifies any LB algo which might
2243 * expect to be informed about any change in the number of active sessions on a
2244 * server.
2245 */
2246void sess_change_server(struct session *sess, struct server *newsrv)
2247{
2248 if (sess->srv_conn == newsrv)
2249 return;
2250
2251 if (sess->srv_conn) {
2252 sess->srv_conn->served--;
2253 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2254 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002255 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002256 }
2257
2258 if (newsrv) {
2259 newsrv->served++;
2260 if (newsrv->proxy->lbprm.server_take_conn)
2261 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002262 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002263 }
2264}
2265
Willy Tarreau84455332009-03-15 22:34:05 +01002266/* Handle server-side errors for default protocols. It is called whenever a a
2267 * connection setup is aborted or a request is aborted in queue. It sets the
2268 * session termination flags so that the caller does not have to worry about
2269 * them. It's installed as ->srv_error for the server-side stream_interface.
2270 */
2271void default_srv_error(struct session *s, struct stream_interface *si)
2272{
2273 int err_type = si->err_type;
2274 int err = 0, fin = 0;
2275
2276 if (err_type & SI_ET_QUEUE_ABRT) {
2277 err = SN_ERR_CLICL;
2278 fin = SN_FINST_Q;
2279 }
2280 else if (err_type & SI_ET_CONN_ABRT) {
2281 err = SN_ERR_CLICL;
2282 fin = SN_FINST_C;
2283 }
2284 else if (err_type & SI_ET_QUEUE_TO) {
2285 err = SN_ERR_SRVTO;
2286 fin = SN_FINST_Q;
2287 }
2288 else if (err_type & SI_ET_QUEUE_ERR) {
2289 err = SN_ERR_SRVCL;
2290 fin = SN_FINST_Q;
2291 }
2292 else if (err_type & SI_ET_CONN_TO) {
2293 err = SN_ERR_SRVTO;
2294 fin = SN_FINST_C;
2295 }
2296 else if (err_type & SI_ET_CONN_ERR) {
2297 err = SN_ERR_SRVCL;
2298 fin = SN_FINST_C;
2299 }
2300 else /* SI_ET_CONN_OTHER and others */ {
2301 err = SN_ERR_INTERNAL;
2302 fin = SN_FINST_C;
2303 }
2304
2305 if (!(s->flags & SN_ERR_MASK))
2306 s->flags |= err;
2307 if (!(s->flags & SN_FINST_MASK))
2308 s->flags |= fin;
2309}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002310
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002311/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
2312void session_shutdown(struct session *session, int why)
2313{
2314 if (session->req->flags & (BF_SHUTW|BF_SHUTW_NOW))
2315 return;
2316
2317 buffer_shutw_now(session->req);
2318 buffer_shutr_now(session->rep);
2319 session->task->nice = 1024;
2320 if (!(session->flags & SN_ERR_MASK))
2321 session->flags |= why;
2322 task_wakeup(session->task, TASK_WOKEN_OTHER);
2323}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002324
Willy Tarreau8b22a712010-06-18 17:46:06 +02002325/************************************************************************/
2326/* All supported ACL keywords must be declared here. */
2327/************************************************************************/
2328
Willy Tarreaua5e37562011-12-16 17:06:15 +01002329/* set temp integer to the General Purpose Counter 0 value in the stksess entry <ts> */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002330static int
Willy Tarreau37406352012-04-23 16:16:37 +02002331acl_fetch_get_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002332{
Willy Tarreau37406352012-04-23 16:16:37 +02002333 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002334 smp->type = SMP_T_UINT;
2335 smp->data.uint = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002336 if (ts != NULL) {
2337 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2338 if (!ptr)
2339 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002340 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002341 }
2342 return 1;
2343}
2344
Willy Tarreaua5e37562011-12-16 17:06:15 +01002345/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002346 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002347 */
2348static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002349acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002350 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002351{
Willy Tarreau56123282010-08-06 19:06:56 +02002352 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002353 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002354 return acl_fetch_get_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002355}
2356
Willy Tarreaua5e37562011-12-16 17:06:15 +01002357/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002358 * backend counters.
2359 */
2360static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002361acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002362 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002363{
Willy Tarreau56123282010-08-06 19:06:56 +02002364 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002365 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002366 return acl_fetch_get_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002367}
2368
Willy Tarreaua5e37562011-12-16 17:06:15 +01002369/* set temp integer to the General Purpose Counter 0 value from the session's source
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002370 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002371 * Accepts exactly 1 argument of type table.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002372 */
2373static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002374acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002375 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002376{
2377 struct stktable_key *key;
2378
David du Colombier4f92d322011-03-24 11:09:31 +01002379 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002380 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002381 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002382
Willy Tarreau24e32d82012-04-23 23:55:44 +02002383 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002384 return acl_fetch_get_gpc0(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002385}
2386
2387/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002388 * return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002389 */
2390static int
Willy Tarreau37406352012-04-23 16:16:37 +02002391acl_fetch_inc_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002392{
Willy Tarreau37406352012-04-23 16:16:37 +02002393 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002394 smp->type = SMP_T_UINT;
2395 smp->data.uint = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002396 if (ts != NULL) {
2397 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2398 if (!ptr)
2399 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002400 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002401 }
2402 return 1;
2403}
2404
2405/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002406 * frontend counters and return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002407 */
2408static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002409acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002410 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002411{
Willy Tarreau56123282010-08-06 19:06:56 +02002412 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002413 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002414 return acl_fetch_inc_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002415}
2416
2417/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002418 * backend counters and return it into temp integer.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002419 */
2420static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002421acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002422 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002423{
Willy Tarreau56123282010-08-06 19:06:56 +02002424 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002425 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002426 return acl_fetch_inc_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002427}
2428
2429/* Increment the General Purpose Counter 0 value from the session's source
Willy Tarreaua5e37562011-12-16 17:06:15 +01002430 * address in the table pointed to by expr, and return it into temp integer.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002431 * Accepts exactly 1 argument of type table.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002432 */
2433static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002434acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002435 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002436{
2437 struct stktable_key *key;
2438
David du Colombier4f92d322011-03-24 11:09:31 +01002439 key = tcp_src_to_stktable_key(l4);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002440 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002441 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002442
Willy Tarreau24e32d82012-04-23 23:55:44 +02002443 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002444 return acl_fetch_inc_gpc0(&px->table, smp, stktable_update_key(&px->table, key));
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002445}
2446
Willy Tarreauf73cd112011-08-13 01:45:16 +02002447/* Clear the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002448 * return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002449 */
2450static int
Willy Tarreau37406352012-04-23 16:16:37 +02002451acl_fetch_clr_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002452{
Willy Tarreau37406352012-04-23 16:16:37 +02002453 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002454 smp->type = SMP_T_UINT;
2455 smp->data.uint = 0;
Willy Tarreauf73cd112011-08-13 01:45:16 +02002456 if (ts != NULL) {
2457 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2458 if (!ptr)
2459 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002460 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002461 stktable_data_cast(ptr, gpc0) = 0;
2462 }
2463 return 1;
2464}
2465
2466/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002467 * frontend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002468 */
2469static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002470acl_fetch_sc1_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002471 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002472{
2473 if (!l4->stkctr1_entry)
2474 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002475 return acl_fetch_clr_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002476}
2477
2478/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002479 * backend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002480 */
2481static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002482acl_fetch_sc2_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002483 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002484{
2485 if (!l4->stkctr2_entry)
2486 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002487 return acl_fetch_clr_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002488}
2489
2490/* Clear the General Purpose Counter 0 value from the session's source address
Willy Tarreaua5e37562011-12-16 17:06:15 +01002491 * in the table pointed to by expr, and return its previous value into temp integer.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002492 * Accepts exactly 1 argument of type table.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002493 */
2494static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002495acl_fetch_src_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002496 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002497{
2498 struct stktable_key *key;
2499
2500 key = tcp_src_to_stktable_key(l4);
2501 if (!key)
2502 return 0;
2503
Willy Tarreau24e32d82012-04-23 23:55:44 +02002504 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002505 return acl_fetch_clr_gpc0(&px->table, smp, stktable_update_key(&px->table, key));
Willy Tarreauf73cd112011-08-13 01:45:16 +02002506}
2507
Willy Tarreaua5e37562011-12-16 17:06:15 +01002508/* set temp integer to the cumulated number of connections in the stksess entry <ts> */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002509static int
Willy Tarreau37406352012-04-23 16:16:37 +02002510acl_fetch_conn_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002511{
Willy Tarreau37406352012-04-23 16:16:37 +02002512 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002513 smp->type = SMP_T_UINT;
2514 smp->data.uint = 0;
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002515 if (ts != NULL) {
2516 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2517 if (!ptr)
2518 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002519 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002520 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002521 return 1;
2522}
2523
Willy Tarreaua5e37562011-12-16 17:06:15 +01002524/* set temp integer to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002525static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002526acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002527 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002528{
Willy Tarreau56123282010-08-06 19:06:56 +02002529 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002530 return 0;
2531
Willy Tarreau37406352012-04-23 16:16:37 +02002532 return acl_fetch_conn_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002533}
2534
Willy Tarreaua5e37562011-12-16 17:06:15 +01002535/* set temp integer to the cumulated number of connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002536static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002537acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002538 const struct arg *args, struct sample *smp)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002539{
Willy Tarreau56123282010-08-06 19:06:56 +02002540 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002541 return 0;
2542
Willy Tarreau37406352012-04-23 16:16:37 +02002543 return acl_fetch_conn_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002544}
2545
Willy Tarreaua5e37562011-12-16 17:06:15 +01002546/* set temp integer to the cumulated number of connections from the session's source
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002547 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002548 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002549 */
2550static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002551acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002552 const struct arg *args, struct sample *smp)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002553{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002554 struct stktable_key *key;
2555
David du Colombier4f92d322011-03-24 11:09:31 +01002556 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002557 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002558 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002559
Willy Tarreau24e32d82012-04-23 23:55:44 +02002560 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002561 return acl_fetch_conn_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002562}
2563
Willy Tarreaua5e37562011-12-16 17:06:15 +01002564/* set temp integer to the connection rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002565static int
Willy Tarreau37406352012-04-23 16:16:37 +02002566acl_fetch_conn_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002567{
Willy Tarreau37406352012-04-23 16:16:37 +02002568 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002569 smp->type = SMP_T_UINT;
2570 smp->data.uint = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002571 if (ts != NULL) {
2572 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2573 if (!ptr)
2574 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002575 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002576 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2577 }
2578 return 1;
2579}
2580
Willy Tarreaua5e37562011-12-16 17:06:15 +01002581/* set temp integer to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002582 * the configured period.
2583 */
2584static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002585acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002586 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002587{
Willy Tarreau56123282010-08-06 19:06:56 +02002588 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002589 return 0;
2590
Willy Tarreau37406352012-04-23 16:16:37 +02002591 return acl_fetch_conn_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002592}
2593
Willy Tarreaua5e37562011-12-16 17:06:15 +01002594/* set temp integer to the connection rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002595 * the configured period.
2596 */
2597static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002598acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002599 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002600{
Willy Tarreau56123282010-08-06 19:06:56 +02002601 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002602 return 0;
2603
Willy Tarreau37406352012-04-23 16:16:37 +02002604 return acl_fetch_conn_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002605}
2606
Willy Tarreaua5e37562011-12-16 17:06:15 +01002607/* set temp integer to the connection rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002608 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002609 * Accepts exactly 1 argument of type table.
Willy Tarreau91c43d72010-06-20 11:19:22 +02002610 */
2611static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002612acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002613 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002614{
2615 struct stktable_key *key;
2616
David du Colombier4f92d322011-03-24 11:09:31 +01002617 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002618 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002619 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002620
Willy Tarreau24e32d82012-04-23 23:55:44 +02002621 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002622 return acl_fetch_conn_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau91c43d72010-06-20 11:19:22 +02002623}
2624
Willy Tarreaua5e37562011-12-16 17:06:15 +01002625/* set temp integer to the number of connections from the session's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002626 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002627 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002628 */
2629static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002630acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002631 const struct arg *args, struct sample *smp)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002632{
2633 struct stksess *ts;
2634 struct stktable_key *key;
2635 void *ptr;
2636
David du Colombier4f92d322011-03-24 11:09:31 +01002637 key = tcp_src_to_stktable_key(l4);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002638 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002639 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002640
Willy Tarreau24e32d82012-04-23 23:55:44 +02002641 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002642
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002643 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2644 /* entry does not exist and could not be created */
2645 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002646
2647 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2648 if (!ptr)
2649 return 0; /* parameter not stored in this table */
2650
Willy Tarreauf853c462012-04-23 18:53:56 +02002651 smp->type = SMP_T_UINT;
2652 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002653 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002654 return 1;
2655}
2656
Willy Tarreaua5e37562011-12-16 17:06:15 +01002657/* set temp integer to the number of concurrent connections in the stksess entry <ts> */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002658static int
Willy Tarreau37406352012-04-23 16:16:37 +02002659acl_fetch_conn_cur(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002660{
Willy Tarreau37406352012-04-23 16:16:37 +02002661 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002662 smp->type = SMP_T_UINT;
2663 smp->data.uint = 0;
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002664
2665 if (ts != NULL) {
2666 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2667 if (!ptr)
2668 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002669 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002670 }
2671 return 1;
2672}
2673
Willy Tarreaua5e37562011-12-16 17:06:15 +01002674/* set temp integer to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002675static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002676acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002677 const struct arg *args, struct sample *smp)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002678{
Willy Tarreau56123282010-08-06 19:06:56 +02002679 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002680 return 0;
2681
Willy Tarreau37406352012-04-23 16:16:37 +02002682 return acl_fetch_conn_cur(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002683}
2684
Willy Tarreaua5e37562011-12-16 17:06:15 +01002685/* set temp integer to the number of concurrent connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002686static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002687acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002688 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002689{
Willy Tarreau56123282010-08-06 19:06:56 +02002690 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002691 return 0;
2692
Willy Tarreau37406352012-04-23 16:16:37 +02002693 return acl_fetch_conn_cur(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002694}
2695
Willy Tarreaua5e37562011-12-16 17:06:15 +01002696/* set temp integer to the number of concurrent connections from the session's source
Willy Tarreau38285c12010-06-18 16:35:43 +02002697 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002698 * Accepts exactly 1 argument of type table.
Willy Tarreau38285c12010-06-18 16:35:43 +02002699 */
2700static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002701acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002702 const struct arg *args, struct sample *smp)
Willy Tarreau38285c12010-06-18 16:35:43 +02002703{
Willy Tarreau38285c12010-06-18 16:35:43 +02002704 struct stktable_key *key;
2705
David du Colombier4f92d322011-03-24 11:09:31 +01002706 key = tcp_src_to_stktable_key(l4);
Willy Tarreau38285c12010-06-18 16:35:43 +02002707 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002708 return 0;
Willy Tarreau38285c12010-06-18 16:35:43 +02002709
Willy Tarreau24e32d82012-04-23 23:55:44 +02002710 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002711 return acl_fetch_conn_cur(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002712}
2713
Willy Tarreaua5e37562011-12-16 17:06:15 +01002714/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002715static int
Willy Tarreau37406352012-04-23 16:16:37 +02002716acl_fetch_sess_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002717{
Willy Tarreau37406352012-04-23 16:16:37 +02002718 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002719 smp->type = SMP_T_UINT;
2720 smp->data.uint = 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002721 if (ts != NULL) {
2722 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2723 if (!ptr)
2724 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002725 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002726 }
2727 return 1;
2728}
2729
Willy Tarreaua5e37562011-12-16 17:06:15 +01002730/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002731static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002732acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002733 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002734{
Willy Tarreau56123282010-08-06 19:06:56 +02002735 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002736 return 0;
2737
Willy Tarreau37406352012-04-23 16:16:37 +02002738 return acl_fetch_sess_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002739}
2740
Willy Tarreaua5e37562011-12-16 17:06:15 +01002741/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002742static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002743acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002744 const struct arg *args, struct sample *smp)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002745{
Willy Tarreau56123282010-08-06 19:06:56 +02002746 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002747 return 0;
2748
Willy Tarreau37406352012-04-23 16:16:37 +02002749 return acl_fetch_sess_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002750}
2751
Willy Tarreaua5e37562011-12-16 17:06:15 +01002752/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002753 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002754 * Accepts exactly 1 argument of type table.
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002755 */
2756static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002757acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002758 const struct arg *args, struct sample *smp)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002759{
2760 struct stktable_key *key;
2761
David du Colombier4f92d322011-03-24 11:09:31 +01002762 key = tcp_src_to_stktable_key(l4);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002763 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002764 return 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002765
Willy Tarreau24e32d82012-04-23 23:55:44 +02002766 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002767 return acl_fetch_sess_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002768}
2769
Willy Tarreaua5e37562011-12-16 17:06:15 +01002770/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002771static int
Willy Tarreau37406352012-04-23 16:16:37 +02002772acl_fetch_sess_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002773{
Willy Tarreau37406352012-04-23 16:16:37 +02002774 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002775 smp->type = SMP_T_UINT;
2776 smp->data.uint = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002777 if (ts != NULL) {
2778 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2779 if (!ptr)
2780 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002781 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002782 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2783 }
2784 return 1;
2785}
2786
Willy Tarreaua5e37562011-12-16 17:06:15 +01002787/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002788 * the configured period.
2789 */
2790static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002791acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002792 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002793{
Willy Tarreau56123282010-08-06 19:06:56 +02002794 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002795 return 0;
2796
Willy Tarreau37406352012-04-23 16:16:37 +02002797 return acl_fetch_sess_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002798}
2799
Willy Tarreaua5e37562011-12-16 17:06:15 +01002800/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002801 * the configured period.
2802 */
2803static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002804acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002805 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002806{
Willy Tarreau56123282010-08-06 19:06:56 +02002807 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002808 return 0;
2809
Willy Tarreau37406352012-04-23 16:16:37 +02002810 return acl_fetch_sess_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002811}
2812
Willy Tarreaua5e37562011-12-16 17:06:15 +01002813/* set temp integer to the session rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002814 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002815 * Accepts exactly 1 argument of type table.
Willy Tarreau91c43d72010-06-20 11:19:22 +02002816 */
2817static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002818acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002819 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002820{
2821 struct stktable_key *key;
2822
David du Colombier4f92d322011-03-24 11:09:31 +01002823 key = tcp_src_to_stktable_key(l4);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002824 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002825 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002826
Willy Tarreau24e32d82012-04-23 23:55:44 +02002827 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002828 return acl_fetch_sess_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau91c43d72010-06-20 11:19:22 +02002829}
2830
Willy Tarreaua5e37562011-12-16 17:06:15 +01002831/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002832static int
Willy Tarreau37406352012-04-23 16:16:37 +02002833acl_fetch_http_req_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002834{
Willy Tarreau37406352012-04-23 16:16:37 +02002835 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002836 smp->type = SMP_T_UINT;
2837 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002838 if (ts != NULL) {
2839 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2840 if (!ptr)
2841 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002842 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002843 }
2844 return 1;
2845}
2846
Willy Tarreaua5e37562011-12-16 17:06:15 +01002847/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002848static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002849acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002850 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002851{
Willy Tarreau56123282010-08-06 19:06:56 +02002852 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002853 return 0;
2854
Willy Tarreau37406352012-04-23 16:16:37 +02002855 return acl_fetch_http_req_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002856}
2857
Willy Tarreaua5e37562011-12-16 17:06:15 +01002858/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002859static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002860acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002861 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002862{
Willy Tarreau56123282010-08-06 19:06:56 +02002863 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002864 return 0;
2865
Willy Tarreau37406352012-04-23 16:16:37 +02002866 return acl_fetch_http_req_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002867}
2868
Willy Tarreaua5e37562011-12-16 17:06:15 +01002869/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02002870 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002871 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002872 */
2873static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002874acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002875 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002876{
2877 struct stktable_key *key;
2878
David du Colombier4f92d322011-03-24 11:09:31 +01002879 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002880 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002881 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002882
Willy Tarreau24e32d82012-04-23 23:55:44 +02002883 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002884 return acl_fetch_http_req_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002885}
2886
Willy Tarreaua5e37562011-12-16 17:06:15 +01002887/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002888static int
Willy Tarreau37406352012-04-23 16:16:37 +02002889acl_fetch_http_req_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002890{
Willy Tarreau37406352012-04-23 16:16:37 +02002891 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002892 smp->type = SMP_T_UINT;
2893 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002894 if (ts != NULL) {
2895 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2896 if (!ptr)
2897 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002898 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02002899 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2900 }
2901 return 1;
2902}
2903
Willy Tarreaua5e37562011-12-16 17:06:15 +01002904/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002905 * the configured period.
2906 */
2907static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002908acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002909 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002910{
Willy Tarreau56123282010-08-06 19:06:56 +02002911 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002912 return 0;
2913
Willy Tarreau37406352012-04-23 16:16:37 +02002914 return acl_fetch_http_req_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002915}
2916
Willy Tarreaua5e37562011-12-16 17:06:15 +01002917/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002918 * the configured period.
2919 */
2920static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002921acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002922 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002923{
Willy Tarreau56123282010-08-06 19:06:56 +02002924 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002925 return 0;
2926
Willy Tarreau37406352012-04-23 16:16:37 +02002927 return acl_fetch_http_req_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002928}
2929
Willy Tarreaua5e37562011-12-16 17:06:15 +01002930/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02002931 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002932 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002933 */
2934static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002935acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002936 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002937{
2938 struct stktable_key *key;
2939
David du Colombier4f92d322011-03-24 11:09:31 +01002940 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002941 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002942 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002943
Willy Tarreau24e32d82012-04-23 23:55:44 +02002944 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002945 return acl_fetch_http_req_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002946}
2947
Willy Tarreaua5e37562011-12-16 17:06:15 +01002948/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002949static int
Willy Tarreau37406352012-04-23 16:16:37 +02002950acl_fetch_http_err_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002951{
Willy Tarreau37406352012-04-23 16:16:37 +02002952 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002953 smp->type = SMP_T_UINT;
2954 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002955 if (ts != NULL) {
2956 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2957 if (!ptr)
2958 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002959 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002960 }
2961 return 1;
2962}
2963
Willy Tarreaua5e37562011-12-16 17:06:15 +01002964/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002965static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002966acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002967 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002968{
Willy Tarreau56123282010-08-06 19:06:56 +02002969 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002970 return 0;
2971
Willy Tarreau37406352012-04-23 16:16:37 +02002972 return acl_fetch_http_err_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002973}
2974
Willy Tarreaua5e37562011-12-16 17:06:15 +01002975/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002976static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002977acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002978 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002979{
Willy Tarreau56123282010-08-06 19:06:56 +02002980 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002981 return 0;
2982
Willy Tarreau37406352012-04-23 16:16:37 +02002983 return acl_fetch_http_err_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002984}
2985
Willy Tarreaua5e37562011-12-16 17:06:15 +01002986/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02002987 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002988 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002989 */
2990static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002991acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002992 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002993{
2994 struct stktable_key *key;
2995
David du Colombier4f92d322011-03-24 11:09:31 +01002996 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002997 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002998 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002999
Willy Tarreau24e32d82012-04-23 23:55:44 +02003000 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003001 return acl_fetch_http_err_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003002}
3003
Willy Tarreaua5e37562011-12-16 17:06:15 +01003004/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003005static int
Willy Tarreau37406352012-04-23 16:16:37 +02003006acl_fetch_http_err_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003007{
Willy Tarreau37406352012-04-23 16:16:37 +02003008 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003009 smp->type = SMP_T_UINT;
3010 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003011 if (ts != NULL) {
3012 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
3013 if (!ptr)
3014 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003015 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02003016 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
3017 }
3018 return 1;
3019}
3020
Willy Tarreaua5e37562011-12-16 17:06:15 +01003021/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02003022 * the configured period.
3023 */
3024static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003025acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003026 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003027{
Willy Tarreau56123282010-08-06 19:06:56 +02003028 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003029 return 0;
3030
Willy Tarreau37406352012-04-23 16:16:37 +02003031 return acl_fetch_http_err_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003032}
3033
Willy Tarreaua5e37562011-12-16 17:06:15 +01003034/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003035 * the configured period.
3036 */
3037static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003038acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003039 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003040{
Willy Tarreau56123282010-08-06 19:06:56 +02003041 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003042 return 0;
3043
Willy Tarreau37406352012-04-23 16:16:37 +02003044 return acl_fetch_http_err_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003045}
3046
Willy Tarreaua5e37562011-12-16 17:06:15 +01003047/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003048 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003049 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003050 */
3051static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003052acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003053 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003054{
3055 struct stktable_key *key;
3056
David du Colombier4f92d322011-03-24 11:09:31 +01003057 key = tcp_src_to_stktable_key(l4);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003058 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003059 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003060
Willy Tarreau24e32d82012-04-23 23:55:44 +02003061 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003062 return acl_fetch_http_err_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003063}
3064
Willy Tarreaua5e37562011-12-16 17:06:15 +01003065/* set temp integer to the number of kbytes received from clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003066static int
Willy Tarreau37406352012-04-23 16:16:37 +02003067acl_fetch_kbytes_in(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003068{
Willy Tarreau37406352012-04-23 16:16:37 +02003069 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003070 smp->type = SMP_T_UINT;
3071 smp->data.uint = 0;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003072
3073 if (ts != NULL) {
3074 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
3075 if (!ptr)
3076 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003077 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003078 }
3079 return 1;
3080}
3081
Willy Tarreaua5e37562011-12-16 17:06:15 +01003082/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003083 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003084 */
3085static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003086acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003087 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003088{
Willy Tarreau56123282010-08-06 19:06:56 +02003089 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003090 return 0;
3091
Willy Tarreau37406352012-04-23 16:16:37 +02003092 return acl_fetch_kbytes_in(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003093}
3094
Willy Tarreaua5e37562011-12-16 17:06:15 +01003095/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003096 * session's tracked BE counters.
3097 */
3098static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003099acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003100 const struct arg *args, struct sample *smp)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003101{
Willy Tarreau56123282010-08-06 19:06:56 +02003102 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003103 return 0;
3104
Willy Tarreau37406352012-04-23 16:16:37 +02003105 return acl_fetch_kbytes_in(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003106}
3107
Willy Tarreaua5e37562011-12-16 17:06:15 +01003108/* set temp integer to the number of kbytes received from the session's source
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003109 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003110 * Accepts exactly 1 argument of type table.
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003111 */
3112static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003113acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003114 const struct arg *args, struct sample *smp)
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003115{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003116 struct stktable_key *key;
3117
David du Colombier4f92d322011-03-24 11:09:31 +01003118 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003119 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003120 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003121
Willy Tarreau24e32d82012-04-23 23:55:44 +02003122 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003123 return acl_fetch_kbytes_in(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003124}
3125
Willy Tarreaua5e37562011-12-16 17:06:15 +01003126/* set temp integer to the bytes rate from clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003127 * configured period.
3128 */
3129static int
Willy Tarreau37406352012-04-23 16:16:37 +02003130acl_fetch_bytes_in_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003131{
Willy Tarreau37406352012-04-23 16:16:37 +02003132 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003133 smp->type = SMP_T_UINT;
3134 smp->data.uint = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003135 if (ts != NULL) {
3136 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
3137 if (!ptr)
3138 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003139 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003140 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
3141 }
3142 return 1;
3143}
3144
Willy Tarreaua5e37562011-12-16 17:06:15 +01003145/* set temp integer to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003146 * counters over the configured period.
3147 */
3148static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003149acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003150 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003151{
Willy Tarreau56123282010-08-06 19:06:56 +02003152 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003153 return 0;
3154
Willy Tarreau37406352012-04-23 16:16:37 +02003155 return acl_fetch_bytes_in_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003156}
3157
Willy Tarreaua5e37562011-12-16 17:06:15 +01003158/* set temp integer to the bytes rate from clients from the session's tracked BE
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003159 * counters over the configured period.
3160 */
3161static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003162acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003163 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003164{
Willy Tarreau56123282010-08-06 19:06:56 +02003165 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003166 return 0;
3167
Willy Tarreau37406352012-04-23 16:16:37 +02003168 return acl_fetch_bytes_in_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003169}
3170
Willy Tarreaua5e37562011-12-16 17:06:15 +01003171/* set temp integer to the bytes rate from clients from the session's source address
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003172 * in the table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003173 * Accepts exactly 1 argument of type table.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003174 */
3175static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003176acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003177 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003178{
3179 struct stktable_key *key;
3180
David du Colombier4f92d322011-03-24 11:09:31 +01003181 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003182 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003183 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003184
Willy Tarreau24e32d82012-04-23 23:55:44 +02003185 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003186 return acl_fetch_bytes_in_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003187}
3188
Willy Tarreaua5e37562011-12-16 17:06:15 +01003189/* set temp integer to the number of kbytes sent to clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003190static int
Willy Tarreau37406352012-04-23 16:16:37 +02003191acl_fetch_kbytes_out(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003192{
Willy Tarreau37406352012-04-23 16:16:37 +02003193 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003194 smp->type = SMP_T_UINT;
3195 smp->data.uint = 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003196
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003197 if (ts != NULL) {
3198 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003199 if (!ptr)
3200 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003201 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003202 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003203 return 1;
3204}
3205
Willy Tarreaua5e37562011-12-16 17:06:15 +01003206/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003207 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003208 */
3209static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003210acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003211 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003212{
Willy Tarreau56123282010-08-06 19:06:56 +02003213 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003214 return 0;
3215
Willy Tarreau37406352012-04-23 16:16:37 +02003216 return acl_fetch_kbytes_out(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003217}
3218
Willy Tarreaua5e37562011-12-16 17:06:15 +01003219/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003220 * tracked BE counters.
3221 */
3222static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003223acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003224 const struct arg *args, struct sample *smp)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003225{
Willy Tarreau56123282010-08-06 19:06:56 +02003226 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003227 return 0;
3228
Willy Tarreau37406352012-04-23 16:16:37 +02003229 return acl_fetch_kbytes_out(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003230}
3231
Willy Tarreaua5e37562011-12-16 17:06:15 +01003232/* set temp integer to the number of kbytes sent to the session's source address in
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003233 * the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003234 * Accepts exactly 1 argument of type table.
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003235 */
3236static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003237acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003238 const struct arg *args, struct sample *smp)
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003239{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003240 struct stktable_key *key;
3241
David du Colombier4f92d322011-03-24 11:09:31 +01003242 key = tcp_src_to_stktable_key(l4);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003243 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003244 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003245
Willy Tarreau24e32d82012-04-23 23:55:44 +02003246 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003247 return acl_fetch_kbytes_out(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003248}
3249
Willy Tarreaua5e37562011-12-16 17:06:15 +01003250/* set temp integer to the bytes rate to clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003251 * configured period.
3252 */
3253static int
Willy Tarreau37406352012-04-23 16:16:37 +02003254acl_fetch_bytes_out_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003255{
Willy Tarreau37406352012-04-23 16:16:37 +02003256 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003257 smp->type = SMP_T_UINT;
3258 smp->data.uint = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003259 if (ts != NULL) {
3260 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3261 if (!ptr)
3262 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003263 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003264 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3265 }
3266 return 1;
3267}
3268
Willy Tarreaua5e37562011-12-16 17:06:15 +01003269/* set temp integer to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003270 * over the configured period.
3271 */
3272static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003273acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003274 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003275{
Willy Tarreau56123282010-08-06 19:06:56 +02003276 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003277 return 0;
3278
Willy Tarreau37406352012-04-23 16:16:37 +02003279 return acl_fetch_bytes_out_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003280}
3281
Willy Tarreaua5e37562011-12-16 17:06:15 +01003282/* set temp integer to the bytes rate to clients from the session's tracked BE counters
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003283 * over the configured period.
3284 */
3285static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003286acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003287 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003288{
Willy Tarreau56123282010-08-06 19:06:56 +02003289 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003290 return 0;
3291
Willy Tarreau37406352012-04-23 16:16:37 +02003292 return acl_fetch_bytes_out_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003293}
3294
Willy Tarreaua5e37562011-12-16 17:06:15 +01003295/* set temp integer to the bytes rate to client from the session's source address in
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003296 * the table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003297 * Accepts exactly 1 argument of type table.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003298 */
3299static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003300acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003301 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003302{
3303 struct stktable_key *key;
3304
David du Colombier4f92d322011-03-24 11:09:31 +01003305 key = tcp_src_to_stktable_key(l4);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003306 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003307 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003308
Willy Tarreau24e32d82012-04-23 23:55:44 +02003309 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003310 return acl_fetch_bytes_out_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003311}
3312
Willy Tarreau34db1082012-04-19 17:16:54 +02003313/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003314 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003315 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003316static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003317acl_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003318 const struct arg *args, struct sample *smp)
Willy Tarreauc735a072011-03-29 00:57:02 +02003319{
Willy Tarreau37406352012-04-23 16:16:37 +02003320 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003321 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003322 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003323 return 1;
3324}
3325
Willy Tarreau34db1082012-04-19 17:16:54 +02003326/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003327 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003328 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003329static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003330acl_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003331 const struct arg *args, struct sample *smp)
Willy Tarreauc735a072011-03-29 00:57:02 +02003332{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003333 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003334 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003335 smp->type = SMP_T_UINT;
3336 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003337 return 1;
3338}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003339
Willy Tarreau61612d42012-04-19 18:42:05 +02003340/* Note: must not be declared <const> as its list will be overwritten.
3341 * Please take care of keeping this list alphabetically sorted.
3342 */
Willy Tarreau8b22a712010-06-18 17:46:06 +02003343static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02003344 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3345 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3346 { "sc1_clr_gpc0", acl_parse_int, acl_fetch_sc1_clr_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3347 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3348 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING, 0 },
3349 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3350 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3351 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3352 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3353 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3354 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3355 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3356 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3357 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3358 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3359 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3360 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3361 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3362 { "sc2_clr_gpc0", acl_parse_int, acl_fetch_sc2_clr_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3363 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3364 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING, 0 },
3365 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3366 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3367 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3368 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3369 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3370 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3371 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3372 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3373 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3374 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3375 { "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 +02003376 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3377 { "src_bytes_out_rate", acl_parse_int, acl_fetch_src_bytes_out_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3378 { "src_clr_gpc0", acl_parse_int, acl_fetch_src_clr_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3379 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3380 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3381 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3382 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3383 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3384 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3385 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3386 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3387 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3388 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3389 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3390 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3391 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3392 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3393 { "table_avl", acl_parse_int, acl_fetch_table_avl, acl_match_int, ACL_USE_NOTHING, ARG1(1,TAB) },
3394 { "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 +02003395 { NULL, NULL, NULL, NULL },
3396}};
3397
3398
Willy Tarreau56123282010-08-06 19:06:56 +02003399/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003400 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3401 * <prm> struct is fed with the table name if any. If unspecified, the caller
3402 * will assume that the current proxy's table is used.
3403 */
3404int parse_track_counters(char **args, int *arg,
3405 int section_type, struct proxy *curpx,
3406 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003407 struct proxy *defpx, char **err)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003408{
Willy Tarreau12785782012-04-27 21:37:17 +02003409 int sample_type = 0;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003410
Willy Tarreau56123282010-08-06 19:06:56 +02003411 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003412 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003413 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003414 */
3415 while (args[*arg]) {
3416 if (strcmp(args[*arg], "src") == 0) {
3417 prm->type = STKTABLE_TYPE_IP;
Willy Tarreau12785782012-04-27 21:37:17 +02003418 sample_type = 1;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003419 }
3420 else if (strcmp(args[*arg], "table") == 0) {
3421 if (!args[*arg + 1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003422 memprintf(err, "missing table name");
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003423 return -1;
3424 }
3425 /* we copy the table name for now, it will be resolved later */
3426 prm->table.n = strdup(args[*arg + 1]);
3427 (*arg)++;
3428 }
3429 else {
3430 /* unhandled keywords are handled by the caller */
3431 break;
3432 }
3433 (*arg)++;
3434 }
3435
Willy Tarreau12785782012-04-27 21:37:17 +02003436 if (!sample_type) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003437 memprintf(err,
3438 "tracking key not specified (found %s, only 'src' is supported)",
3439 quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003440 return -1;
3441 }
3442
3443 return 0;
3444}
3445
Willy Tarreau8b22a712010-06-18 17:46:06 +02003446__attribute__((constructor))
3447static void __session_init(void)
3448{
3449 acl_register_keywords(&acl_kws);
3450}
3451
Willy Tarreaubaaee002006-06-26 02:48:02 +02003452/*
3453 * Local variables:
3454 * c-indent-level: 8
3455 * c-basic-offset: 8
3456 * End:
3457 */