blob: 9c5920dd6a31fe3072bce50a4f4f5f48ae4944aa [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;
154 fdtab[cfd].flags = 0;
155 fdtab[cfd].iocb = conn_fd_handler;
156 conn_data_want_recv(&s->si[0].conn);
157 if (conn_data_init(&s->si[0].conn) < 0)
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100158 goto out_free_task;
Willy Tarreau2542b532012-08-31 16:01:23 +0200159
160 /* OK, now either we have a pending handshake to execute with and
161 * then we must return to the I/O layer, or we can proceed with the
162 * end of the session initialization. In case of handshake, we also
163 * set the I/O timeout to the frontend's client timeout.
164 */
165
166 if (s->si[0].conn.flags & CO_FL_HANDSHAKE) {
167 t->process = expire_mini_session;
168 t->expire = tick_add_ifset(now_ms, p->timeout.client);
169 task_queue(t);
170 s->si[0].conn.flags |= CO_FL_INIT_SESS;
171 return 1;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200172 }
173
Willy Tarreau2542b532012-08-31 16:01:23 +0200174 /* OK let's complete session initialization */
175 ret = session_complete(s);
176 if (ret > 0)
177 return ret;
178
179 /* Error unrolling */
180 out_free_task:
181 task_free(t);
182 out_free_session:
183 p->feconn--;
184 if (s->stkctr1_entry || s->stkctr2_entry)
185 session_store_counters(s);
186 pool_free2(pool2_session, s);
187 out_close:
188 if (ret < 0 && p->mode == PR_MODE_HTTP) {
189 /* critical error, no more memory, try to emit a 500 response */
190 struct chunk *err_msg = error_message(s, HTTP_ERR_500);
191 send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
192 }
193
194 if (fdtab[cfd].owner)
195 fd_delete(cfd);
196 else
197 close(cfd);
198 return ret;
199}
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100200
Willy Tarreau2542b532012-08-31 16:01:23 +0200201/* This function kills an existing embryonic session. It stops the connection's
202 * data layer, releases assigned resources, resumes the listener if it was
203 * disabled and finally kills the file descriptor.
204 */
205static void kill_mini_session(struct session *s)
206{
207 /* kill the connection now */
208 conn_data_close(&s->si[0].conn);
209
210 s->fe->feconn--;
211 if (s->stkctr1_entry || s->stkctr2_entry)
212 session_store_counters(s);
213
214 if (!(s->listener->options & LI_O_UNLIMITED))
215 actconn--;
216 jobs--;
217 s->listener->nbconn--;
218 if (s->listener->state == LI_FULL)
219 resume_listener(s->listener);
220
221 /* Dequeues all of the listeners waiting for a resource */
222 if (!LIST_ISEMPTY(&global_listener_queue))
223 dequeue_all_listeners(&global_listener_queue);
224
225 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
226 (!s->fe->fe_sps_lim || freq_ctr_remain(&s->fe->fe_sess_per_sec, s->fe->fe_sps_lim, 0) > 0))
227 dequeue_all_listeners(&s->fe->listener_queue);
228
229 task_delete(s->task);
230 task_free(s->task);
231
232 if (fdtab[s->si[0].conn.t.sock.fd].owner)
233 fd_delete(s->si[0].conn.t.sock.fd);
234 else
235 close(s->si[0].conn.t.sock.fd);
236
237 pool_free2(pool2_session, s);
238}
239
Willy Tarreau22cda212012-08-31 17:43:29 +0200240/* Finish initializing a session from a connection, or kills it if the
241 * connection shows and error. Returns <0 if the connection was killed.
Willy Tarreau2542b532012-08-31 16:01:23 +0200242 */
Willy Tarreau22cda212012-08-31 17:43:29 +0200243int conn_session_complete(struct connection *conn, int flag)
Willy Tarreau2542b532012-08-31 16:01:23 +0200244{
245 struct session *s = container_of(conn, struct session, si[0].conn);
246
Willy Tarreau22cda212012-08-31 17:43:29 +0200247 if (!(conn->flags & CO_FL_ERROR) && (session_complete(s) > 0)) {
Willy Tarreau2542b532012-08-31 16:01:23 +0200248 conn->flags &= ~flag;
249 return 0;
250 }
251
252 /* kill the connection now */
253 kill_mini_session(s);
254 return -1;
255}
256
257/* Manages embryonic sessions timeout. It is only called when the timeout
258 * strikes and performs the required cleanup.
259 */
260static struct task *expire_mini_session(struct task *t)
261{
262 struct session *s = t->context;
263
264 if (!(t->state & TASK_WOKEN_TIMER))
265 return t;
266
267 kill_mini_session(s);
268 return NULL;
269}
270
271/* This function is called from the I/O handler which detects the end of
272 * handshake, in order to complete initialization of a valid session. It must
273 * be called with an embryonic session. It returns a positive value upon
274 * success, 0 if the connection can be ignored, or a negative value upon
275 * critical failure. The accepted file descriptor is closed if we return <= 0.
276 */
277int session_complete(struct session *s)
278{
279 struct listener *l = s->listener;
280 struct proxy *p = s->fe;
281 struct http_txn *txn;
282 struct task *t = s->task;
283 int ret;
284
285 ret = -1; /* assume unrecoverable error by default */
286
287 /* OK, we're keeping the session, so let's properly initialize the session */
288 LIST_ADDQ(&sessions, &s->list);
289 LIST_INIT(&s->back_refs);
290 s->flags |= SN_INITIALIZED;
291
292 s->unique_id = NULL;
293 s->term_trace = 0;
294
295 t->process = l->handler;
296 t->context = s;
297 t->expire = TICK_ETERNITY;
298
299 /* Note: initially, the session's backend points to the frontend.
300 * This changes later when switching rules are executed or
301 * when the default backend is assigned.
302 */
303 s->be = s->fe;
304 s->req = s->rep = NULL; /* will be allocated later */
305
306 /* Let's count a session now */
Willy Tarreaub36b4242010-06-04 20:59:39 +0200307 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200308 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200309 void *ptr;
310
Willy Tarreau56123282010-08-06 19:06:56 +0200311 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200312 if (ptr)
313 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200314
Willy Tarreau56123282010-08-06 19:06:56 +0200315 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200316 if (ptr)
317 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200318 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200319 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200320
Willy Tarreau56123282010-08-06 19:06:56 +0200321 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200322 void *ptr;
323
Willy Tarreau56123282010-08-06 19:06:56 +0200324 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200325 if (ptr)
326 stktable_data_cast(ptr, sess_cnt)++;
327
Willy Tarreau56123282010-08-06 19:06:56 +0200328 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200329 if (ptr)
330 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200331 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200332 }
333
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200334 /* this part should be common with other protocols */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200335 s->si[0].owner = t;
336 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
337 s->si[0].err_type = SI_ET_NONE;
338 s->si[0].err_loc = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200339 s->si[0].release = NULL;
Willy Tarreau63e7fe32012-05-08 15:20:43 +0200340 s->si[0].send_proxy_ofs = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200341 s->si[0].exp = TICK_ETERNITY;
342 s->si[0].flags = SI_FL_NONE;
343
344 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
345 s->si[0].flags |= SI_FL_INDEP_STR;
346
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200347 /* pre-initialize the other side's stream interface to an INIT state. The
348 * callbacks will be initialized before attempting to connect.
349 */
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200350 s->si[1].conn.t.sock.fd = -1; /* just to help with debugging */
Willy Tarreau505e34a2012-07-06 10:17:53 +0200351 s->si[1].conn.flags = CO_FL_NONE;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200352 s->si[1].owner = t;
353 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
354 s->si[1].err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +0200355 s->si[1].conn_retries = 0; /* used for logging too */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200356 s->si[1].err_loc = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200357 s->si[1].release = NULL;
Willy Tarreau63e7fe32012-05-08 15:20:43 +0200358 s->si[1].send_proxy_ofs = 0;
Willy Tarreau3cefd522012-08-30 15:49:18 +0200359 clear_target(&s->si[1].conn.target);
Willy Tarreauc5788912012-08-24 18:12:41 +0200360 si_prepare_embedded(&s->si[1]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200361 s->si[1].exp = TICK_ETERNITY;
362 s->si[1].flags = SI_FL_NONE;
363
364 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
365 s->si[1].flags |= SI_FL_INDEP_STR;
366
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200367 session_init_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +0100368 clear_target(&s->target);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200369 s->pend_pos = NULL;
370
371 /* init store persistence */
372 s->store_count = 0;
373
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200374 if (unlikely((s->req = pool_alloc2(pool2_channel)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200375 goto out_free_task; /* no memory */
376
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200377 if (unlikely((s->rep = pool_alloc2(pool2_channel)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200378 goto out_free_req; /* no memory */
379
380 /* initialize the request buffer */
Willy Tarreau572bf902012-07-02 17:01:20 +0200381 s->req->buf.size = global.tune.bufsize;
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200382 channel_init(s->req);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200383 s->req->prod = &s->si[0];
384 s->req->cons = &s->si[1];
385 s->si[0].ib = s->si[1].ob = s->req;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200386 s->req->flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200387
388 /* activate default analysers enabled for this listener */
389 s->req->analysers = l->analysers;
390
391 s->req->wto = TICK_ETERNITY;
392 s->req->rto = TICK_ETERNITY;
393 s->req->rex = TICK_ETERNITY;
394 s->req->wex = TICK_ETERNITY;
395 s->req->analyse_exp = TICK_ETERNITY;
396
397 /* initialize response buffer */
Willy Tarreau572bf902012-07-02 17:01:20 +0200398 s->rep->buf.size = global.tune.bufsize;
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200399 channel_init(s->rep);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200400 s->rep->prod = &s->si[1];
401 s->rep->cons = &s->si[0];
402 s->si[0].ob = s->si[1].ib = s->rep;
403 s->rep->analysers = 0;
404
Willy Tarreau96e31212011-05-30 18:10:30 +0200405 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200406 s->req->flags |= CF_NEVER_WAIT;
407 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200408 }
409
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200410 s->rep->rto = TICK_ETERNITY;
411 s->rep->wto = TICK_ETERNITY;
412 s->rep->rex = TICK_ETERNITY;
413 s->rep->wex = TICK_ETERNITY;
414 s->rep->analyse_exp = TICK_ETERNITY;
415
Willy Tarreau62f791e2012-03-09 11:32:30 +0100416 txn = &s->txn;
417 /* Those variables will be checked and freed if non-NULL in
418 * session.c:session_free(). It is important that they are
419 * properly initialized.
420 */
421 txn->sessid = NULL;
422 txn->srv_cookie = NULL;
423 txn->cli_cookie = NULL;
424 txn->uri = NULL;
425 txn->req.cap = NULL;
426 txn->rsp.cap = NULL;
427 txn->hdr_idx.v = NULL;
428 txn->hdr_idx.size = txn->hdr_idx.used = 0;
429 txn->req.flags = 0;
430 txn->rsp.flags = 0;
431 /* the HTTP messages need to know what buffer they're associated with */
432 txn->req.buf = s->req;
433 txn->rsp.buf = s->rep;
434
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200435 /* finish initialization of the accepted file descriptor */
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200436 conn_data_want_recv(&s->si[0].conn);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200437
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100438 if (p->accept && (ret = p->accept(s)) <= 0) {
439 /* Either we had an unrecoverable error (<0) or work is
440 * finished (=0, eg: monitoring), in both situations,
441 * we can release everything and close.
442 */
443 goto out_free_rep;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200444 }
445
Willy Tarreau2542b532012-08-31 16:01:23 +0200446 /* we want the connection handler to notify the stream interface about updates. */
447 s->si[0].conn.flags |= CO_FL_NOTIFY_SI;
448
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200449 /* it is important not to call the wakeup function directly but to
450 * pass through task_wakeup(), because this one knows how to apply
451 * priorities to tasks.
452 */
453 task_wakeup(t, TASK_WOKEN_INIT);
454 return 1;
455
456 /* Error unrolling */
457 out_free_rep:
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200458 pool_free2(pool2_channel, s->rep);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200459 out_free_req:
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200460 pool_free2(pool2_channel, s->req);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200461 out_free_task:
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100462 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200463}
464
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465/*
466 * frees the context associated to a session. It must have been removed first.
467 */
Simon Hormandec5be42011-06-08 09:19:07 +0900468static void session_free(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100470 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200471 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100472 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200473 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100474
Willy Tarreaubaaee002006-06-26 02:48:02 +0200475 if (s->pend_pos)
476 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100477
Willy Tarreau827aee92011-03-10 16:55:02 +0100478 if (target_srv(&s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100479 if (s->flags & SN_CURR_SESS) {
480 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100481 target_srv(&s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100482 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100483 if (may_dequeue_tasks(target_srv(&s->target), s->be))
484 process_srv_queue(target_srv(&s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100485 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100486
Willy Tarreau7c669d72008-06-20 15:04:11 +0200487 if (unlikely(s->srv_conn)) {
488 /* the session still has a reserved slot on a server, but
489 * it should normally be only the same as the one above,
490 * so this should not happen in fact.
491 */
492 sess_change_server(s, NULL);
493 }
494
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100495 if (s->req->pipe)
496 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100497
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100498 if (s->rep->pipe)
499 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100500
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200501 pool_free2(pool2_channel, s->req);
502 pool_free2(pool2_channel, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200503
Willy Tarreau46023632010-01-07 22:51:47 +0100504 http_end_txn(s);
505
Willy Tarreaua4cda672010-06-06 18:28:49 +0200506 for (i = 0; i < s->store_count; i++) {
507 if (!s->store[i].ts)
508 continue;
509 stksess_free(s->store[i].table, s->store[i].ts);
510 s->store[i].ts = NULL;
511 }
512
Willy Tarreau34eb6712011-10-24 18:15:04 +0200513 pool_free2(pool2_hdr_idx, txn->hdr_idx.v);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200514 if (fe) {
Willy Tarreau46023632010-01-07 22:51:47 +0100515 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
516 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200517 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100518
Willy Tarreau56123282010-08-06 19:06:56 +0200519 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200520 session_store_counters(s);
521
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100522 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100523 /* we have to unlink all watchers. We must not relink them if
524 * this session was the last one in the list.
525 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100526 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100527 LIST_INIT(&bref->users);
528 if (s->list.n != &sessions)
529 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100530 bref->ref = s->list.n;
531 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100532 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200533 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200534
535 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200536 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200537 pool_flush2(pool2_channel);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200538 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200539 pool_flush2(pool2_requri);
540 pool_flush2(pool2_capture);
541 pool_flush2(pool2_session);
542 pool_flush2(fe->req_cap_pool);
543 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200544 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200545}
546
547
548/* perform minimal intializations, report 0 in case of error, 1 if OK. */
549int init_session()
550{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100551 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200552 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
553 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554}
555
Willy Tarreau30e71012007-11-26 20:15:35 +0100556void session_process_counters(struct session *s)
557{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100558 unsigned long long bytes;
559
Willy Tarreau30e71012007-11-26 20:15:35 +0100560 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100561 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100562 s->logs.bytes_in = s->req->total;
563 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100564 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100565
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100566 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100567
Willy Tarreau827aee92011-03-10 16:55:02 +0100568 if (target_srv(&s->target))
569 target_srv(&s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200570
571 if (s->listener->counters)
572 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200573
Willy Tarreau56123282010-08-06 19:06:56 +0200574 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200575 void *ptr;
576
Willy Tarreau56123282010-08-06 19:06:56 +0200577 ptr = stktable_data_ptr(s->stkctr2_table,
578 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200579 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200580 if (ptr)
581 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200582
Willy Tarreau56123282010-08-06 19:06:56 +0200583 ptr = stktable_data_ptr(s->stkctr2_table,
584 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200585 STKTABLE_DT_BYTES_IN_RATE);
586 if (ptr)
587 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200588 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200589 }
590
Willy Tarreau56123282010-08-06 19:06:56 +0200591 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200592 void *ptr;
593
Willy Tarreau56123282010-08-06 19:06:56 +0200594 ptr = stktable_data_ptr(s->stkctr1_table,
595 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200596 STKTABLE_DT_BYTES_IN_CNT);
597 if (ptr)
598 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
599
Willy Tarreau56123282010-08-06 19:06:56 +0200600 ptr = stktable_data_ptr(s->stkctr1_table,
601 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200602 STKTABLE_DT_BYTES_IN_RATE);
603 if (ptr)
604 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200605 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200606 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100607 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100608 }
609
Willy Tarreau30e71012007-11-26 20:15:35 +0100610 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100611 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100612 s->logs.bytes_out = s->rep->total;
613 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100614 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100615
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100616 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100617
Willy Tarreau827aee92011-03-10 16:55:02 +0100618 if (target_srv(&s->target))
619 target_srv(&s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200620
621 if (s->listener->counters)
622 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200623
Willy Tarreau56123282010-08-06 19:06:56 +0200624 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200625 void *ptr;
626
Willy Tarreau56123282010-08-06 19:06:56 +0200627 ptr = stktable_data_ptr(s->stkctr2_table,
628 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200629 STKTABLE_DT_BYTES_OUT_CNT);
630 if (ptr)
631 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
632
Willy Tarreau56123282010-08-06 19:06:56 +0200633 ptr = stktable_data_ptr(s->stkctr2_table,
634 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200635 STKTABLE_DT_BYTES_OUT_RATE);
636 if (ptr)
637 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200638 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200639 }
640
Willy Tarreau56123282010-08-06 19:06:56 +0200641 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200642 void *ptr;
643
Willy Tarreau56123282010-08-06 19:06:56 +0200644 ptr = stktable_data_ptr(s->stkctr1_table,
645 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200646 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200647 if (ptr)
648 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200649
Willy Tarreau56123282010-08-06 19:06:56 +0200650 ptr = stktable_data_ptr(s->stkctr1_table,
651 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200652 STKTABLE_DT_BYTES_OUT_RATE);
653 if (ptr)
654 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200655 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200656 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100657 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100658 }
659}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100661/* This function is called with (si->state == SI_ST_CON) meaning that a
662 * connection was attempted and that the file descriptor is already allocated.
663 * We must check for establishment, error and abort. Possible output states
664 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
665 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
666 * otherwise 1.
667 */
Simon Hormandec5be42011-06-08 09:19:07 +0900668static int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100669{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200670 struct channel *req = si->ob;
671 struct channel *rep = si->ib;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100672
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100673 /* If we got an error, or if nothing happened and the connection timed
674 * out, we must give up. The CER state handler will take care of retry
675 * attempts and error reports.
676 */
677 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100678 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100679 si->state = SI_ST_CER;
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200680 fd_delete(si_fd(si));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681
Willy Tarreau8b117082012-08-06 15:06:49 +0200682 conn_data_close(&si->conn);
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200683 if (si->release)
684 si->release(si);
685
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100686 if (si->err_type)
687 return 0;
688
Willy Tarreau827aee92011-03-10 16:55:02 +0100689 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100690 if (si->flags & SI_FL_ERR)
691 si->err_type = SI_ET_CONN_ERR;
692 else
693 si->err_type = SI_ET_CONN_TO;
694 return 0;
695 }
696
697 /* OK, maybe we want to abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200698 if (unlikely((rep->flags & CF_SHUTW) ||
699 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
700 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100701 s->be->options & PR_O_ABRT_CLOSE)))) {
702 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200703 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100704 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau827aee92011-03-10 16:55:02 +0100705 si->err_loc = target_srv(&s->target);
Willy Tarreau84455332009-03-15 22:34:05 +0100706 if (s->srv_error)
707 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100708 return 1;
709 }
710
711 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200712 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100713 return 1;
714
715 /* OK, this means that a connection succeeded. The caller will be
716 * responsible for handling the transition from CON to EST.
717 */
718 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100719 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100720 si->state = SI_ST_EST;
721 si->err_type = SI_ET_NONE;
722 si->err_loc = NULL;
723 return 1;
724}
725
726/* This function is called with (si->state == SI_ST_CER) meaning that a
727 * previous connection attempt has failed and that the file descriptor
728 * has already been released. Possible causes include asynchronous error
729 * notification and time out. Possible output states are SI_ST_CLO when
730 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
731 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
732 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
733 * marked as in error state. It returns 0.
734 */
Simon Hormandec5be42011-06-08 09:19:07 +0900735static int sess_update_st_cer(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736{
737 /* we probably have to release last session from the server */
Willy Tarreau827aee92011-03-10 16:55:02 +0100738 if (target_srv(&s->target)) {
739 health_adjust(target_srv(&s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100740
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100741 if (s->flags & SN_CURR_SESS) {
742 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +0100743 target_srv(&s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100745 }
746
747 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200748 si->conn_retries--;
749 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100750 if (!si->err_type) {
751 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +0100752 si->err_loc = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100753 }
754
Willy Tarreau827aee92011-03-10 16:55:02 +0100755 if (target_srv(&s->target))
756 target_srv(&s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100757 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100758 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100759 if (may_dequeue_tasks(target_srv(&s->target), s->be))
760 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100761
762 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200763 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200764 si->ob->flags |= CF_WRITE_ERROR;
765 si->ib->flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100766
767 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100768 if (s->srv_error)
769 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100770 return 0;
771 }
772
773 /* If the "redispatch" option is set on the backend, we are allowed to
774 * retry on another server for the last retry. In order to achieve this,
775 * we must mark the session unassigned, and eventually clear the DIRECT
776 * bit to ignore any persistence cookie. We won't count a retry nor a
777 * redispatch yet, because this will depend on what server is selected.
778 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100779 if (target_srv(&s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100780 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100781 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100782 if (may_dequeue_tasks(target_srv(&s->target), s->be))
783 process_srv_queue(target_srv(&s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100784
785 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100786 si->state = SI_ST_REQ;
787 } else {
Willy Tarreau827aee92011-03-10 16:55:02 +0100788 if (target_srv(&s->target))
789 target_srv(&s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100790 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100791 si->state = SI_ST_ASS;
792 }
793
794 if (si->flags & SI_FL_ERR) {
795 /* The error was an asynchronous connection error, and we will
796 * likely have to retry connecting to the same server, most
797 * likely leading to the same result. To avoid this, we wait
798 * one second before retrying.
799 */
800
801 if (!si->err_type)
802 si->err_type = SI_ET_CONN_ERR;
803
804 si->state = SI_ST_TAR;
805 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
806 return 0;
807 }
808 return 0;
809}
810
811/*
812 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200813 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200814 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100815 */
Simon Hormandec5be42011-06-08 09:19:07 +0900816static void sess_establish(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100817{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200818 struct channel *req = si->ob;
819 struct channel *rep = si->ib;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100820
Willy Tarreau827aee92011-03-10 16:55:02 +0100821 if (target_srv(&s->target))
822 health_adjust(target_srv(&s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100823
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100824 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100825 /* if the user wants to log as soon as possible, without counting
826 * bytes from the server, then this is the right moment. */
827 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
828 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100829 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100830 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100831 }
832 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100833 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
834 /* reset hdr_idx which was already initialized by the request.
835 * right now, the http parser does it.
836 * hdr_idx_init(&s->txn.hdr_idx);
837 */
838 }
839
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200840 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200841 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200842 if (si_ctrl(si)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200843 /* real connections have timeouts */
844 req->wto = s->be->timeout.server;
845 rep->rto = s->be->timeout.server;
846 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100847 req->wex = TICK_ETERNITY;
848}
849
850/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
851 * Other input states are simply ignored.
852 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
853 * Flags must have previously been updated for timeouts and other conditions.
854 */
Simon Hormandec5be42011-06-08 09:19:07 +0900855static void sess_update_stream_int(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100856{
Willy Tarreau827aee92011-03-10 16:55:02 +0100857 struct server *srv = target_srv(&s->target);
858
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100859 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 +0100860 now_ms, __FUNCTION__,
861 s,
862 s->req, s->rep,
863 s->req->rex, s->rep->wex,
864 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100865 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 +0100866
867 if (si->state == SI_ST_ASS) {
868 /* Server assigned to connection request, we have to try to connect now */
869 int conn_err;
870
871 conn_err = connect_server(s);
Willy Tarreau827aee92011-03-10 16:55:02 +0100872 srv = target_srv(&s->target);
873
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100874 if (conn_err == SN_ERR_NONE) {
875 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100876 if (srv)
877 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100878 return;
879 }
880
881 /* We have received a synchronous error. We might have to
882 * abort, retry immediately or redispatch.
883 */
884 if (conn_err == SN_ERR_INTERNAL) {
885 if (!si->err_type) {
886 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +0100887 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100888 }
889
Willy Tarreau827aee92011-03-10 16:55:02 +0100890 if (srv)
891 srv_inc_sess_ctr(srv);
892 if (srv)
893 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100894 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100895
896 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100897 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100898 if (may_dequeue_tasks(srv, s->be))
899 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100900
901 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200902 si_shutr(si);
903 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200904 si->ob->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100905
906 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
907
908 /* no session was ever accounted for this server */
909 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100910 if (s->srv_error)
911 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100912 return;
913 }
914
915 /* We are facing a retryable error, but we don't want to run a
916 * turn-around now, as the problem is likely a source port
917 * allocation problem, so we want to retry now.
918 */
919 si->state = SI_ST_CER;
920 si->flags &= ~SI_FL_ERR;
921 sess_update_st_cer(s, si);
922 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
923 return;
924 }
925 else if (si->state == SI_ST_QUE) {
926 /* connection request was queued, check for any update */
927 if (!s->pend_pos) {
928 /* The connection is not in the queue anymore. Either
929 * we have a server connection slot available and we
930 * go directly to the assigned state, or we need to
931 * load-balance first and go to the INI state.
932 */
933 si->exp = TICK_ETERNITY;
934 if (unlikely(!(s->flags & SN_ASSIGNED)))
935 si->state = SI_ST_REQ;
936 else {
937 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
938 si->state = SI_ST_ASS;
939 }
940 return;
941 }
942
943 /* Connection request still in queue... */
944 if (si->flags & SI_FL_EXP) {
945 /* ... and timeout expired */
946 si->exp = TICK_ETERNITY;
947 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100948 if (srv)
949 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100950 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200951 si_shutr(si);
952 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200953 si->ob->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100954 if (!si->err_type)
955 si->err_type = SI_ET_QUEUE_TO;
956 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100957 if (s->srv_error)
958 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100959 return;
960 }
961
962 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200963 if ((si->ob->flags & (CF_READ_ERROR)) ||
964 ((si->ob->flags & CF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreau8e21bb92012-08-24 22:40:29 +0200965 (channel_is_empty(si->ob) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100966 /* give up */
967 si->exp = TICK_ETERNITY;
968 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200969 si_shutr(si);
970 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100971 si->err_type |= SI_ET_QUEUE_ABRT;
972 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100973 if (s->srv_error)
974 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100975 return;
976 }
977
978 /* Nothing changed */
979 return;
980 }
981 else if (si->state == SI_ST_TAR) {
982 /* Connection request might be aborted */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200983 if ((si->ob->flags & (CF_READ_ERROR)) ||
984 ((si->ob->flags & CF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreau8e21bb92012-08-24 22:40:29 +0200985 (channel_is_empty(si->ob) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100986 /* give up */
987 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200988 si_shutr(si);
989 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100990 si->err_type |= SI_ET_CONN_ABRT;
991 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100992 if (s->srv_error)
993 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100994 return;
995 }
996
997 if (!(si->flags & SI_FL_EXP))
998 return; /* still in turn-around */
999
1000 si->exp = TICK_ETERNITY;
1001
1002 /* we keep trying on the same server as long as the session is
1003 * marked "assigned".
1004 * FIXME: Should we force a redispatch attempt when the server is down ?
1005 */
1006 if (s->flags & SN_ASSIGNED)
1007 si->state = SI_ST_ASS;
1008 else
1009 si->state = SI_ST_REQ;
1010 return;
1011 }
1012}
1013
Simon Hormandec5be42011-06-08 09:19:07 +09001014/* Set correct session termination flags in case no analyser has done it. It
1015 * also counts a failed request if the server state has not reached the request
1016 * stage.
1017 */
1018static void sess_set_term_flags(struct session *s)
1019{
1020 if (!(s->flags & SN_FINST_MASK)) {
1021 if (s->si[1].state < SI_ST_REQ) {
1022
1023 s->fe->fe_counters.failed_req++;
1024 if (s->listener->counters)
1025 s->listener->counters->failed_req++;
1026
1027 s->flags |= SN_FINST_R;
1028 }
1029 else if (s->si[1].state == SI_ST_QUE)
1030 s->flags |= SN_FINST_Q;
1031 else if (s->si[1].state < SI_ST_EST)
1032 s->flags |= SN_FINST_C;
1033 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
1034 s->flags |= SN_FINST_D;
1035 else
1036 s->flags |= SN_FINST_L;
1037 }
1038}
1039
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001040/* This function initiates a server connection request on a stream interface
1041 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
1042 * indicating that a server has been assigned. It may also return SI_ST_QUE,
1043 * or SI_ST_CLO upon error.
1044 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001045static void sess_prepare_conn_req(struct session *s, struct stream_interface *si)
1046{
1047 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 +01001048 now_ms, __FUNCTION__,
1049 s,
1050 s->req, s->rep,
1051 s->req->rex, s->rep->wex,
1052 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001053 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 +01001054
1055 if (si->state != SI_ST_REQ)
1056 return;
1057
1058 /* Try to assign a server */
1059 if (srv_redispatch_connect(s) != 0) {
1060 /* We did not get a server. Either we queued the
1061 * connection request, or we encountered an error.
1062 */
1063 if (si->state == SI_ST_QUE)
1064 return;
1065
1066 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001067 si_shutr(si);
1068 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001069 si->ob->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001070 if (!si->err_type)
1071 si->err_type = SI_ET_CONN_OTHER;
1072 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001073 if (s->srv_error)
1074 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001075 return;
1076 }
1077
1078 /* The server is assigned */
1079 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1080 si->state = SI_ST_ASS;
1081}
1082
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001083/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001084 * if appropriate. The default_backend rule is also considered, then the
1085 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001086 * It returns 1 if the processing can continue on next analysers, or zero if it
1087 * either needs more data or wants to immediately abort the request.
1088 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001089static int process_switching_rules(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001090{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001091 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +01001092
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001093 req->analysers &= ~an_bit;
1094 req->analyse_exp = TICK_ETERNITY;
1095
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001096 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 +02001097 now_ms, __FUNCTION__,
1098 s,
1099 req,
1100 req->rex, req->wex,
1101 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001102 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001103 req->analysers);
1104
1105 /* now check whether we have some switching rules for this request */
1106 if (!(s->flags & SN_BE_ASSIGNED)) {
1107 struct switching_rule *rule;
1108
1109 list_for_each_entry(rule, &s->fe->switching_rules, list) {
1110 int ret;
1111
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001112 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 +02001113 ret = acl_pass(ret);
1114 if (rule->cond->pol == ACL_COND_UNLESS)
1115 ret = !ret;
1116
1117 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001118 if (!session_set_backend(s, rule->be.backend))
1119 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001120 break;
1121 }
1122 }
1123
1124 /* To ensure correct connection accounting on the backend, we
1125 * have to assign one if it was not set (eg: a listen). This
1126 * measure also takes care of correctly setting the default
1127 * backend if any.
1128 */
1129 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001130 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
1131 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001132 }
1133
Willy Tarreaufb356202010-08-03 14:02:05 +02001134 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
1135 if (s->fe == s->be) {
1136 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001137 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001138 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001139
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001140 /* 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 +01001141 * persistence rule, and report that in the session.
1142 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001143 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001144 int ret = 1;
1145
1146 if (prst_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001147 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 +01001148 ret = acl_pass(ret);
1149 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1150 ret = !ret;
1151 }
1152
1153 if (ret) {
1154 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001155 if (prst_rule->type == PERSIST_TYPE_FORCE) {
1156 s->flags |= SN_FORCE_PRST;
1157 } else {
1158 s->flags |= SN_IGNORE_PRST;
1159 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001160 break;
1161 }
1162 }
1163
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001164 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001165
1166 sw_failed:
1167 /* immediately abort this request in case of allocation failure */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001168 channel_abort(s->req);
1169 channel_abort(s->rep);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001170
1171 if (!(s->flags & SN_ERR_MASK))
1172 s->flags |= SN_ERR_RESOURCE;
1173 if (!(s->flags & SN_FINST_MASK))
1174 s->flags |= SN_FINST_R;
1175
1176 s->txn.status = 500;
1177 s->req->analysers = 0;
1178 s->req->analyse_exp = TICK_ETERNITY;
1179 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001180}
1181
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001182/* This stream analyser works on a request. It applies all use-server rules on
1183 * it then returns 1. The data must already be present in the buffer otherwise
1184 * they won't match. It always returns 1.
1185 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001186static int process_server_rules(struct session *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001187{
1188 struct proxy *px = s->be;
1189 struct server_rule *rule;
1190
1191 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1192 now_ms, __FUNCTION__,
1193 s,
1194 req,
1195 req->rex, req->wex,
1196 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001197 req->i + req->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001198 req->analysers);
1199
1200 if (!(s->flags & SN_ASSIGNED)) {
1201 list_for_each_entry(rule, &px->server_rules, list) {
1202 int ret;
1203
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001204 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 +02001205 ret = acl_pass(ret);
1206 if (rule->cond->pol == ACL_COND_UNLESS)
1207 ret = !ret;
1208
1209 if (ret) {
1210 struct server *srv = rule->srv.ptr;
1211
1212 if ((srv->state & SRV_RUNNING) ||
1213 (px->options & PR_O_PERSIST) ||
1214 (s->flags & SN_FORCE_PRST)) {
1215 s->flags |= SN_DIRECT | SN_ASSIGNED;
1216 set_target_server(&s->target, srv);
1217 break;
1218 }
1219 /* if the server is not UP, let's go on with next rules
1220 * just in case another one is suited.
1221 */
1222 }
1223 }
1224 }
1225
1226 req->analysers &= ~an_bit;
1227 req->analyse_exp = TICK_ETERNITY;
1228 return 1;
1229}
1230
Emeric Brun1d33b292010-01-04 15:47:17 +01001231/* This stream analyser works on a request. It applies all sticking rules on
1232 * it then returns 1. The data must already be present in the buffer otherwise
1233 * they won't match. It always returns 1.
1234 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001235static int process_sticking_rules(struct session *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001236{
1237 struct proxy *px = s->be;
1238 struct sticking_rule *rule;
1239
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001240 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 +01001241 now_ms, __FUNCTION__,
1242 s,
1243 req,
1244 req->rex, req->wex,
1245 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001246 req->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001247 req->analysers);
1248
1249 list_for_each_entry(rule, &px->sticking_rules, list) {
1250 int ret = 1 ;
1251 int i;
1252
1253 for (i = 0; i < s->store_count; i++) {
1254 if (rule->table.t == s->store[i].table)
1255 break;
1256 }
1257
1258 if (i != s->store_count)
1259 continue;
1260
1261 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001262 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001263 ret = acl_pass(ret);
1264 if (rule->cond->pol == ACL_COND_UNLESS)
1265 ret = !ret;
1266 }
1267
1268 if (ret) {
1269 struct stktable_key *key;
1270
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001271 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 +01001272 if (!key)
1273 continue;
1274
1275 if (rule->flags & STK_IS_MATCH) {
1276 struct stksess *ts;
1277
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001278 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001279 if (!(s->flags & SN_ASSIGNED)) {
1280 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001281 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001282
1283 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001284 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1285 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001286 if (node) {
1287 struct server *srv;
1288
1289 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001290 if ((srv->state & SRV_RUNNING) ||
1291 (px->options & PR_O_PERSIST) ||
1292 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001293 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001294 set_target_server(&s->target, srv);
Emeric Brun1d33b292010-01-04 15:47:17 +01001295 }
1296 }
1297 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001298 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001299 }
1300 }
1301 if (rule->flags & STK_IS_STORE) {
1302 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1303 struct stksess *ts;
1304
1305 ts = stksess_new(rule->table.t, key);
1306 if (ts) {
1307 s->store[s->store_count].table = rule->table.t;
1308 s->store[s->store_count++].ts = ts;
1309 }
1310 }
1311 }
1312 }
1313 }
1314
1315 req->analysers &= ~an_bit;
1316 req->analyse_exp = TICK_ETERNITY;
1317 return 1;
1318}
1319
1320/* This stream analyser works on a response. It applies all store rules on it
1321 * then returns 1. The data must already be present in the buffer otherwise
1322 * they won't match. It always returns 1.
1323 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001324static int process_store_rules(struct session *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001325{
1326 struct proxy *px = s->be;
1327 struct sticking_rule *rule;
1328 int i;
1329
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001330 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 +01001331 now_ms, __FUNCTION__,
1332 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001333 rep,
1334 rep->rex, rep->wex,
1335 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001336 rep->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001337 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001338
1339 list_for_each_entry(rule, &px->storersp_rules, list) {
1340 int ret = 1 ;
1341 int storereqidx = -1;
1342
1343 for (i = 0; i < s->store_count; i++) {
1344 if (rule->table.t == s->store[i].table) {
1345 if (!(s->store[i].flags))
1346 storereqidx = i;
1347 break;
1348 }
1349 }
1350
1351 if ((i != s->store_count) && (storereqidx == -1))
1352 continue;
1353
1354 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001355 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001356 ret = acl_pass(ret);
1357 if (rule->cond->pol == ACL_COND_UNLESS)
1358 ret = !ret;
1359 }
1360
1361 if (ret) {
1362 struct stktable_key *key;
1363
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001364 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 +01001365 if (!key)
1366 continue;
1367
1368 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001369 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001370 s->store[storereqidx].flags = 1;
1371 }
1372 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1373 struct stksess *ts;
1374
1375 ts = stksess_new(rule->table.t, key);
1376 if (ts) {
1377 s->store[s->store_count].table = rule->table.t;
1378 s->store[s->store_count].flags = 1;
1379 s->store[s->store_count++].ts = ts;
1380 }
1381 }
1382 }
1383 }
1384
1385 /* process store request and store response */
1386 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001387 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001388 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001389
Simon Hormanfa461682011-06-25 09:39:49 +09001390 if (target_srv(&s->target) && target_srv(&s->target)->state & SRV_NON_STICK) {
1391 stksess_free(s->store[i].table, s->store[i].ts);
1392 s->store[i].ts = NULL;
1393 continue;
1394 }
1395
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001396 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1397 if (ts) {
1398 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001399 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001400 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001401 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001402 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001403 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001404
1405 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001406 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau827aee92011-03-10 16:55:02 +01001407 stktable_data_cast(ptr, server_id) = target_srv(&s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001408 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001409 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001410
1411 rep->analysers &= ~an_bit;
1412 rep->analyse_exp = TICK_ETERNITY;
1413 return 1;
1414}
1415
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001416/* This macro is very specific to the function below. See the comments in
1417 * process_session() below to understand the logic and the tests.
1418 */
1419#define UPDATE_ANALYSERS(real, list, back, flag) { \
1420 list = (((list) & ~(flag)) | ~(back)) & (real); \
1421 back = real; \
1422 if (!(list)) \
1423 break; \
1424 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1425 continue; \
1426}
1427
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001428/* Processes the client, server, request and response jobs of a session task,
1429 * then puts it back to the wait queue in a clean state, or cleans up its
1430 * resources if it must be deleted. Returns in <next> the date the task wants
1431 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1432 * nothing too many times, the request and response buffers flags are monitored
1433 * and each function is called only if at least another function has changed at
1434 * least one flag it is interested in.
1435 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001436struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001437{
Willy Tarreau827aee92011-03-10 16:55:02 +01001438 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001439 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001440 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001441 unsigned int rq_prod_last, rq_cons_last;
1442 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001443 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001444
1445 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1446 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1447
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001448 /* this data may be no longer valid, clear it */
1449 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1450
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001451 /* This flag must explicitly be set every time */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001452 s->req->flags &= ~CF_READ_NOEXP;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001453
1454 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001455 rqf_last = s->req->flags & ~CF_MASK_ANALYSER;
1456 rpf_last = s->rep->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001457
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001458 /* we don't want the stream interface functions to recursively wake us up */
1459 if (s->req->prod->owner == t)
1460 s->req->prod->flags |= SI_FL_DONT_WAKE;
1461 if (s->req->cons->owner == t)
1462 s->req->cons->flags |= SI_FL_DONT_WAKE;
1463
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001464 /* 1a: Check for low level timeouts if needed. We just set a flag on
1465 * stream interfaces when their timeouts have expired.
1466 */
1467 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1468 stream_int_check_timeouts(&s->si[0]);
1469 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001470
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001471 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001472 * for future reads or writes. Note: this will also concern upper layers
1473 * but we do not touch any other flag. We must be careful and correctly
1474 * detect state changes when calling them.
1475 */
1476
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001477 channel_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001478
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001479 if (unlikely((s->req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
Willy Tarreau14641402009-12-29 14:49:56 +01001480 s->req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001481 si_shutw(s->req->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001482 }
1483
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001484 if (unlikely((s->req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001485 if (s->req->prod->flags & SI_FL_NOHALF)
1486 s->req->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001487 si_shutr(s->req->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001488 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001489
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001490 channel_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001491
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001492 if (unlikely((s->rep->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
Willy Tarreau14641402009-12-29 14:49:56 +01001493 s->rep->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001494 si_shutw(s->rep->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001495 }
1496
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001497 if (unlikely((s->rep->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001498 if (s->rep->prod->flags & SI_FL_NOHALF)
1499 s->rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001500 si_shutr(s->rep->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001501 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001502 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001503
1504 /* 1b: check for low-level errors reported at the stream interface.
1505 * First we check if it's a retryable error (in which case we don't
1506 * want to tell the buffer). Otherwise we report the error one level
1507 * upper by setting flags into the buffers. Note that the side towards
1508 * the client cannot have connect (hence retryable) errors. Also, the
1509 * connection setup code must be able to deal with any type of abort.
1510 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001511 srv = target_srv(&s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001512 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1513 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001514 si_shutr(&s->si[0]);
1515 si_shutw(&s->si[0]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001516 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001517 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001518 s->be->be_counters.cli_aborts++;
1519 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001520 if (srv)
1521 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001522 if (!(s->flags & SN_ERR_MASK))
1523 s->flags |= SN_ERR_CLICL;
1524 if (!(s->flags & SN_FINST_MASK))
1525 s->flags |= SN_FINST_D;
1526 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001527 }
1528 }
1529
1530 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1531 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001532 si_shutr(&s->si[1]);
1533 si_shutw(&s->si[1]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001534 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001535 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001536 if (srv)
1537 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001538 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001539 s->be->be_counters.srv_aborts++;
1540 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001541 if (srv)
1542 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001543 if (!(s->flags & SN_ERR_MASK))
1544 s->flags |= SN_ERR_SRVCL;
1545 if (!(s->flags & SN_FINST_MASK))
1546 s->flags |= SN_FINST_D;
1547 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001548 }
1549 /* note: maybe we should process connection errors here ? */
1550 }
1551
1552 if (s->si[1].state == SI_ST_CON) {
1553 /* we were trying to establish a connection on the server side,
1554 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1555 */
1556 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1557 sess_update_st_cer(s, &s->si[1]);
1558 else if (s->si[1].state == SI_ST_EST)
1559 sess_establish(s, &s->si[1]);
1560
1561 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1562 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1563 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1564 */
1565 }
1566
Willy Tarreau815a9b22010-07-27 17:15:12 +02001567 rq_prod_last = s->si[0].state;
1568 rq_cons_last = s->si[1].state;
1569 rp_cons_last = s->si[0].state;
1570 rp_prod_last = s->si[1].state;
1571
1572 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001573 /* Check for connection closure */
1574
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001575 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001576 "[%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 +01001577 now_ms, __FUNCTION__, __LINE__,
1578 t,
1579 s, s->flags,
1580 s->req, s->rep,
1581 s->req->rex, s->rep->wex,
1582 s->req->flags, s->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001583 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 +01001584 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001585 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001586
1587 /* nothing special to be done on client side */
1588 if (unlikely(s->req->prod->state == SI_ST_DIS))
1589 s->req->prod->state = SI_ST_CLO;
1590
1591 /* When a server-side connection is released, we have to count it and
1592 * check for pending connections on this server.
1593 */
1594 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1595 s->req->cons->state = SI_ST_CLO;
Willy Tarreau827aee92011-03-10 16:55:02 +01001596 srv = target_srv(&s->target);
1597 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001598 if (s->flags & SN_CURR_SESS) {
1599 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001600 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001601 }
1602 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001603 if (may_dequeue_tasks(srv, s->be))
1604 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001605 }
1606 }
1607
1608 /*
1609 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1610 * at this point.
1611 */
1612
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001613 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001614 /* Analyse request */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001615 if (((s->req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1616 ((s->req->flags ^ rqf_last) & CF_MASK_STATIC) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001617 s->si[0].state != rq_prod_last ||
1618 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001619 unsigned int flags = s->req->flags;
1620
1621 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001622 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001623 unsigned int ana_list;
1624 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001625
Willy Tarreau90deb182010-01-07 00:20:41 +01001626 /* it's up to the analysers to stop new connections,
1627 * disable reading or closing. Note: if an analyser
1628 * disables any of these bits, it is responsible for
1629 * enabling them again when it disables itself, so
1630 * that other analysers are called in similar conditions.
1631 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001632 channel_auto_read(s->req);
1633 channel_auto_connect(s->req);
1634 channel_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001635
1636 /* We will call all analysers for which a bit is set in
1637 * s->req->analysers, following the bit order from LSB
1638 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001639 * the list when not needed. Any analyser may return 0
1640 * to break out of the loop, either because of missing
1641 * data to take a decision, or because it decides to
1642 * kill the session. We loop at least once through each
1643 * analyser, and we may loop again if other analysers
1644 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001645 *
1646 * We build a list of analysers to run. We evaluate all
1647 * of these analysers in the order of the lower bit to
1648 * the higher bit. This ordering is very important.
1649 * An analyser will often add/remove other analysers,
1650 * including itself. Any changes to itself have no effect
1651 * on the loop. If it removes any other analysers, we
1652 * want those analysers not to be called anymore during
1653 * this loop. If it adds an analyser that is located
1654 * after itself, we want it to be scheduled for being
1655 * processed during the loop. If it adds an analyser
1656 * which is located before it, we want it to switch to
1657 * it immediately, even if it has already been called
1658 * once but removed since.
1659 *
1660 * In order to achieve this, we compare the analyser
1661 * list after the call with a copy of it before the
1662 * call. The work list is fed with analyser bits that
1663 * appeared during the call. Then we compare previous
1664 * work list with the new one, and check the bits that
1665 * appeared. If the lowest of these bits is lower than
1666 * the current bit, it means we have enabled a previous
1667 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001668 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001669
1670 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001671 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001672 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001673
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001674 if (ana_list & AN_REQ_DECODE_PROXY) {
1675 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1676 break;
1677 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1678 }
1679
Willy Tarreaufb356202010-08-03 14:02:05 +02001680 if (ana_list & AN_REQ_INSPECT_FE) {
1681 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001682 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001683 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001684 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001685
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001686 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001687 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001688 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001689 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001690 }
1691
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001692 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001693 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1694 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001695 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001696 }
1697
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001698 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001699 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1700 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001701 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001702 }
1703
Willy Tarreaufb356202010-08-03 14:02:05 +02001704 if (ana_list & AN_REQ_INSPECT_BE) {
1705 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1706 break;
1707 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1708 }
1709
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001710 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001711 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1712 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001713 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001714 }
1715
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001716 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001717 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001718 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001719 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001720 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001721
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001722 if (ana_list & AN_REQ_SRV_RULES) {
1723 if (!process_server_rules(s, s->req, AN_REQ_SRV_RULES))
1724 break;
1725 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
1726 }
1727
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001728 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001729 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1730 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001731 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001732 }
1733
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001734 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001735 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001736 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001737 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001738 }
Emeric Brun647caf12009-06-30 17:57:00 +02001739
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001740 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001741 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1742 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001743 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001744 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001745
Emeric Brun1d33b292010-01-04 15:47:17 +01001746 if (ana_list & AN_REQ_STICKING_RULES) {
1747 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1748 break;
1749 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1750 }
1751
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001752 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001753 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1754 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001755 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001756 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001757 break;
1758 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001759 }
Willy Tarreau84455332009-03-15 22:34:05 +01001760
Willy Tarreau815a9b22010-07-27 17:15:12 +02001761 rq_prod_last = s->si[0].state;
1762 rq_cons_last = s->si[1].state;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001763 s->req->flags &= ~CF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001764 rqf_last = s->req->flags;
1765
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001766 if ((s->req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001767 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001768 }
1769
Willy Tarreau576507f2010-01-07 00:09:04 +01001770 /* we'll monitor the request analysers while parsing the response,
1771 * because some response analysers may indirectly enable new request
1772 * analysers (eg: HTTP keep-alive).
1773 */
1774 req_ana_back = s->req->analysers;
1775
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001776 resync_response:
1777 /* Analyse response */
1778
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001779 if (unlikely(s->rep->flags & CF_HIJACK)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001780 /* In inject mode, we wake up everytime something has
1781 * happened on the write side of the buffer.
1782 */
1783 unsigned int flags = s->rep->flags;
1784
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001785 if ((s->rep->flags & (CF_WRITE_PARTIAL|CF_WRITE_ERROR|CF_SHUTW)) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02001786 !channel_full(s->rep)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001787 s->rep->hijacker(s, s->rep);
1788 }
1789
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001790 if ((s->rep->flags ^ flags) & CF_MASK_STATIC) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001791 rpf_last = s->rep->flags;
1792 goto resync_response;
1793 }
1794 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001795 else if (((s->rep->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1796 (s->rep->flags ^ rpf_last) & CF_MASK_STATIC ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001797 s->si[0].state != rp_cons_last ||
1798 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001799 unsigned int flags = s->rep->flags;
1800
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001801 if ((s->rep->flags & CF_MASK_ANALYSER) &&
Willy Tarreau0499e352010-12-17 07:13:42 +01001802 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1803 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1804 * for some free space in the response buffer, so we might need to call
1805 * it when something changes in the response buffer, but still we pass
1806 * it the request buffer. Note that the SI state might very well still
1807 * be zero due to us returning a flow of redirects!
1808 */
1809 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001810 s->req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001811 }
1812
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001813 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001814 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001815 unsigned int ana_list;
1816 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001817
Willy Tarreau90deb182010-01-07 00:20:41 +01001818 /* it's up to the analysers to stop disable reading or
1819 * closing. Note: if an analyser disables any of these
1820 * bits, it is responsible for enabling them again when
1821 * it disables itself, so that other analysers are called
1822 * in similar conditions.
1823 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001824 channel_auto_read(s->rep);
1825 channel_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001826
1827 /* We will call all analysers for which a bit is set in
1828 * s->rep->analysers, following the bit order from LSB
1829 * to MSB. The analysers must remove themselves from
1830 * the list when not needed. Any analyser may return 0
1831 * to break out of the loop, either because of missing
1832 * data to take a decision, or because it decides to
1833 * kill the session. We loop at least once through each
1834 * analyser, and we may loop again if other analysers
1835 * are added in the middle.
1836 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001837
1838 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001839 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001840 /* Warning! ensure that analysers are always placed in ascending order! */
1841
Emeric Brun97679e72010-09-23 17:56:44 +02001842 if (ana_list & AN_RES_INSPECT) {
1843 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1844 break;
1845 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1846 }
1847
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001848 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001849 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1850 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001851 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001852 }
1853
Emeric Brun1d33b292010-01-04 15:47:17 +01001854 if (ana_list & AN_RES_STORE_RULES) {
1855 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1856 break;
1857 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1858 }
1859
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001860 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001861 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1862 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001863 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001864 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001865
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001866 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001867 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1868 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001869 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001870 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001871 break;
1872 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001873 }
1874
Willy Tarreau815a9b22010-07-27 17:15:12 +02001875 rp_cons_last = s->si[0].state;
1876 rp_prod_last = s->si[1].state;
1877 rpf_last = s->rep->flags;
1878
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001879 if ((s->rep->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001880 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001881 }
1882
Willy Tarreau576507f2010-01-07 00:09:04 +01001883 /* maybe someone has added some request analysers, so we must check and loop */
1884 if (s->req->analysers & ~req_ana_back)
1885 goto resync_request;
1886
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001887 if ((s->req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001888 goto resync_request;
1889
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001890 /* FIXME: here we should call protocol handlers which rely on
1891 * both buffers.
1892 */
1893
1894
1895 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001896 * Now we propagate unhandled errors to the session. Normally
1897 * we're just in a data phase here since it means we have not
1898 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001899 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001900 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001901 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001902 if (s->req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001903 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001904 s->req->analysers = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001905 if (s->req->flags & CF_READ_ERROR) {
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_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001911 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001912 else if (s->req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001913 s->be->be_counters.cli_aborts++;
1914 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001915 if (srv)
1916 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001917 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001918 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001919 else if (s->req->flags & CF_WRITE_ERROR) {
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_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001925 }
1926 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001927 s->be->be_counters.srv_aborts++;
1928 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001929 if (srv)
1930 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001931 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001932 }
Willy Tarreau84455332009-03-15 22:34:05 +01001933 sess_set_term_flags(s);
1934 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001935 else if (s->rep->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001936 /* Report it if the server got an error or a read timeout expired */
1937 s->rep->analysers = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001938 if (s->rep->flags & CF_READ_ERROR) {
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_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001944 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001945 else if (s->rep->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001946 s->be->be_counters.srv_aborts++;
1947 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001948 if (srv)
1949 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001950 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001951 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001952 else if (s->rep->flags & CF_WRITE_ERROR) {
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 Tarreau3deb3d02009-06-21 22:43:05 +02001957 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001958 }
1959 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001960 s->be->be_counters.cli_aborts++;
1961 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001962 if (srv)
1963 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001964 s->flags |= SN_ERR_CLITO;
1965 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001966 sess_set_term_flags(s);
1967 }
Willy Tarreau84455332009-03-15 22:34:05 +01001968 }
1969
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001970 /*
1971 * Here we take care of forwarding unhandled data. This also includes
1972 * connection establishments and shutdown requests.
1973 */
1974
1975
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001976 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001977 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001978 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001979 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001980 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001981 if (!s->req->analysers &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001982 !(s->req->flags & (CF_HIJACK|CF_SHUTW|CF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001983 (s->req->prod->state >= SI_ST_EST) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001984 (s->req->to_forward != CHN_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001985 /* This buffer is freewheeling, there's no analyser nor hijacker
1986 * attached to it. If any data are left in, we'll permit them to
1987 * move.
1988 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001989 channel_auto_read(s->req);
1990 channel_auto_connect(s->req);
1991 channel_auto_close(s->req);
Willy Tarreaua75bcef2012-08-24 22:56:11 +02001992 buffer_flush(&s->req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001993
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001994 /* We'll let data flow between the producer (if still connected)
1995 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001996 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001997 if (!(s->req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001998 channel_forward(s->req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001999 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002000
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002001 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002002 if (!(s->req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002003 s->req->to_forward &&
2004 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau93b0f4f2012-08-30 21:23:53 +02002005 (s->si[0].conn.data && s->si[0].conn.data->rcv_pipe && s->si[0].conn.data->snd_pipe) &&
2006 (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 +01002007 (pipes_used < global.maxpipes) &&
2008 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2009 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002010 (s->req->flags & CF_STREAMER_FAST)))) {
2011 s->req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002012 }
2013
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002014 /* reflect what the L7 analysers have seen last */
2015 rqf_last = s->req->flags;
2016
2017 /*
2018 * Now forward all shutdown requests between both sides of the buffer
2019 */
2020
Willy Tarreau520d95e2009-09-19 21:04:57 +02002021 /* first, let's check if the request buffer needs to shutdown(write), which may
2022 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01002023 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002024 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002025 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_HIJACK|CF_AUTO_CLOSE|CF_SHUTR)) ==
2026 (CF_AUTO_CLOSE|CF_SHUTR)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002027 channel_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002028
2029 /* shutdown(write) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002030 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
Willy Tarreau8e21bb92012-08-24 22:40:29 +02002031 channel_is_empty(s->req)))
Willy Tarreau73b013b2012-05-21 16:31:45 +02002032 si_shutw(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002033
2034 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002035 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002036 !s->req->analysers))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002037 channel_shutr_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002038
2039 /* shutdown(read) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002040 if (unlikely((s->req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002041 if (s->req->prod->flags & SI_FL_NOHALF)
2042 s->req->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02002043 si_shutr(s->req->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002044 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002045
Willy Tarreau520d95e2009-09-19 21:04:57 +02002046 /* it's possible that an upper layer has requested a connection setup or abort.
2047 * There are 2 situations where we decide to establish a new connection :
2048 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002049 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002050 */
2051 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002052 if (!(s->req->flags & CF_SHUTW)) {
2053 if ((s->req->flags & CF_AUTO_CONNECT) || !channel_is_empty(s->req)) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01002054 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02002055 * switch to the connected state, otherwise we perform a connection
2056 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002057 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02002058 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02002059 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau3cefd522012-08-30 15:49:18 +02002060 if (unlikely(s->req->cons->conn.target.type == TARG_TYPE_APPLET &&
Willy Tarreau73b013b2012-05-21 16:31:45 +02002061 !(si_ctrl(s->req->cons) && si_ctrl(s->req->cons)->connect))) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02002062 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002063 s->rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau85e7d002010-05-31 11:57:51 +02002064 s->req->wex = TICK_ETERNITY;
2065 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02002066 }
Willy Tarreau73201222009-08-16 18:27:24 +02002067 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002068 else {
Willy Tarreau92795622009-03-06 12:51:23 +01002069 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002070 channel_shutw_now(s->req); /* fix buffer flags upon abort */
2071 channel_shutr_now(s->rep);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002072 }
Willy Tarreau92795622009-03-06 12:51:23 +01002073 }
2074
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002075
2076 /* we may have a pending connection request, or a connection waiting
2077 * for completion.
2078 */
2079 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
2080 do {
2081 /* nb: step 1 might switch from QUE to ASS, but we first want
2082 * to give a chance to step 2 to perform a redirect if needed.
2083 */
2084 if (s->si[1].state != SI_ST_REQ)
2085 sess_update_stream_int(s, &s->si[1]);
2086 if (s->si[1].state == SI_ST_REQ)
2087 sess_prepare_conn_req(s, &s->si[1]);
2088
Willy Tarreau827aee92011-03-10 16:55:02 +01002089 srv = target_srv(&s->target);
2090 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002091 perform_http_redirect(s, &s->si[1]);
2092 } while (s->si[1].state == SI_ST_ASS);
Mark Lamourinec2247f02012-01-04 13:02:01 -05002093
2094 /* Now we can add the server name to a header (if requested) */
2095 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Stathis Voukelatos09a030a2012-01-09 14:27:13 +01002096 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau45c0d982012-03-09 12:11:57 +01002097 (s->be->mode == PR_MODE_HTTP) &&
2098 (s->be->server_id_hdr_name != NULL)) {
2099 http_send_name_header(&s->txn, s->be, target_srv(&s->target)->id);
Mark Lamourinec2247f02012-01-04 13:02:01 -05002100 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002101 }
2102
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002103 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002104 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002105 goto resync_stream_interface;
2106
Willy Tarreau815a9b22010-07-27 17:15:12 +02002107 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002108 if ((s->req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002109 goto resync_request;
2110
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002111 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002112
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002113 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002114 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002115 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002116 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002117 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002118 if (!s->rep->analysers &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002119 !(s->rep->flags & (CF_HIJACK|CF_SHUTW|CF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02002120 (s->rep->prod->state >= SI_ST_EST) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002121 (s->rep->to_forward != CHN_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002122 /* This buffer is freewheeling, there's no analyser nor hijacker
2123 * attached to it. If any data are left in, we'll permit them to
2124 * move.
2125 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002126 channel_auto_read(s->rep);
2127 channel_auto_close(s->rep);
Willy Tarreaua75bcef2012-08-24 22:56:11 +02002128 buffer_flush(&s->rep->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002129
2130 /* We'll let data flow between the producer (if still connected)
2131 * to the consumer.
2132 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002133 if (!(s->rep->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002134 channel_forward(s->rep, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002135
2136 /* if we have no analyser anymore in any direction and have a
2137 * tunnel timeout set, use it now.
2138 */
2139 if (!s->req->analysers && s->be->timeout.tunnel) {
2140 s->req->rto = s->req->wto = s->rep->rto = s->rep->wto =
2141 s->be->timeout.tunnel;
2142 s->req->rex = s->req->wex = s->rep->rex = s->rep->wex =
2143 tick_add(now_ms, s->be->timeout.tunnel);
2144 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002145 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002146
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002147 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002148 if (!(s->rep->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002149 s->rep->to_forward &&
2150 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau93b0f4f2012-08-30 21:23:53 +02002151 (s->si[0].conn.data && s->si[0].conn.data->rcv_pipe && s->si[0].conn.data->snd_pipe) &&
2152 (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 +01002153 (pipes_used < global.maxpipes) &&
2154 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2155 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002156 (s->rep->flags & CF_STREAMER_FAST)))) {
2157 s->rep->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002158 }
2159
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002160 /* reflect what the L7 analysers have seen last */
2161 rpf_last = s->rep->flags;
2162
2163 /*
2164 * Now forward all shutdown requests between both sides of the buffer
2165 */
2166
2167 /*
2168 * FIXME: this is probably where we should produce error responses.
2169 */
2170
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002171 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002172 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_HIJACK|CF_AUTO_CLOSE|CF_SHUTR)) ==
2173 (CF_AUTO_CLOSE|CF_SHUTR)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002174 channel_shutw_now(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002175
2176 /* shutdown(write) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002177 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
Willy Tarreau8e21bb92012-08-24 22:40:29 +02002178 channel_is_empty(s->rep)))
Willy Tarreau73b013b2012-05-21 16:31:45 +02002179 si_shutw(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002180
2181 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002182 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002183 !s->rep->analysers)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002184 channel_shutr_now(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002185
2186 /* shutdown(read) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002187 if (unlikely((s->rep->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002188 if (s->rep->prod->flags & SI_FL_NOHALF)
2189 s->rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02002190 si_shutr(s->rep->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002191 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002192
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002193 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002194 goto resync_stream_interface;
2195
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002196 if (s->req->flags != rqf_last)
2197 goto resync_request;
2198
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002199 if ((s->rep->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002200 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002201
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002202 /* we're interested in getting wakeups again */
2203 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
2204 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
2205
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002206 /* This is needed only when debugging is enabled, to indicate
2207 * client-side or server-side close. Please note that in the unlikely
2208 * event where both sides would close at once, the sequence is reported
2209 * on the server side first.
2210 */
2211 if (unlikely((global.mode & MODE_DEBUG) &&
2212 (!(global.mode & MODE_QUIET) ||
2213 (global.mode & MODE_VERBOSE)))) {
2214 int len;
2215
2216 if (s->si[1].state == SI_ST_CLO &&
2217 s->si[1].prev_state == SI_ST_EST) {
2218 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
2219 s->uniq_id, s->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002220 (unsigned short)si_fd(&s->si[0]),
2221 (unsigned short)si_fd(&s->si[1]));
Willy Tarreau21337822012-04-29 14:11:38 +02002222 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002223 }
2224
2225 if (s->si[0].state == SI_ST_CLO &&
2226 s->si[0].prev_state == SI_ST_EST) {
2227 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
2228 s->uniq_id, s->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002229 (unsigned short)si_fd(&s->si[0]),
2230 (unsigned short)si_fd(&s->si[1]));
Willy Tarreau21337822012-04-29 14:11:38 +02002231 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002232 }
2233 }
2234
2235 if (likely((s->rep->cons->state != SI_ST_CLO) ||
2236 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
2237
2238 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
2239 session_process_counters(s);
2240
Willy Tarreau3cefd522012-08-30 15:49:18 +02002241 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->conn.target.type != TARG_TYPE_APPLET)
Willy Tarreau73b013b2012-05-21 16:31:45 +02002242 si_update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002243
Willy Tarreau3cefd522012-08-30 15:49:18 +02002244 if (s->req->cons->state == SI_ST_EST && s->req->cons->conn.target.type != TARG_TYPE_APPLET)
Willy Tarreau73b013b2012-05-21 16:31:45 +02002245 si_update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002246
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002247 s->req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2248 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 +01002249 s->si[0].prev_state = s->si[0].state;
2250 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002251 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2252 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002253
2254 /* Trick: if a request is being waiting for the server to respond,
2255 * and if we know the server can timeout, we don't want the timeout
2256 * to expire on the client side first, but we're still interested
2257 * in passing data from the client to the server (eg: POST). Thus,
2258 * we can cancel the client's request timeout if the server's
2259 * request timeout is set and the server has not yet sent a response.
2260 */
2261
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002262 if ((s->rep->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01002263 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002264 s->req->flags |= CF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002265 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01002266 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002267
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002268 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002269 * Note that this one may wake the task up again.
2270 */
Willy Tarreau3cefd522012-08-30 15:49:18 +02002271 if (s->req->cons->conn.target.type == TARG_TYPE_APPLET ||
2272 s->rep->cons->conn.target.type == TARG_TYPE_APPLET) {
2273 if (s->req->cons->conn.target.type == TARG_TYPE_APPLET)
2274 s->req->cons->conn.target.ptr.a->fct(s->req->cons);
2275 if (s->rep->cons->conn.target.type == TARG_TYPE_APPLET)
2276 s->rep->cons->conn.target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002277 if (task_in_rq(t)) {
2278 /* If we woke up, we don't want to requeue the
2279 * task to the wait queue, but rather requeue
2280 * it into the runqueue ASAP.
2281 */
2282 t->expire = TICK_ETERNITY;
2283 return t;
2284 }
2285 }
2286
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002287 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2288 tick_first(s->rep->rex, s->rep->wex));
2289 if (s->req->analysers)
2290 t->expire = tick_first(t->expire, s->req->analyse_exp);
2291
2292 if (s->si[0].exp)
2293 t->expire = tick_first(t->expire, s->si[0].exp);
2294
2295 if (s->si[1].exp)
2296 t->expire = tick_first(t->expire, s->si[1].exp);
2297
2298#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002299 fprintf(stderr,
2300 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2301 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2302 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2303 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 +01002304#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002305
2306#ifdef DEBUG_DEV
2307 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002308 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002309 ABORT_NOW();
2310#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002311 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002312 }
2313
2314 s->fe->feconn--;
2315 if (s->flags & SN_BE_ASSIGNED)
2316 s->be->beconn--;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002317 if (!(s->listener->options & LI_O_UNLIMITED))
2318 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002319 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002320 s->listener->nbconn--;
Willy Tarreau62793712011-07-24 19:23:38 +02002321 if (s->listener->state == LI_FULL)
2322 resume_listener(s->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002323
Willy Tarreau08ceb102011-07-24 22:58:00 +02002324 /* Dequeues all of the listeners waiting for a resource */
2325 if (!LIST_ISEMPTY(&global_listener_queue))
2326 dequeue_all_listeners(&global_listener_queue);
2327
Willy Tarreaub32907b2011-07-25 08:37:44 +02002328 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
2329 (!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 +02002330 dequeue_all_listeners(&s->fe->listener_queue);
2331
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002332 if (unlikely((global.mode & MODE_DEBUG) &&
2333 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2334 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002335 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002336 s->uniq_id, s->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002337 (unsigned short)si_fd(s->req->prod), (unsigned short)si_fd(s->req->cons));
Willy Tarreau21337822012-04-29 14:11:38 +02002338 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002339 }
2340
2341 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2342 session_process_counters(s);
2343
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002344 if (s->txn.status) {
2345 int n;
2346
2347 n = s->txn.status / 100;
2348 if (n < 1 || n > 5)
2349 n = 0;
2350
2351 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002352 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002353
Willy Tarreau24657792010-02-26 10:30:28 +01002354 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002355 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002356 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002357 }
2358
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002359 /* let's do a final log if we need it */
2360 if (s->logs.logwait &&
2361 !(s->flags & SN_MONITOR) &&
2362 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002363 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002364 }
2365
2366 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002367 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002368 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002369 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002370 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002371}
2372
Willy Tarreau7c669d72008-06-20 15:04:11 +02002373/*
2374 * This function adjusts sess->srv_conn and maintains the previous and new
2375 * server's served session counts. Setting newsrv to NULL is enough to release
2376 * current connection slot. This function also notifies any LB algo which might
2377 * expect to be informed about any change in the number of active sessions on a
2378 * server.
2379 */
2380void sess_change_server(struct session *sess, struct server *newsrv)
2381{
2382 if (sess->srv_conn == newsrv)
2383 return;
2384
2385 if (sess->srv_conn) {
2386 sess->srv_conn->served--;
2387 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2388 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002389 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002390 }
2391
2392 if (newsrv) {
2393 newsrv->served++;
2394 if (newsrv->proxy->lbprm.server_take_conn)
2395 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002396 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002397 }
2398}
2399
Willy Tarreau84455332009-03-15 22:34:05 +01002400/* Handle server-side errors for default protocols. It is called whenever a a
2401 * connection setup is aborted or a request is aborted in queue. It sets the
2402 * session termination flags so that the caller does not have to worry about
2403 * them. It's installed as ->srv_error for the server-side stream_interface.
2404 */
2405void default_srv_error(struct session *s, struct stream_interface *si)
2406{
2407 int err_type = si->err_type;
2408 int err = 0, fin = 0;
2409
2410 if (err_type & SI_ET_QUEUE_ABRT) {
2411 err = SN_ERR_CLICL;
2412 fin = SN_FINST_Q;
2413 }
2414 else if (err_type & SI_ET_CONN_ABRT) {
2415 err = SN_ERR_CLICL;
2416 fin = SN_FINST_C;
2417 }
2418 else if (err_type & SI_ET_QUEUE_TO) {
2419 err = SN_ERR_SRVTO;
2420 fin = SN_FINST_Q;
2421 }
2422 else if (err_type & SI_ET_QUEUE_ERR) {
2423 err = SN_ERR_SRVCL;
2424 fin = SN_FINST_Q;
2425 }
2426 else if (err_type & SI_ET_CONN_TO) {
2427 err = SN_ERR_SRVTO;
2428 fin = SN_FINST_C;
2429 }
2430 else if (err_type & SI_ET_CONN_ERR) {
2431 err = SN_ERR_SRVCL;
2432 fin = SN_FINST_C;
2433 }
2434 else /* SI_ET_CONN_OTHER and others */ {
2435 err = SN_ERR_INTERNAL;
2436 fin = SN_FINST_C;
2437 }
2438
2439 if (!(s->flags & SN_ERR_MASK))
2440 s->flags |= err;
2441 if (!(s->flags & SN_FINST_MASK))
2442 s->flags |= fin;
2443}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002444
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002445/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
2446void session_shutdown(struct session *session, int why)
2447{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002448 if (session->req->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002449 return;
2450
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002451 channel_shutw_now(session->req);
2452 channel_shutr_now(session->rep);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002453 session->task->nice = 1024;
2454 if (!(session->flags & SN_ERR_MASK))
2455 session->flags |= why;
2456 task_wakeup(session->task, TASK_WOKEN_OTHER);
2457}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002458
Willy Tarreau8b22a712010-06-18 17:46:06 +02002459/************************************************************************/
2460/* All supported ACL keywords must be declared here. */
2461/************************************************************************/
2462
Willy Tarreaua5e37562011-12-16 17:06:15 +01002463/* set temp integer to the General Purpose Counter 0 value in the stksess entry <ts> */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002464static int
Willy Tarreau37406352012-04-23 16:16:37 +02002465acl_fetch_get_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002466{
Willy Tarreau37406352012-04-23 16:16:37 +02002467 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002468 smp->type = SMP_T_UINT;
2469 smp->data.uint = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002470 if (ts != NULL) {
2471 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2472 if (!ptr)
2473 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002474 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002475 }
2476 return 1;
2477}
2478
Willy Tarreaua5e37562011-12-16 17:06:15 +01002479/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002480 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002481 */
2482static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002483acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002484 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002485{
Willy Tarreau56123282010-08-06 19:06:56 +02002486 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002487 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002488 return acl_fetch_get_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002489}
2490
Willy Tarreaua5e37562011-12-16 17:06:15 +01002491/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002492 * backend counters.
2493 */
2494static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002495acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002496 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002497{
Willy Tarreau56123282010-08-06 19:06:56 +02002498 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002499 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002500 return acl_fetch_get_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002501}
2502
Willy Tarreaua5e37562011-12-16 17:06:15 +01002503/* set temp integer to the General Purpose Counter 0 value from the session's source
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002504 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002505 * Accepts exactly 1 argument of type table.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002506 */
2507static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002508acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002509 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002510{
2511 struct stktable_key *key;
2512
Willy Tarreau64ee4912012-08-30 22:59:48 +02002513 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002514 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002515 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002516
Willy Tarreau24e32d82012-04-23 23:55:44 +02002517 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002518 return acl_fetch_get_gpc0(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002519}
2520
2521/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002522 * return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002523 */
2524static int
Willy Tarreau37406352012-04-23 16:16:37 +02002525acl_fetch_inc_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002526{
Willy Tarreau37406352012-04-23 16:16:37 +02002527 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002528 smp->type = SMP_T_UINT;
2529 smp->data.uint = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002530 if (ts != NULL) {
2531 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2532 if (!ptr)
2533 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002534 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002535 }
2536 return 1;
2537}
2538
2539/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002540 * frontend counters and return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002541 */
2542static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002543acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002544 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002545{
Willy Tarreau56123282010-08-06 19:06:56 +02002546 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002547 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002548 return acl_fetch_inc_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002549}
2550
2551/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002552 * backend counters and return it into temp integer.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002553 */
2554static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002555acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002556 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002557{
Willy Tarreau56123282010-08-06 19:06:56 +02002558 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002559 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002560 return acl_fetch_inc_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002561}
2562
2563/* Increment the General Purpose Counter 0 value from the session's source
Willy Tarreaua5e37562011-12-16 17:06:15 +01002564 * address in the table pointed to by expr, and return it into temp integer.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002565 * Accepts exactly 1 argument of type table.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002566 */
2567static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002568acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002569 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002570{
2571 struct stktable_key *key;
2572
Willy Tarreau64ee4912012-08-30 22:59:48 +02002573 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002574 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002575 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002576
Willy Tarreau24e32d82012-04-23 23:55:44 +02002577 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002578 return acl_fetch_inc_gpc0(&px->table, smp, stktable_update_key(&px->table, key));
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002579}
2580
Willy Tarreauf73cd112011-08-13 01:45:16 +02002581/* Clear the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002582 * return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002583 */
2584static int
Willy Tarreau37406352012-04-23 16:16:37 +02002585acl_fetch_clr_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002586{
Willy Tarreau37406352012-04-23 16:16:37 +02002587 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002588 smp->type = SMP_T_UINT;
2589 smp->data.uint = 0;
Willy Tarreauf73cd112011-08-13 01:45:16 +02002590 if (ts != NULL) {
2591 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2592 if (!ptr)
2593 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002594 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002595 stktable_data_cast(ptr, gpc0) = 0;
2596 }
2597 return 1;
2598}
2599
2600/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002601 * frontend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002602 */
2603static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002604acl_fetch_sc1_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002605 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002606{
2607 if (!l4->stkctr1_entry)
2608 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002609 return acl_fetch_clr_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002610}
2611
2612/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002613 * backend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002614 */
2615static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002616acl_fetch_sc2_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002617 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002618{
2619 if (!l4->stkctr2_entry)
2620 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002621 return acl_fetch_clr_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002622}
2623
2624/* Clear the General Purpose Counter 0 value from the session's source address
Willy Tarreaua5e37562011-12-16 17:06:15 +01002625 * in the table pointed to by expr, and return its previous value into temp integer.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002626 * Accepts exactly 1 argument of type table.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002627 */
2628static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002629acl_fetch_src_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002630 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002631{
2632 struct stktable_key *key;
2633
Willy Tarreau64ee4912012-08-30 22:59:48 +02002634 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002635 if (!key)
2636 return 0;
2637
Willy Tarreau24e32d82012-04-23 23:55:44 +02002638 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002639 return acl_fetch_clr_gpc0(&px->table, smp, stktable_update_key(&px->table, key));
Willy Tarreauf73cd112011-08-13 01:45:16 +02002640}
2641
Willy Tarreaua5e37562011-12-16 17:06:15 +01002642/* set temp integer to the cumulated number of connections in the stksess entry <ts> */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002643static int
Willy Tarreau37406352012-04-23 16:16:37 +02002644acl_fetch_conn_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002645{
Willy Tarreau37406352012-04-23 16:16:37 +02002646 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002647 smp->type = SMP_T_UINT;
2648 smp->data.uint = 0;
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002649 if (ts != NULL) {
2650 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2651 if (!ptr)
2652 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002653 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002654 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002655 return 1;
2656}
2657
Willy Tarreaua5e37562011-12-16 17:06:15 +01002658/* set temp integer to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002659static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002660acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002661 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002662{
Willy Tarreau56123282010-08-06 19:06:56 +02002663 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002664 return 0;
2665
Willy Tarreau37406352012-04-23 16:16:37 +02002666 return acl_fetch_conn_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002667}
2668
Willy Tarreaua5e37562011-12-16 17:06:15 +01002669/* set temp integer to the cumulated number of connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002670static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002671acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002672 const struct arg *args, struct sample *smp)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002673{
Willy Tarreau56123282010-08-06 19:06:56 +02002674 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002675 return 0;
2676
Willy Tarreau37406352012-04-23 16:16:37 +02002677 return acl_fetch_conn_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002678}
2679
Willy Tarreaua5e37562011-12-16 17:06:15 +01002680/* set temp integer to the cumulated number of connections from the session's source
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002681 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002682 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002683 */
2684static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002685acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002686 const struct arg *args, struct sample *smp)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002687{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002688 struct stktable_key *key;
2689
Willy Tarreau64ee4912012-08-30 22:59:48 +02002690 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002691 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002692 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002693
Willy Tarreau24e32d82012-04-23 23:55:44 +02002694 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002695 return acl_fetch_conn_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002696}
2697
Willy Tarreaua5e37562011-12-16 17:06:15 +01002698/* set temp integer to the connection rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002699static int
Willy Tarreau37406352012-04-23 16:16:37 +02002700acl_fetch_conn_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002701{
Willy Tarreau37406352012-04-23 16:16:37 +02002702 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002703 smp->type = SMP_T_UINT;
2704 smp->data.uint = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002705 if (ts != NULL) {
2706 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2707 if (!ptr)
2708 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002709 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002710 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2711 }
2712 return 1;
2713}
2714
Willy Tarreaua5e37562011-12-16 17:06:15 +01002715/* set temp integer to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002716 * the configured period.
2717 */
2718static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002719acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002720 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002721{
Willy Tarreau56123282010-08-06 19:06:56 +02002722 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002723 return 0;
2724
Willy Tarreau37406352012-04-23 16:16:37 +02002725 return acl_fetch_conn_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002726}
2727
Willy Tarreaua5e37562011-12-16 17:06:15 +01002728/* set temp integer to the connection rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002729 * the configured period.
2730 */
2731static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002732acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002733 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002734{
Willy Tarreau56123282010-08-06 19:06:56 +02002735 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002736 return 0;
2737
Willy Tarreau37406352012-04-23 16:16:37 +02002738 return acl_fetch_conn_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002739}
2740
Willy Tarreaua5e37562011-12-16 17:06:15 +01002741/* set temp integer to the connection rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002742 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002743 * Accepts exactly 1 argument of type table.
Willy Tarreau91c43d72010-06-20 11:19:22 +02002744 */
2745static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002746acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002747 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002748{
2749 struct stktable_key *key;
2750
Willy Tarreau64ee4912012-08-30 22:59:48 +02002751 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002752 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002753 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002754
Willy Tarreau24e32d82012-04-23 23:55:44 +02002755 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002756 return acl_fetch_conn_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau91c43d72010-06-20 11:19:22 +02002757}
2758
Willy Tarreaua5e37562011-12-16 17:06:15 +01002759/* set temp integer to the number of connections from the session's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002760 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002761 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002762 */
2763static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002764acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002765 const struct arg *args, struct sample *smp)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002766{
2767 struct stksess *ts;
2768 struct stktable_key *key;
2769 void *ptr;
2770
Willy Tarreau64ee4912012-08-30 22:59:48 +02002771 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002772 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002773 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002774
Willy Tarreau24e32d82012-04-23 23:55:44 +02002775 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002776
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002777 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2778 /* entry does not exist and could not be created */
2779 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002780
2781 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2782 if (!ptr)
2783 return 0; /* parameter not stored in this table */
2784
Willy Tarreauf853c462012-04-23 18:53:56 +02002785 smp->type = SMP_T_UINT;
2786 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002787 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002788 return 1;
2789}
2790
Willy Tarreaua5e37562011-12-16 17:06:15 +01002791/* set temp integer to the number of concurrent connections in the stksess entry <ts> */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002792static int
Willy Tarreau37406352012-04-23 16:16:37 +02002793acl_fetch_conn_cur(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002794{
Willy Tarreau37406352012-04-23 16:16:37 +02002795 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002796 smp->type = SMP_T_UINT;
2797 smp->data.uint = 0;
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002798
2799 if (ts != NULL) {
2800 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2801 if (!ptr)
2802 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002803 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002804 }
2805 return 1;
2806}
2807
Willy Tarreaua5e37562011-12-16 17:06:15 +01002808/* set temp integer to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002809static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002810acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002811 const struct arg *args, struct sample *smp)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002812{
Willy Tarreau56123282010-08-06 19:06:56 +02002813 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002814 return 0;
2815
Willy Tarreau37406352012-04-23 16:16:37 +02002816 return acl_fetch_conn_cur(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002817}
2818
Willy Tarreaua5e37562011-12-16 17:06:15 +01002819/* set temp integer to the number of concurrent connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002820static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002821acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002822 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002823{
Willy Tarreau56123282010-08-06 19:06:56 +02002824 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002825 return 0;
2826
Willy Tarreau37406352012-04-23 16:16:37 +02002827 return acl_fetch_conn_cur(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002828}
2829
Willy Tarreaua5e37562011-12-16 17:06:15 +01002830/* set temp integer to the number of concurrent connections from the session's source
Willy Tarreau38285c12010-06-18 16:35:43 +02002831 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002832 * Accepts exactly 1 argument of type table.
Willy Tarreau38285c12010-06-18 16:35:43 +02002833 */
2834static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002835acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002836 const struct arg *args, struct sample *smp)
Willy Tarreau38285c12010-06-18 16:35:43 +02002837{
Willy Tarreau38285c12010-06-18 16:35:43 +02002838 struct stktable_key *key;
2839
Willy Tarreau64ee4912012-08-30 22:59:48 +02002840 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau38285c12010-06-18 16:35:43 +02002841 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002842 return 0;
Willy Tarreau38285c12010-06-18 16:35:43 +02002843
Willy Tarreau24e32d82012-04-23 23:55:44 +02002844 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002845 return acl_fetch_conn_cur(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002846}
2847
Willy Tarreaua5e37562011-12-16 17:06:15 +01002848/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002849static int
Willy Tarreau37406352012-04-23 16:16:37 +02002850acl_fetch_sess_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002851{
Willy Tarreau37406352012-04-23 16:16:37 +02002852 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002853 smp->type = SMP_T_UINT;
2854 smp->data.uint = 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002855 if (ts != NULL) {
2856 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2857 if (!ptr)
2858 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002859 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002860 }
2861 return 1;
2862}
2863
Willy Tarreaua5e37562011-12-16 17:06:15 +01002864/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002865static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002866acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002867 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002868{
Willy Tarreau56123282010-08-06 19:06:56 +02002869 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002870 return 0;
2871
Willy Tarreau37406352012-04-23 16:16:37 +02002872 return acl_fetch_sess_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002873}
2874
Willy Tarreaua5e37562011-12-16 17:06:15 +01002875/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002876static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002877acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002878 const struct arg *args, struct sample *smp)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002879{
Willy Tarreau56123282010-08-06 19:06:56 +02002880 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002881 return 0;
2882
Willy Tarreau37406352012-04-23 16:16:37 +02002883 return acl_fetch_sess_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002884}
2885
Willy Tarreaua5e37562011-12-16 17:06:15 +01002886/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002887 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002888 * Accepts exactly 1 argument of type table.
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002889 */
2890static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002891acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002892 const struct arg *args, struct sample *smp)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002893{
2894 struct stktable_key *key;
2895
Willy Tarreau64ee4912012-08-30 22:59:48 +02002896 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002897 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002898 return 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002899
Willy Tarreau24e32d82012-04-23 23:55:44 +02002900 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002901 return acl_fetch_sess_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002902}
2903
Willy Tarreaua5e37562011-12-16 17:06:15 +01002904/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002905static int
Willy Tarreau37406352012-04-23 16:16:37 +02002906acl_fetch_sess_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002907{
Willy Tarreau37406352012-04-23 16:16:37 +02002908 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002909 smp->type = SMP_T_UINT;
2910 smp->data.uint = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002911 if (ts != NULL) {
2912 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2913 if (!ptr)
2914 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002915 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002916 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2917 }
2918 return 1;
2919}
2920
Willy Tarreaua5e37562011-12-16 17:06:15 +01002921/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002922 * the configured period.
2923 */
2924static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002925acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002926 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002927{
Willy Tarreau56123282010-08-06 19:06:56 +02002928 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002929 return 0;
2930
Willy Tarreau37406352012-04-23 16:16:37 +02002931 return acl_fetch_sess_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002932}
2933
Willy Tarreaua5e37562011-12-16 17:06:15 +01002934/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002935 * the configured period.
2936 */
2937static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002938acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002939 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002940{
Willy Tarreau56123282010-08-06 19:06:56 +02002941 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002942 return 0;
2943
Willy Tarreau37406352012-04-23 16:16:37 +02002944 return acl_fetch_sess_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002945}
2946
Willy Tarreaua5e37562011-12-16 17:06:15 +01002947/* set temp integer to the session rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002948 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002949 * Accepts exactly 1 argument of type table.
Willy Tarreau91c43d72010-06-20 11:19:22 +02002950 */
2951static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002952acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002953 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002954{
2955 struct stktable_key *key;
2956
Willy Tarreau64ee4912012-08-30 22:59:48 +02002957 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002958 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002959 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002960
Willy Tarreau24e32d82012-04-23 23:55:44 +02002961 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002962 return acl_fetch_sess_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau91c43d72010-06-20 11:19:22 +02002963}
2964
Willy Tarreaua5e37562011-12-16 17:06:15 +01002965/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002966static int
Willy Tarreau37406352012-04-23 16:16:37 +02002967acl_fetch_http_req_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002968{
Willy Tarreau37406352012-04-23 16:16:37 +02002969 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002970 smp->type = SMP_T_UINT;
2971 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002972 if (ts != NULL) {
2973 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2974 if (!ptr)
2975 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002976 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002977 }
2978 return 1;
2979}
2980
Willy Tarreaua5e37562011-12-16 17:06:15 +01002981/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002982static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002983acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002984 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002985{
Willy Tarreau56123282010-08-06 19:06:56 +02002986 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002987 return 0;
2988
Willy Tarreau37406352012-04-23 16:16:37 +02002989 return acl_fetch_http_req_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002990}
2991
Willy Tarreaua5e37562011-12-16 17:06:15 +01002992/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002993static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002994acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002995 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002996{
Willy Tarreau56123282010-08-06 19:06:56 +02002997 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002998 return 0;
2999
Willy Tarreau37406352012-04-23 16:16:37 +02003000 return acl_fetch_http_req_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003001}
3002
Willy Tarreaua5e37562011-12-16 17:06:15 +01003003/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02003004 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003005 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003006 */
3007static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003008acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003009 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003010{
3011 struct stktable_key *key;
3012
Willy Tarreau64ee4912012-08-30 22:59:48 +02003013 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003014 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003015 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003016
Willy Tarreau24e32d82012-04-23 23:55:44 +02003017 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003018 return acl_fetch_http_req_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003019}
3020
Willy Tarreaua5e37562011-12-16 17:06:15 +01003021/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003022static int
Willy Tarreau37406352012-04-23 16:16:37 +02003023acl_fetch_http_req_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003024{
Willy Tarreau37406352012-04-23 16:16:37 +02003025 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003026 smp->type = SMP_T_UINT;
3027 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003028 if (ts != NULL) {
3029 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
3030 if (!ptr)
3031 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003032 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02003033 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
3034 }
3035 return 1;
3036}
3037
Willy Tarreaua5e37562011-12-16 17:06:15 +01003038/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02003039 * the configured period.
3040 */
3041static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003042acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003043 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003044{
Willy Tarreau56123282010-08-06 19:06:56 +02003045 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003046 return 0;
3047
Willy Tarreau37406352012-04-23 16:16:37 +02003048 return acl_fetch_http_req_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003049}
3050
Willy Tarreaua5e37562011-12-16 17:06:15 +01003051/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003052 * the configured period.
3053 */
3054static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003055acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003056 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003057{
Willy Tarreau56123282010-08-06 19:06:56 +02003058 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003059 return 0;
3060
Willy Tarreau37406352012-04-23 16:16:37 +02003061 return acl_fetch_http_req_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003062}
3063
Willy Tarreaua5e37562011-12-16 17:06:15 +01003064/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003065 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003066 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003067 */
3068static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003069acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003070 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003071{
3072 struct stktable_key *key;
3073
Willy Tarreau64ee4912012-08-30 22:59:48 +02003074 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003075 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003076 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003077
Willy Tarreau24e32d82012-04-23 23:55:44 +02003078 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003079 return acl_fetch_http_req_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003080}
3081
Willy Tarreaua5e37562011-12-16 17:06:15 +01003082/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003083static int
Willy Tarreau37406352012-04-23 16:16:37 +02003084acl_fetch_http_err_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003085{
Willy Tarreau37406352012-04-23 16:16:37 +02003086 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003087 smp->type = SMP_T_UINT;
3088 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003089 if (ts != NULL) {
3090 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
3091 if (!ptr)
3092 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003093 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003094 }
3095 return 1;
3096}
3097
Willy Tarreaua5e37562011-12-16 17:06:15 +01003098/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003099static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003100acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003101 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003102{
Willy Tarreau56123282010-08-06 19:06:56 +02003103 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003104 return 0;
3105
Willy Tarreau37406352012-04-23 16:16:37 +02003106 return acl_fetch_http_err_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003107}
3108
Willy Tarreaua5e37562011-12-16 17:06:15 +01003109/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003110static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003111acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003112 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003113{
Willy Tarreau56123282010-08-06 19:06:56 +02003114 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003115 return 0;
3116
Willy Tarreau37406352012-04-23 16:16:37 +02003117 return acl_fetch_http_err_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003118}
3119
Willy Tarreaua5e37562011-12-16 17:06:15 +01003120/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02003121 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003122 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003123 */
3124static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003125acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003126 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003127{
3128 struct stktable_key *key;
3129
Willy Tarreau64ee4912012-08-30 22:59:48 +02003130 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003131 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003132 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003133
Willy Tarreau24e32d82012-04-23 23:55:44 +02003134 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003135 return acl_fetch_http_err_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003136}
3137
Willy Tarreaua5e37562011-12-16 17:06:15 +01003138/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003139static int
Willy Tarreau37406352012-04-23 16:16:37 +02003140acl_fetch_http_err_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003141{
Willy Tarreau37406352012-04-23 16:16:37 +02003142 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003143 smp->type = SMP_T_UINT;
3144 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003145 if (ts != NULL) {
3146 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
3147 if (!ptr)
3148 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003149 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02003150 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
3151 }
3152 return 1;
3153}
3154
Willy Tarreaua5e37562011-12-16 17:06:15 +01003155/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02003156 * the configured period.
3157 */
3158static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003159acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003160 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003161{
Willy Tarreau56123282010-08-06 19:06:56 +02003162 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003163 return 0;
3164
Willy Tarreau37406352012-04-23 16:16:37 +02003165 return acl_fetch_http_err_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003166}
3167
Willy Tarreaua5e37562011-12-16 17:06:15 +01003168/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003169 * the configured period.
3170 */
3171static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003172acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003173 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003174{
Willy Tarreau56123282010-08-06 19:06:56 +02003175 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003176 return 0;
3177
Willy Tarreau37406352012-04-23 16:16:37 +02003178 return acl_fetch_http_err_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003179}
3180
Willy Tarreaua5e37562011-12-16 17:06:15 +01003181/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003182 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003183 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003184 */
3185static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003186acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003187 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003188{
3189 struct stktable_key *key;
3190
Willy Tarreau64ee4912012-08-30 22:59:48 +02003191 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003192 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003193 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003194
Willy Tarreau24e32d82012-04-23 23:55:44 +02003195 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003196 return acl_fetch_http_err_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003197}
3198
Willy Tarreaua5e37562011-12-16 17:06:15 +01003199/* set temp integer to the number of kbytes received from clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003200static int
Willy Tarreau37406352012-04-23 16:16:37 +02003201acl_fetch_kbytes_in(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003202{
Willy Tarreau37406352012-04-23 16:16:37 +02003203 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003204 smp->type = SMP_T_UINT;
3205 smp->data.uint = 0;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003206
3207 if (ts != NULL) {
3208 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
3209 if (!ptr)
3210 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003211 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003212 }
3213 return 1;
3214}
3215
Willy Tarreaua5e37562011-12-16 17:06:15 +01003216/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003217 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003218 */
3219static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003220acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003221 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003222{
Willy Tarreau56123282010-08-06 19:06:56 +02003223 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003224 return 0;
3225
Willy Tarreau37406352012-04-23 16:16:37 +02003226 return acl_fetch_kbytes_in(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003227}
3228
Willy Tarreaua5e37562011-12-16 17:06:15 +01003229/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003230 * session's tracked BE counters.
3231 */
3232static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003233acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003234 const struct arg *args, struct sample *smp)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003235{
Willy Tarreau56123282010-08-06 19:06:56 +02003236 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003237 return 0;
3238
Willy Tarreau37406352012-04-23 16:16:37 +02003239 return acl_fetch_kbytes_in(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003240}
3241
Willy Tarreaua5e37562011-12-16 17:06:15 +01003242/* set temp integer to the number of kbytes received from the session's source
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003243 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003244 * Accepts exactly 1 argument of type table.
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003245 */
3246static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003247acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003248 const struct arg *args, struct sample *smp)
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003249{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003250 struct stktable_key *key;
3251
Willy Tarreau64ee4912012-08-30 22:59:48 +02003252 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003253 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003254 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003255
Willy Tarreau24e32d82012-04-23 23:55:44 +02003256 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003257 return acl_fetch_kbytes_in(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003258}
3259
Willy Tarreaua5e37562011-12-16 17:06:15 +01003260/* set temp integer to the bytes rate from clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003261 * configured period.
3262 */
3263static int
Willy Tarreau37406352012-04-23 16:16:37 +02003264acl_fetch_bytes_in_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003265{
Willy Tarreau37406352012-04-23 16:16:37 +02003266 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003267 smp->type = SMP_T_UINT;
3268 smp->data.uint = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003269 if (ts != NULL) {
3270 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
3271 if (!ptr)
3272 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003273 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003274 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
3275 }
3276 return 1;
3277}
3278
Willy Tarreaua5e37562011-12-16 17:06:15 +01003279/* set temp integer to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003280 * counters over the configured period.
3281 */
3282static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003283acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003284 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003285{
Willy Tarreau56123282010-08-06 19:06:56 +02003286 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003287 return 0;
3288
Willy Tarreau37406352012-04-23 16:16:37 +02003289 return acl_fetch_bytes_in_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003290}
3291
Willy Tarreaua5e37562011-12-16 17:06:15 +01003292/* set temp integer to the bytes rate from clients from the session's tracked BE
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003293 * counters over the configured period.
3294 */
3295static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003296acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003297 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003298{
Willy Tarreau56123282010-08-06 19:06:56 +02003299 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003300 return 0;
3301
Willy Tarreau37406352012-04-23 16:16:37 +02003302 return acl_fetch_bytes_in_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003303}
3304
Willy Tarreaua5e37562011-12-16 17:06:15 +01003305/* set temp integer to the bytes rate from clients from the session's source address
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003306 * in the table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003307 * Accepts exactly 1 argument of type table.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003308 */
3309static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003310acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003311 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003312{
3313 struct stktable_key *key;
3314
Willy Tarreau64ee4912012-08-30 22:59:48 +02003315 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003316 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003317 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003318
Willy Tarreau24e32d82012-04-23 23:55:44 +02003319 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003320 return acl_fetch_bytes_in_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003321}
3322
Willy Tarreaua5e37562011-12-16 17:06:15 +01003323/* set temp integer to the number of kbytes sent to clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003324static int
Willy Tarreau37406352012-04-23 16:16:37 +02003325acl_fetch_kbytes_out(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003326{
Willy Tarreau37406352012-04-23 16:16:37 +02003327 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003328 smp->type = SMP_T_UINT;
3329 smp->data.uint = 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003330
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003331 if (ts != NULL) {
3332 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003333 if (!ptr)
3334 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003335 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003336 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003337 return 1;
3338}
3339
Willy Tarreaua5e37562011-12-16 17:06:15 +01003340/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003341 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003342 */
3343static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003344acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003345 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003346{
Willy Tarreau56123282010-08-06 19:06:56 +02003347 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003348 return 0;
3349
Willy Tarreau37406352012-04-23 16:16:37 +02003350 return acl_fetch_kbytes_out(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003351}
3352
Willy Tarreaua5e37562011-12-16 17:06:15 +01003353/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003354 * tracked BE counters.
3355 */
3356static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003357acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003358 const struct arg *args, struct sample *smp)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003359{
Willy Tarreau56123282010-08-06 19:06:56 +02003360 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003361 return 0;
3362
Willy Tarreau37406352012-04-23 16:16:37 +02003363 return acl_fetch_kbytes_out(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003364}
3365
Willy Tarreaua5e37562011-12-16 17:06:15 +01003366/* set temp integer to the number of kbytes sent to the session's source address in
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003367 * the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003368 * Accepts exactly 1 argument of type table.
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003369 */
3370static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003371acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003372 const struct arg *args, struct sample *smp)
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003373{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003374 struct stktable_key *key;
3375
Willy Tarreau64ee4912012-08-30 22:59:48 +02003376 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003377 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003378 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003379
Willy Tarreau24e32d82012-04-23 23:55:44 +02003380 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003381 return acl_fetch_kbytes_out(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003382}
3383
Willy Tarreaua5e37562011-12-16 17:06:15 +01003384/* set temp integer to the bytes rate to clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003385 * configured period.
3386 */
3387static int
Willy Tarreau37406352012-04-23 16:16:37 +02003388acl_fetch_bytes_out_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003389{
Willy Tarreau37406352012-04-23 16:16:37 +02003390 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003391 smp->type = SMP_T_UINT;
3392 smp->data.uint = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003393 if (ts != NULL) {
3394 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3395 if (!ptr)
3396 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003397 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003398 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3399 }
3400 return 1;
3401}
3402
Willy Tarreaua5e37562011-12-16 17:06:15 +01003403/* set temp integer to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003404 * over the configured period.
3405 */
3406static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003407acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003408 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003409{
Willy Tarreau56123282010-08-06 19:06:56 +02003410 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003411 return 0;
3412
Willy Tarreau37406352012-04-23 16:16:37 +02003413 return acl_fetch_bytes_out_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003414}
3415
Willy Tarreaua5e37562011-12-16 17:06:15 +01003416/* set temp integer to the bytes rate to clients from the session's tracked BE counters
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003417 * over the configured period.
3418 */
3419static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003420acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003421 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003422{
Willy Tarreau56123282010-08-06 19:06:56 +02003423 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003424 return 0;
3425
Willy Tarreau37406352012-04-23 16:16:37 +02003426 return acl_fetch_bytes_out_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003427}
3428
Willy Tarreaua5e37562011-12-16 17:06:15 +01003429/* set temp integer to the bytes rate to client from the session's source address in
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003430 * the table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003431 * Accepts exactly 1 argument of type table.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003432 */
3433static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003434acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003435 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003436{
3437 struct stktable_key *key;
3438
Willy Tarreau64ee4912012-08-30 22:59:48 +02003439 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003440 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003441 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003442
Willy Tarreau24e32d82012-04-23 23:55:44 +02003443 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003444 return acl_fetch_bytes_out_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003445}
3446
Willy Tarreau34db1082012-04-19 17:16:54 +02003447/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003448 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003449 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003450static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003451acl_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003452 const struct arg *args, struct sample *smp)
Willy Tarreauc735a072011-03-29 00:57:02 +02003453{
Willy Tarreau37406352012-04-23 16:16:37 +02003454 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003455 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003456 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003457 return 1;
3458}
3459
Willy Tarreau34db1082012-04-19 17:16:54 +02003460/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003461 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003462 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003463static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003464acl_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003465 const struct arg *args, struct sample *smp)
Willy Tarreauc735a072011-03-29 00:57:02 +02003466{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003467 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003468 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003469 smp->type = SMP_T_UINT;
3470 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003471 return 1;
3472}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003473
Willy Tarreau61612d42012-04-19 18:42:05 +02003474/* Note: must not be declared <const> as its list will be overwritten.
3475 * Please take care of keeping this list alphabetically sorted.
3476 */
Willy Tarreau8b22a712010-06-18 17:46:06 +02003477static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02003478 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3479 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3480 { "sc1_clr_gpc0", acl_parse_int, acl_fetch_sc1_clr_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3481 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3482 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING, 0 },
3483 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3484 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3485 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3486 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3487 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3488 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3489 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3490 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3491 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3492 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3493 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3494 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3495 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3496 { "sc2_clr_gpc0", acl_parse_int, acl_fetch_sc2_clr_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3497 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3498 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING, 0 },
3499 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3500 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3501 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3502 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3503 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3504 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3505 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3506 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3507 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3508 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3509 { "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 +02003510 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3511 { "src_bytes_out_rate", acl_parse_int, acl_fetch_src_bytes_out_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3512 { "src_clr_gpc0", acl_parse_int, acl_fetch_src_clr_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3513 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3514 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3515 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3516 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3517 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3518 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3519 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3520 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3521 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3522 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3523 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3524 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3525 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3526 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3527 { "table_avl", acl_parse_int, acl_fetch_table_avl, acl_match_int, ACL_USE_NOTHING, ARG1(1,TAB) },
3528 { "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 +02003529 { NULL, NULL, NULL, NULL },
3530}};
3531
3532
Willy Tarreau56123282010-08-06 19:06:56 +02003533/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003534 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3535 * <prm> struct is fed with the table name if any. If unspecified, the caller
3536 * will assume that the current proxy's table is used.
3537 */
3538int parse_track_counters(char **args, int *arg,
3539 int section_type, struct proxy *curpx,
3540 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003541 struct proxy *defpx, char **err)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003542{
Willy Tarreau12785782012-04-27 21:37:17 +02003543 int sample_type = 0;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003544
Willy Tarreau56123282010-08-06 19:06:56 +02003545 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003546 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003547 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003548 */
3549 while (args[*arg]) {
3550 if (strcmp(args[*arg], "src") == 0) {
3551 prm->type = STKTABLE_TYPE_IP;
Willy Tarreau12785782012-04-27 21:37:17 +02003552 sample_type = 1;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003553 }
3554 else if (strcmp(args[*arg], "table") == 0) {
3555 if (!args[*arg + 1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003556 memprintf(err, "missing table name");
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003557 return -1;
3558 }
3559 /* we copy the table name for now, it will be resolved later */
3560 prm->table.n = strdup(args[*arg + 1]);
3561 (*arg)++;
3562 }
3563 else {
3564 /* unhandled keywords are handled by the caller */
3565 break;
3566 }
3567 (*arg)++;
3568 }
3569
Willy Tarreau12785782012-04-27 21:37:17 +02003570 if (!sample_type) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003571 memprintf(err,
3572 "tracking key not specified (found %s, only 'src' is supported)",
3573 quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003574 return -1;
3575 }
3576
3577 return 0;
3578}
3579
Willy Tarreau8b22a712010-06-18 17:46:06 +02003580__attribute__((constructor))
3581static void __session_init(void)
3582{
3583 acl_register_keywords(&acl_kws);
3584}
3585
Willy Tarreaubaaee002006-06-26 02:48:02 +02003586/*
3587 * Local variables:
3588 * c-indent-level: 8
3589 * c-basic-offset: 8
3590 * End:
3591 */