blob: c4323da126409de735642458b42ae1801cef9a1e [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 Tarreauc7e42382012-08-24 19:22:53 +020027#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010028#include <proto/checks.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020029#include <proto/connection.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020030#include <proto/dumpstats.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020031#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020032#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010033#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020034#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010036#include <proto/pipe.h>
Willy Tarreau62793712011-07-24 19:23:38 +020037#include <proto/protocols.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010038#include <proto/proto_http.h>
39#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020040#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010042#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020043#include <proto/sample.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 Tarreau2542b532012-08-31 16:01:23 +020051static struct task *expire_mini_session(struct task *t);
52int session_complete(struct session *s);
53
54/* This function is called from the protocol layer accept() in order to
55 * instanciate a new embryonic session on behalf of a given listener and
56 * frontend. It returns a positive value upon success, 0 if the connection
57 * can be ignored, or a negative value upon critical failure. The accepted
58 * file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020059 */
60int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
61{
62 struct proxy *p = l->frontend;
63 struct session *s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020064 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010065 int ret;
66
67
68 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020069
Willy Tarreaufffe1322010-11-11 09:48:16 +010070 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020071 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020072
Willy Tarreau2542b532012-08-31 16:01:23 +020073 /* minimum session initialization required for an embryonic session is
74 * fairly low. We need very little to execute L4 ACLs, then we need a
75 * task to make the client-side connection live on its own.
76 * - flags
77 * - stick-entry tracking
78 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020079 s->flags = 0;
80 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020081 s->stkctr1_entry = NULL;
82 s->stkctr2_entry = NULL;
83 s->stkctr1_table = NULL;
84 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020085
Willy Tarreau2542b532012-08-31 16:01:23 +020086 s->listener = l;
87 s->fe = p;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010088
Willy Tarreau81f9aa32010-06-01 17:45:26 +020089 /* OK, we're keeping the session, so let's properly initialize the session */
Willy Tarreau96596ae2012-06-08 22:57:36 +020090 s->si[0].conn.t.sock.fd = cfd;
91 s->si[0].conn.ctrl = l->proto;
Willy Tarreau2542b532012-08-31 16:01:23 +020092 s->si[0].conn.flags = CO_FL_NONE;
Willy Tarreau986a9d22012-08-30 21:11:38 +020093 s->si[0].conn.addr.from = *addr;
Willy Tarreau2542b532012-08-31 16:01:23 +020094 set_target_client(&s->si[0].conn.target, l);
95
Willy Tarreau81f9aa32010-06-01 17:45:26 +020096 s->logs.accept_date = date; /* user-visible date for logging */
97 s->logs.tv_accept = now; /* corrected date for internal use */
98 s->uniq_id = totalconn;
Willy Tarreau2542b532012-08-31 16:01:23 +020099 p->feconn++;
100 /* This session was accepted, count it now */
101 if (p->feconn > p->fe_counters.conn_max)
102 p->fe_counters.conn_max = p->feconn;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200103
Willy Tarreau2542b532012-08-31 16:01:23 +0200104 proxy_inc_fe_conn_ctr(l, p);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200105
Willy Tarreaufe7f1ea2012-05-20 19:22:25 +0200106 /* if this session comes from a known monitoring system, we want to ignore
107 * it as soon as possible, which means closing it immediately for TCP, but
108 * cleanly.
109 */
110 if (unlikely((l->options & LI_O_CHK_MONNET) &&
111 addr->ss_family == AF_INET &&
112 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
113 s->flags |= SN_MONITOR;
114 s->logs.logwait = 0;
115 }
116
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200117 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
118 * to abort right here as soon as possible, we check the rules before
119 * even initializing the stream interfaces.
120 */
121 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200122 /* let's do a no-linger now to close with a single RST. */
123 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100124 ret = 0; /* successful termination */
Willy Tarreau2542b532012-08-31 16:01:23 +0200125 goto out_free_session;
126 }
127
Willy Tarreau22cda212012-08-31 17:43:29 +0200128 /* wait for a PROXY protocol header */
129 if (l->options & LI_O_ACC_PROXY) {
130 s->si[0].conn.flags |= CO_FL_ACCEPT_PROXY;
131 conn_sock_want_recv(&s->si[0].conn);
132 }
133
Willy Tarreau2542b532012-08-31 16:01:23 +0200134 /* Adjust some socket options */
135 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1))
136 goto out_free_session;
137
138 if (unlikely((t = task_new()) == NULL))
139 goto out_free_session;
140
141 t->context = s;
142 t->nice = l->nice;
143 s->task = t;
144
145 /* add the various callbacks. Right now the data layer is present but
146 * not initialized. Also note we need to be careful as the stream int
147 * is not initialized yet.
148 */
149 si_prepare_conn(&s->si[0], l->proto, l->data);
150
151 /* finish initialization of the accepted file descriptor */
152 fd_insert(cfd);
153 fdtab[cfd].owner = &s->si[0].conn;
Willy Tarreau2542b532012-08-31 16:01:23 +0200154 fdtab[cfd].iocb = conn_fd_handler;
155 conn_data_want_recv(&s->si[0].conn);
156 if (conn_data_init(&s->si[0].conn) < 0)
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100157 goto out_free_task;
Willy Tarreau2542b532012-08-31 16:01:23 +0200158
159 /* OK, now either we have a pending handshake to execute with and
160 * then we must return to the I/O layer, or we can proceed with the
161 * end of the session initialization. In case of handshake, we also
162 * set the I/O timeout to the frontend's client timeout.
163 */
164
165 if (s->si[0].conn.flags & CO_FL_HANDSHAKE) {
166 t->process = expire_mini_session;
167 t->expire = tick_add_ifset(now_ms, p->timeout.client);
168 task_queue(t);
169 s->si[0].conn.flags |= CO_FL_INIT_SESS;
170 return 1;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200171 }
172
Willy Tarreau2542b532012-08-31 16:01:23 +0200173 /* OK let's complete session initialization */
174 ret = session_complete(s);
175 if (ret > 0)
176 return ret;
177
178 /* Error unrolling */
179 out_free_task:
180 task_free(t);
181 out_free_session:
182 p->feconn--;
183 if (s->stkctr1_entry || s->stkctr2_entry)
184 session_store_counters(s);
185 pool_free2(pool2_session, s);
186 out_close:
187 if (ret < 0 && p->mode == PR_MODE_HTTP) {
188 /* critical error, no more memory, try to emit a 500 response */
189 struct chunk *err_msg = error_message(s, HTTP_ERR_500);
190 send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
191 }
192
193 if (fdtab[cfd].owner)
194 fd_delete(cfd);
195 else
196 close(cfd);
197 return ret;
198}
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100199
Willy Tarreau2542b532012-08-31 16:01:23 +0200200/* This function kills an existing embryonic session. It stops the connection's
201 * data layer, releases assigned resources, resumes the listener if it was
202 * disabled and finally kills the file descriptor.
203 */
204static void kill_mini_session(struct session *s)
205{
206 /* kill the connection now */
207 conn_data_close(&s->si[0].conn);
208
209 s->fe->feconn--;
210 if (s->stkctr1_entry || s->stkctr2_entry)
211 session_store_counters(s);
212
213 if (!(s->listener->options & LI_O_UNLIMITED))
214 actconn--;
215 jobs--;
216 s->listener->nbconn--;
217 if (s->listener->state == LI_FULL)
218 resume_listener(s->listener);
219
220 /* Dequeues all of the listeners waiting for a resource */
221 if (!LIST_ISEMPTY(&global_listener_queue))
222 dequeue_all_listeners(&global_listener_queue);
223
224 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
225 (!s->fe->fe_sps_lim || freq_ctr_remain(&s->fe->fe_sess_per_sec, s->fe->fe_sps_lim, 0) > 0))
226 dequeue_all_listeners(&s->fe->listener_queue);
227
228 task_delete(s->task);
229 task_free(s->task);
230
231 if (fdtab[s->si[0].conn.t.sock.fd].owner)
232 fd_delete(s->si[0].conn.t.sock.fd);
233 else
234 close(s->si[0].conn.t.sock.fd);
235
236 pool_free2(pool2_session, s);
237}
238
Willy Tarreau22cda212012-08-31 17:43:29 +0200239/* Finish initializing a session from a connection, or kills it if the
240 * connection shows and error. Returns <0 if the connection was killed.
Willy Tarreau2542b532012-08-31 16:01:23 +0200241 */
Willy Tarreau22cda212012-08-31 17:43:29 +0200242int conn_session_complete(struct connection *conn, int flag)
Willy Tarreau2542b532012-08-31 16:01:23 +0200243{
244 struct session *s = container_of(conn, struct session, si[0].conn);
245
Willy Tarreau22cda212012-08-31 17:43:29 +0200246 if (!(conn->flags & CO_FL_ERROR) && (session_complete(s) > 0)) {
Willy Tarreau2542b532012-08-31 16:01:23 +0200247 conn->flags &= ~flag;
248 return 0;
249 }
250
251 /* kill the connection now */
252 kill_mini_session(s);
253 return -1;
254}
255
256/* Manages embryonic sessions timeout. It is only called when the timeout
257 * strikes and performs the required cleanup.
258 */
259static struct task *expire_mini_session(struct task *t)
260{
261 struct session *s = t->context;
262
263 if (!(t->state & TASK_WOKEN_TIMER))
264 return t;
265
266 kill_mini_session(s);
267 return NULL;
268}
269
270/* This function is called from the I/O handler which detects the end of
271 * handshake, in order to complete initialization of a valid session. It must
272 * be called with an embryonic session. It returns a positive value upon
273 * success, 0 if the connection can be ignored, or a negative value upon
274 * critical failure. The accepted file descriptor is closed if we return <= 0.
275 */
276int session_complete(struct session *s)
277{
278 struct listener *l = s->listener;
279 struct proxy *p = s->fe;
280 struct http_txn *txn;
281 struct task *t = s->task;
282 int ret;
283
284 ret = -1; /* assume unrecoverable error by default */
285
286 /* OK, we're keeping the session, so let's properly initialize the session */
287 LIST_ADDQ(&sessions, &s->list);
288 LIST_INIT(&s->back_refs);
289 s->flags |= SN_INITIALIZED;
290
291 s->unique_id = NULL;
292 s->term_trace = 0;
293
294 t->process = l->handler;
295 t->context = s;
296 t->expire = TICK_ETERNITY;
297
298 /* Note: initially, the session's backend points to the frontend.
299 * This changes later when switching rules are executed or
300 * when the default backend is assigned.
301 */
302 s->be = s->fe;
303 s->req = s->rep = NULL; /* will be allocated later */
304
305 /* Let's count a session now */
Willy Tarreaub36b4242010-06-04 20:59:39 +0200306 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200307 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200308 void *ptr;
309
Willy Tarreau56123282010-08-06 19:06:56 +0200310 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200311 if (ptr)
312 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200313
Willy Tarreau56123282010-08-06 19:06:56 +0200314 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200315 if (ptr)
316 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200317 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200318 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200319
Willy Tarreau56123282010-08-06 19:06:56 +0200320 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200321 void *ptr;
322
Willy Tarreau56123282010-08-06 19:06:56 +0200323 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200324 if (ptr)
325 stktable_data_cast(ptr, sess_cnt)++;
326
Willy Tarreau56123282010-08-06 19:06:56 +0200327 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200328 if (ptr)
329 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200330 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200331 }
332
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200333 /* this part should be common with other protocols */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200334 s->si[0].owner = t;
335 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
336 s->si[0].err_type = SI_ET_NONE;
337 s->si[0].err_loc = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200338 s->si[0].release = NULL;
Willy Tarreau63e7fe32012-05-08 15:20:43 +0200339 s->si[0].send_proxy_ofs = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200340 s->si[0].exp = TICK_ETERNITY;
341 s->si[0].flags = SI_FL_NONE;
342
343 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
344 s->si[0].flags |= SI_FL_INDEP_STR;
345
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200346 /* pre-initialize the other side's stream interface to an INIT state. The
347 * callbacks will be initialized before attempting to connect.
348 */
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200349 s->si[1].conn.t.sock.fd = -1; /* just to help with debugging */
Willy Tarreau505e34a2012-07-06 10:17:53 +0200350 s->si[1].conn.flags = CO_FL_NONE;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200351 s->si[1].owner = t;
352 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
353 s->si[1].err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +0200354 s->si[1].conn_retries = 0; /* used for logging too */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200355 s->si[1].err_loc = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200356 s->si[1].release = NULL;
Willy Tarreau63e7fe32012-05-08 15:20:43 +0200357 s->si[1].send_proxy_ofs = 0;
Willy Tarreau3cefd522012-08-30 15:49:18 +0200358 clear_target(&s->si[1].conn.target);
Willy Tarreauc5788912012-08-24 18:12:41 +0200359 si_prepare_embedded(&s->si[1]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200360 s->si[1].exp = TICK_ETERNITY;
361 s->si[1].flags = SI_FL_NONE;
362
363 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
364 s->si[1].flags |= SI_FL_INDEP_STR;
365
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200366 session_init_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +0100367 clear_target(&s->target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200368 s->pend_pos = NULL;
369
370 /* init store persistence */
371 s->store_count = 0;
372
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200373 if (unlikely((s->req = pool_alloc2(pool2_channel)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200374 goto out_free_task; /* no memory */
375
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200376 if (unlikely((s->rep = pool_alloc2(pool2_channel)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200377 goto out_free_req; /* no memory */
378
379 /* initialize the request buffer */
Willy Tarreau572bf902012-07-02 17:01:20 +0200380 s->req->buf.size = global.tune.bufsize;
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200381 channel_init(s->req);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200382 s->req->prod = &s->si[0];
383 s->req->cons = &s->si[1];
384 s->si[0].ib = s->si[1].ob = s->req;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200385 s->req->flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200386
387 /* activate default analysers enabled for this listener */
388 s->req->analysers = l->analysers;
389
390 s->req->wto = TICK_ETERNITY;
391 s->req->rto = TICK_ETERNITY;
392 s->req->rex = TICK_ETERNITY;
393 s->req->wex = TICK_ETERNITY;
394 s->req->analyse_exp = TICK_ETERNITY;
395
396 /* initialize response buffer */
Willy Tarreau572bf902012-07-02 17:01:20 +0200397 s->rep->buf.size = global.tune.bufsize;
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200398 channel_init(s->rep);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200399 s->rep->prod = &s->si[1];
400 s->rep->cons = &s->si[0];
401 s->si[0].ob = s->si[1].ib = s->rep;
402 s->rep->analysers = 0;
403
Willy Tarreau96e31212011-05-30 18:10:30 +0200404 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200405 s->req->flags |= CF_NEVER_WAIT;
406 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200407 }
408
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200409 s->rep->rto = TICK_ETERNITY;
410 s->rep->wto = TICK_ETERNITY;
411 s->rep->rex = TICK_ETERNITY;
412 s->rep->wex = TICK_ETERNITY;
413 s->rep->analyse_exp = TICK_ETERNITY;
414
Willy Tarreau62f791e2012-03-09 11:32:30 +0100415 txn = &s->txn;
416 /* Those variables will be checked and freed if non-NULL in
417 * session.c:session_free(). It is important that they are
418 * properly initialized.
419 */
420 txn->sessid = NULL;
421 txn->srv_cookie = NULL;
422 txn->cli_cookie = NULL;
423 txn->uri = NULL;
424 txn->req.cap = NULL;
425 txn->rsp.cap = NULL;
426 txn->hdr_idx.v = NULL;
427 txn->hdr_idx.size = txn->hdr_idx.used = 0;
428 txn->req.flags = 0;
429 txn->rsp.flags = 0;
430 /* the HTTP messages need to know what buffer they're associated with */
431 txn->req.buf = s->req;
432 txn->rsp.buf = s->rep;
433
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200434 /* finish initialization of the accepted file descriptor */
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200435 conn_data_want_recv(&s->si[0].conn);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200436
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100437 if (p->accept && (ret = p->accept(s)) <= 0) {
438 /* Either we had an unrecoverable error (<0) or work is
439 * finished (=0, eg: monitoring), in both situations,
440 * we can release everything and close.
441 */
442 goto out_free_rep;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200443 }
444
Willy Tarreau2542b532012-08-31 16:01:23 +0200445 /* we want the connection handler to notify the stream interface about updates. */
446 s->si[0].conn.flags |= CO_FL_NOTIFY_SI;
447
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200448 /* it is important not to call the wakeup function directly but to
449 * pass through task_wakeup(), because this one knows how to apply
450 * priorities to tasks.
451 */
452 task_wakeup(t, TASK_WOKEN_INIT);
453 return 1;
454
455 /* Error unrolling */
456 out_free_rep:
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200457 pool_free2(pool2_channel, s->rep);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200458 out_free_req:
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200459 pool_free2(pool2_channel, s->req);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200460 out_free_task:
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100461 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200462}
463
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464/*
465 * frees the context associated to a session. It must have been removed first.
466 */
Simon Hormandec5be42011-06-08 09:19:07 +0900467static void session_free(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200468{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100469 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200470 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100471 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200472 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100473
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474 if (s->pend_pos)
475 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100476
Willy Tarreau827aee92011-03-10 16:55:02 +0100477 if (target_srv(&s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100478 if (s->flags & SN_CURR_SESS) {
479 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100480 target_srv(&s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100481 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100482 if (may_dequeue_tasks(target_srv(&s->target), s->be))
483 process_srv_queue(target_srv(&s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100484 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100485
Willy Tarreau7c669d72008-06-20 15:04:11 +0200486 if (unlikely(s->srv_conn)) {
487 /* the session still has a reserved slot on a server, but
488 * it should normally be only the same as the one above,
489 * so this should not happen in fact.
490 */
491 sess_change_server(s, NULL);
492 }
493
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100494 if (s->req->pipe)
495 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100496
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100497 if (s->rep->pipe)
498 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100499
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200500 pool_free2(pool2_channel, s->req);
501 pool_free2(pool2_channel, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200502
Willy Tarreau46023632010-01-07 22:51:47 +0100503 http_end_txn(s);
504
Willy Tarreaua4cda672010-06-06 18:28:49 +0200505 for (i = 0; i < s->store_count; i++) {
506 if (!s->store[i].ts)
507 continue;
508 stksess_free(s->store[i].table, s->store[i].ts);
509 s->store[i].ts = NULL;
510 }
511
Willy Tarreau34eb6712011-10-24 18:15:04 +0200512 pool_free2(pool2_hdr_idx, txn->hdr_idx.v);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200513 if (fe) {
Willy Tarreau46023632010-01-07 22:51:47 +0100514 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
515 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200516 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100517
Willy Tarreau56123282010-08-06 19:06:56 +0200518 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200519 session_store_counters(s);
520
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100521 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100522 /* we have to unlink all watchers. We must not relink them if
523 * this session was the last one in the list.
524 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100525 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100526 LIST_INIT(&bref->users);
527 if (s->list.n != &sessions)
528 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100529 bref->ref = s->list.n;
530 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100531 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200532 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200533
534 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200535 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200536 pool_flush2(pool2_channel);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200537 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200538 pool_flush2(pool2_requri);
539 pool_flush2(pool2_capture);
540 pool_flush2(pool2_session);
541 pool_flush2(fe->req_cap_pool);
542 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200543 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200544}
545
546
547/* perform minimal intializations, report 0 in case of error, 1 if OK. */
548int init_session()
549{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100550 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200551 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
552 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553}
554
Willy Tarreau30e71012007-11-26 20:15:35 +0100555void session_process_counters(struct session *s)
556{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100557 unsigned long long bytes;
558
Willy Tarreau30e71012007-11-26 20:15:35 +0100559 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100560 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100561 s->logs.bytes_in = s->req->total;
562 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100563 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100564
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100565 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100566
Willy Tarreau827aee92011-03-10 16:55:02 +0100567 if (target_srv(&s->target))
568 target_srv(&s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200569
570 if (s->listener->counters)
571 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200572
Willy Tarreau56123282010-08-06 19:06:56 +0200573 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200574 void *ptr;
575
Willy Tarreau56123282010-08-06 19:06:56 +0200576 ptr = stktable_data_ptr(s->stkctr2_table,
577 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200578 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200579 if (ptr)
580 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200581
Willy Tarreau56123282010-08-06 19:06:56 +0200582 ptr = stktable_data_ptr(s->stkctr2_table,
583 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200584 STKTABLE_DT_BYTES_IN_RATE);
585 if (ptr)
586 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200587 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200588 }
589
Willy Tarreau56123282010-08-06 19:06:56 +0200590 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200591 void *ptr;
592
Willy Tarreau56123282010-08-06 19:06:56 +0200593 ptr = stktable_data_ptr(s->stkctr1_table,
594 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200595 STKTABLE_DT_BYTES_IN_CNT);
596 if (ptr)
597 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
598
Willy Tarreau56123282010-08-06 19:06:56 +0200599 ptr = stktable_data_ptr(s->stkctr1_table,
600 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200601 STKTABLE_DT_BYTES_IN_RATE);
602 if (ptr)
603 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200604 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200605 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100606 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100607 }
608
Willy Tarreau30e71012007-11-26 20:15:35 +0100609 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100610 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100611 s->logs.bytes_out = s->rep->total;
612 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100613 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100614
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100615 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100616
Willy Tarreau827aee92011-03-10 16:55:02 +0100617 if (target_srv(&s->target))
618 target_srv(&s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200619
620 if (s->listener->counters)
621 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200622
Willy Tarreau56123282010-08-06 19:06:56 +0200623 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200624 void *ptr;
625
Willy Tarreau56123282010-08-06 19:06:56 +0200626 ptr = stktable_data_ptr(s->stkctr2_table,
627 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200628 STKTABLE_DT_BYTES_OUT_CNT);
629 if (ptr)
630 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
631
Willy Tarreau56123282010-08-06 19:06:56 +0200632 ptr = stktable_data_ptr(s->stkctr2_table,
633 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200634 STKTABLE_DT_BYTES_OUT_RATE);
635 if (ptr)
636 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200637 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200638 }
639
Willy Tarreau56123282010-08-06 19:06:56 +0200640 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200641 void *ptr;
642
Willy Tarreau56123282010-08-06 19:06:56 +0200643 ptr = stktable_data_ptr(s->stkctr1_table,
644 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200645 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200646 if (ptr)
647 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200648
Willy Tarreau56123282010-08-06 19:06:56 +0200649 ptr = stktable_data_ptr(s->stkctr1_table,
650 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200651 STKTABLE_DT_BYTES_OUT_RATE);
652 if (ptr)
653 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200654 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200655 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100656 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100657 }
658}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200659
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100660/* This function is called with (si->state == SI_ST_CON) meaning that a
661 * connection was attempted and that the file descriptor is already allocated.
662 * We must check for establishment, error and abort. Possible output states
663 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
664 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
665 * otherwise 1.
666 */
Simon Hormandec5be42011-06-08 09:19:07 +0900667static int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100668{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200669 struct channel *req = si->ob;
670 struct channel *rep = si->ib;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100671
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100672 /* If we got an error, or if nothing happened and the connection timed
673 * out, we must give up. The CER state handler will take care of retry
674 * attempts and error reports.
675 */
676 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100677 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100678 si->state = SI_ST_CER;
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200679 fd_delete(si_fd(si));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100680
Willy Tarreau8b117082012-08-06 15:06:49 +0200681 conn_data_close(&si->conn);
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200682 if (si->release)
683 si->release(si);
684
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100685 if (si->err_type)
686 return 0;
687
Willy Tarreau827aee92011-03-10 16:55:02 +0100688 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100689 if (si->flags & SI_FL_ERR)
690 si->err_type = SI_ET_CONN_ERR;
691 else
692 si->err_type = SI_ET_CONN_TO;
693 return 0;
694 }
695
696 /* OK, maybe we want to abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200697 if (unlikely((rep->flags & CF_SHUTW) ||
698 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
699 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100700 s->be->options & PR_O_ABRT_CLOSE)))) {
701 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200702 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau827aee92011-03-10 16:55:02 +0100704 si->err_loc = target_srv(&s->target);
Willy Tarreau84455332009-03-15 22:34:05 +0100705 if (s->srv_error)
706 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100707 return 1;
708 }
709
710 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200711 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100712 return 1;
713
714 /* OK, this means that a connection succeeded. The caller will be
715 * responsible for handling the transition from CON to EST.
716 */
717 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100718 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100719 si->state = SI_ST_EST;
720 si->err_type = SI_ET_NONE;
721 si->err_loc = NULL;
722 return 1;
723}
724
725/* This function is called with (si->state == SI_ST_CER) meaning that a
726 * previous connection attempt has failed and that the file descriptor
727 * has already been released. Possible causes include asynchronous error
728 * notification and time out. Possible output states are SI_ST_CLO when
729 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
730 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
731 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
732 * marked as in error state. It returns 0.
733 */
Simon Hormandec5be42011-06-08 09:19:07 +0900734static int sess_update_st_cer(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100735{
736 /* we probably have to release last session from the server */
Willy Tarreau827aee92011-03-10 16:55:02 +0100737 if (target_srv(&s->target)) {
738 health_adjust(target_srv(&s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100739
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100740 if (s->flags & SN_CURR_SESS) {
741 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100742 target_srv(&s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100743 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744 }
745
746 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200747 si->conn_retries--;
748 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100749 if (!si->err_type) {
750 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +0100751 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100752 }
753
Willy Tarreau827aee92011-03-10 16:55:02 +0100754 if (target_srv(&s->target))
755 target_srv(&s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100756 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100757 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100758 if (may_dequeue_tasks(target_srv(&s->target), s->be))
759 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100760
761 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200762 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200763 si->ob->flags |= CF_WRITE_ERROR;
764 si->ib->flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100765
766 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100767 if (s->srv_error)
768 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100769 return 0;
770 }
771
772 /* If the "redispatch" option is set on the backend, we are allowed to
773 * retry on another server for the last retry. In order to achieve this,
774 * we must mark the session unassigned, and eventually clear the DIRECT
775 * bit to ignore any persistence cookie. We won't count a retry nor a
776 * redispatch yet, because this will depend on what server is selected.
777 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100778 if (target_srv(&s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100779 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100780 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100781 if (may_dequeue_tasks(target_srv(&s->target), s->be))
782 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100783
784 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100785 si->state = SI_ST_REQ;
786 } else {
Willy Tarreau827aee92011-03-10 16:55:02 +0100787 if (target_srv(&s->target))
788 target_srv(&s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100789 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100790 si->state = SI_ST_ASS;
791 }
792
793 if (si->flags & SI_FL_ERR) {
794 /* The error was an asynchronous connection error, and we will
795 * likely have to retry connecting to the same server, most
796 * likely leading to the same result. To avoid this, we wait
797 * one second before retrying.
798 */
799
800 if (!si->err_type)
801 si->err_type = SI_ET_CONN_ERR;
802
803 si->state = SI_ST_TAR;
804 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
805 return 0;
806 }
807 return 0;
808}
809
810/*
811 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200812 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200813 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100814 */
Simon Hormandec5be42011-06-08 09:19:07 +0900815static void sess_establish(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100816{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200817 struct channel *req = si->ob;
818 struct channel *rep = si->ib;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100819
Willy Tarreau827aee92011-03-10 16:55:02 +0100820 if (target_srv(&s->target))
821 health_adjust(target_srv(&s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100822
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100823 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100824 /* if the user wants to log as soon as possible, without counting
825 * bytes from the server, then this is the right moment. */
826 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
827 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100828 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100829 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100830 }
831 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100832 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
833 /* reset hdr_idx which was already initialized by the request.
834 * right now, the http parser does it.
835 * hdr_idx_init(&s->txn.hdr_idx);
836 */
837 }
838
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200839 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200840 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200841 if (si_ctrl(si)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200842 /* real connections have timeouts */
843 req->wto = s->be->timeout.server;
844 rep->rto = s->be->timeout.server;
845 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100846 req->wex = TICK_ETERNITY;
847}
848
849/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
850 * Other input states are simply ignored.
851 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
852 * Flags must have previously been updated for timeouts and other conditions.
853 */
Simon Hormandec5be42011-06-08 09:19:07 +0900854static void sess_update_stream_int(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100855{
Willy Tarreau827aee92011-03-10 16:55:02 +0100856 struct server *srv = target_srv(&s->target);
857
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100858 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 +0100859 now_ms, __FUNCTION__,
860 s,
861 s->req, s->rep,
862 s->req->rex, s->rep->wex,
863 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100864 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 +0100865
866 if (si->state == SI_ST_ASS) {
867 /* Server assigned to connection request, we have to try to connect now */
868 int conn_err;
869
870 conn_err = connect_server(s);
Willy Tarreau827aee92011-03-10 16:55:02 +0100871 srv = target_srv(&s->target);
872
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100873 if (conn_err == SN_ERR_NONE) {
874 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100875 if (srv)
876 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100877 return;
878 }
879
880 /* We have received a synchronous error. We might have to
881 * abort, retry immediately or redispatch.
882 */
883 if (conn_err == SN_ERR_INTERNAL) {
884 if (!si->err_type) {
885 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +0100886 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100887 }
888
Willy Tarreau827aee92011-03-10 16:55:02 +0100889 if (srv)
890 srv_inc_sess_ctr(srv);
891 if (srv)
892 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100893 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100894
895 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100896 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100897 if (may_dequeue_tasks(srv, s->be))
898 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100899
900 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200901 si_shutr(si);
902 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200903 si->ob->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100904
905 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
906
907 /* no session was ever accounted for this server */
908 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100909 if (s->srv_error)
910 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100911 return;
912 }
913
914 /* We are facing a retryable error, but we don't want to run a
915 * turn-around now, as the problem is likely a source port
916 * allocation problem, so we want to retry now.
917 */
918 si->state = SI_ST_CER;
919 si->flags &= ~SI_FL_ERR;
920 sess_update_st_cer(s, si);
921 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
922 return;
923 }
924 else if (si->state == SI_ST_QUE) {
925 /* connection request was queued, check for any update */
926 if (!s->pend_pos) {
927 /* The connection is not in the queue anymore. Either
928 * we have a server connection slot available and we
929 * go directly to the assigned state, or we need to
930 * load-balance first and go to the INI state.
931 */
932 si->exp = TICK_ETERNITY;
933 if (unlikely(!(s->flags & SN_ASSIGNED)))
934 si->state = SI_ST_REQ;
935 else {
936 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
937 si->state = SI_ST_ASS;
938 }
939 return;
940 }
941
942 /* Connection request still in queue... */
943 if (si->flags & SI_FL_EXP) {
944 /* ... and timeout expired */
945 si->exp = TICK_ETERNITY;
946 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100947 if (srv)
948 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100949 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200950 si_shutr(si);
951 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200952 si->ob->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100953 if (!si->err_type)
954 si->err_type = SI_ET_QUEUE_TO;
955 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100956 if (s->srv_error)
957 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100958 return;
959 }
960
961 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200962 if ((si->ob->flags & (CF_READ_ERROR)) ||
963 ((si->ob->flags & CF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreau8e21bb92012-08-24 22:40:29 +0200964 (channel_is_empty(si->ob) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100965 /* give up */
966 si->exp = TICK_ETERNITY;
967 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200968 si_shutr(si);
969 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100970 si->err_type |= SI_ET_QUEUE_ABRT;
971 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100972 if (s->srv_error)
973 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100974 return;
975 }
976
977 /* Nothing changed */
978 return;
979 }
980 else if (si->state == SI_ST_TAR) {
981 /* Connection request might be aborted */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200982 if ((si->ob->flags & (CF_READ_ERROR)) ||
983 ((si->ob->flags & CF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreau8e21bb92012-08-24 22:40:29 +0200984 (channel_is_empty(si->ob) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100985 /* give up */
986 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200987 si_shutr(si);
988 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100989 si->err_type |= SI_ET_CONN_ABRT;
990 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100991 if (s->srv_error)
992 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100993 return;
994 }
995
996 if (!(si->flags & SI_FL_EXP))
997 return; /* still in turn-around */
998
999 si->exp = TICK_ETERNITY;
1000
1001 /* we keep trying on the same server as long as the session is
1002 * marked "assigned".
1003 * FIXME: Should we force a redispatch attempt when the server is down ?
1004 */
1005 if (s->flags & SN_ASSIGNED)
1006 si->state = SI_ST_ASS;
1007 else
1008 si->state = SI_ST_REQ;
1009 return;
1010 }
1011}
1012
Simon Hormandec5be42011-06-08 09:19:07 +09001013/* Set correct session termination flags in case no analyser has done it. It
1014 * also counts a failed request if the server state has not reached the request
1015 * stage.
1016 */
1017static void sess_set_term_flags(struct session *s)
1018{
1019 if (!(s->flags & SN_FINST_MASK)) {
1020 if (s->si[1].state < SI_ST_REQ) {
1021
1022 s->fe->fe_counters.failed_req++;
1023 if (s->listener->counters)
1024 s->listener->counters->failed_req++;
1025
1026 s->flags |= SN_FINST_R;
1027 }
1028 else if (s->si[1].state == SI_ST_QUE)
1029 s->flags |= SN_FINST_Q;
1030 else if (s->si[1].state < SI_ST_EST)
1031 s->flags |= SN_FINST_C;
1032 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
1033 s->flags |= SN_FINST_D;
1034 else
1035 s->flags |= SN_FINST_L;
1036 }
1037}
1038
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001039/* This function initiates a server connection request on a stream interface
1040 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
1041 * indicating that a server has been assigned. It may also return SI_ST_QUE,
1042 * or SI_ST_CLO upon error.
1043 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001044static void sess_prepare_conn_req(struct session *s, struct stream_interface *si)
1045{
1046 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 +01001047 now_ms, __FUNCTION__,
1048 s,
1049 s->req, s->rep,
1050 s->req->rex, s->rep->wex,
1051 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001052 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 +01001053
1054 if (si->state != SI_ST_REQ)
1055 return;
1056
1057 /* Try to assign a server */
1058 if (srv_redispatch_connect(s) != 0) {
1059 /* We did not get a server. Either we queued the
1060 * connection request, or we encountered an error.
1061 */
1062 if (si->state == SI_ST_QUE)
1063 return;
1064
1065 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001066 si_shutr(si);
1067 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001068 si->ob->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001069 if (!si->err_type)
1070 si->err_type = SI_ET_CONN_OTHER;
1071 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001072 if (s->srv_error)
1073 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001074 return;
1075 }
1076
1077 /* The server is assigned */
1078 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1079 si->state = SI_ST_ASS;
1080}
1081
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001082/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001083 * if appropriate. The default_backend rule is also considered, then the
1084 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001085 * It returns 1 if the processing can continue on next analysers, or zero if it
1086 * either needs more data or wants to immediately abort the request.
1087 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001088static int process_switching_rules(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001089{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001090 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +01001091
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001092 req->analysers &= ~an_bit;
1093 req->analyse_exp = TICK_ETERNITY;
1094
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001095 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 +02001096 now_ms, __FUNCTION__,
1097 s,
1098 req,
1099 req->rex, req->wex,
1100 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001101 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001102 req->analysers);
1103
1104 /* now check whether we have some switching rules for this request */
1105 if (!(s->flags & SN_BE_ASSIGNED)) {
1106 struct switching_rule *rule;
1107
1108 list_for_each_entry(rule, &s->fe->switching_rules, list) {
1109 int ret;
1110
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001111 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 +02001112 ret = acl_pass(ret);
1113 if (rule->cond->pol == ACL_COND_UNLESS)
1114 ret = !ret;
1115
1116 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001117 if (!session_set_backend(s, rule->be.backend))
1118 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001119 break;
1120 }
1121 }
1122
1123 /* To ensure correct connection accounting on the backend, we
1124 * have to assign one if it was not set (eg: a listen). This
1125 * measure also takes care of correctly setting the default
1126 * backend if any.
1127 */
1128 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001129 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
1130 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001131 }
1132
Willy Tarreaufb356202010-08-03 14:02:05 +02001133 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
1134 if (s->fe == s->be) {
1135 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001136 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001137 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001138
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001139 /* 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 +01001140 * persistence rule, and report that in the session.
1141 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001142 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001143 int ret = 1;
1144
1145 if (prst_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001146 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 +01001147 ret = acl_pass(ret);
1148 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1149 ret = !ret;
1150 }
1151
1152 if (ret) {
1153 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001154 if (prst_rule->type == PERSIST_TYPE_FORCE) {
1155 s->flags |= SN_FORCE_PRST;
1156 } else {
1157 s->flags |= SN_IGNORE_PRST;
1158 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001159 break;
1160 }
1161 }
1162
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001163 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001164
1165 sw_failed:
1166 /* immediately abort this request in case of allocation failure */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001167 channel_abort(s->req);
1168 channel_abort(s->rep);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001169
1170 if (!(s->flags & SN_ERR_MASK))
1171 s->flags |= SN_ERR_RESOURCE;
1172 if (!(s->flags & SN_FINST_MASK))
1173 s->flags |= SN_FINST_R;
1174
1175 s->txn.status = 500;
1176 s->req->analysers = 0;
1177 s->req->analyse_exp = TICK_ETERNITY;
1178 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001179}
1180
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001181/* This stream analyser works on a request. It applies all use-server rules on
1182 * it then returns 1. The data must already be present in the buffer otherwise
1183 * they won't match. It always returns 1.
1184 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001185static int process_server_rules(struct session *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001186{
1187 struct proxy *px = s->be;
1188 struct server_rule *rule;
1189
1190 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1191 now_ms, __FUNCTION__,
1192 s,
1193 req,
1194 req->rex, req->wex,
1195 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001196 req->i + req->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001197 req->analysers);
1198
1199 if (!(s->flags & SN_ASSIGNED)) {
1200 list_for_each_entry(rule, &px->server_rules, list) {
1201 int ret;
1202
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001203 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 +02001204 ret = acl_pass(ret);
1205 if (rule->cond->pol == ACL_COND_UNLESS)
1206 ret = !ret;
1207
1208 if (ret) {
1209 struct server *srv = rule->srv.ptr;
1210
1211 if ((srv->state & SRV_RUNNING) ||
1212 (px->options & PR_O_PERSIST) ||
1213 (s->flags & SN_FORCE_PRST)) {
1214 s->flags |= SN_DIRECT | SN_ASSIGNED;
1215 set_target_server(&s->target, srv);
1216 break;
1217 }
1218 /* if the server is not UP, let's go on with next rules
1219 * just in case another one is suited.
1220 */
1221 }
1222 }
1223 }
1224
1225 req->analysers &= ~an_bit;
1226 req->analyse_exp = TICK_ETERNITY;
1227 return 1;
1228}
1229
Emeric Brun1d33b292010-01-04 15:47:17 +01001230/* This stream analyser works on a request. It applies all sticking rules on
1231 * it then returns 1. The data must already be present in the buffer otherwise
1232 * they won't match. It always returns 1.
1233 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001234static int process_sticking_rules(struct session *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001235{
1236 struct proxy *px = s->be;
1237 struct sticking_rule *rule;
1238
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001239 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 +01001240 now_ms, __FUNCTION__,
1241 s,
1242 req,
1243 req->rex, req->wex,
1244 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001245 req->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001246 req->analysers);
1247
1248 list_for_each_entry(rule, &px->sticking_rules, list) {
1249 int ret = 1 ;
1250 int i;
1251
1252 for (i = 0; i < s->store_count; i++) {
1253 if (rule->table.t == s->store[i].table)
1254 break;
1255 }
1256
1257 if (i != s->store_count)
1258 continue;
1259
1260 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001261 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001262 ret = acl_pass(ret);
1263 if (rule->cond->pol == ACL_COND_UNLESS)
1264 ret = !ret;
1265 }
1266
1267 if (ret) {
1268 struct stktable_key *key;
1269
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001270 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 +01001271 if (!key)
1272 continue;
1273
1274 if (rule->flags & STK_IS_MATCH) {
1275 struct stksess *ts;
1276
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001277 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001278 if (!(s->flags & SN_ASSIGNED)) {
1279 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001280 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001281
1282 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001283 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1284 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001285 if (node) {
1286 struct server *srv;
1287
1288 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001289 if ((srv->state & SRV_RUNNING) ||
1290 (px->options & PR_O_PERSIST) ||
1291 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001292 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001293 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001294 }
1295 }
1296 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001297 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001298 }
1299 }
1300 if (rule->flags & STK_IS_STORE) {
1301 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1302 struct stksess *ts;
1303
1304 ts = stksess_new(rule->table.t, key);
1305 if (ts) {
1306 s->store[s->store_count].table = rule->table.t;
1307 s->store[s->store_count++].ts = ts;
1308 }
1309 }
1310 }
1311 }
1312 }
1313
1314 req->analysers &= ~an_bit;
1315 req->analyse_exp = TICK_ETERNITY;
1316 return 1;
1317}
1318
1319/* This stream analyser works on a response. It applies all store rules on it
1320 * then returns 1. The data must already be present in the buffer otherwise
1321 * they won't match. It always returns 1.
1322 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001323static int process_store_rules(struct session *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001324{
1325 struct proxy *px = s->be;
1326 struct sticking_rule *rule;
1327 int i;
1328
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001329 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 +01001330 now_ms, __FUNCTION__,
1331 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001332 rep,
1333 rep->rex, rep->wex,
1334 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001335 rep->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001336 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001337
1338 list_for_each_entry(rule, &px->storersp_rules, list) {
1339 int ret = 1 ;
1340 int storereqidx = -1;
1341
1342 for (i = 0; i < s->store_count; i++) {
1343 if (rule->table.t == s->store[i].table) {
1344 if (!(s->store[i].flags))
1345 storereqidx = i;
1346 break;
1347 }
1348 }
1349
1350 if ((i != s->store_count) && (storereqidx == -1))
1351 continue;
1352
1353 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001354 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001355 ret = acl_pass(ret);
1356 if (rule->cond->pol == ACL_COND_UNLESS)
1357 ret = !ret;
1358 }
1359
1360 if (ret) {
1361 struct stktable_key *key;
1362
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001363 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 +01001364 if (!key)
1365 continue;
1366
1367 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001368 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001369 s->store[storereqidx].flags = 1;
1370 }
1371 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1372 struct stksess *ts;
1373
1374 ts = stksess_new(rule->table.t, key);
1375 if (ts) {
1376 s->store[s->store_count].table = rule->table.t;
1377 s->store[s->store_count].flags = 1;
1378 s->store[s->store_count++].ts = ts;
1379 }
1380 }
1381 }
1382 }
1383
1384 /* process store request and store response */
1385 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001386 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001387 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001388
Simon Hormanfa461682011-06-25 09:39:49 +09001389 if (target_srv(&s->target) && target_srv(&s->target)->state & SRV_NON_STICK) {
1390 stksess_free(s->store[i].table, s->store[i].ts);
1391 s->store[i].ts = NULL;
1392 continue;
1393 }
1394
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001395 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1396 if (ts) {
1397 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001398 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001399 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001400 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001401 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001402 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001403
1404 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001405 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001406 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001407 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001408 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001409
1410 rep->analysers &= ~an_bit;
1411 rep->analyse_exp = TICK_ETERNITY;
1412 return 1;
1413}
1414
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001415/* This macro is very specific to the function below. See the comments in
1416 * process_session() below to understand the logic and the tests.
1417 */
1418#define UPDATE_ANALYSERS(real, list, back, flag) { \
1419 list = (((list) & ~(flag)) | ~(back)) & (real); \
1420 back = real; \
1421 if (!(list)) \
1422 break; \
1423 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1424 continue; \
1425}
1426
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001427/* Processes the client, server, request and response jobs of a session task,
1428 * then puts it back to the wait queue in a clean state, or cleans up its
1429 * resources if it must be deleted. Returns in <next> the date the task wants
1430 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1431 * nothing too many times, the request and response buffers flags are monitored
1432 * and each function is called only if at least another function has changed at
1433 * least one flag it is interested in.
1434 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001435struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001436{
Willy Tarreau827aee92011-03-10 16:55:02 +01001437 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001438 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001439 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001440 unsigned int rq_prod_last, rq_cons_last;
1441 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001442 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001443
1444 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1445 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1446
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001447 /* this data may be no longer valid, clear it */
1448 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1449
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001450 /* This flag must explicitly be set every time */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001451 s->req->flags &= ~CF_READ_NOEXP;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001452
1453 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001454 rqf_last = s->req->flags & ~CF_MASK_ANALYSER;
1455 rpf_last = s->rep->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001456
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001457 /* we don't want the stream interface functions to recursively wake us up */
1458 if (s->req->prod->owner == t)
1459 s->req->prod->flags |= SI_FL_DONT_WAKE;
1460 if (s->req->cons->owner == t)
1461 s->req->cons->flags |= SI_FL_DONT_WAKE;
1462
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001463 /* 1a: Check for low level timeouts if needed. We just set a flag on
1464 * stream interfaces when their timeouts have expired.
1465 */
1466 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1467 stream_int_check_timeouts(&s->si[0]);
1468 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001469
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001470 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001471 * for future reads or writes. Note: this will also concern upper layers
1472 * but we do not touch any other flag. We must be careful and correctly
1473 * detect state changes when calling them.
1474 */
1475
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001476 channel_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001477
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001478 if (unlikely((s->req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
Willy Tarreau14641402009-12-29 14:49:56 +01001479 s->req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001480 si_shutw(s->req->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001481 }
1482
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001483 if (unlikely((s->req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001484 if (s->req->prod->flags & SI_FL_NOHALF)
1485 s->req->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001486 si_shutr(s->req->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001487 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001488
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001489 channel_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001490
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001491 if (unlikely((s->rep->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
Willy Tarreau14641402009-12-29 14:49:56 +01001492 s->rep->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001493 si_shutw(s->rep->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001494 }
1495
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001496 if (unlikely((s->rep->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001497 if (s->rep->prod->flags & SI_FL_NOHALF)
1498 s->rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001499 si_shutr(s->rep->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001500 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001501 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001502
1503 /* 1b: check for low-level errors reported at the stream interface.
1504 * First we check if it's a retryable error (in which case we don't
1505 * want to tell the buffer). Otherwise we report the error one level
1506 * upper by setting flags into the buffers. Note that the side towards
1507 * the client cannot have connect (hence retryable) errors. Also, the
1508 * connection setup code must be able to deal with any type of abort.
1509 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001510 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001511 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1512 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001513 si_shutr(&s->si[0]);
1514 si_shutw(&s->si[0]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001515 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001516 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001517 s->be->be_counters.cli_aborts++;
1518 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001519 if (srv)
1520 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001521 if (!(s->flags & SN_ERR_MASK))
1522 s->flags |= SN_ERR_CLICL;
1523 if (!(s->flags & SN_FINST_MASK))
1524 s->flags |= SN_FINST_D;
1525 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001526 }
1527 }
1528
1529 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1530 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001531 si_shutr(&s->si[1]);
1532 si_shutw(&s->si[1]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001533 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001534 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001535 if (srv)
1536 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001537 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001538 s->be->be_counters.srv_aborts++;
1539 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001540 if (srv)
1541 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001542 if (!(s->flags & SN_ERR_MASK))
1543 s->flags |= SN_ERR_SRVCL;
1544 if (!(s->flags & SN_FINST_MASK))
1545 s->flags |= SN_FINST_D;
1546 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001547 }
1548 /* note: maybe we should process connection errors here ? */
1549 }
1550
1551 if (s->si[1].state == SI_ST_CON) {
1552 /* we were trying to establish a connection on the server side,
1553 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1554 */
1555 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1556 sess_update_st_cer(s, &s->si[1]);
1557 else if (s->si[1].state == SI_ST_EST)
1558 sess_establish(s, &s->si[1]);
1559
1560 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1561 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1562 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1563 */
1564 }
1565
Willy Tarreau815a9b22010-07-27 17:15:12 +02001566 rq_prod_last = s->si[0].state;
1567 rq_cons_last = s->si[1].state;
1568 rp_cons_last = s->si[0].state;
1569 rp_prod_last = s->si[1].state;
1570
1571 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001572 /* Check for connection closure */
1573
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001574 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001575 "[%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 +01001576 now_ms, __FUNCTION__, __LINE__,
1577 t,
1578 s, s->flags,
1579 s->req, s->rep,
1580 s->req->rex, s->rep->wex,
1581 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001582 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 +01001583 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001584 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001585
1586 /* nothing special to be done on client side */
1587 if (unlikely(s->req->prod->state == SI_ST_DIS))
1588 s->req->prod->state = SI_ST_CLO;
1589
1590 /* When a server-side connection is released, we have to count it and
1591 * check for pending connections on this server.
1592 */
1593 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1594 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001595 srv = target_srv(&s->target);
1596 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001597 if (s->flags & SN_CURR_SESS) {
1598 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001599 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001600 }
1601 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001602 if (may_dequeue_tasks(srv, s->be))
1603 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001604 }
1605 }
1606
1607 /*
1608 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1609 * at this point.
1610 */
1611
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001612 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001613 /* Analyse request */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001614 if (((s->req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1615 ((s->req->flags ^ rqf_last) & CF_MASK_STATIC) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001616 s->si[0].state != rq_prod_last ||
1617 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001618 unsigned int flags = s->req->flags;
1619
1620 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001621 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001622 unsigned int ana_list;
1623 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001624
Willy Tarreau90deb182010-01-07 00:20:41 +01001625 /* it's up to the analysers to stop new connections,
1626 * disable reading or closing. Note: if an analyser
1627 * disables any of these bits, it is responsible for
1628 * enabling them again when it disables itself, so
1629 * that other analysers are called in similar conditions.
1630 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001631 channel_auto_read(s->req);
1632 channel_auto_connect(s->req);
1633 channel_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001634
1635 /* We will call all analysers for which a bit is set in
1636 * s->req->analysers, following the bit order from LSB
1637 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001638 * the list when not needed. Any analyser may return 0
1639 * to break out of the loop, either because of missing
1640 * data to take a decision, or because it decides to
1641 * kill the session. We loop at least once through each
1642 * analyser, and we may loop again if other analysers
1643 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001644 *
1645 * We build a list of analysers to run. We evaluate all
1646 * of these analysers in the order of the lower bit to
1647 * the higher bit. This ordering is very important.
1648 * An analyser will often add/remove other analysers,
1649 * including itself. Any changes to itself have no effect
1650 * on the loop. If it removes any other analysers, we
1651 * want those analysers not to be called anymore during
1652 * this loop. If it adds an analyser that is located
1653 * after itself, we want it to be scheduled for being
1654 * processed during the loop. If it adds an analyser
1655 * which is located before it, we want it to switch to
1656 * it immediately, even if it has already been called
1657 * once but removed since.
1658 *
1659 * In order to achieve this, we compare the analyser
1660 * list after the call with a copy of it before the
1661 * call. The work list is fed with analyser bits that
1662 * appeared during the call. Then we compare previous
1663 * work list with the new one, and check the bits that
1664 * appeared. If the lowest of these bits is lower than
1665 * the current bit, it means we have enabled a previous
1666 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001667 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001668
1669 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001670 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001671 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001672
Willy Tarreaufb356202010-08-03 14:02:05 +02001673 if (ana_list & AN_REQ_INSPECT_FE) {
1674 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001675 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001676 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001677 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001678
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001679 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001680 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001681 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001682 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001683 }
1684
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001685 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001686 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1687 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001688 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001689 }
1690
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001691 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001692 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1693 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001694 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001695 }
1696
Willy Tarreaufb356202010-08-03 14:02:05 +02001697 if (ana_list & AN_REQ_INSPECT_BE) {
1698 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1699 break;
1700 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1701 }
1702
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001703 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001704 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1705 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001706 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001707 }
1708
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001709 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001710 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001711 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001712 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001713 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001714
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001715 if (ana_list & AN_REQ_SRV_RULES) {
1716 if (!process_server_rules(s, s->req, AN_REQ_SRV_RULES))
1717 break;
1718 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
1719 }
1720
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001721 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001722 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1723 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001724 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001725 }
1726
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001727 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001728 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001729 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001730 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001731 }
Emeric Brun647caf12009-06-30 17:57:00 +02001732
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001733 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001734 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1735 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001736 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001737 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001738
Emeric Brun1d33b292010-01-04 15:47:17 +01001739 if (ana_list & AN_REQ_STICKING_RULES) {
1740 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1741 break;
1742 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1743 }
1744
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001745 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001746 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1747 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001748 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001749 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001750 break;
1751 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001752 }
Willy Tarreau84455332009-03-15 22:34:05 +01001753
Willy Tarreau815a9b22010-07-27 17:15:12 +02001754 rq_prod_last = s->si[0].state;
1755 rq_cons_last = s->si[1].state;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001756 s->req->flags &= ~CF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001757 rqf_last = s->req->flags;
1758
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001759 if ((s->req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001760 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001761 }
1762
Willy Tarreau576507f2010-01-07 00:09:04 +01001763 /* we'll monitor the request analysers while parsing the response,
1764 * because some response analysers may indirectly enable new request
1765 * analysers (eg: HTTP keep-alive).
1766 */
1767 req_ana_back = s->req->analysers;
1768
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001769 resync_response:
1770 /* Analyse response */
1771
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001772 if (unlikely(s->rep->flags & CF_HIJACK)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001773 /* In inject mode, we wake up everytime something has
1774 * happened on the write side of the buffer.
1775 */
1776 unsigned int flags = s->rep->flags;
1777
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001778 if ((s->rep->flags & (CF_WRITE_PARTIAL|CF_WRITE_ERROR|CF_SHUTW)) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02001779 !channel_full(s->rep)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001780 s->rep->hijacker(s, s->rep);
1781 }
1782
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001783 if ((s->rep->flags ^ flags) & CF_MASK_STATIC) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001784 rpf_last = s->rep->flags;
1785 goto resync_response;
1786 }
1787 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001788 else if (((s->rep->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1789 (s->rep->flags ^ rpf_last) & CF_MASK_STATIC ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001790 s->si[0].state != rp_cons_last ||
1791 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001792 unsigned int flags = s->rep->flags;
1793
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001794 if ((s->rep->flags & CF_MASK_ANALYSER) &&
Willy Tarreau0499e352010-12-17 07:13:42 +01001795 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1796 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1797 * for some free space in the response buffer, so we might need to call
1798 * it when something changes in the response buffer, but still we pass
1799 * it the request buffer. Note that the SI state might very well still
1800 * be zero due to us returning a flow of redirects!
1801 */
1802 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001803 s->req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001804 }
1805
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001806 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001807 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001808 unsigned int ana_list;
1809 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001810
Willy Tarreau90deb182010-01-07 00:20:41 +01001811 /* it's up to the analysers to stop disable reading or
1812 * closing. Note: if an analyser disables any of these
1813 * bits, it is responsible for enabling them again when
1814 * it disables itself, so that other analysers are called
1815 * in similar conditions.
1816 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001817 channel_auto_read(s->rep);
1818 channel_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001819
1820 /* We will call all analysers for which a bit is set in
1821 * s->rep->analysers, following the bit order from LSB
1822 * to MSB. The analysers must remove themselves from
1823 * the list when not needed. Any analyser may return 0
1824 * to break out of the loop, either because of missing
1825 * data to take a decision, or because it decides to
1826 * kill the session. We loop at least once through each
1827 * analyser, and we may loop again if other analysers
1828 * are added in the middle.
1829 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001830
1831 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001832 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001833 /* Warning! ensure that analysers are always placed in ascending order! */
1834
Emeric Brun97679e72010-09-23 17:56:44 +02001835 if (ana_list & AN_RES_INSPECT) {
1836 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1837 break;
1838 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1839 }
1840
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001841 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001842 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1843 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001844 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001845 }
1846
Emeric Brun1d33b292010-01-04 15:47:17 +01001847 if (ana_list & AN_RES_STORE_RULES) {
1848 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1849 break;
1850 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1851 }
1852
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001853 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001854 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1855 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001856 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001857 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001859 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001860 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1861 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001862 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001863 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001864 break;
1865 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001866 }
1867
Willy Tarreau815a9b22010-07-27 17:15:12 +02001868 rp_cons_last = s->si[0].state;
1869 rp_prod_last = s->si[1].state;
1870 rpf_last = s->rep->flags;
1871
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001872 if ((s->rep->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001873 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001874 }
1875
Willy Tarreau576507f2010-01-07 00:09:04 +01001876 /* maybe someone has added some request analysers, so we must check and loop */
1877 if (s->req->analysers & ~req_ana_back)
1878 goto resync_request;
1879
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001880 if ((s->req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001881 goto resync_request;
1882
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001883 /* FIXME: here we should call protocol handlers which rely on
1884 * both buffers.
1885 */
1886
1887
1888 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001889 * Now we propagate unhandled errors to the session. Normally
1890 * we're just in a data phase here since it means we have not
1891 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001892 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001893 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001894 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001895 if (s->req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001896 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001897 s->req->analysers = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001898 if (s->req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001899 s->be->be_counters.cli_aborts++;
1900 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001901 if (srv)
1902 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001903 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001904 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001905 else if (s->req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001906 s->be->be_counters.cli_aborts++;
1907 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001908 if (srv)
1909 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001910 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001911 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001912 else if (s->req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001913 s->be->be_counters.srv_aborts++;
1914 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001915 if (srv)
1916 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001917 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001918 }
1919 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001920 s->be->be_counters.srv_aborts++;
1921 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001922 if (srv)
1923 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001924 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001925 }
Willy Tarreau84455332009-03-15 22:34:05 +01001926 sess_set_term_flags(s);
1927 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001928 else if (s->rep->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001929 /* Report it if the server got an error or a read timeout expired */
1930 s->rep->analysers = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001931 if (s->rep->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001932 s->be->be_counters.srv_aborts++;
1933 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001934 if (srv)
1935 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001936 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001937 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001938 else if (s->rep->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001939 s->be->be_counters.srv_aborts++;
1940 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001941 if (srv)
1942 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001943 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001944 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001945 else if (s->rep->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001946 s->be->be_counters.cli_aborts++;
1947 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001948 if (srv)
1949 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001950 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001951 }
1952 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001953 s->be->be_counters.cli_aborts++;
1954 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001955 if (srv)
1956 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001957 s->flags |= SN_ERR_CLITO;
1958 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001959 sess_set_term_flags(s);
1960 }
Willy Tarreau84455332009-03-15 22:34:05 +01001961 }
1962
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001963 /*
1964 * Here we take care of forwarding unhandled data. This also includes
1965 * connection establishments and shutdown requests.
1966 */
1967
1968
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001969 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001970 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001971 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001972 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001973 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001974 if (!s->req->analysers &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001975 !(s->req->flags & (CF_HIJACK|CF_SHUTW|CF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001976 (s->req->prod->state >= SI_ST_EST) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001977 (s->req->to_forward != CHN_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001978 /* This buffer is freewheeling, there's no analyser nor hijacker
1979 * attached to it. If any data are left in, we'll permit them to
1980 * move.
1981 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001982 channel_auto_read(s->req);
1983 channel_auto_connect(s->req);
1984 channel_auto_close(s->req);
Willy Tarreaua75bcef2012-08-24 22:56:11 +02001985 buffer_flush(&s->req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001986
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001987 /* We'll let data flow between the producer (if still connected)
1988 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001989 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001990 if (!(s->req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001991 channel_forward(s->req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001992 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001993
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001994 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001995 if (!(s->req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001996 s->req->to_forward &&
1997 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau93b0f4f2012-08-30 21:23:53 +02001998 (s->si[0].conn.data && s->si[0].conn.data->rcv_pipe && s->si[0].conn.data->snd_pipe) &&
1999 (s->si[1].conn.data && s->si[1].conn.data->rcv_pipe && s->si[1].conn.data->snd_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002000 (pipes_used < global.maxpipes) &&
2001 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2002 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002003 (s->req->flags & CF_STREAMER_FAST)))) {
2004 s->req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002005 }
2006
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002007 /* reflect what the L7 analysers have seen last */
2008 rqf_last = s->req->flags;
2009
2010 /*
2011 * Now forward all shutdown requests between both sides of the buffer
2012 */
2013
Willy Tarreau520d95e2009-09-19 21:04:57 +02002014 /* first, let's check if the request buffer needs to shutdown(write), which may
2015 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01002016 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002017 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002018 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_HIJACK|CF_AUTO_CLOSE|CF_SHUTR)) ==
2019 (CF_AUTO_CLOSE|CF_SHUTR)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002020 channel_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002021
2022 /* shutdown(write) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002023 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
Willy Tarreau8e21bb92012-08-24 22:40:29 +02002024 channel_is_empty(s->req)))
Willy Tarreau73b013b2012-05-21 16:31:45 +02002025 si_shutw(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002026
2027 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002028 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002029 !s->req->analysers))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002030 channel_shutr_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002031
2032 /* shutdown(read) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002033 if (unlikely((s->req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002034 if (s->req->prod->flags & SI_FL_NOHALF)
2035 s->req->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02002036 si_shutr(s->req->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002037 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002038
Willy Tarreau520d95e2009-09-19 21:04:57 +02002039 /* it's possible that an upper layer has requested a connection setup or abort.
2040 * There are 2 situations where we decide to establish a new connection :
2041 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002042 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002043 */
2044 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002045 if (!(s->req->flags & CF_SHUTW)) {
2046 if ((s->req->flags & CF_AUTO_CONNECT) || !channel_is_empty(s->req)) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01002047 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02002048 * switch to the connected state, otherwise we perform a connection
2049 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002050 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02002051 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02002052 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau3cefd522012-08-30 15:49:18 +02002053 if (unlikely(s->req->cons->conn.target.type == TARG_TYPE_APPLET &&
Willy Tarreau73b013b2012-05-21 16:31:45 +02002054 !(si_ctrl(s->req->cons) && si_ctrl(s->req->cons)->connect))) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02002055 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002056 s->rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau85e7d002010-05-31 11:57:51 +02002057 s->req->wex = TICK_ETERNITY;
2058 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02002059 }
Willy Tarreau73201222009-08-16 18:27:24 +02002060 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002061 else {
Willy Tarreau92795622009-03-06 12:51:23 +01002062 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002063 channel_shutw_now(s->req); /* fix buffer flags upon abort */
2064 channel_shutr_now(s->rep);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002065 }
Willy Tarreau92795622009-03-06 12:51:23 +01002066 }
2067
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002068
2069 /* we may have a pending connection request, or a connection waiting
2070 * for completion.
2071 */
2072 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
2073 do {
2074 /* nb: step 1 might switch from QUE to ASS, but we first want
2075 * to give a chance to step 2 to perform a redirect if needed.
2076 */
2077 if (s->si[1].state != SI_ST_REQ)
2078 sess_update_stream_int(s, &s->si[1]);
2079 if (s->si[1].state == SI_ST_REQ)
2080 sess_prepare_conn_req(s, &s->si[1]);
2081
Willy Tarreau827aee92011-03-10 16:55:02 +01002082 srv = target_srv(&s->target);
2083 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002084 perform_http_redirect(s, &s->si[1]);
2085 } while (s->si[1].state == SI_ST_ASS);
Mark Lamourinec2247f02012-01-04 13:02:01 -05002086
2087 /* Now we can add the server name to a header (if requested) */
2088 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Stathis Voukelatos09a030a2012-01-09 14:27:13 +01002089 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau45c0d982012-03-09 12:11:57 +01002090 (s->be->mode == PR_MODE_HTTP) &&
2091 (s->be->server_id_hdr_name != NULL)) {
2092 http_send_name_header(&s->txn, s->be, target_srv(&s->target)->id);
Mark Lamourinec2247f02012-01-04 13:02:01 -05002093 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002094 }
2095
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002096 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002097 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002098 goto resync_stream_interface;
2099
Willy Tarreau815a9b22010-07-27 17:15:12 +02002100 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002101 if ((s->req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002102 goto resync_request;
2103
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002104 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002105
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002106 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002107 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002108 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002109 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002110 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002111 if (!s->rep->analysers &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002112 !(s->rep->flags & (CF_HIJACK|CF_SHUTW|CF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02002113 (s->rep->prod->state >= SI_ST_EST) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002114 (s->rep->to_forward != CHN_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002115 /* This buffer is freewheeling, there's no analyser nor hijacker
2116 * attached to it. If any data are left in, we'll permit them to
2117 * move.
2118 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002119 channel_auto_read(s->rep);
2120 channel_auto_close(s->rep);
Willy Tarreaua75bcef2012-08-24 22:56:11 +02002121 buffer_flush(&s->rep->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002122
2123 /* We'll let data flow between the producer (if still connected)
2124 * to the consumer.
2125 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002126 if (!(s->rep->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002127 channel_forward(s->rep, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002128
2129 /* if we have no analyser anymore in any direction and have a
2130 * tunnel timeout set, use it now.
2131 */
2132 if (!s->req->analysers && s->be->timeout.tunnel) {
2133 s->req->rto = s->req->wto = s->rep->rto = s->rep->wto =
2134 s->be->timeout.tunnel;
2135 s->req->rex = s->req->wex = s->rep->rex = s->rep->wex =
2136 tick_add(now_ms, s->be->timeout.tunnel);
2137 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002138 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002139
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002140 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002141 if (!(s->rep->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002142 s->rep->to_forward &&
2143 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau93b0f4f2012-08-30 21:23:53 +02002144 (s->si[0].conn.data && s->si[0].conn.data->rcv_pipe && s->si[0].conn.data->snd_pipe) &&
2145 (s->si[1].conn.data && s->si[1].conn.data->rcv_pipe && s->si[1].conn.data->snd_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002146 (pipes_used < global.maxpipes) &&
2147 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2148 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002149 (s->rep->flags & CF_STREAMER_FAST)))) {
2150 s->rep->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002151 }
2152
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002153 /* reflect what the L7 analysers have seen last */
2154 rpf_last = s->rep->flags;
2155
2156 /*
2157 * Now forward all shutdown requests between both sides of the buffer
2158 */
2159
2160 /*
2161 * FIXME: this is probably where we should produce error responses.
2162 */
2163
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002164 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002165 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_HIJACK|CF_AUTO_CLOSE|CF_SHUTR)) ==
2166 (CF_AUTO_CLOSE|CF_SHUTR)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002167 channel_shutw_now(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002168
2169 /* shutdown(write) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002170 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
Willy Tarreau8e21bb92012-08-24 22:40:29 +02002171 channel_is_empty(s->rep)))
Willy Tarreau73b013b2012-05-21 16:31:45 +02002172 si_shutw(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002173
2174 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002175 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002176 !s->rep->analysers)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002177 channel_shutr_now(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002178
2179 /* shutdown(read) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002180 if (unlikely((s->rep->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002181 if (s->rep->prod->flags & SI_FL_NOHALF)
2182 s->rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02002183 si_shutr(s->rep->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002184 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002185
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002186 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002187 goto resync_stream_interface;
2188
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002189 if (s->req->flags != rqf_last)
2190 goto resync_request;
2191
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002192 if ((s->rep->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002193 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002194
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002195 /* we're interested in getting wakeups again */
2196 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
2197 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
2198
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002199 /* This is needed only when debugging is enabled, to indicate
2200 * client-side or server-side close. Please note that in the unlikely
2201 * event where both sides would close at once, the sequence is reported
2202 * on the server side first.
2203 */
2204 if (unlikely((global.mode & MODE_DEBUG) &&
2205 (!(global.mode & MODE_QUIET) ||
2206 (global.mode & MODE_VERBOSE)))) {
2207 int len;
2208
2209 if (s->si[1].state == SI_ST_CLO &&
2210 s->si[1].prev_state == SI_ST_EST) {
2211 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
2212 s->uniq_id, s->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002213 (unsigned short)si_fd(&s->si[0]),
2214 (unsigned short)si_fd(&s->si[1]));
Willy Tarreau21337822012-04-29 14:11:38 +02002215 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002216 }
2217
2218 if (s->si[0].state == SI_ST_CLO &&
2219 s->si[0].prev_state == SI_ST_EST) {
2220 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
2221 s->uniq_id, s->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002222 (unsigned short)si_fd(&s->si[0]),
2223 (unsigned short)si_fd(&s->si[1]));
Willy Tarreau21337822012-04-29 14:11:38 +02002224 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002225 }
2226 }
2227
2228 if (likely((s->rep->cons->state != SI_ST_CLO) ||
2229 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
2230
2231 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
2232 session_process_counters(s);
2233
Willy Tarreau3cefd522012-08-30 15:49:18 +02002234 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->conn.target.type != TARG_TYPE_APPLET)
Willy Tarreau73b013b2012-05-21 16:31:45 +02002235 si_update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002236
Willy Tarreau3cefd522012-08-30 15:49:18 +02002237 if (s->req->cons->state == SI_ST_EST && s->req->cons->conn.target.type != TARG_TYPE_APPLET)
Willy Tarreau73b013b2012-05-21 16:31:45 +02002238 si_update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002239
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002240 s->req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2241 s->rep->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002242 s->si[0].prev_state = s->si[0].state;
2243 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002244 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2245 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002246
2247 /* Trick: if a request is being waiting for the server to respond,
2248 * and if we know the server can timeout, we don't want the timeout
2249 * to expire on the client side first, but we're still interested
2250 * in passing data from the client to the server (eg: POST). Thus,
2251 * we can cancel the client's request timeout if the server's
2252 * request timeout is set and the server has not yet sent a response.
2253 */
2254
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002255 if ((s->rep->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01002256 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002257 s->req->flags |= CF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002258 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01002259 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002260
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002261 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002262 * Note that this one may wake the task up again.
2263 */
Willy Tarreau3cefd522012-08-30 15:49:18 +02002264 if (s->req->cons->conn.target.type == TARG_TYPE_APPLET ||
2265 s->rep->cons->conn.target.type == TARG_TYPE_APPLET) {
2266 if (s->req->cons->conn.target.type == TARG_TYPE_APPLET)
2267 s->req->cons->conn.target.ptr.a->fct(s->req->cons);
2268 if (s->rep->cons->conn.target.type == TARG_TYPE_APPLET)
2269 s->rep->cons->conn.target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002270 if (task_in_rq(t)) {
2271 /* If we woke up, we don't want to requeue the
2272 * task to the wait queue, but rather requeue
2273 * it into the runqueue ASAP.
2274 */
2275 t->expire = TICK_ETERNITY;
2276 return t;
2277 }
2278 }
2279
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002280 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2281 tick_first(s->rep->rex, s->rep->wex));
2282 if (s->req->analysers)
2283 t->expire = tick_first(t->expire, s->req->analyse_exp);
2284
2285 if (s->si[0].exp)
2286 t->expire = tick_first(t->expire, s->si[0].exp);
2287
2288 if (s->si[1].exp)
2289 t->expire = tick_first(t->expire, s->si[1].exp);
2290
2291#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002292 fprintf(stderr,
2293 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2294 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2295 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2296 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 +01002297#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002298
2299#ifdef DEBUG_DEV
2300 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002301 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002302 ABORT_NOW();
2303#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002304 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002305 }
2306
2307 s->fe->feconn--;
2308 if (s->flags & SN_BE_ASSIGNED)
2309 s->be->beconn--;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002310 if (!(s->listener->options & LI_O_UNLIMITED))
2311 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002312 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002313 s->listener->nbconn--;
Willy Tarreau62793712011-07-24 19:23:38 +02002314 if (s->listener->state == LI_FULL)
2315 resume_listener(s->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002316
Willy Tarreau08ceb102011-07-24 22:58:00 +02002317 /* Dequeues all of the listeners waiting for a resource */
2318 if (!LIST_ISEMPTY(&global_listener_queue))
2319 dequeue_all_listeners(&global_listener_queue);
2320
Willy Tarreaub32907b2011-07-25 08:37:44 +02002321 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
2322 (!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 +02002323 dequeue_all_listeners(&s->fe->listener_queue);
2324
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002325 if (unlikely((global.mode & MODE_DEBUG) &&
2326 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2327 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002328 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002329 s->uniq_id, s->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002330 (unsigned short)si_fd(s->req->prod), (unsigned short)si_fd(s->req->cons));
Willy Tarreau21337822012-04-29 14:11:38 +02002331 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002332 }
2333
2334 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2335 session_process_counters(s);
2336
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002337 if (s->txn.status) {
2338 int n;
2339
2340 n = s->txn.status / 100;
2341 if (n < 1 || n > 5)
2342 n = 0;
2343
2344 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002345 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002346
Willy Tarreau24657792010-02-26 10:30:28 +01002347 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002348 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002349 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002350 }
2351
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002352 /* let's do a final log if we need it */
2353 if (s->logs.logwait &&
2354 !(s->flags & SN_MONITOR) &&
2355 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002356 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002357 }
2358
2359 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002360 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002361 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002362 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002363 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002364}
2365
Willy Tarreau7c669d72008-06-20 15:04:11 +02002366/*
2367 * This function adjusts sess->srv_conn and maintains the previous and new
2368 * server's served session counts. Setting newsrv to NULL is enough to release
2369 * current connection slot. This function also notifies any LB algo which might
2370 * expect to be informed about any change in the number of active sessions on a
2371 * server.
2372 */
2373void sess_change_server(struct session *sess, struct server *newsrv)
2374{
2375 if (sess->srv_conn == newsrv)
2376 return;
2377
2378 if (sess->srv_conn) {
2379 sess->srv_conn->served--;
2380 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2381 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002382 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002383 }
2384
2385 if (newsrv) {
2386 newsrv->served++;
2387 if (newsrv->proxy->lbprm.server_take_conn)
2388 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002389 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002390 }
2391}
2392
Willy Tarreau84455332009-03-15 22:34:05 +01002393/* Handle server-side errors for default protocols. It is called whenever a a
2394 * connection setup is aborted or a request is aborted in queue. It sets the
2395 * session termination flags so that the caller does not have to worry about
2396 * them. It's installed as ->srv_error for the server-side stream_interface.
2397 */
2398void default_srv_error(struct session *s, struct stream_interface *si)
2399{
2400 int err_type = si->err_type;
2401 int err = 0, fin = 0;
2402
2403 if (err_type & SI_ET_QUEUE_ABRT) {
2404 err = SN_ERR_CLICL;
2405 fin = SN_FINST_Q;
2406 }
2407 else if (err_type & SI_ET_CONN_ABRT) {
2408 err = SN_ERR_CLICL;
2409 fin = SN_FINST_C;
2410 }
2411 else if (err_type & SI_ET_QUEUE_TO) {
2412 err = SN_ERR_SRVTO;
2413 fin = SN_FINST_Q;
2414 }
2415 else if (err_type & SI_ET_QUEUE_ERR) {
2416 err = SN_ERR_SRVCL;
2417 fin = SN_FINST_Q;
2418 }
2419 else if (err_type & SI_ET_CONN_TO) {
2420 err = SN_ERR_SRVTO;
2421 fin = SN_FINST_C;
2422 }
2423 else if (err_type & SI_ET_CONN_ERR) {
2424 err = SN_ERR_SRVCL;
2425 fin = SN_FINST_C;
2426 }
2427 else /* SI_ET_CONN_OTHER and others */ {
2428 err = SN_ERR_INTERNAL;
2429 fin = SN_FINST_C;
2430 }
2431
2432 if (!(s->flags & SN_ERR_MASK))
2433 s->flags |= err;
2434 if (!(s->flags & SN_FINST_MASK))
2435 s->flags |= fin;
2436}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002437
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002438/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
2439void session_shutdown(struct session *session, int why)
2440{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002441 if (session->req->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002442 return;
2443
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002444 channel_shutw_now(session->req);
2445 channel_shutr_now(session->rep);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002446 session->task->nice = 1024;
2447 if (!(session->flags & SN_ERR_MASK))
2448 session->flags |= why;
2449 task_wakeup(session->task, TASK_WOKEN_OTHER);
2450}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002451
Willy Tarreau8b22a712010-06-18 17:46:06 +02002452/************************************************************************/
2453/* All supported ACL keywords must be declared here. */
2454/************************************************************************/
2455
Willy Tarreaua5e37562011-12-16 17:06:15 +01002456/* set temp integer to the General Purpose Counter 0 value in the stksess entry <ts> */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002457static int
Willy Tarreau37406352012-04-23 16:16:37 +02002458acl_fetch_get_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002459{
Willy Tarreau37406352012-04-23 16:16:37 +02002460 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002461 smp->type = SMP_T_UINT;
2462 smp->data.uint = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002463 if (ts != NULL) {
2464 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2465 if (!ptr)
2466 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002467 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002468 }
2469 return 1;
2470}
2471
Willy Tarreaua5e37562011-12-16 17:06:15 +01002472/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002473 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002474 */
2475static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002476acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002477 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002478{
Willy Tarreau56123282010-08-06 19:06:56 +02002479 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002480 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002481 return acl_fetch_get_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002482}
2483
Willy Tarreaua5e37562011-12-16 17:06:15 +01002484/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002485 * backend counters.
2486 */
2487static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002488acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002489 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002490{
Willy Tarreau56123282010-08-06 19:06:56 +02002491 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002492 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002493 return acl_fetch_get_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002494}
2495
Willy Tarreaua5e37562011-12-16 17:06:15 +01002496/* set temp integer to the General Purpose Counter 0 value from the session's source
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002497 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002498 * Accepts exactly 1 argument of type table.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002499 */
2500static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002501acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002502 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002503{
2504 struct stktable_key *key;
2505
Willy Tarreau64ee4912012-08-30 22:59:48 +02002506 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002507 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002508 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002509
Willy Tarreau24e32d82012-04-23 23:55:44 +02002510 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002511 return acl_fetch_get_gpc0(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002512}
2513
2514/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002515 * return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002516 */
2517static int
Willy Tarreau37406352012-04-23 16:16:37 +02002518acl_fetch_inc_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002519{
Willy Tarreau37406352012-04-23 16:16:37 +02002520 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002521 smp->type = SMP_T_UINT;
2522 smp->data.uint = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002523 if (ts != NULL) {
2524 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2525 if (!ptr)
2526 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002527 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002528 }
2529 return 1;
2530}
2531
2532/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002533 * frontend counters and return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002534 */
2535static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002536acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002537 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002538{
Willy Tarreau56123282010-08-06 19:06:56 +02002539 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002540 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002541 return acl_fetch_inc_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002542}
2543
2544/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002545 * backend counters and return it into temp integer.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002546 */
2547static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002548acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002549 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002550{
Willy Tarreau56123282010-08-06 19:06:56 +02002551 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002552 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002553 return acl_fetch_inc_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002554}
2555
2556/* Increment the General Purpose Counter 0 value from the session's source
Willy Tarreaua5e37562011-12-16 17:06:15 +01002557 * address in the table pointed to by expr, and return it into temp integer.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002558 * Accepts exactly 1 argument of type table.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002559 */
2560static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002561acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002562 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002563{
2564 struct stktable_key *key;
2565
Willy Tarreau64ee4912012-08-30 22:59:48 +02002566 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002567 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002568 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002569
Willy Tarreau24e32d82012-04-23 23:55:44 +02002570 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002571 return acl_fetch_inc_gpc0(&px->table, smp, stktable_update_key(&px->table, key));
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002572}
2573
Willy Tarreauf73cd112011-08-13 01:45:16 +02002574/* Clear the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002575 * return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002576 */
2577static int
Willy Tarreau37406352012-04-23 16:16:37 +02002578acl_fetch_clr_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002579{
Willy Tarreau37406352012-04-23 16:16:37 +02002580 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002581 smp->type = SMP_T_UINT;
2582 smp->data.uint = 0;
Willy Tarreauf73cd112011-08-13 01:45:16 +02002583 if (ts != NULL) {
2584 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2585 if (!ptr)
2586 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002587 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002588 stktable_data_cast(ptr, gpc0) = 0;
2589 }
2590 return 1;
2591}
2592
2593/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002594 * frontend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002595 */
2596static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002597acl_fetch_sc1_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002598 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002599{
2600 if (!l4->stkctr1_entry)
2601 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002602 return acl_fetch_clr_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002603}
2604
2605/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002606 * backend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002607 */
2608static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002609acl_fetch_sc2_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002610 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002611{
2612 if (!l4->stkctr2_entry)
2613 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002614 return acl_fetch_clr_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002615}
2616
2617/* Clear the General Purpose Counter 0 value from the session's source address
Willy Tarreaua5e37562011-12-16 17:06:15 +01002618 * in the table pointed to by expr, and return its previous value into temp integer.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002619 * Accepts exactly 1 argument of type table.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002620 */
2621static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002622acl_fetch_src_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002623 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002624{
2625 struct stktable_key *key;
2626
Willy Tarreau64ee4912012-08-30 22:59:48 +02002627 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002628 if (!key)
2629 return 0;
2630
Willy Tarreau24e32d82012-04-23 23:55:44 +02002631 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002632 return acl_fetch_clr_gpc0(&px->table, smp, stktable_update_key(&px->table, key));
Willy Tarreauf73cd112011-08-13 01:45:16 +02002633}
2634
Willy Tarreaua5e37562011-12-16 17:06:15 +01002635/* set temp integer to the cumulated number of connections in the stksess entry <ts> */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002636static int
Willy Tarreau37406352012-04-23 16:16:37 +02002637acl_fetch_conn_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002638{
Willy Tarreau37406352012-04-23 16:16:37 +02002639 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002640 smp->type = SMP_T_UINT;
2641 smp->data.uint = 0;
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002642 if (ts != NULL) {
2643 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2644 if (!ptr)
2645 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002646 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002647 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002648 return 1;
2649}
2650
Willy Tarreaua5e37562011-12-16 17:06:15 +01002651/* set temp integer to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002652static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002653acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002654 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002655{
Willy Tarreau56123282010-08-06 19:06:56 +02002656 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002657 return 0;
2658
Willy Tarreau37406352012-04-23 16:16:37 +02002659 return acl_fetch_conn_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002660}
2661
Willy Tarreaua5e37562011-12-16 17:06:15 +01002662/* set temp integer to the cumulated number of connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002663static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002664acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002665 const struct arg *args, struct sample *smp)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002666{
Willy Tarreau56123282010-08-06 19:06:56 +02002667 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002668 return 0;
2669
Willy Tarreau37406352012-04-23 16:16:37 +02002670 return acl_fetch_conn_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002671}
2672
Willy Tarreaua5e37562011-12-16 17:06:15 +01002673/* set temp integer to the cumulated number of connections from the session's source
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002674 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002675 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002676 */
2677static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002678acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002679 const struct arg *args, struct sample *smp)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002680{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002681 struct stktable_key *key;
2682
Willy Tarreau64ee4912012-08-30 22:59:48 +02002683 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002684 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002685 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002686
Willy Tarreau24e32d82012-04-23 23:55:44 +02002687 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002688 return acl_fetch_conn_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002689}
2690
Willy Tarreaua5e37562011-12-16 17:06:15 +01002691/* set temp integer to the connection rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002692static int
Willy Tarreau37406352012-04-23 16:16:37 +02002693acl_fetch_conn_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002694{
Willy Tarreau37406352012-04-23 16:16:37 +02002695 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002696 smp->type = SMP_T_UINT;
2697 smp->data.uint = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002698 if (ts != NULL) {
2699 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2700 if (!ptr)
2701 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002702 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002703 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2704 }
2705 return 1;
2706}
2707
Willy Tarreaua5e37562011-12-16 17:06:15 +01002708/* set temp integer to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002709 * the configured period.
2710 */
2711static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002712acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002713 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002714{
Willy Tarreau56123282010-08-06 19:06:56 +02002715 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002716 return 0;
2717
Willy Tarreau37406352012-04-23 16:16:37 +02002718 return acl_fetch_conn_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002719}
2720
Willy Tarreaua5e37562011-12-16 17:06:15 +01002721/* set temp integer to the connection rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002722 * the configured period.
2723 */
2724static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002725acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002726 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002727{
Willy Tarreau56123282010-08-06 19:06:56 +02002728 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002729 return 0;
2730
Willy Tarreau37406352012-04-23 16:16:37 +02002731 return acl_fetch_conn_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002732}
2733
Willy Tarreaua5e37562011-12-16 17:06:15 +01002734/* set temp integer to the connection rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002735 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002736 * Accepts exactly 1 argument of type table.
Willy Tarreau91c43d72010-06-20 11:19:22 +02002737 */
2738static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002739acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002740 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002741{
2742 struct stktable_key *key;
2743
Willy Tarreau64ee4912012-08-30 22:59:48 +02002744 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002745 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002746 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002747
Willy Tarreau24e32d82012-04-23 23:55:44 +02002748 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002749 return acl_fetch_conn_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau91c43d72010-06-20 11:19:22 +02002750}
2751
Willy Tarreaua5e37562011-12-16 17:06:15 +01002752/* set temp integer to the number of connections from the session's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002753 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002754 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002755 */
2756static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002757acl_fetch_src_updt_conn_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 Tarreau8b22a712010-06-18 17:46:06 +02002759{
2760 struct stksess *ts;
2761 struct stktable_key *key;
2762 void *ptr;
2763
Willy Tarreau64ee4912012-08-30 22:59:48 +02002764 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002765 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002766 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002767
Willy Tarreau24e32d82012-04-23 23:55:44 +02002768 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002769
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002770 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2771 /* entry does not exist and could not be created */
2772 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002773
2774 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2775 if (!ptr)
2776 return 0; /* parameter not stored in this table */
2777
Willy Tarreauf853c462012-04-23 18:53:56 +02002778 smp->type = SMP_T_UINT;
2779 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002780 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002781 return 1;
2782}
2783
Willy Tarreaua5e37562011-12-16 17:06:15 +01002784/* set temp integer to the number of concurrent connections in the stksess entry <ts> */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002785static int
Willy Tarreau37406352012-04-23 16:16:37 +02002786acl_fetch_conn_cur(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002787{
Willy Tarreau37406352012-04-23 16:16:37 +02002788 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002789 smp->type = SMP_T_UINT;
2790 smp->data.uint = 0;
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002791
2792 if (ts != NULL) {
2793 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2794 if (!ptr)
2795 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002796 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002797 }
2798 return 1;
2799}
2800
Willy Tarreaua5e37562011-12-16 17:06:15 +01002801/* set temp integer to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002802static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002803acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002804 const struct arg *args, struct sample *smp)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002805{
Willy Tarreau56123282010-08-06 19:06:56 +02002806 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002807 return 0;
2808
Willy Tarreau37406352012-04-23 16:16:37 +02002809 return acl_fetch_conn_cur(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002810}
2811
Willy Tarreaua5e37562011-12-16 17:06:15 +01002812/* set temp integer to the number of concurrent connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002813static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002814acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002815 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002816{
Willy Tarreau56123282010-08-06 19:06:56 +02002817 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002818 return 0;
2819
Willy Tarreau37406352012-04-23 16:16:37 +02002820 return acl_fetch_conn_cur(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002821}
2822
Willy Tarreaua5e37562011-12-16 17:06:15 +01002823/* set temp integer to the number of concurrent connections from the session's source
Willy Tarreau38285c12010-06-18 16:35:43 +02002824 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002825 * Accepts exactly 1 argument of type table.
Willy Tarreau38285c12010-06-18 16:35:43 +02002826 */
2827static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002828acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002829 const struct arg *args, struct sample *smp)
Willy Tarreau38285c12010-06-18 16:35:43 +02002830{
Willy Tarreau38285c12010-06-18 16:35:43 +02002831 struct stktable_key *key;
2832
Willy Tarreau64ee4912012-08-30 22:59:48 +02002833 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau38285c12010-06-18 16:35:43 +02002834 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002835 return 0;
Willy Tarreau38285c12010-06-18 16:35:43 +02002836
Willy Tarreau24e32d82012-04-23 23:55:44 +02002837 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002838 return acl_fetch_conn_cur(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002839}
2840
Willy Tarreaua5e37562011-12-16 17:06:15 +01002841/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002842static int
Willy Tarreau37406352012-04-23 16:16:37 +02002843acl_fetch_sess_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002844{
Willy Tarreau37406352012-04-23 16:16:37 +02002845 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002846 smp->type = SMP_T_UINT;
2847 smp->data.uint = 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002848 if (ts != NULL) {
2849 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2850 if (!ptr)
2851 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002852 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002853 }
2854 return 1;
2855}
2856
Willy Tarreaua5e37562011-12-16 17:06:15 +01002857/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002858static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002859acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002860 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002861{
Willy Tarreau56123282010-08-06 19:06:56 +02002862 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002863 return 0;
2864
Willy Tarreau37406352012-04-23 16:16:37 +02002865 return acl_fetch_sess_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002866}
2867
Willy Tarreaua5e37562011-12-16 17:06:15 +01002868/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002869static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002870acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002871 const struct arg *args, struct sample *smp)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002872{
Willy Tarreau56123282010-08-06 19:06:56 +02002873 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002874 return 0;
2875
Willy Tarreau37406352012-04-23 16:16:37 +02002876 return acl_fetch_sess_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002877}
2878
Willy Tarreaua5e37562011-12-16 17:06:15 +01002879/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002880 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002881 * Accepts exactly 1 argument of type table.
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002882 */
2883static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002884acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002885 const struct arg *args, struct sample *smp)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002886{
2887 struct stktable_key *key;
2888
Willy Tarreau64ee4912012-08-30 22:59:48 +02002889 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002890 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002891 return 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002892
Willy Tarreau24e32d82012-04-23 23:55:44 +02002893 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002894 return acl_fetch_sess_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002895}
2896
Willy Tarreaua5e37562011-12-16 17:06:15 +01002897/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002898static int
Willy Tarreau37406352012-04-23 16:16:37 +02002899acl_fetch_sess_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002900{
Willy Tarreau37406352012-04-23 16:16:37 +02002901 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002902 smp->type = SMP_T_UINT;
2903 smp->data.uint = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002904 if (ts != NULL) {
2905 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2906 if (!ptr)
2907 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002908 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002909 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2910 }
2911 return 1;
2912}
2913
Willy Tarreaua5e37562011-12-16 17:06:15 +01002914/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002915 * the configured period.
2916 */
2917static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002918acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002919 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002920{
Willy Tarreau56123282010-08-06 19:06:56 +02002921 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002922 return 0;
2923
Willy Tarreau37406352012-04-23 16:16:37 +02002924 return acl_fetch_sess_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002925}
2926
Willy Tarreaua5e37562011-12-16 17:06:15 +01002927/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002928 * the configured period.
2929 */
2930static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002931acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002932 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002933{
Willy Tarreau56123282010-08-06 19:06:56 +02002934 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002935 return 0;
2936
Willy Tarreau37406352012-04-23 16:16:37 +02002937 return acl_fetch_sess_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002938}
2939
Willy Tarreaua5e37562011-12-16 17:06:15 +01002940/* set temp integer to the session rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002941 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002942 * Accepts exactly 1 argument of type table.
Willy Tarreau91c43d72010-06-20 11:19:22 +02002943 */
2944static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002945acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002946 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002947{
2948 struct stktable_key *key;
2949
Willy Tarreau64ee4912012-08-30 22:59:48 +02002950 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002951 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002952 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002953
Willy Tarreau24e32d82012-04-23 23:55:44 +02002954 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002955 return acl_fetch_sess_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau91c43d72010-06-20 11:19:22 +02002956}
2957
Willy Tarreaua5e37562011-12-16 17:06:15 +01002958/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002959static int
Willy Tarreau37406352012-04-23 16:16:37 +02002960acl_fetch_http_req_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002961{
Willy Tarreau37406352012-04-23 16:16:37 +02002962 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002963 smp->type = SMP_T_UINT;
2964 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002965 if (ts != NULL) {
2966 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2967 if (!ptr)
2968 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002969 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002970 }
2971 return 1;
2972}
2973
Willy Tarreaua5e37562011-12-16 17:06:15 +01002974/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002975static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002976acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002977 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002978{
Willy Tarreau56123282010-08-06 19:06:56 +02002979 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002980 return 0;
2981
Willy Tarreau37406352012-04-23 16:16:37 +02002982 return acl_fetch_http_req_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002983}
2984
Willy Tarreaua5e37562011-12-16 17:06:15 +01002985/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002986static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002987acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002988 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002989{
Willy Tarreau56123282010-08-06 19:06:56 +02002990 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002991 return 0;
2992
Willy Tarreau37406352012-04-23 16:16:37 +02002993 return acl_fetch_http_req_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002994}
2995
Willy Tarreaua5e37562011-12-16 17:06:15 +01002996/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02002997 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002998 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002999 */
3000static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003001acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003002 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003003{
3004 struct stktable_key *key;
3005
Willy Tarreau64ee4912012-08-30 22:59:48 +02003006 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003007 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003008 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003009
Willy Tarreau24e32d82012-04-23 23:55:44 +02003010 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003011 return acl_fetch_http_req_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003012}
3013
Willy Tarreaua5e37562011-12-16 17:06:15 +01003014/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003015static int
Willy Tarreau37406352012-04-23 16:16:37 +02003016acl_fetch_http_req_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003017{
Willy Tarreau37406352012-04-23 16:16:37 +02003018 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003019 smp->type = SMP_T_UINT;
3020 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003021 if (ts != NULL) {
3022 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
3023 if (!ptr)
3024 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003025 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02003026 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
3027 }
3028 return 1;
3029}
3030
Willy Tarreaua5e37562011-12-16 17:06:15 +01003031/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02003032 * the configured period.
3033 */
3034static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003035acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003036 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003037{
Willy Tarreau56123282010-08-06 19:06:56 +02003038 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003039 return 0;
3040
Willy Tarreau37406352012-04-23 16:16:37 +02003041 return acl_fetch_http_req_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003042}
3043
Willy Tarreaua5e37562011-12-16 17:06:15 +01003044/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003045 * the configured period.
3046 */
3047static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003048acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003049 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003050{
Willy Tarreau56123282010-08-06 19:06:56 +02003051 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003052 return 0;
3053
Willy Tarreau37406352012-04-23 16:16:37 +02003054 return acl_fetch_http_req_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003055}
3056
Willy Tarreaua5e37562011-12-16 17:06:15 +01003057/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003058 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003059 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003060 */
3061static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003062acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003063 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003064{
3065 struct stktable_key *key;
3066
Willy Tarreau64ee4912012-08-30 22:59:48 +02003067 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003068 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003069 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003070
Willy Tarreau24e32d82012-04-23 23:55:44 +02003071 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003072 return acl_fetch_http_req_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003073}
3074
Willy Tarreaua5e37562011-12-16 17:06:15 +01003075/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003076static int
Willy Tarreau37406352012-04-23 16:16:37 +02003077acl_fetch_http_err_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003078{
Willy Tarreau37406352012-04-23 16:16:37 +02003079 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003080 smp->type = SMP_T_UINT;
3081 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003082 if (ts != NULL) {
3083 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
3084 if (!ptr)
3085 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003086 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003087 }
3088 return 1;
3089}
3090
Willy Tarreaua5e37562011-12-16 17:06:15 +01003091/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003092static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003093acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003094 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003095{
Willy Tarreau56123282010-08-06 19:06:56 +02003096 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003097 return 0;
3098
Willy Tarreau37406352012-04-23 16:16:37 +02003099 return acl_fetch_http_err_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003100}
3101
Willy Tarreaua5e37562011-12-16 17:06:15 +01003102/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003103static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003104acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003105 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003106{
Willy Tarreau56123282010-08-06 19:06:56 +02003107 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003108 return 0;
3109
Willy Tarreau37406352012-04-23 16:16:37 +02003110 return acl_fetch_http_err_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003111}
3112
Willy Tarreaua5e37562011-12-16 17:06:15 +01003113/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02003114 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003115 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003116 */
3117static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003118acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003119 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003120{
3121 struct stktable_key *key;
3122
Willy Tarreau64ee4912012-08-30 22:59:48 +02003123 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003124 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003125 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003126
Willy Tarreau24e32d82012-04-23 23:55:44 +02003127 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003128 return acl_fetch_http_err_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003129}
3130
Willy Tarreaua5e37562011-12-16 17:06:15 +01003131/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003132static int
Willy Tarreau37406352012-04-23 16:16:37 +02003133acl_fetch_http_err_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003134{
Willy Tarreau37406352012-04-23 16:16:37 +02003135 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003136 smp->type = SMP_T_UINT;
3137 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003138 if (ts != NULL) {
3139 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
3140 if (!ptr)
3141 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003142 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02003143 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
3144 }
3145 return 1;
3146}
3147
Willy Tarreaua5e37562011-12-16 17:06:15 +01003148/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02003149 * the configured period.
3150 */
3151static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003152acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003153 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003154{
Willy Tarreau56123282010-08-06 19:06:56 +02003155 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003156 return 0;
3157
Willy Tarreau37406352012-04-23 16:16:37 +02003158 return acl_fetch_http_err_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003159}
3160
Willy Tarreaua5e37562011-12-16 17:06:15 +01003161/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003162 * the configured period.
3163 */
3164static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003165acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003166 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003167{
Willy Tarreau56123282010-08-06 19:06:56 +02003168 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003169 return 0;
3170
Willy Tarreau37406352012-04-23 16:16:37 +02003171 return acl_fetch_http_err_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003172}
3173
Willy Tarreaua5e37562011-12-16 17:06:15 +01003174/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003175 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003176 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003177 */
3178static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003179acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003180 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003181{
3182 struct stktable_key *key;
3183
Willy Tarreau64ee4912012-08-30 22:59:48 +02003184 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003185 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003186 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003187
Willy Tarreau24e32d82012-04-23 23:55:44 +02003188 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003189 return acl_fetch_http_err_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003190}
3191
Willy Tarreaua5e37562011-12-16 17:06:15 +01003192/* set temp integer to the number of kbytes received from clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003193static int
Willy Tarreau37406352012-04-23 16:16:37 +02003194acl_fetch_kbytes_in(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003195{
Willy Tarreau37406352012-04-23 16:16:37 +02003196 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003197 smp->type = SMP_T_UINT;
3198 smp->data.uint = 0;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003199
3200 if (ts != NULL) {
3201 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
3202 if (!ptr)
3203 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003204 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003205 }
3206 return 1;
3207}
3208
Willy Tarreaua5e37562011-12-16 17:06:15 +01003209/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003210 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003211 */
3212static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003213acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003214 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003215{
Willy Tarreau56123282010-08-06 19:06:56 +02003216 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003217 return 0;
3218
Willy Tarreau37406352012-04-23 16:16:37 +02003219 return acl_fetch_kbytes_in(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003220}
3221
Willy Tarreaua5e37562011-12-16 17:06:15 +01003222/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003223 * session's tracked BE counters.
3224 */
3225static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003226acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003227 const struct arg *args, struct sample *smp)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003228{
Willy Tarreau56123282010-08-06 19:06:56 +02003229 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003230 return 0;
3231
Willy Tarreau37406352012-04-23 16:16:37 +02003232 return acl_fetch_kbytes_in(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003233}
3234
Willy Tarreaua5e37562011-12-16 17:06:15 +01003235/* set temp integer to the number of kbytes received from the session's source
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003236 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003237 * Accepts exactly 1 argument of type table.
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003238 */
3239static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003240acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003241 const struct arg *args, struct sample *smp)
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003242{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003243 struct stktable_key *key;
3244
Willy Tarreau64ee4912012-08-30 22:59:48 +02003245 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003246 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003247 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003248
Willy Tarreau24e32d82012-04-23 23:55:44 +02003249 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003250 return acl_fetch_kbytes_in(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003251}
3252
Willy Tarreaua5e37562011-12-16 17:06:15 +01003253/* set temp integer to the bytes rate from clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003254 * configured period.
3255 */
3256static int
Willy Tarreau37406352012-04-23 16:16:37 +02003257acl_fetch_bytes_in_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003258{
Willy Tarreau37406352012-04-23 16:16:37 +02003259 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003260 smp->type = SMP_T_UINT;
3261 smp->data.uint = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003262 if (ts != NULL) {
3263 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
3264 if (!ptr)
3265 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003266 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003267 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
3268 }
3269 return 1;
3270}
3271
Willy Tarreaua5e37562011-12-16 17:06:15 +01003272/* set temp integer to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003273 * counters over the configured period.
3274 */
3275static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003276acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003277 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003278{
Willy Tarreau56123282010-08-06 19:06:56 +02003279 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003280 return 0;
3281
Willy Tarreau37406352012-04-23 16:16:37 +02003282 return acl_fetch_bytes_in_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003283}
3284
Willy Tarreaua5e37562011-12-16 17:06:15 +01003285/* set temp integer to the bytes rate from clients from the session's tracked BE
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003286 * counters over the configured period.
3287 */
3288static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003289acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003290 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003291{
Willy Tarreau56123282010-08-06 19:06:56 +02003292 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003293 return 0;
3294
Willy Tarreau37406352012-04-23 16:16:37 +02003295 return acl_fetch_bytes_in_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003296}
3297
Willy Tarreaua5e37562011-12-16 17:06:15 +01003298/* set temp integer to the bytes rate from clients from the session's source address
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003299 * in the table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003300 * Accepts exactly 1 argument of type table.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003301 */
3302static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003303acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003304 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003305{
3306 struct stktable_key *key;
3307
Willy Tarreau64ee4912012-08-30 22:59:48 +02003308 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003309 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003310 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003311
Willy Tarreau24e32d82012-04-23 23:55:44 +02003312 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003313 return acl_fetch_bytes_in_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003314}
3315
Willy Tarreaua5e37562011-12-16 17:06:15 +01003316/* set temp integer to the number of kbytes sent to clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003317static int
Willy Tarreau37406352012-04-23 16:16:37 +02003318acl_fetch_kbytes_out(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau1aa006f2010-06-18 21:52:52 +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;
3322 smp->data.uint = 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003323
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003324 if (ts != NULL) {
3325 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003326 if (!ptr)
3327 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003328 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003329 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003330 return 1;
3331}
3332
Willy Tarreaua5e37562011-12-16 17:06:15 +01003333/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003334 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003335 */
3336static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003337acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003338 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003339{
Willy Tarreau56123282010-08-06 19:06:56 +02003340 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003341 return 0;
3342
Willy Tarreau37406352012-04-23 16:16:37 +02003343 return acl_fetch_kbytes_out(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003344}
3345
Willy Tarreaua5e37562011-12-16 17:06:15 +01003346/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003347 * tracked BE counters.
3348 */
3349static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003350acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003351 const struct arg *args, struct sample *smp)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003352{
Willy Tarreau56123282010-08-06 19:06:56 +02003353 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003354 return 0;
3355
Willy Tarreau37406352012-04-23 16:16:37 +02003356 return acl_fetch_kbytes_out(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003357}
3358
Willy Tarreaua5e37562011-12-16 17:06:15 +01003359/* set temp integer to the number of kbytes sent to the session's source address in
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003360 * the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003361 * Accepts exactly 1 argument of type table.
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003362 */
3363static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003364acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003365 const struct arg *args, struct sample *smp)
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003366{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003367 struct stktable_key *key;
3368
Willy Tarreau64ee4912012-08-30 22:59:48 +02003369 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003370 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003371 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003372
Willy Tarreau24e32d82012-04-23 23:55:44 +02003373 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003374 return acl_fetch_kbytes_out(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003375}
3376
Willy Tarreaua5e37562011-12-16 17:06:15 +01003377/* set temp integer to the bytes rate to clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003378 * configured period.
3379 */
3380static int
Willy Tarreau37406352012-04-23 16:16:37 +02003381acl_fetch_bytes_out_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003382{
Willy Tarreau37406352012-04-23 16:16:37 +02003383 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003384 smp->type = SMP_T_UINT;
3385 smp->data.uint = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003386 if (ts != NULL) {
3387 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3388 if (!ptr)
3389 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003390 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003391 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3392 }
3393 return 1;
3394}
3395
Willy Tarreaua5e37562011-12-16 17:06:15 +01003396/* set temp integer to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003397 * over the configured period.
3398 */
3399static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003400acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003401 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003402{
Willy Tarreau56123282010-08-06 19:06:56 +02003403 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003404 return 0;
3405
Willy Tarreau37406352012-04-23 16:16:37 +02003406 return acl_fetch_bytes_out_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003407}
3408
Willy Tarreaua5e37562011-12-16 17:06:15 +01003409/* set temp integer to the bytes rate to clients from the session's tracked BE counters
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003410 * over the configured period.
3411 */
3412static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003413acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003414 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003415{
Willy Tarreau56123282010-08-06 19:06:56 +02003416 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003417 return 0;
3418
Willy Tarreau37406352012-04-23 16:16:37 +02003419 return acl_fetch_bytes_out_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003420}
3421
Willy Tarreaua5e37562011-12-16 17:06:15 +01003422/* set temp integer to the bytes rate to client from the session's source address in
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003423 * the table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003424 * Accepts exactly 1 argument of type table.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003425 */
3426static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003427acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003428 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003429{
3430 struct stktable_key *key;
3431
Willy Tarreau64ee4912012-08-30 22:59:48 +02003432 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003433 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003434 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003435
Willy Tarreau24e32d82012-04-23 23:55:44 +02003436 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003437 return acl_fetch_bytes_out_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003438}
3439
Willy Tarreau34db1082012-04-19 17:16:54 +02003440/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003441 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003442 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003443static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003444acl_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003445 const struct arg *args, struct sample *smp)
Willy Tarreauc735a072011-03-29 00:57:02 +02003446{
Willy Tarreau37406352012-04-23 16:16:37 +02003447 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003448 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003449 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003450 return 1;
3451}
3452
Willy Tarreau34db1082012-04-19 17:16:54 +02003453/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003454 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003455 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003456static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003457acl_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003458 const struct arg *args, struct sample *smp)
Willy Tarreauc735a072011-03-29 00:57:02 +02003459{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003460 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003461 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003462 smp->type = SMP_T_UINT;
3463 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003464 return 1;
3465}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003466
Willy Tarreau61612d42012-04-19 18:42:05 +02003467/* Note: must not be declared <const> as its list will be overwritten.
3468 * Please take care of keeping this list alphabetically sorted.
3469 */
Willy Tarreau8b22a712010-06-18 17:46:06 +02003470static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02003471 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3472 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3473 { "sc1_clr_gpc0", acl_parse_int, acl_fetch_sc1_clr_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3474 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3475 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING, 0 },
3476 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3477 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3478 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3479 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3480 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3481 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3482 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3483 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3484 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3485 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3486 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3487 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3488 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3489 { "sc2_clr_gpc0", acl_parse_int, acl_fetch_sc2_clr_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3490 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3491 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING, 0 },
3492 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3493 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3494 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3495 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3496 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3497 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3498 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3499 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3500 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3501 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3502 { "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 +02003503 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3504 { "src_bytes_out_rate", acl_parse_int, acl_fetch_src_bytes_out_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3505 { "src_clr_gpc0", acl_parse_int, acl_fetch_src_clr_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3506 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3507 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3508 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3509 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3510 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3511 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3512 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3513 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3514 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3515 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3516 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3517 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3518 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3519 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3520 { "table_avl", acl_parse_int, acl_fetch_table_avl, acl_match_int, ACL_USE_NOTHING, ARG1(1,TAB) },
3521 { "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 +02003522 { NULL, NULL, NULL, NULL },
3523}};
3524
3525
Willy Tarreau56123282010-08-06 19:06:56 +02003526/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003527 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3528 * <prm> struct is fed with the table name if any. If unspecified, the caller
3529 * will assume that the current proxy's table is used.
3530 */
3531int parse_track_counters(char **args, int *arg,
3532 int section_type, struct proxy *curpx,
3533 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003534 struct proxy *defpx, char **err)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003535{
Willy Tarreau12785782012-04-27 21:37:17 +02003536 int sample_type = 0;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003537
Willy Tarreau56123282010-08-06 19:06:56 +02003538 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003539 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003540 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003541 */
3542 while (args[*arg]) {
3543 if (strcmp(args[*arg], "src") == 0) {
3544 prm->type = STKTABLE_TYPE_IP;
Willy Tarreau12785782012-04-27 21:37:17 +02003545 sample_type = 1;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003546 }
3547 else if (strcmp(args[*arg], "table") == 0) {
3548 if (!args[*arg + 1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003549 memprintf(err, "missing table name");
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003550 return -1;
3551 }
3552 /* we copy the table name for now, it will be resolved later */
3553 prm->table.n = strdup(args[*arg + 1]);
3554 (*arg)++;
3555 }
3556 else {
3557 /* unhandled keywords are handled by the caller */
3558 break;
3559 }
3560 (*arg)++;
3561 }
3562
Willy Tarreau12785782012-04-27 21:37:17 +02003563 if (!sample_type) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003564 memprintf(err,
3565 "tracking key not specified (found %s, only 'src' is supported)",
3566 quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003567 return -1;
3568 }
3569
3570 return 0;
3571}
3572
Willy Tarreau8b22a712010-06-18 17:46:06 +02003573__attribute__((constructor))
3574static void __session_init(void)
3575{
3576 acl_register_keywords(&acl_kws);
3577}
3578
Willy Tarreaubaaee002006-06-26 02:48:02 +02003579/*
3580 * Local variables:
3581 * c-indent-level: 8
3582 * c-basic-offset: 8
3583 * End:
3584 */