blob: 7f72156e99a7ab2932e640957bdc6ab224985d8c [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 Tarreaufb356202010-08-03 14:02:05 +02001674 if (ana_list & AN_REQ_INSPECT_FE) {
1675 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001676 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001677 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001678 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001679
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001680 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001681 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001682 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001683 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001684 }
1685
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001686 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001687 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1688 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001689 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001690 }
1691
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001692 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001693 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1694 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001695 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001696 }
1697
Willy Tarreaufb356202010-08-03 14:02:05 +02001698 if (ana_list & AN_REQ_INSPECT_BE) {
1699 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1700 break;
1701 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1702 }
1703
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001704 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001705 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1706 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001707 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001708 }
1709
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001710 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001711 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001712 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001713 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001714 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001715
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001716 if (ana_list & AN_REQ_SRV_RULES) {
1717 if (!process_server_rules(s, s->req, AN_REQ_SRV_RULES))
1718 break;
1719 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
1720 }
1721
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001722 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001723 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1724 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001725 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001726 }
1727
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001728 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001729 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001730 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001731 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001732 }
Emeric Brun647caf12009-06-30 17:57:00 +02001733
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001734 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001735 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1736 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001737 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001738 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001739
Emeric Brun1d33b292010-01-04 15:47:17 +01001740 if (ana_list & AN_REQ_STICKING_RULES) {
1741 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1742 break;
1743 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1744 }
1745
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001746 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001747 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1748 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001749 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001750 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001751 break;
1752 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001753 }
Willy Tarreau84455332009-03-15 22:34:05 +01001754
Willy Tarreau815a9b22010-07-27 17:15:12 +02001755 rq_prod_last = s->si[0].state;
1756 rq_cons_last = s->si[1].state;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001757 s->req->flags &= ~CF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001758 rqf_last = s->req->flags;
1759
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001760 if ((s->req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001761 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001762 }
1763
Willy Tarreau576507f2010-01-07 00:09:04 +01001764 /* we'll monitor the request analysers while parsing the response,
1765 * because some response analysers may indirectly enable new request
1766 * analysers (eg: HTTP keep-alive).
1767 */
1768 req_ana_back = s->req->analysers;
1769
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001770 resync_response:
1771 /* Analyse response */
1772
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001773 if (unlikely(s->rep->flags & CF_HIJACK)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001774 /* In inject mode, we wake up everytime something has
1775 * happened on the write side of the buffer.
1776 */
1777 unsigned int flags = s->rep->flags;
1778
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001779 if ((s->rep->flags & (CF_WRITE_PARTIAL|CF_WRITE_ERROR|CF_SHUTW)) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02001780 !channel_full(s->rep)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001781 s->rep->hijacker(s, s->rep);
1782 }
1783
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001784 if ((s->rep->flags ^ flags) & CF_MASK_STATIC) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001785 rpf_last = s->rep->flags;
1786 goto resync_response;
1787 }
1788 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001789 else if (((s->rep->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1790 (s->rep->flags ^ rpf_last) & CF_MASK_STATIC ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001791 s->si[0].state != rp_cons_last ||
1792 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001793 unsigned int flags = s->rep->flags;
1794
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001795 if ((s->rep->flags & CF_MASK_ANALYSER) &&
Willy Tarreau0499e352010-12-17 07:13:42 +01001796 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1797 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1798 * for some free space in the response buffer, so we might need to call
1799 * it when something changes in the response buffer, but still we pass
1800 * it the request buffer. Note that the SI state might very well still
1801 * be zero due to us returning a flow of redirects!
1802 */
1803 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001804 s->req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001805 }
1806
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001807 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001808 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001809 unsigned int ana_list;
1810 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001811
Willy Tarreau90deb182010-01-07 00:20:41 +01001812 /* it's up to the analysers to stop disable reading or
1813 * closing. Note: if an analyser disables any of these
1814 * bits, it is responsible for enabling them again when
1815 * it disables itself, so that other analysers are called
1816 * in similar conditions.
1817 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001818 channel_auto_read(s->rep);
1819 channel_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001820
1821 /* We will call all analysers for which a bit is set in
1822 * s->rep->analysers, following the bit order from LSB
1823 * to MSB. The analysers must remove themselves from
1824 * the list when not needed. Any analyser may return 0
1825 * to break out of the loop, either because of missing
1826 * data to take a decision, or because it decides to
1827 * kill the session. We loop at least once through each
1828 * analyser, and we may loop again if other analysers
1829 * are added in the middle.
1830 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001831
1832 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001833 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001834 /* Warning! ensure that analysers are always placed in ascending order! */
1835
Emeric Brun97679e72010-09-23 17:56:44 +02001836 if (ana_list & AN_RES_INSPECT) {
1837 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1838 break;
1839 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1840 }
1841
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001842 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001843 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1844 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001845 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001846 }
1847
Emeric Brun1d33b292010-01-04 15:47:17 +01001848 if (ana_list & AN_RES_STORE_RULES) {
1849 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1850 break;
1851 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1852 }
1853
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001854 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001855 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1856 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001857 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001858 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001860 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001861 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1862 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001863 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001864 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001865 break;
1866 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001867 }
1868
Willy Tarreau815a9b22010-07-27 17:15:12 +02001869 rp_cons_last = s->si[0].state;
1870 rp_prod_last = s->si[1].state;
1871 rpf_last = s->rep->flags;
1872
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001873 if ((s->rep->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001874 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001875 }
1876
Willy Tarreau576507f2010-01-07 00:09:04 +01001877 /* maybe someone has added some request analysers, so we must check and loop */
1878 if (s->req->analysers & ~req_ana_back)
1879 goto resync_request;
1880
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001881 if ((s->req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001882 goto resync_request;
1883
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001884 /* FIXME: here we should call protocol handlers which rely on
1885 * both buffers.
1886 */
1887
1888
1889 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001890 * Now we propagate unhandled errors to the session. Normally
1891 * we're just in a data phase here since it means we have not
1892 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001893 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001894 srv = target_srv(&s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01001895 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001896 if (s->req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001897 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001898 s->req->analysers = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001899 if (s->req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001900 s->be->be_counters.cli_aborts++;
1901 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001902 if (srv)
1903 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001904 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001905 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001906 else if (s->req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001907 s->be->be_counters.cli_aborts++;
1908 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001909 if (srv)
1910 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001911 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001912 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001913 else if (s->req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001914 s->be->be_counters.srv_aborts++;
1915 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001916 if (srv)
1917 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001918 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001919 }
1920 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001921 s->be->be_counters.srv_aborts++;
1922 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001923 if (srv)
1924 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001925 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001926 }
Willy Tarreau84455332009-03-15 22:34:05 +01001927 sess_set_term_flags(s);
1928 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001929 else if (s->rep->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001930 /* Report it if the server got an error or a read timeout expired */
1931 s->rep->analysers = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001932 if (s->rep->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001933 s->be->be_counters.srv_aborts++;
1934 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001935 if (srv)
1936 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001937 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001938 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001939 else if (s->rep->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001940 s->be->be_counters.srv_aborts++;
1941 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001942 if (srv)
1943 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001944 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001945 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001946 else if (s->rep->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001947 s->be->be_counters.cli_aborts++;
1948 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001949 if (srv)
1950 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001951 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001952 }
1953 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001954 s->be->be_counters.cli_aborts++;
1955 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001956 if (srv)
1957 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01001958 s->flags |= SN_ERR_CLITO;
1959 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001960 sess_set_term_flags(s);
1961 }
Willy Tarreau84455332009-03-15 22:34:05 +01001962 }
1963
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001964 /*
1965 * Here we take care of forwarding unhandled data. This also includes
1966 * connection establishments and shutdown requests.
1967 */
1968
1969
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001970 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001971 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001972 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001973 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001974 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001975 if (!s->req->analysers &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001976 !(s->req->flags & (CF_HIJACK|CF_SHUTW|CF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001977 (s->req->prod->state >= SI_ST_EST) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001978 (s->req->to_forward != CHN_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001979 /* This buffer is freewheeling, there's no analyser nor hijacker
1980 * attached to it. If any data are left in, we'll permit them to
1981 * move.
1982 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001983 channel_auto_read(s->req);
1984 channel_auto_connect(s->req);
1985 channel_auto_close(s->req);
Willy Tarreaua75bcef2012-08-24 22:56:11 +02001986 buffer_flush(&s->req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001987
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001988 /* We'll let data flow between the producer (if still connected)
1989 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001990 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001991 if (!(s->req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001992 channel_forward(s->req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001993 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001994
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001995 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001996 if (!(s->req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001997 s->req->to_forward &&
1998 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau93b0f4f2012-08-30 21:23:53 +02001999 (s->si[0].conn.data && s->si[0].conn.data->rcv_pipe && s->si[0].conn.data->snd_pipe) &&
2000 (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 +01002001 (pipes_used < global.maxpipes) &&
2002 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2003 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002004 (s->req->flags & CF_STREAMER_FAST)))) {
2005 s->req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002006 }
2007
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002008 /* reflect what the L7 analysers have seen last */
2009 rqf_last = s->req->flags;
2010
2011 /*
2012 * Now forward all shutdown requests between both sides of the buffer
2013 */
2014
Willy Tarreau520d95e2009-09-19 21:04:57 +02002015 /* first, let's check if the request buffer needs to shutdown(write), which may
2016 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01002017 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002018 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002019 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_HIJACK|CF_AUTO_CLOSE|CF_SHUTR)) ==
2020 (CF_AUTO_CLOSE|CF_SHUTR)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002021 channel_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002022
2023 /* shutdown(write) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002024 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
Willy Tarreau8e21bb92012-08-24 22:40:29 +02002025 channel_is_empty(s->req)))
Willy Tarreau73b013b2012-05-21 16:31:45 +02002026 si_shutw(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002027
2028 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002029 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002030 !s->req->analysers))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002031 channel_shutr_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002032
2033 /* shutdown(read) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002034 if (unlikely((s->req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002035 if (s->req->prod->flags & SI_FL_NOHALF)
2036 s->req->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02002037 si_shutr(s->req->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002038 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002039
Willy Tarreau520d95e2009-09-19 21:04:57 +02002040 /* it's possible that an upper layer has requested a connection setup or abort.
2041 * There are 2 situations where we decide to establish a new connection :
2042 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002043 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002044 */
2045 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002046 if (!(s->req->flags & CF_SHUTW)) {
2047 if ((s->req->flags & CF_AUTO_CONNECT) || !channel_is_empty(s->req)) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01002048 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02002049 * switch to the connected state, otherwise we perform a connection
2050 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002051 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02002052 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02002053 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau3cefd522012-08-30 15:49:18 +02002054 if (unlikely(s->req->cons->conn.target.type == TARG_TYPE_APPLET &&
Willy Tarreau73b013b2012-05-21 16:31:45 +02002055 !(si_ctrl(s->req->cons) && si_ctrl(s->req->cons)->connect))) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02002056 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002057 s->rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau85e7d002010-05-31 11:57:51 +02002058 s->req->wex = TICK_ETERNITY;
2059 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02002060 }
Willy Tarreau73201222009-08-16 18:27:24 +02002061 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002062 else {
Willy Tarreau92795622009-03-06 12:51:23 +01002063 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002064 channel_shutw_now(s->req); /* fix buffer flags upon abort */
2065 channel_shutr_now(s->rep);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002066 }
Willy Tarreau92795622009-03-06 12:51:23 +01002067 }
2068
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002069
2070 /* we may have a pending connection request, or a connection waiting
2071 * for completion.
2072 */
2073 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
2074 do {
2075 /* nb: step 1 might switch from QUE to ASS, but we first want
2076 * to give a chance to step 2 to perform a redirect if needed.
2077 */
2078 if (s->si[1].state != SI_ST_REQ)
2079 sess_update_stream_int(s, &s->si[1]);
2080 if (s->si[1].state == SI_ST_REQ)
2081 sess_prepare_conn_req(s, &s->si[1]);
2082
Willy Tarreau827aee92011-03-10 16:55:02 +01002083 srv = target_srv(&s->target);
2084 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002085 perform_http_redirect(s, &s->si[1]);
2086 } while (s->si[1].state == SI_ST_ASS);
Mark Lamourinec2247f02012-01-04 13:02:01 -05002087
2088 /* Now we can add the server name to a header (if requested) */
2089 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Stathis Voukelatos09a030a2012-01-09 14:27:13 +01002090 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau45c0d982012-03-09 12:11:57 +01002091 (s->be->mode == PR_MODE_HTTP) &&
2092 (s->be->server_id_hdr_name != NULL)) {
2093 http_send_name_header(&s->txn, s->be, target_srv(&s->target)->id);
Mark Lamourinec2247f02012-01-04 13:02:01 -05002094 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002095 }
2096
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002097 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002098 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002099 goto resync_stream_interface;
2100
Willy Tarreau815a9b22010-07-27 17:15:12 +02002101 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002102 if ((s->req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002103 goto resync_request;
2104
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002105 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002106
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002107 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002108 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002109 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002110 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002111 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002112 if (!s->rep->analysers &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002113 !(s->rep->flags & (CF_HIJACK|CF_SHUTW|CF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02002114 (s->rep->prod->state >= SI_ST_EST) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002115 (s->rep->to_forward != CHN_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002116 /* This buffer is freewheeling, there's no analyser nor hijacker
2117 * attached to it. If any data are left in, we'll permit them to
2118 * move.
2119 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002120 channel_auto_read(s->rep);
2121 channel_auto_close(s->rep);
Willy Tarreaua75bcef2012-08-24 22:56:11 +02002122 buffer_flush(&s->rep->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002123
2124 /* We'll let data flow between the producer (if still connected)
2125 * to the consumer.
2126 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002127 if (!(s->rep->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002128 channel_forward(s->rep, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002129
2130 /* if we have no analyser anymore in any direction and have a
2131 * tunnel timeout set, use it now.
2132 */
2133 if (!s->req->analysers && s->be->timeout.tunnel) {
2134 s->req->rto = s->req->wto = s->rep->rto = s->rep->wto =
2135 s->be->timeout.tunnel;
2136 s->req->rex = s->req->wex = s->rep->rex = s->rep->wex =
2137 tick_add(now_ms, s->be->timeout.tunnel);
2138 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002139 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002140
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002141 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002142 if (!(s->rep->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002143 s->rep->to_forward &&
2144 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau93b0f4f2012-08-30 21:23:53 +02002145 (s->si[0].conn.data && s->si[0].conn.data->rcv_pipe && s->si[0].conn.data->snd_pipe) &&
2146 (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 +01002147 (pipes_used < global.maxpipes) &&
2148 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2149 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002150 (s->rep->flags & CF_STREAMER_FAST)))) {
2151 s->rep->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002152 }
2153
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002154 /* reflect what the L7 analysers have seen last */
2155 rpf_last = s->rep->flags;
2156
2157 /*
2158 * Now forward all shutdown requests between both sides of the buffer
2159 */
2160
2161 /*
2162 * FIXME: this is probably where we should produce error responses.
2163 */
2164
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002165 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002166 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_HIJACK|CF_AUTO_CLOSE|CF_SHUTR)) ==
2167 (CF_AUTO_CLOSE|CF_SHUTR)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002168 channel_shutw_now(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002169
2170 /* shutdown(write) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002171 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
Willy Tarreau8e21bb92012-08-24 22:40:29 +02002172 channel_is_empty(s->rep)))
Willy Tarreau73b013b2012-05-21 16:31:45 +02002173 si_shutw(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002174
2175 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002176 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002177 !s->rep->analysers)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002178 channel_shutr_now(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002179
2180 /* shutdown(read) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002181 if (unlikely((s->rep->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002182 if (s->rep->prod->flags & SI_FL_NOHALF)
2183 s->rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02002184 si_shutr(s->rep->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002185 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002186
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002187 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002188 goto resync_stream_interface;
2189
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002190 if (s->req->flags != rqf_last)
2191 goto resync_request;
2192
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002193 if ((s->rep->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002194 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002195
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002196 /* we're interested in getting wakeups again */
2197 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
2198 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
2199
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002200 /* This is needed only when debugging is enabled, to indicate
2201 * client-side or server-side close. Please note that in the unlikely
2202 * event where both sides would close at once, the sequence is reported
2203 * on the server side first.
2204 */
2205 if (unlikely((global.mode & MODE_DEBUG) &&
2206 (!(global.mode & MODE_QUIET) ||
2207 (global.mode & MODE_VERBOSE)))) {
2208 int len;
2209
2210 if (s->si[1].state == SI_ST_CLO &&
2211 s->si[1].prev_state == SI_ST_EST) {
2212 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
2213 s->uniq_id, s->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002214 (unsigned short)si_fd(&s->si[0]),
2215 (unsigned short)si_fd(&s->si[1]));
Willy Tarreau21337822012-04-29 14:11:38 +02002216 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002217 }
2218
2219 if (s->si[0].state == SI_ST_CLO &&
2220 s->si[0].prev_state == SI_ST_EST) {
2221 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
2222 s->uniq_id, s->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002223 (unsigned short)si_fd(&s->si[0]),
2224 (unsigned short)si_fd(&s->si[1]));
Willy Tarreau21337822012-04-29 14:11:38 +02002225 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002226 }
2227 }
2228
2229 if (likely((s->rep->cons->state != SI_ST_CLO) ||
2230 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
2231
2232 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
2233 session_process_counters(s);
2234
Willy Tarreau3cefd522012-08-30 15:49:18 +02002235 if (s->rep->cons->state == SI_ST_EST && s->rep->cons->conn.target.type != TARG_TYPE_APPLET)
Willy Tarreau73b013b2012-05-21 16:31:45 +02002236 si_update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002237
Willy Tarreau3cefd522012-08-30 15:49:18 +02002238 if (s->req->cons->state == SI_ST_EST && s->req->cons->conn.target.type != TARG_TYPE_APPLET)
Willy Tarreau73b013b2012-05-21 16:31:45 +02002239 si_update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002240
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002241 s->req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2242 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 +01002243 s->si[0].prev_state = s->si[0].state;
2244 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002245 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2246 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002247
2248 /* Trick: if a request is being waiting for the server to respond,
2249 * and if we know the server can timeout, we don't want the timeout
2250 * to expire on the client side first, but we're still interested
2251 * in passing data from the client to the server (eg: POST). Thus,
2252 * we can cancel the client's request timeout if the server's
2253 * request timeout is set and the server has not yet sent a response.
2254 */
2255
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002256 if ((s->rep->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01002257 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002258 s->req->flags |= CF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002259 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01002260 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002261
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002262 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002263 * Note that this one may wake the task up again.
2264 */
Willy Tarreau3cefd522012-08-30 15:49:18 +02002265 if (s->req->cons->conn.target.type == TARG_TYPE_APPLET ||
2266 s->rep->cons->conn.target.type == TARG_TYPE_APPLET) {
2267 if (s->req->cons->conn.target.type == TARG_TYPE_APPLET)
2268 s->req->cons->conn.target.ptr.a->fct(s->req->cons);
2269 if (s->rep->cons->conn.target.type == TARG_TYPE_APPLET)
2270 s->rep->cons->conn.target.ptr.a->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002271 if (task_in_rq(t)) {
2272 /* If we woke up, we don't want to requeue the
2273 * task to the wait queue, but rather requeue
2274 * it into the runqueue ASAP.
2275 */
2276 t->expire = TICK_ETERNITY;
2277 return t;
2278 }
2279 }
2280
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002281 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2282 tick_first(s->rep->rex, s->rep->wex));
2283 if (s->req->analysers)
2284 t->expire = tick_first(t->expire, s->req->analyse_exp);
2285
2286 if (s->si[0].exp)
2287 t->expire = tick_first(t->expire, s->si[0].exp);
2288
2289 if (s->si[1].exp)
2290 t->expire = tick_first(t->expire, s->si[1].exp);
2291
2292#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002293 fprintf(stderr,
2294 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2295 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2296 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2297 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 +01002298#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002299
2300#ifdef DEBUG_DEV
2301 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002302 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002303 ABORT_NOW();
2304#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002305 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002306 }
2307
2308 s->fe->feconn--;
2309 if (s->flags & SN_BE_ASSIGNED)
2310 s->be->beconn--;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002311 if (!(s->listener->options & LI_O_UNLIMITED))
2312 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002313 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002314 s->listener->nbconn--;
Willy Tarreau62793712011-07-24 19:23:38 +02002315 if (s->listener->state == LI_FULL)
2316 resume_listener(s->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002317
Willy Tarreau08ceb102011-07-24 22:58:00 +02002318 /* Dequeues all of the listeners waiting for a resource */
2319 if (!LIST_ISEMPTY(&global_listener_queue))
2320 dequeue_all_listeners(&global_listener_queue);
2321
Willy Tarreaub32907b2011-07-25 08:37:44 +02002322 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
2323 (!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 +02002324 dequeue_all_listeners(&s->fe->listener_queue);
2325
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002326 if (unlikely((global.mode & MODE_DEBUG) &&
2327 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2328 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002329 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002330 s->uniq_id, s->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002331 (unsigned short)si_fd(s->req->prod), (unsigned short)si_fd(s->req->cons));
Willy Tarreau21337822012-04-29 14:11:38 +02002332 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002333 }
2334
2335 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2336 session_process_counters(s);
2337
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002338 if (s->txn.status) {
2339 int n;
2340
2341 n = s->txn.status / 100;
2342 if (n < 1 || n > 5)
2343 n = 0;
2344
2345 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002346 s->fe->fe_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002347
Willy Tarreau24657792010-02-26 10:30:28 +01002348 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002349 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002350 s->be->be_counters.p.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002351 }
2352
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002353 /* let's do a final log if we need it */
2354 if (s->logs.logwait &&
2355 !(s->flags & SN_MONITOR) &&
2356 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002357 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002358 }
2359
2360 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002361 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002362 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002363 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002364 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002365}
2366
Willy Tarreau7c669d72008-06-20 15:04:11 +02002367/*
2368 * This function adjusts sess->srv_conn and maintains the previous and new
2369 * server's served session counts. Setting newsrv to NULL is enough to release
2370 * current connection slot. This function also notifies any LB algo which might
2371 * expect to be informed about any change in the number of active sessions on a
2372 * server.
2373 */
2374void sess_change_server(struct session *sess, struct server *newsrv)
2375{
2376 if (sess->srv_conn == newsrv)
2377 return;
2378
2379 if (sess->srv_conn) {
2380 sess->srv_conn->served--;
2381 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2382 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002383 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002384 }
2385
2386 if (newsrv) {
2387 newsrv->served++;
2388 if (newsrv->proxy->lbprm.server_take_conn)
2389 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002390 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002391 }
2392}
2393
Willy Tarreau84455332009-03-15 22:34:05 +01002394/* Handle server-side errors for default protocols. It is called whenever a a
2395 * connection setup is aborted or a request is aborted in queue. It sets the
2396 * session termination flags so that the caller does not have to worry about
2397 * them. It's installed as ->srv_error for the server-side stream_interface.
2398 */
2399void default_srv_error(struct session *s, struct stream_interface *si)
2400{
2401 int err_type = si->err_type;
2402 int err = 0, fin = 0;
2403
2404 if (err_type & SI_ET_QUEUE_ABRT) {
2405 err = SN_ERR_CLICL;
2406 fin = SN_FINST_Q;
2407 }
2408 else if (err_type & SI_ET_CONN_ABRT) {
2409 err = SN_ERR_CLICL;
2410 fin = SN_FINST_C;
2411 }
2412 else if (err_type & SI_ET_QUEUE_TO) {
2413 err = SN_ERR_SRVTO;
2414 fin = SN_FINST_Q;
2415 }
2416 else if (err_type & SI_ET_QUEUE_ERR) {
2417 err = SN_ERR_SRVCL;
2418 fin = SN_FINST_Q;
2419 }
2420 else if (err_type & SI_ET_CONN_TO) {
2421 err = SN_ERR_SRVTO;
2422 fin = SN_FINST_C;
2423 }
2424 else if (err_type & SI_ET_CONN_ERR) {
2425 err = SN_ERR_SRVCL;
2426 fin = SN_FINST_C;
2427 }
2428 else /* SI_ET_CONN_OTHER and others */ {
2429 err = SN_ERR_INTERNAL;
2430 fin = SN_FINST_C;
2431 }
2432
2433 if (!(s->flags & SN_ERR_MASK))
2434 s->flags |= err;
2435 if (!(s->flags & SN_FINST_MASK))
2436 s->flags |= fin;
2437}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002438
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002439/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
2440void session_shutdown(struct session *session, int why)
2441{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002442 if (session->req->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002443 return;
2444
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002445 channel_shutw_now(session->req);
2446 channel_shutr_now(session->rep);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002447 session->task->nice = 1024;
2448 if (!(session->flags & SN_ERR_MASK))
2449 session->flags |= why;
2450 task_wakeup(session->task, TASK_WOKEN_OTHER);
2451}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002452
Willy Tarreau8b22a712010-06-18 17:46:06 +02002453/************************************************************************/
2454/* All supported ACL keywords must be declared here. */
2455/************************************************************************/
2456
Willy Tarreaua5e37562011-12-16 17:06:15 +01002457/* set temp integer to the General Purpose Counter 0 value in the stksess entry <ts> */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002458static int
Willy Tarreau37406352012-04-23 16:16:37 +02002459acl_fetch_get_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002460{
Willy Tarreau37406352012-04-23 16:16:37 +02002461 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002462 smp->type = SMP_T_UINT;
2463 smp->data.uint = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002464 if (ts != NULL) {
2465 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2466 if (!ptr)
2467 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002468 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002469 }
2470 return 1;
2471}
2472
Willy Tarreaua5e37562011-12-16 17:06:15 +01002473/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002474 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002475 */
2476static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002477acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002478 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002479{
Willy Tarreau56123282010-08-06 19:06:56 +02002480 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002481 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002482 return acl_fetch_get_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002483}
2484
Willy Tarreaua5e37562011-12-16 17:06:15 +01002485/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002486 * backend counters.
2487 */
2488static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002489acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002490 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002491{
Willy Tarreau56123282010-08-06 19:06:56 +02002492 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002493 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002494 return acl_fetch_get_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002495}
2496
Willy Tarreaua5e37562011-12-16 17:06:15 +01002497/* set temp integer to the General Purpose Counter 0 value from the session's source
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002498 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002499 * Accepts exactly 1 argument of type table.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002500 */
2501static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002502acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002503 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002504{
2505 struct stktable_key *key;
2506
Willy Tarreau64ee4912012-08-30 22:59:48 +02002507 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002508 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002509 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002510
Willy Tarreau24e32d82012-04-23 23:55:44 +02002511 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002512 return acl_fetch_get_gpc0(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002513}
2514
2515/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002516 * return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002517 */
2518static int
Willy Tarreau37406352012-04-23 16:16:37 +02002519acl_fetch_inc_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002520{
Willy Tarreau37406352012-04-23 16:16:37 +02002521 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002522 smp->type = SMP_T_UINT;
2523 smp->data.uint = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002524 if (ts != NULL) {
2525 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2526 if (!ptr)
2527 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002528 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002529 }
2530 return 1;
2531}
2532
2533/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002534 * frontend counters and return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002535 */
2536static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002537acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002538 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002539{
Willy Tarreau56123282010-08-06 19:06:56 +02002540 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002541 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002542 return acl_fetch_inc_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002543}
2544
2545/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002546 * backend counters and return it into temp integer.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002547 */
2548static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002549acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002550 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002551{
Willy Tarreau56123282010-08-06 19:06:56 +02002552 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002553 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002554 return acl_fetch_inc_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002555}
2556
2557/* Increment the General Purpose Counter 0 value from the session's source
Willy Tarreaua5e37562011-12-16 17:06:15 +01002558 * address in the table pointed to by expr, and return it into temp integer.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002559 * Accepts exactly 1 argument of type table.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002560 */
2561static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002562acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002563 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002564{
2565 struct stktable_key *key;
2566
Willy Tarreau64ee4912012-08-30 22:59:48 +02002567 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002568 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002569 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002570
Willy Tarreau24e32d82012-04-23 23:55:44 +02002571 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002572 return acl_fetch_inc_gpc0(&px->table, smp, stktable_update_key(&px->table, key));
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002573}
2574
Willy Tarreauf73cd112011-08-13 01:45:16 +02002575/* Clear the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002576 * return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002577 */
2578static int
Willy Tarreau37406352012-04-23 16:16:37 +02002579acl_fetch_clr_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002580{
Willy Tarreau37406352012-04-23 16:16:37 +02002581 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002582 smp->type = SMP_T_UINT;
2583 smp->data.uint = 0;
Willy Tarreauf73cd112011-08-13 01:45:16 +02002584 if (ts != NULL) {
2585 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2586 if (!ptr)
2587 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002588 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002589 stktable_data_cast(ptr, gpc0) = 0;
2590 }
2591 return 1;
2592}
2593
2594/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002595 * frontend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002596 */
2597static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002598acl_fetch_sc1_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002599 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002600{
2601 if (!l4->stkctr1_entry)
2602 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002603 return acl_fetch_clr_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002604}
2605
2606/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002607 * backend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002608 */
2609static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002610acl_fetch_sc2_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002611 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002612{
2613 if (!l4->stkctr2_entry)
2614 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002615 return acl_fetch_clr_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002616}
2617
2618/* Clear the General Purpose Counter 0 value from the session's source address
Willy Tarreaua5e37562011-12-16 17:06:15 +01002619 * in the table pointed to by expr, and return its previous value into temp integer.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002620 * Accepts exactly 1 argument of type table.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002621 */
2622static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002623acl_fetch_src_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002624 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002625{
2626 struct stktable_key *key;
2627
Willy Tarreau64ee4912012-08-30 22:59:48 +02002628 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002629 if (!key)
2630 return 0;
2631
Willy Tarreau24e32d82012-04-23 23:55:44 +02002632 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002633 return acl_fetch_clr_gpc0(&px->table, smp, stktable_update_key(&px->table, key));
Willy Tarreauf73cd112011-08-13 01:45:16 +02002634}
2635
Willy Tarreaua5e37562011-12-16 17:06:15 +01002636/* set temp integer to the cumulated number of connections in the stksess entry <ts> */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002637static int
Willy Tarreau37406352012-04-23 16:16:37 +02002638acl_fetch_conn_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002639{
Willy Tarreau37406352012-04-23 16:16:37 +02002640 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002641 smp->type = SMP_T_UINT;
2642 smp->data.uint = 0;
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002643 if (ts != NULL) {
2644 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2645 if (!ptr)
2646 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002647 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002648 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002649 return 1;
2650}
2651
Willy Tarreaua5e37562011-12-16 17:06:15 +01002652/* set temp integer to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002653static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002654acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002655 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002656{
Willy Tarreau56123282010-08-06 19:06:56 +02002657 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002658 return 0;
2659
Willy Tarreau37406352012-04-23 16:16:37 +02002660 return acl_fetch_conn_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002661}
2662
Willy Tarreaua5e37562011-12-16 17:06:15 +01002663/* set temp integer to the cumulated number of connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002664static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002665acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002666 const struct arg *args, struct sample *smp)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002667{
Willy Tarreau56123282010-08-06 19:06:56 +02002668 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002669 return 0;
2670
Willy Tarreau37406352012-04-23 16:16:37 +02002671 return acl_fetch_conn_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002672}
2673
Willy Tarreaua5e37562011-12-16 17:06:15 +01002674/* set temp integer to the cumulated number of connections from the session's source
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002675 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002676 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002677 */
2678static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002679acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002680 const struct arg *args, struct sample *smp)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002681{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002682 struct stktable_key *key;
2683
Willy Tarreau64ee4912012-08-30 22:59:48 +02002684 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002685 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002686 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002687
Willy Tarreau24e32d82012-04-23 23:55:44 +02002688 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002689 return acl_fetch_conn_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002690}
2691
Willy Tarreaua5e37562011-12-16 17:06:15 +01002692/* set temp integer to the connection rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002693static int
Willy Tarreau37406352012-04-23 16:16:37 +02002694acl_fetch_conn_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002695{
Willy Tarreau37406352012-04-23 16:16:37 +02002696 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002697 smp->type = SMP_T_UINT;
2698 smp->data.uint = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002699 if (ts != NULL) {
2700 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2701 if (!ptr)
2702 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002703 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002704 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2705 }
2706 return 1;
2707}
2708
Willy Tarreaua5e37562011-12-16 17:06:15 +01002709/* set temp integer to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002710 * the configured period.
2711 */
2712static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002713acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002714 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002715{
Willy Tarreau56123282010-08-06 19:06:56 +02002716 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002717 return 0;
2718
Willy Tarreau37406352012-04-23 16:16:37 +02002719 return acl_fetch_conn_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002720}
2721
Willy Tarreaua5e37562011-12-16 17:06:15 +01002722/* set temp integer to the connection rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002723 * the configured period.
2724 */
2725static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002726acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002727 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002728{
Willy Tarreau56123282010-08-06 19:06:56 +02002729 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002730 return 0;
2731
Willy Tarreau37406352012-04-23 16:16:37 +02002732 return acl_fetch_conn_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002733}
2734
Willy Tarreaua5e37562011-12-16 17:06:15 +01002735/* set temp integer to the connection rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002736 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002737 * Accepts exactly 1 argument of type table.
Willy Tarreau91c43d72010-06-20 11:19:22 +02002738 */
2739static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002740acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002741 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002742{
2743 struct stktable_key *key;
2744
Willy Tarreau64ee4912012-08-30 22:59:48 +02002745 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002746 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002747 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002748
Willy Tarreau24e32d82012-04-23 23:55:44 +02002749 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002750 return acl_fetch_conn_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau91c43d72010-06-20 11:19:22 +02002751}
2752
Willy Tarreaua5e37562011-12-16 17:06:15 +01002753/* set temp integer to the number of connections from the session's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002754 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002755 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002756 */
2757static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002758acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002759 const struct arg *args, struct sample *smp)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002760{
2761 struct stksess *ts;
2762 struct stktable_key *key;
2763 void *ptr;
2764
Willy Tarreau64ee4912012-08-30 22:59:48 +02002765 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002766 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002767 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002768
Willy Tarreau24e32d82012-04-23 23:55:44 +02002769 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002770
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002771 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2772 /* entry does not exist and could not be created */
2773 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002774
2775 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2776 if (!ptr)
2777 return 0; /* parameter not stored in this table */
2778
Willy Tarreauf853c462012-04-23 18:53:56 +02002779 smp->type = SMP_T_UINT;
2780 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002781 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002782 return 1;
2783}
2784
Willy Tarreaua5e37562011-12-16 17:06:15 +01002785/* set temp integer to the number of concurrent connections in the stksess entry <ts> */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002786static int
Willy Tarreau37406352012-04-23 16:16:37 +02002787acl_fetch_conn_cur(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002788{
Willy Tarreau37406352012-04-23 16:16:37 +02002789 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002790 smp->type = SMP_T_UINT;
2791 smp->data.uint = 0;
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002792
2793 if (ts != NULL) {
2794 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2795 if (!ptr)
2796 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002797 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002798 }
2799 return 1;
2800}
2801
Willy Tarreaua5e37562011-12-16 17:06:15 +01002802/* set temp integer to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002803static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002804acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002805 const struct arg *args, struct sample *smp)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002806{
Willy Tarreau56123282010-08-06 19:06:56 +02002807 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002808 return 0;
2809
Willy Tarreau37406352012-04-23 16:16:37 +02002810 return acl_fetch_conn_cur(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002811}
2812
Willy Tarreaua5e37562011-12-16 17:06:15 +01002813/* set temp integer to the number of concurrent connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002814static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002815acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002816 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002817{
Willy Tarreau56123282010-08-06 19:06:56 +02002818 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002819 return 0;
2820
Willy Tarreau37406352012-04-23 16:16:37 +02002821 return acl_fetch_conn_cur(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002822}
2823
Willy Tarreaua5e37562011-12-16 17:06:15 +01002824/* set temp integer to the number of concurrent connections from the session's source
Willy Tarreau38285c12010-06-18 16:35:43 +02002825 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002826 * Accepts exactly 1 argument of type table.
Willy Tarreau38285c12010-06-18 16:35:43 +02002827 */
2828static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002829acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002830 const struct arg *args, struct sample *smp)
Willy Tarreau38285c12010-06-18 16:35:43 +02002831{
Willy Tarreau38285c12010-06-18 16:35:43 +02002832 struct stktable_key *key;
2833
Willy Tarreau64ee4912012-08-30 22:59:48 +02002834 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau38285c12010-06-18 16:35:43 +02002835 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002836 return 0;
Willy Tarreau38285c12010-06-18 16:35:43 +02002837
Willy Tarreau24e32d82012-04-23 23:55:44 +02002838 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002839 return acl_fetch_conn_cur(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002840}
2841
Willy Tarreaua5e37562011-12-16 17:06:15 +01002842/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002843static int
Willy Tarreau37406352012-04-23 16:16:37 +02002844acl_fetch_sess_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002845{
Willy Tarreau37406352012-04-23 16:16:37 +02002846 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002847 smp->type = SMP_T_UINT;
2848 smp->data.uint = 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002849 if (ts != NULL) {
2850 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2851 if (!ptr)
2852 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002853 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002854 }
2855 return 1;
2856}
2857
Willy Tarreaua5e37562011-12-16 17:06:15 +01002858/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002859static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002860acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002861 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002862{
Willy Tarreau56123282010-08-06 19:06:56 +02002863 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002864 return 0;
2865
Willy Tarreau37406352012-04-23 16:16:37 +02002866 return acl_fetch_sess_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002867}
2868
Willy Tarreaua5e37562011-12-16 17:06:15 +01002869/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002870static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002871acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002872 const struct arg *args, struct sample *smp)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002873{
Willy Tarreau56123282010-08-06 19:06:56 +02002874 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002875 return 0;
2876
Willy Tarreau37406352012-04-23 16:16:37 +02002877 return acl_fetch_sess_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002878}
2879
Willy Tarreaua5e37562011-12-16 17:06:15 +01002880/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002881 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002882 * Accepts exactly 1 argument of type table.
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002883 */
2884static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002885acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002886 const struct arg *args, struct sample *smp)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002887{
2888 struct stktable_key *key;
2889
Willy Tarreau64ee4912012-08-30 22:59:48 +02002890 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002891 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002892 return 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002893
Willy Tarreau24e32d82012-04-23 23:55:44 +02002894 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002895 return acl_fetch_sess_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002896}
2897
Willy Tarreaua5e37562011-12-16 17:06:15 +01002898/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002899static int
Willy Tarreau37406352012-04-23 16:16:37 +02002900acl_fetch_sess_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002901{
Willy Tarreau37406352012-04-23 16:16:37 +02002902 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002903 smp->type = SMP_T_UINT;
2904 smp->data.uint = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002905 if (ts != NULL) {
2906 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2907 if (!ptr)
2908 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002909 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002910 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2911 }
2912 return 1;
2913}
2914
Willy Tarreaua5e37562011-12-16 17:06:15 +01002915/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002916 * the configured period.
2917 */
2918static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002919acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002920 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002921{
Willy Tarreau56123282010-08-06 19:06:56 +02002922 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002923 return 0;
2924
Willy Tarreau37406352012-04-23 16:16:37 +02002925 return acl_fetch_sess_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002926}
2927
Willy Tarreaua5e37562011-12-16 17:06:15 +01002928/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002929 * the configured period.
2930 */
2931static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002932acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002933 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002934{
Willy Tarreau56123282010-08-06 19:06:56 +02002935 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002936 return 0;
2937
Willy Tarreau37406352012-04-23 16:16:37 +02002938 return acl_fetch_sess_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002939}
2940
Willy Tarreaua5e37562011-12-16 17:06:15 +01002941/* set temp integer to the session rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002942 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002943 * Accepts exactly 1 argument of type table.
Willy Tarreau91c43d72010-06-20 11:19:22 +02002944 */
2945static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002946acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002947 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002948{
2949 struct stktable_key *key;
2950
Willy Tarreau64ee4912012-08-30 22:59:48 +02002951 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002952 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002953 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002954
Willy Tarreau24e32d82012-04-23 23:55:44 +02002955 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002956 return acl_fetch_sess_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau91c43d72010-06-20 11:19:22 +02002957}
2958
Willy Tarreaua5e37562011-12-16 17:06:15 +01002959/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002960static int
Willy Tarreau37406352012-04-23 16:16:37 +02002961acl_fetch_http_req_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002962{
Willy Tarreau37406352012-04-23 16:16:37 +02002963 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002964 smp->type = SMP_T_UINT;
2965 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02002966 if (ts != NULL) {
2967 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2968 if (!ptr)
2969 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002970 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002971 }
2972 return 1;
2973}
2974
Willy Tarreaua5e37562011-12-16 17:06:15 +01002975/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002976static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002977acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002978 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002979{
Willy Tarreau56123282010-08-06 19:06:56 +02002980 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002981 return 0;
2982
Willy Tarreau37406352012-04-23 16:16:37 +02002983 return acl_fetch_http_req_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002984}
2985
Willy Tarreaua5e37562011-12-16 17:06:15 +01002986/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002987static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002988acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002989 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002990{
Willy Tarreau56123282010-08-06 19:06:56 +02002991 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002992 return 0;
2993
Willy Tarreau37406352012-04-23 16:16:37 +02002994 return acl_fetch_http_req_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002995}
2996
Willy Tarreaua5e37562011-12-16 17:06:15 +01002997/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02002998 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002999 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003000 */
3001static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003002acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003003 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003004{
3005 struct stktable_key *key;
3006
Willy Tarreau64ee4912012-08-30 22:59:48 +02003007 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003008 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003009 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003010
Willy Tarreau24e32d82012-04-23 23:55:44 +02003011 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003012 return acl_fetch_http_req_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003013}
3014
Willy Tarreaua5e37562011-12-16 17:06:15 +01003015/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003016static int
Willy Tarreau37406352012-04-23 16:16:37 +02003017acl_fetch_http_req_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003018{
Willy Tarreau37406352012-04-23 16:16:37 +02003019 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003020 smp->type = SMP_T_UINT;
3021 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003022 if (ts != NULL) {
3023 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
3024 if (!ptr)
3025 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003026 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02003027 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
3028 }
3029 return 1;
3030}
3031
Willy Tarreaua5e37562011-12-16 17:06:15 +01003032/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02003033 * the configured period.
3034 */
3035static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003036acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003037 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003038{
Willy Tarreau56123282010-08-06 19:06:56 +02003039 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003040 return 0;
3041
Willy Tarreau37406352012-04-23 16:16:37 +02003042 return acl_fetch_http_req_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003043}
3044
Willy Tarreaua5e37562011-12-16 17:06:15 +01003045/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003046 * the configured period.
3047 */
3048static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003049acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003050 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003051{
Willy Tarreau56123282010-08-06 19:06:56 +02003052 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003053 return 0;
3054
Willy Tarreau37406352012-04-23 16:16:37 +02003055 return acl_fetch_http_req_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003056}
3057
Willy Tarreaua5e37562011-12-16 17:06:15 +01003058/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003059 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003060 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003061 */
3062static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003063acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003064 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003065{
3066 struct stktable_key *key;
3067
Willy Tarreau64ee4912012-08-30 22:59:48 +02003068 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003069 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003070 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003071
Willy Tarreau24e32d82012-04-23 23:55:44 +02003072 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003073 return acl_fetch_http_req_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003074}
3075
Willy Tarreaua5e37562011-12-16 17:06:15 +01003076/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003077static int
Willy Tarreau37406352012-04-23 16:16:37 +02003078acl_fetch_http_err_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003079{
Willy Tarreau37406352012-04-23 16:16:37 +02003080 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003081 smp->type = SMP_T_UINT;
3082 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003083 if (ts != NULL) {
3084 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
3085 if (!ptr)
3086 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003087 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003088 }
3089 return 1;
3090}
3091
Willy Tarreaua5e37562011-12-16 17:06:15 +01003092/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003093static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003094acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003095 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003096{
Willy Tarreau56123282010-08-06 19:06:56 +02003097 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003098 return 0;
3099
Willy Tarreau37406352012-04-23 16:16:37 +02003100 return acl_fetch_http_err_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003101}
3102
Willy Tarreaua5e37562011-12-16 17:06:15 +01003103/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003104static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003105acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003106 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003107{
Willy Tarreau56123282010-08-06 19:06:56 +02003108 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003109 return 0;
3110
Willy Tarreau37406352012-04-23 16:16:37 +02003111 return acl_fetch_http_err_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003112}
3113
Willy Tarreaua5e37562011-12-16 17:06:15 +01003114/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02003115 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003116 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003117 */
3118static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003119acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003120 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003121{
3122 struct stktable_key *key;
3123
Willy Tarreau64ee4912012-08-30 22:59:48 +02003124 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003125 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003126 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003127
Willy Tarreau24e32d82012-04-23 23:55:44 +02003128 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003129 return acl_fetch_http_err_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003130}
3131
Willy Tarreaua5e37562011-12-16 17:06:15 +01003132/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003133static int
Willy Tarreau37406352012-04-23 16:16:37 +02003134acl_fetch_http_err_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003135{
Willy Tarreau37406352012-04-23 16:16:37 +02003136 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003137 smp->type = SMP_T_UINT;
3138 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003139 if (ts != NULL) {
3140 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
3141 if (!ptr)
3142 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003143 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02003144 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
3145 }
3146 return 1;
3147}
3148
Willy Tarreaua5e37562011-12-16 17:06:15 +01003149/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02003150 * the configured period.
3151 */
3152static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003153acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003154 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003155{
Willy Tarreau56123282010-08-06 19:06:56 +02003156 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003157 return 0;
3158
Willy Tarreau37406352012-04-23 16:16:37 +02003159 return acl_fetch_http_err_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003160}
3161
Willy Tarreaua5e37562011-12-16 17:06:15 +01003162/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003163 * the configured period.
3164 */
3165static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003166acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003167 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003168{
Willy Tarreau56123282010-08-06 19:06:56 +02003169 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003170 return 0;
3171
Willy Tarreau37406352012-04-23 16:16:37 +02003172 return acl_fetch_http_err_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003173}
3174
Willy Tarreaua5e37562011-12-16 17:06:15 +01003175/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003176 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003177 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003178 */
3179static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003180acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003181 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003182{
3183 struct stktable_key *key;
3184
Willy Tarreau64ee4912012-08-30 22:59:48 +02003185 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003186 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003187 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003188
Willy Tarreau24e32d82012-04-23 23:55:44 +02003189 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003190 return acl_fetch_http_err_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003191}
3192
Willy Tarreaua5e37562011-12-16 17:06:15 +01003193/* set temp integer to the number of kbytes received from clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003194static int
Willy Tarreau37406352012-04-23 16:16:37 +02003195acl_fetch_kbytes_in(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003196{
Willy Tarreau37406352012-04-23 16:16:37 +02003197 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003198 smp->type = SMP_T_UINT;
3199 smp->data.uint = 0;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003200
3201 if (ts != NULL) {
3202 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
3203 if (!ptr)
3204 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003205 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003206 }
3207 return 1;
3208}
3209
Willy Tarreaua5e37562011-12-16 17:06:15 +01003210/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003211 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003212 */
3213static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003214acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003215 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003216{
Willy Tarreau56123282010-08-06 19:06:56 +02003217 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003218 return 0;
3219
Willy Tarreau37406352012-04-23 16:16:37 +02003220 return acl_fetch_kbytes_in(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003221}
3222
Willy Tarreaua5e37562011-12-16 17:06:15 +01003223/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003224 * session's tracked BE counters.
3225 */
3226static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003227acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003228 const struct arg *args, struct sample *smp)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003229{
Willy Tarreau56123282010-08-06 19:06:56 +02003230 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003231 return 0;
3232
Willy Tarreau37406352012-04-23 16:16:37 +02003233 return acl_fetch_kbytes_in(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003234}
3235
Willy Tarreaua5e37562011-12-16 17:06:15 +01003236/* set temp integer to the number of kbytes received from the session's source
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003237 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003238 * Accepts exactly 1 argument of type table.
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003239 */
3240static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003241acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003242 const struct arg *args, struct sample *smp)
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003243{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003244 struct stktable_key *key;
3245
Willy Tarreau64ee4912012-08-30 22:59:48 +02003246 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003247 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003248 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003249
Willy Tarreau24e32d82012-04-23 23:55:44 +02003250 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003251 return acl_fetch_kbytes_in(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003252}
3253
Willy Tarreaua5e37562011-12-16 17:06:15 +01003254/* set temp integer to the bytes rate from clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003255 * configured period.
3256 */
3257static int
Willy Tarreau37406352012-04-23 16:16:37 +02003258acl_fetch_bytes_in_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003259{
Willy Tarreau37406352012-04-23 16:16:37 +02003260 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003261 smp->type = SMP_T_UINT;
3262 smp->data.uint = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003263 if (ts != NULL) {
3264 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
3265 if (!ptr)
3266 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003267 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003268 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
3269 }
3270 return 1;
3271}
3272
Willy Tarreaua5e37562011-12-16 17:06:15 +01003273/* set temp integer to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003274 * counters over the configured period.
3275 */
3276static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003277acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003278 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003279{
Willy Tarreau56123282010-08-06 19:06:56 +02003280 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003281 return 0;
3282
Willy Tarreau37406352012-04-23 16:16:37 +02003283 return acl_fetch_bytes_in_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003284}
3285
Willy Tarreaua5e37562011-12-16 17:06:15 +01003286/* set temp integer to the bytes rate from clients from the session's tracked BE
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003287 * counters over the configured period.
3288 */
3289static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003290acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003291 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003292{
Willy Tarreau56123282010-08-06 19:06:56 +02003293 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003294 return 0;
3295
Willy Tarreau37406352012-04-23 16:16:37 +02003296 return acl_fetch_bytes_in_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003297}
3298
Willy Tarreaua5e37562011-12-16 17:06:15 +01003299/* set temp integer to the bytes rate from clients from the session's source address
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003300 * in the table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003301 * Accepts exactly 1 argument of type table.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003302 */
3303static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003304acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003305 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003306{
3307 struct stktable_key *key;
3308
Willy Tarreau64ee4912012-08-30 22:59:48 +02003309 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003310 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003311 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003312
Willy Tarreau24e32d82012-04-23 23:55:44 +02003313 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003314 return acl_fetch_bytes_in_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003315}
3316
Willy Tarreaua5e37562011-12-16 17:06:15 +01003317/* set temp integer to the number of kbytes sent to clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003318static int
Willy Tarreau37406352012-04-23 16:16:37 +02003319acl_fetch_kbytes_out(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003320{
Willy Tarreau37406352012-04-23 16:16:37 +02003321 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003322 smp->type = SMP_T_UINT;
3323 smp->data.uint = 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003324
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003325 if (ts != NULL) {
3326 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003327 if (!ptr)
3328 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003329 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003330 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003331 return 1;
3332}
3333
Willy Tarreaua5e37562011-12-16 17:06:15 +01003334/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003335 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003336 */
3337static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003338acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003339 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003340{
Willy Tarreau56123282010-08-06 19:06:56 +02003341 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003342 return 0;
3343
Willy Tarreau37406352012-04-23 16:16:37 +02003344 return acl_fetch_kbytes_out(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003345}
3346
Willy Tarreaua5e37562011-12-16 17:06:15 +01003347/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003348 * tracked BE counters.
3349 */
3350static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003351acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003352 const struct arg *args, struct sample *smp)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003353{
Willy Tarreau56123282010-08-06 19:06:56 +02003354 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003355 return 0;
3356
Willy Tarreau37406352012-04-23 16:16:37 +02003357 return acl_fetch_kbytes_out(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003358}
3359
Willy Tarreaua5e37562011-12-16 17:06:15 +01003360/* set temp integer to the number of kbytes sent to the session's source address in
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003361 * the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003362 * Accepts exactly 1 argument of type table.
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003363 */
3364static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003365acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003366 const struct arg *args, struct sample *smp)
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003367{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003368 struct stktable_key *key;
3369
Willy Tarreau64ee4912012-08-30 22:59:48 +02003370 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003371 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003372 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003373
Willy Tarreau24e32d82012-04-23 23:55:44 +02003374 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003375 return acl_fetch_kbytes_out(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003376}
3377
Willy Tarreaua5e37562011-12-16 17:06:15 +01003378/* set temp integer to the bytes rate to clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003379 * configured period.
3380 */
3381static int
Willy Tarreau37406352012-04-23 16:16:37 +02003382acl_fetch_bytes_out_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003383{
Willy Tarreau37406352012-04-23 16:16:37 +02003384 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003385 smp->type = SMP_T_UINT;
3386 smp->data.uint = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003387 if (ts != NULL) {
3388 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3389 if (!ptr)
3390 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003391 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003392 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3393 }
3394 return 1;
3395}
3396
Willy Tarreaua5e37562011-12-16 17:06:15 +01003397/* set temp integer to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003398 * over the configured period.
3399 */
3400static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003401acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003402 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003403{
Willy Tarreau56123282010-08-06 19:06:56 +02003404 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003405 return 0;
3406
Willy Tarreau37406352012-04-23 16:16:37 +02003407 return acl_fetch_bytes_out_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003408}
3409
Willy Tarreaua5e37562011-12-16 17:06:15 +01003410/* set temp integer to the bytes rate to clients from the session's tracked BE counters
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003411 * over the configured period.
3412 */
3413static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003414acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003415 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003416{
Willy Tarreau56123282010-08-06 19:06:56 +02003417 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003418 return 0;
3419
Willy Tarreau37406352012-04-23 16:16:37 +02003420 return acl_fetch_bytes_out_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003421}
3422
Willy Tarreaua5e37562011-12-16 17:06:15 +01003423/* set temp integer to the bytes rate to client from the session's source address in
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003424 * the table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003425 * Accepts exactly 1 argument of type table.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003426 */
3427static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003428acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003429 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003430{
3431 struct stktable_key *key;
3432
Willy Tarreau64ee4912012-08-30 22:59:48 +02003433 key = addr_to_stktable_key(&l4->si[0].conn.addr.from);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003434 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003435 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003436
Willy Tarreau24e32d82012-04-23 23:55:44 +02003437 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003438 return acl_fetch_bytes_out_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003439}
3440
Willy Tarreau34db1082012-04-19 17:16:54 +02003441/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003442 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003443 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003444static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003445acl_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003446 const struct arg *args, struct sample *smp)
Willy Tarreauc735a072011-03-29 00:57:02 +02003447{
Willy Tarreau37406352012-04-23 16:16:37 +02003448 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003449 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003450 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003451 return 1;
3452}
3453
Willy Tarreau34db1082012-04-19 17:16:54 +02003454/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003455 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003456 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003457static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003458acl_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003459 const struct arg *args, struct sample *smp)
Willy Tarreauc735a072011-03-29 00:57:02 +02003460{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003461 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003462 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003463 smp->type = SMP_T_UINT;
3464 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003465 return 1;
3466}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003467
Willy Tarreau61612d42012-04-19 18:42:05 +02003468/* Note: must not be declared <const> as its list will be overwritten.
3469 * Please take care of keeping this list alphabetically sorted.
3470 */
Willy Tarreau8b22a712010-06-18 17:46:06 +02003471static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02003472 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3473 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3474 { "sc1_clr_gpc0", acl_parse_int, acl_fetch_sc1_clr_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3475 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3476 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING, 0 },
3477 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3478 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3479 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3480 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3481 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3482 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3483 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3484 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3485 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3486 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3487 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3488 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3489 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3490 { "sc2_clr_gpc0", acl_parse_int, acl_fetch_sc2_clr_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3491 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3492 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING, 0 },
3493 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3494 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3495 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3496 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3497 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3498 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3499 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3500 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3501 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3502 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3503 { "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 +02003504 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3505 { "src_bytes_out_rate", acl_parse_int, acl_fetch_src_bytes_out_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3506 { "src_clr_gpc0", acl_parse_int, acl_fetch_src_clr_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3507 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3508 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3509 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3510 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3511 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3512 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3513 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3514 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3515 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3516 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3517 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3518 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3519 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3520 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3521 { "table_avl", acl_parse_int, acl_fetch_table_avl, acl_match_int, ACL_USE_NOTHING, ARG1(1,TAB) },
3522 { "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 +02003523 { NULL, NULL, NULL, NULL },
3524}};
3525
3526
Willy Tarreau56123282010-08-06 19:06:56 +02003527/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003528 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3529 * <prm> struct is fed with the table name if any. If unspecified, the caller
3530 * will assume that the current proxy's table is used.
3531 */
3532int parse_track_counters(char **args, int *arg,
3533 int section_type, struct proxy *curpx,
3534 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003535 struct proxy *defpx, char **err)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003536{
Willy Tarreau12785782012-04-27 21:37:17 +02003537 int sample_type = 0;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003538
Willy Tarreau56123282010-08-06 19:06:56 +02003539 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003540 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003541 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003542 */
3543 while (args[*arg]) {
3544 if (strcmp(args[*arg], "src") == 0) {
3545 prm->type = STKTABLE_TYPE_IP;
Willy Tarreau12785782012-04-27 21:37:17 +02003546 sample_type = 1;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003547 }
3548 else if (strcmp(args[*arg], "table") == 0) {
3549 if (!args[*arg + 1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003550 memprintf(err, "missing table name");
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003551 return -1;
3552 }
3553 /* we copy the table name for now, it will be resolved later */
3554 prm->table.n = strdup(args[*arg + 1]);
3555 (*arg)++;
3556 }
3557 else {
3558 /* unhandled keywords are handled by the caller */
3559 break;
3560 }
3561 (*arg)++;
3562 }
3563
Willy Tarreau12785782012-04-27 21:37:17 +02003564 if (!sample_type) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003565 memprintf(err,
3566 "tracking key not specified (found %s, only 'src' is supported)",
3567 quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003568 return -1;
3569 }
3570
3571 return 0;
3572}
3573
Willy Tarreau8b22a712010-06-18 17:46:06 +02003574__attribute__((constructor))
3575static void __session_init(void)
3576{
3577 acl_register_keywords(&acl_kws);
3578}
3579
Willy Tarreaubaaee002006-06-26 02:48:02 +02003580/*
3581 * Local variables:
3582 * c-indent-level: 8
3583 * c-basic-offset: 8
3584 * End:
3585 */