blob: 1bcd2eb90b10f54669cc53e7bdcf8f6e7aaaa28c [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002 * Stream 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 Tarreau9b28e032012-10-12 23:49:43 +020018#include <common/buffer.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020019#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010023#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020025#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020026#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010027#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020028#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010029#include <proto/checks.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020030#include <proto/connection.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020031#include <proto/dumpstats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020032#include <proto/fd.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020033#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020034#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010035#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010036#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020037#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020038#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020039#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020040#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020041#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010042#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010043#include <proto/proto_http.h>
44#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020045#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020048#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010049#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010050#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010051#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052
Willy Tarreau87b09662015-04-03 00:22:06 +020053struct pool_head *pool2_stream;
54struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
Willy Tarreau87b09662015-04-03 00:22:06 +020056/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010057struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
58
Willy Tarreau9903f0e2015-04-04 18:50:31 +020059/* This function is called from the session handler which detects the end of
Willy Tarreau87b09662015-04-03 00:22:06 +020060 * handshake, in order to complete initialization of a valid stream. It must
Willy Tarreau9903f0e2015-04-04 18:50:31 +020061 * be called with an embryonic session. It returns a positive value upon
Willy Tarreau2542b532012-08-31 16:01:23 +020062 * success, 0 if the connection can be ignored, or a negative value upon
63 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreaub4f98092014-05-08 21:06:11 +020064 * The client-side end point is assumed to be a connection, whose pointer is
Willy Tarreau1f52bb22015-04-04 14:28:46 +020065 * taken from sess->origin which is assumed to be valid.
Willy Tarreau2542b532012-08-31 16:01:23 +020066 */
Willy Tarreau9903f0e2015-04-04 18:50:31 +020067int stream_accept_session(struct session *sess, struct task *t)
Willy Tarreau2542b532012-08-31 16:01:23 +020068{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020069 struct stream *s;
Willy Tarreaufb0afa72015-04-03 14:46:27 +020070 struct listener *l = sess->listener;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020071 struct proxy *p = sess->fe;
Willy Tarreauf2b98742015-04-05 01:30:42 +020072 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau470280e2015-04-05 01:33:13 +020073 struct appctx *appctx = objt_appctx(sess->origin);
Willy Tarreau2542b532012-08-31 16:01:23 +020074 int ret;
Willy Tarreau20d46a52012-12-09 15:55:40 +010075 int i;
Willy Tarreau2542b532012-08-31 16:01:23 +020076
77 ret = -1; /* assume unrecoverable error by default */
78
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020079 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
80 goto out_return;
81
82 /* minimum stream initialization required for an embryonic stream is
83 * fairly low. We need very little to execute L4 ACLs, then we need a
84 * task to make the client-side connection live on its own.
85 * - flags
86 * - stick-entry tracking
87 */
88 s->flags = 0;
89 s->logs.logwait = p->to_log;
90 s->logs.level = 0;
91
92 /* Initialise the current rule list pointer to NULL. We are sure that
93 * any rulelist match the NULL pointer.
94 */
95 s->current_rule_list = NULL;
96
97 memset(s->stkctr, 0, sizeof(s->stkctr));
98
99 s->sess = sess;
100 s->si[0].flags = SI_FL_NONE;
101 s->si[1].flags = SI_FL_ISBACK;
102
103 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
104 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
105 s->uniq_id = global.req_count++;
106
Willy Tarreau87b09662015-04-03 00:22:06 +0200107 /* OK, we're keeping the stream, so let's properly initialize the stream */
108 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200109 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100110 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200111
Willy Tarreaue7dff022015-04-03 01:14:29 +0200112 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200113 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200114
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200115 s->task = t;
Willy Tarreau2542b532012-08-31 16:01:23 +0200116 t->process = l->handler;
117 t->context = s;
118 t->expire = TICK_ETERNITY;
119
Willy Tarreau87b09662015-04-03 00:22:06 +0200120 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200121 * This changes later when switching rules are executed or
122 * when the default backend is assigned.
123 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200124 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200125 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100126 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200127 s->req_cap = NULL;
128 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200129
Willy Tarreau87b09662015-04-03 00:22:06 +0200130 /* Let's count a stream now */
Willy Tarreaub36b4242010-06-04 20:59:39 +0200131 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200132
Willy Tarreaub4c84932013-07-23 19:15:30 +0200133 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200134 void *ptr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200135 struct stkctr *stkctr = &sess->stkctr[i];
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200136
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200137 if (!stkctr_entry(stkctr))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100138 continue;
139
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200140 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200141 if (ptr)
142 stktable_data_cast(ptr, sess_cnt)++;
143
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200144 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200145 if (ptr)
146 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200147 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200148 }
149
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200150 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100151 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200152 si_set_state(&s->si[0], SI_ST_EST);
153
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200154 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200155 if (conn)
156 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200157 else if (appctx)
158 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200159
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200160 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200161 s->si[0].flags |= SI_FL_INDEP_STR;
162
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200163 /* pre-initialize the other side's stream interface to an INIT state. The
164 * callbacks will be initialized before attempting to connect.
165 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100166 si_reset(&s->si[1]);
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200167 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200168
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200169 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200170 s->si[1].flags |= SI_FL_INDEP_STR;
171
Willy Tarreau87b09662015-04-03 00:22:06 +0200172 stream_init_srv_conn(s);
Willy Tarreau10b688f2015-03-13 16:43:12 +0100173 s->target = l->default_target; /* used by peers and CLI */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200174 s->pend_pos = NULL;
175
176 /* init store persistence */
177 s->store_count = 0;
178
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100179 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100180 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200181
182 /* activate default analysers enabled for this listener */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100183 s->req.analysers = l->analysers;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200184
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100185 s->req.wto = TICK_ETERNITY;
186 s->req.rto = TICK_ETERNITY;
187 s->req.rex = TICK_ETERNITY;
188 s->req.wex = TICK_ETERNITY;
189 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200190
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100191 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100192 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100193 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200194
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200195 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100196 s->req.flags |= CF_NEVER_WAIT;
197 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200198 }
199
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100200 s->res.rto = TICK_ETERNITY;
201 s->res.wto = TICK_ETERNITY;
202 s->res.rex = TICK_ETERNITY;
203 s->res.wex = TICK_ETERNITY;
204 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200205
Willy Tarreaueee5b512015-04-03 23:46:31 +0200206 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100207
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100208 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100209
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200210 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200211 if (conn)
212 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200213 else if (appctx)
214 s->si[0].flags |= SI_FL_WAIT_DATA;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200215
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200216 if (conn && p->accept && p->accept(s) < 0)
217 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200218
Willy Tarreauf2b98742015-04-05 01:30:42 +0200219 if (conn) {
220 /* if logs require transport layer information, note it on the connection */
221 if (s->logs.logwait & LW_XPRT)
222 conn->flags |= CO_FL_XPRT_TRACKED;
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200223
Willy Tarreauf2b98742015-04-05 01:30:42 +0200224 /* we want the connection handler to notify the stream interface about updates. */
225 conn->flags |= CO_FL_WAKE_DATA;
226 }
Willy Tarreau2542b532012-08-31 16:01:23 +0200227
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200228 /* it is important not to call the wakeup function directly but to
229 * pass through task_wakeup(), because this one knows how to apply
230 * priorities to tasks.
231 */
232 task_wakeup(t, TASK_WOKEN_INIT);
233 return 1;
234
235 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200236 out_fail_accept:
Willy Tarreau3b246412014-11-25 17:10:33 +0100237 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200238 pool_free2(pool2_stream, s);
239 out_return:
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100240 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200241}
242
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200244 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200246static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200247{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200248 struct session *sess = strm_sess(s);
249 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100250 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200251 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200252 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100253
Willy Tarreaubaaee002006-06-26 02:48:02 +0200254 if (s->pend_pos)
255 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100256
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100257 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200258 if (s->flags & SF_CURR_SESS) {
259 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100260 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100261 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100262 if (may_dequeue_tasks(objt_server(s->target), s->be))
263 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100264 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100265
Willy Tarreau7c669d72008-06-20 15:04:11 +0200266 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200267 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200268 * it should normally be only the same as the one above,
269 * so this should not happen in fact.
270 */
271 sess_change_server(s, NULL);
272 }
273
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100274 if (s->req.pipe)
275 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100276
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100277 if (s->res.pipe)
278 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100279
Willy Tarreaubf883e02014-11-25 21:10:35 +0100280 /* We may still be present in the buffer wait queue */
281 if (!LIST_ISEMPTY(&s->buffer_wait)) {
282 LIST_DEL(&s->buffer_wait);
283 LIST_INIT(&s->buffer_wait);
284 }
285
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100286 b_drop(&s->req.buf);
287 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100288 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200289 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200290
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100291 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200292 if (s->txn)
293 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100294
Willy Tarreau1e954912012-10-12 17:50:05 +0200295 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200296 if (cli_conn)
297 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200298
Willy Tarreaua4cda672010-06-06 18:28:49 +0200299 for (i = 0; i < s->store_count; i++) {
300 if (!s->store[i].ts)
301 continue;
302 stksess_free(s->store[i].table, s->store[i].ts);
303 s->store[i].ts = NULL;
304 }
305
Willy Tarreaueee5b512015-04-03 23:46:31 +0200306 if (s->txn) {
307 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
308 pool_free2(pool2_http_txn, s->txn);
309 s->txn = NULL;
310 }
311
Willy Tarreau92fb9832007-10-16 17:34:28 +0200312 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200313 pool_free2(fe->rsp_cap_pool, s->res_cap);
314 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200315 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100316
Willy Tarreau87b09662015-04-03 00:22:06 +0200317 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200318
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100319 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100320 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200321 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100322 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100323 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100324 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200325 if (s->list.n != &streams)
326 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100327 bref->ref = s->list.n;
328 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100329 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200330 si_release_endpoint(&s->si[1]);
331 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200332
333 /* FIXME: for now we have a 1:1 relation between stream and session so
334 * the stream must free the session.
335 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200336 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200337 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200338
339 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200340 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200341 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200342 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200343 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200344 pool_flush2(pool2_requri);
345 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200346 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200347 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100348 pool_flush2(pool2_connection);
349 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200350 pool_flush2(fe->req_cap_pool);
351 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200352 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200353}
354
Willy Tarreau78955f42014-12-28 13:09:02 +0100355/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
356 * that it's not the last available buffer or it's the response buffer. Unless
357 * the buffer is the response buffer, an extra control is made so that we always
358 * keep <tune.buffers.reserved> buffers available after this allocation. To be
359 * called at the beginning of recv() callbacks to ensure that the required
360 * buffers are properly allocated. Returns 0 in case of failure, non-zero
361 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100362 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200363int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100364{
Willy Tarreau87b09662015-04-03 00:22:06 +0200365 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100366 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100367 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100368
Willy Tarreau78955f42014-12-28 13:09:02 +0100369 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100370 margin = global.tune.reserved_bufs;
371
Willy Tarreau78955f42014-12-28 13:09:02 +0100372 s = chn_sess(chn);
373
374 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100375 if (b)
376 return 1;
377
Willy Tarreaubf883e02014-11-25 21:10:35 +0100378 if (LIST_ISEMPTY(&s->buffer_wait))
379 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100380 return 0;
381}
382
Willy Tarreau87b09662015-04-03 00:22:06 +0200383/* Allocates a work buffer for stream <s>. It is meant to be called inside
384 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaua24adf02014-11-27 01:11:56 +0100385 * to work fine. For a regular connection, only the response is needed so that
386 * an error message may be built and returned. In case where the initiator is
387 * an applet (eg: peers), then we need to allocate the request buffer for the
388 * applet to be able to send its data (in this case a response is not needed).
389 * Request buffers are never picked from the reserved pool, but response
390 * buffers may be allocated from the reserve. This is critical to ensure that
391 * a response may always flow and will never block a server from releasing a
392 * connection. Returns 0 in case of failure, non-zero otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100393 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200394int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100395{
Willy Tarreaua24adf02014-11-27 01:11:56 +0100396 int margin;
397 struct buffer **buf;
398
399 if (objt_appctx(s->si[0].end)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100400 buf = &s->req.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100401 margin = global.tune.reserved_bufs;
402 }
403 else {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100404 buf = &s->res.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100405 margin = 0;
406 }
407
Willy Tarreaubf883e02014-11-25 21:10:35 +0100408 if (!LIST_ISEMPTY(&s->buffer_wait)) {
409 LIST_DEL(&s->buffer_wait);
410 LIST_INIT(&s->buffer_wait);
411 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100412
Willy Tarreaua24adf02014-11-27 01:11:56 +0100413 if (b_alloc_margin(buf, margin))
Willy Tarreau656859d2014-11-25 19:46:36 +0100414 return 1;
415
Willy Tarreaubf883e02014-11-25 21:10:35 +0100416 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100417 return 0;
418}
419
420/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100421 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200422 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100423 * a single buffer, so it makes sense to try to wake two up when two buffers
424 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100425 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200426void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100427{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100428 if (s->req.buf->size && buffer_empty(s->req.buf))
429 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100430
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100431 if (s->res.buf->size && buffer_empty(s->res.buf))
432 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100433
Willy Tarreaubf883e02014-11-25 21:10:35 +0100434 /* if we're certain to have at least 1 buffer available, and there is
435 * someone waiting, we can wake up a waiter and offer them.
436 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100437 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200438 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100439}
440
Willy Tarreau87b09662015-04-03 00:22:06 +0200441/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100442 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200443 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100444 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200445void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100446{
Willy Tarreau87b09662015-04-03 00:22:06 +0200447 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100448
449 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100450 if (rqlimit <= run_queue)
451 break;
452
Willy Tarreaubf883e02014-11-25 21:10:35 +0100453 if (sess->task->state & TASK_RUNNING)
454 continue;
455
456 LIST_DEL(&sess->buffer_wait);
457 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100458 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100459 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100460}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200461
462/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200463int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200464{
Willy Tarreau87b09662015-04-03 00:22:06 +0200465 LIST_INIT(&streams);
466 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
467 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200468}
469
Willy Tarreau87b09662015-04-03 00:22:06 +0200470void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100471{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200472 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100473 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100474 void *ptr;
475 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100476
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100477 bytes = s->req.total - s->logs.bytes_in;
478 s->logs.bytes_in = s->req.total;
479 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200480 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100481
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100482 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100483
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100484 if (objt_server(s->target))
485 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200486
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200487 if (sess->listener && sess->listener->counters)
488 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200489
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100490 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200491 struct stkctr *stkctr = &s->stkctr[i];
492
493 if (!stkctr_entry(stkctr)) {
494 stkctr = &sess->stkctr[i];
495 if (!stkctr_entry(stkctr))
496 continue;
497 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200498
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200499 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100500 if (ptr)
501 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200502
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200503 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100504 if (ptr)
505 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200506 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100507 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100508 }
509
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100510 bytes = s->res.total - s->logs.bytes_out;
511 s->logs.bytes_out = s->res.total;
512 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200513 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100514
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100515 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100516
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100517 if (objt_server(s->target))
518 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200519
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200520 if (sess->listener && sess->listener->counters)
521 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200522
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100523 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200524 struct stkctr *stkctr = &s->stkctr[i];
525
526 if (!stkctr_entry(stkctr)) {
527 stkctr = &sess->stkctr[i];
528 if (!stkctr_entry(stkctr))
529 continue;
530 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200531
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200532 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100533 if (ptr)
534 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200535
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200536 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100537 if (ptr)
538 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200539 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100540 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100541 }
542}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100544/* This function is called with (si->state == SI_ST_CON) meaning that a
545 * connection was attempted and that the file descriptor is already allocated.
546 * We must check for establishment, error and abort. Possible output states
547 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
548 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200549 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100550 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200551static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100552{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100553 struct stream_interface *si = &s->si[1];
554 struct channel *req = &s->req;
555 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200556 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100557
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100558 /* If we got an error, or if nothing happened and the connection timed
559 * out, we must give up. The CER state handler will take care of retry
560 * attempts and error reports.
561 */
562 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100563 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100564 /* Some data were sent past the connection establishment,
565 * so we need to pretend we're established to log correctly
566 * and let later states handle the failure.
567 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100568 si->state = SI_ST_EST;
569 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100570 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100571 return 1;
572 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100573 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100574 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100575
Willy Tarreauf79c8172013-10-21 16:30:56 +0200576 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100577
578 if (si->err_type)
579 return 0;
580
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100581 if (si->flags & SI_FL_ERR)
582 si->err_type = SI_ET_CONN_ERR;
583 else
584 si->err_type = SI_ET_CONN_TO;
585 return 0;
586 }
587
588 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100589 if (!(req->flags & CF_WRITE_PARTIAL) &&
590 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200591 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
592 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100593 s->be->options & PR_O_ABRT_CLOSE)))) {
594 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200595 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100596 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100597 if (s->srv_error)
598 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100599 return 1;
600 }
601
602 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200603 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100604 return 1;
605
606 /* OK, this means that a connection succeeded. The caller will be
607 * responsible for handling the transition from CON to EST.
608 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100609 si->state = SI_ST_EST;
610 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100611 return 1;
612}
613
614/* This function is called with (si->state == SI_ST_CER) meaning that a
615 * previous connection attempt has failed and that the file descriptor
616 * has already been released. Possible causes include asynchronous error
617 * notification and time out. Possible output states are SI_ST_CLO when
618 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
619 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
620 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
621 * marked as in error state. It returns 0.
622 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200623static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100624{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100625 struct stream_interface *si = &s->si[1];
626
Willy Tarreau87b09662015-04-03 00:22:06 +0200627 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100628 if (objt_server(s->target)) {
629 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100630
Willy Tarreaue7dff022015-04-03 01:14:29 +0200631 if (s->flags & SF_CURR_SESS) {
632 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100633 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100635 }
636
637 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200638 si->conn_retries--;
639 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100640 if (!si->err_type) {
641 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100642 }
643
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100644 if (objt_server(s->target))
645 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100646 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100647 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100648 if (may_dequeue_tasks(objt_server(s->target), s->be))
649 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100650
651 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200652 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100653 s->req.flags |= CF_WRITE_ERROR;
654 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100655
656 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100657 if (s->srv_error)
658 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100659 return 0;
660 }
661
662 /* If the "redispatch" option is set on the backend, we are allowed to
663 * retry on another server for the last retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200664 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100665 * bit to ignore any persistence cookie. We won't count a retry nor a
666 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200667 * If the connection is not persistent, the balancing algorithm is not
668 * determinist (round robin) and there is more than one active server,
669 * we accept to perform an immediate redispatch without waiting since
670 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100671 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200672 if (objt_server(s->target) &&
673 (si->conn_retries == 0 ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200674 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Willy Tarreau33a14e52014-06-13 17:49:40 +0200675 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200676 s->be->options & PR_O_REDISP && !(s->flags & SF_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100677 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100678 if (may_dequeue_tasks(objt_server(s->target), s->be))
679 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100680
Willy Tarreaue7dff022015-04-03 01:14:29 +0200681 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100682 si->state = SI_ST_REQ;
683 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100684 if (objt_server(s->target))
685 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100686 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687 si->state = SI_ST_ASS;
688 }
689
690 if (si->flags & SI_FL_ERR) {
691 /* The error was an asynchronous connection error, and we will
692 * likely have to retry connecting to the same server, most
693 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200694 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100695 */
696
Willy Tarreaub0290662014-06-13 17:04:44 +0200697 int delay = 1000;
698
699 if (s->be->timeout.connect && s->be->timeout.connect < delay)
700 delay = s->be->timeout.connect;
701
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100702 if (!si->err_type)
703 si->err_type = SI_ET_CONN_ERR;
704
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200705 /* only wait when we're retrying on the same server */
706 if (si->state == SI_ST_ASS ||
707 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
708 (s->be->srv_act <= 1)) {
709 si->state = SI_ST_TAR;
710 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
711 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100712 return 0;
713 }
714 return 0;
715}
716
717/*
718 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200719 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200720 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100721 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200722static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100723{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100724 struct stream_interface *si = &s->si[1];
725 struct channel *req = &s->req;
726 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100727
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100728 /* First, centralize the timers information */
729 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
730 si->exp = TICK_ETERNITY;
731
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100732 if (objt_server(s->target))
733 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100734
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100735 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736 /* if the user wants to log as soon as possible, without counting
737 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200738 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100740 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100741 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100742 }
743 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100744 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100745 }
746
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200747 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200748 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200749 if (req->flags & CF_WAKE_CONNECT) {
750 req->flags |= CF_WAKE_ONCE;
751 req->flags &= ~CF_WAKE_CONNECT;
752 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200753 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200754 /* real connections have timeouts */
755 req->wto = s->be->timeout.server;
756 rep->rto = s->be->timeout.server;
757 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758 req->wex = TICK_ETERNITY;
759}
760
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100761/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
762 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100763 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
764 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
765 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100766 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200767static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100768{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100769 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100770 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100771 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100772
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100773 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 +0100774 now_ms, __FUNCTION__,
775 s,
Willy Tarreau103197d2014-11-28 15:26:12 +0100776 req, s->rep,
777 req->rex, s->res.wex,
778 req->flags, s->res.flags,
779 req->buf->i, req->buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, req->cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100780
781 if (si->state == SI_ST_ASS) {
782 /* Server assigned to connection request, we have to try to connect now */
783 int conn_err;
784
785 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100786 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100787
Willy Tarreaue7dff022015-04-03 01:14:29 +0200788 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100789 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100790 if (srv)
791 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500792 if (srv)
793 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100794 return;
795 }
796
797 /* We have received a synchronous error. We might have to
798 * abort, retry immediately or redispatch.
799 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200800 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100801 if (!si->err_type) {
802 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100803 }
804
Willy Tarreau827aee92011-03-10 16:55:02 +0100805 if (srv)
806 srv_inc_sess_ctr(srv);
807 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500808 srv_set_sess_last(srv);
809 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100810 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100811 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100812
Willy Tarreau87b09662015-04-03 00:22:06 +0200813 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100814 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100815 if (may_dequeue_tasks(srv, s->be))
816 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100817
818 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200819 si_shutr(si);
820 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100821 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100822
823 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
824
Willy Tarreau87b09662015-04-03 00:22:06 +0200825 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100826 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100827 if (s->srv_error)
828 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100829 return;
830 }
831
832 /* We are facing a retryable error, but we don't want to run a
833 * turn-around now, as the problem is likely a source port
834 * allocation problem, so we want to retry now.
835 */
836 si->state = SI_ST_CER;
837 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100838 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100839 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
840 return;
841 }
842 else if (si->state == SI_ST_QUE) {
843 /* connection request was queued, check for any update */
844 if (!s->pend_pos) {
845 /* The connection is not in the queue anymore. Either
846 * we have a server connection slot available and we
847 * go directly to the assigned state, or we need to
848 * load-balance first and go to the INI state.
849 */
850 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200851 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100852 si->state = SI_ST_REQ;
853 else {
854 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
855 si->state = SI_ST_ASS;
856 }
857 return;
858 }
859
860 /* Connection request still in queue... */
861 if (si->flags & SI_FL_EXP) {
862 /* ... and timeout expired */
863 si->exp = TICK_ETERNITY;
864 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100865 if (srv)
866 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100867 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200868 si_shutr(si);
869 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100870 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100871 if (!si->err_type)
872 si->err_type = SI_ET_QUEUE_TO;
873 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100874 if (s->srv_error)
875 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100876 return;
877 }
878
879 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100880 if ((req->flags & (CF_READ_ERROR)) ||
881 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
882 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100883 /* give up */
884 si->exp = TICK_ETERNITY;
885 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200886 si_shutr(si);
887 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100888 si->err_type |= SI_ET_QUEUE_ABRT;
889 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100890 if (s->srv_error)
891 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100892 return;
893 }
894
895 /* Nothing changed */
896 return;
897 }
898 else if (si->state == SI_ST_TAR) {
899 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100900 if ((req->flags & (CF_READ_ERROR)) ||
901 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
902 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100903 /* give up */
904 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200905 si_shutr(si);
906 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100907 si->err_type |= SI_ET_CONN_ABRT;
908 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100909 if (s->srv_error)
910 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100911 return;
912 }
913
914 if (!(si->flags & SI_FL_EXP))
915 return; /* still in turn-around */
916
917 si->exp = TICK_ETERNITY;
918
Willy Tarreau87b09662015-04-03 00:22:06 +0200919 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100920 * marked "assigned".
921 * FIXME: Should we force a redispatch attempt when the server is down ?
922 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200923 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100924 si->state = SI_ST_ASS;
925 else
926 si->state = SI_ST_REQ;
927 return;
928 }
929}
930
Willy Tarreau87b09662015-04-03 00:22:06 +0200931/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900932 * also counts a failed request if the server state has not reached the request
933 * stage.
934 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200935static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900936{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200937 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900938 if (s->si[1].state < SI_ST_REQ) {
939
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200940 strm_fe(s)->fe_counters.failed_req++;
941 if (strm_li(s)->counters)
942 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900943
Willy Tarreaue7dff022015-04-03 01:14:29 +0200944 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900945 }
946 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200947 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900948 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200949 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900950 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200951 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900952 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200953 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900954 }
955}
956
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100957/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100958 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
959 * a real connection to a server, indicating that a server has been assigned,
960 * or SI_ST_EST for a successful connection to an applet. It may also return
961 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100962 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200963static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100964{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100965 struct stream_interface *si = &s->si[1];
966
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100967 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 +0100968 now_ms, __FUNCTION__,
969 s,
970 s->req, s->rep,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100971 s->req.rex, s->res.wex,
972 s->req.flags, s->res.flags,
Willy Tarreau350f4872014-11-28 14:42:25 +0100973 s->req.buf->i, s->req.buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, s->req.cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100974
975 if (si->state != SI_ST_REQ)
976 return;
977
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100978 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
979 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100980 struct appctx *appctx = objt_appctx(si->end);
981
982 if (!appctx || appctx->applet != __objt_applet(s->target))
983 appctx = stream_int_register_handler(si, objt_applet(s->target));
984
985 if (!appctx) {
986 /* No more memory, let's immediately abort. Force the
987 * error code to ignore the ERR_LOCAL which is not a
988 * real error.
989 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200990 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100991
992 si_shutr(si);
993 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100994 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +0100995 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100996 si->state = SI_ST_CLO;
997 if (s->srv_error)
998 s->srv_error(s, si);
999 return;
1000 }
1001
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001002 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001003 si->state = SI_ST_EST;
1004 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001005 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001006 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001007 return;
1008 }
1009
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001010 /* Try to assign a server */
1011 if (srv_redispatch_connect(s) != 0) {
1012 /* We did not get a server. Either we queued the
1013 * connection request, or we encountered an error.
1014 */
1015 if (si->state == SI_ST_QUE)
1016 return;
1017
1018 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001019 si_shutr(si);
1020 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001021 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001022 if (!si->err_type)
1023 si->err_type = SI_ET_CONN_OTHER;
1024 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001025 if (s->srv_error)
1026 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001027 return;
1028 }
1029
1030 /* The server is assigned */
1031 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1032 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001033 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001034}
1035
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001036/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001037 * if appropriate. The default_backend rule is also considered, then the
1038 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001039 * It returns 1 if the processing can continue on next analysers, or zero if it
1040 * either needs more data or wants to immediately abort the request.
1041 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001042static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001043{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001044 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001045 struct session *sess = s->sess;
1046 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001047
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001048 req->analysers &= ~an_bit;
1049 req->analyse_exp = TICK_ETERNITY;
1050
Willy Tarreau87b09662015-04-03 00:22:06 +02001051 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001052 now_ms, __FUNCTION__,
1053 s,
1054 req,
1055 req->rex, req->wex,
1056 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001057 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001058 req->analysers);
1059
1060 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001061 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001062 struct switching_rule *rule;
1063
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001064 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001065 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001066
Willy Tarreauf51658d2014-04-23 01:21:56 +02001067 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001068 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001069 ret = acl_pass(ret);
1070 if (rule->cond->pol == ACL_COND_UNLESS)
1071 ret = !ret;
1072 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001073
1074 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001075 /* If the backend name is dynamic, try to resolve the name.
1076 * If we can't resolve the name, or if any error occurs, break
1077 * the loop and fallback to the default backend.
1078 */
1079 struct proxy *backend;
1080
1081 if (rule->dynamic) {
1082 struct chunk *tmp = get_trash_chunk();
1083 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1084 break;
1085 backend = findproxy(tmp->str, PR_CAP_BE);
1086 if (!backend)
1087 break;
1088 }
1089 else
1090 backend = rule->be.backend;
1091
Willy Tarreau87b09662015-04-03 00:22:06 +02001092 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001093 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001094 break;
1095 }
1096 }
1097
1098 /* To ensure correct connection accounting on the backend, we
1099 * have to assign one if it was not set (eg: a listen). This
1100 * measure also takes care of correctly setting the default
1101 * backend if any.
1102 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001103 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001104 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001105 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001106 }
1107
Willy Tarreaufb356202010-08-03 14:02:05 +02001108 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001109 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001110 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1111 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001112 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001113
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001114 /* as soon as we know the backend, we must check if we have a matching forced or ignored
Willy Tarreau87b09662015-04-03 00:22:06 +02001115 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001116 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001117 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001118 int ret = 1;
1119
1120 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001121 ret = acl_exec_cond(prst_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4de91492010-01-22 19:10:05 +01001122 ret = acl_pass(ret);
1123 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1124 ret = !ret;
1125 }
1126
1127 if (ret) {
1128 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001129 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001130 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001131 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001132 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001133 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001134 break;
1135 }
1136 }
1137
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001138 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001139
1140 sw_failed:
1141 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001142 channel_abort(&s->req);
1143 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001144
Willy Tarreaue7dff022015-04-03 01:14:29 +02001145 if (!(s->flags & SF_ERR_MASK))
1146 s->flags |= SF_ERR_RESOURCE;
1147 if (!(s->flags & SF_FINST_MASK))
1148 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001149
Willy Tarreaueee5b512015-04-03 23:46:31 +02001150 if (s->txn)
1151 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001152 s->req.analysers = 0;
1153 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001154 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001155}
1156
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001157/* This stream analyser works on a request. It applies all use-server rules on
1158 * it then returns 1. The data must already be present in the buffer otherwise
1159 * they won't match. It always returns 1.
1160 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001161static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001162{
1163 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001164 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001165 struct server_rule *rule;
1166
Willy Tarreau87b09662015-04-03 00:22:06 +02001167 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001168 now_ms, __FUNCTION__,
1169 s,
1170 req,
1171 req->rex, req->wex,
1172 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001173 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001174 req->analysers);
1175
Willy Tarreaue7dff022015-04-03 01:14:29 +02001176 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001177 list_for_each_entry(rule, &px->server_rules, list) {
1178 int ret;
1179
Willy Tarreau192252e2015-04-04 01:47:55 +02001180 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001181 ret = acl_pass(ret);
1182 if (rule->cond->pol == ACL_COND_UNLESS)
1183 ret = !ret;
1184
1185 if (ret) {
1186 struct server *srv = rule->srv.ptr;
1187
Willy Tarreau892337c2014-05-13 23:41:20 +02001188 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001189 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001190 (s->flags & SF_FORCE_PRST)) {
1191 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001192 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001193 break;
1194 }
1195 /* if the server is not UP, let's go on with next rules
1196 * just in case another one is suited.
1197 */
1198 }
1199 }
1200 }
1201
1202 req->analysers &= ~an_bit;
1203 req->analyse_exp = TICK_ETERNITY;
1204 return 1;
1205}
1206
Emeric Brun1d33b292010-01-04 15:47:17 +01001207/* This stream analyser works on a request. It applies all sticking rules on
1208 * it then returns 1. The data must already be present in the buffer otherwise
1209 * they won't match. It always returns 1.
1210 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001211static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001212{
1213 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001214 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001215 struct sticking_rule *rule;
1216
Willy Tarreau87b09662015-04-03 00:22:06 +02001217 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001218 now_ms, __FUNCTION__,
1219 s,
1220 req,
1221 req->rex, req->wex,
1222 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001223 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001224 req->analysers);
1225
1226 list_for_each_entry(rule, &px->sticking_rules, list) {
1227 int ret = 1 ;
1228 int i;
1229
Willy Tarreau9667a802013-12-09 12:52:13 +01001230 /* Only the first stick store-request of each table is applied
1231 * and other ones are ignored. The purpose is to allow complex
1232 * configurations which look for multiple entries by decreasing
1233 * order of precision and to stop at the first which matches.
1234 * An example could be a store of the IP address from an HTTP
1235 * header first, then from the source if not found.
1236 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001237 for (i = 0; i < s->store_count; i++) {
1238 if (rule->table.t == s->store[i].table)
1239 break;
1240 }
1241
1242 if (i != s->store_count)
1243 continue;
1244
1245 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001246 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001247 ret = acl_pass(ret);
1248 if (rule->cond->pol == ACL_COND_UNLESS)
1249 ret = !ret;
1250 }
1251
1252 if (ret) {
1253 struct stktable_key *key;
1254
Willy Tarreau192252e2015-04-04 01:47:55 +02001255 key = stktable_fetch_key(rule->table.t, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->expr, NULL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001256 if (!key)
1257 continue;
1258
1259 if (rule->flags & STK_IS_MATCH) {
1260 struct stksess *ts;
1261
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001262 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001263 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001264 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001265 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001266
1267 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001268 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1269 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001270 if (node) {
1271 struct server *srv;
1272
1273 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001274 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001275 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001276 (s->flags & SF_FORCE_PRST)) {
1277 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001278 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001279 }
1280 }
1281 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001282 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001283 }
1284 }
1285 if (rule->flags & STK_IS_STORE) {
1286 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1287 struct stksess *ts;
1288
1289 ts = stksess_new(rule->table.t, key);
1290 if (ts) {
1291 s->store[s->store_count].table = rule->table.t;
1292 s->store[s->store_count++].ts = ts;
1293 }
1294 }
1295 }
1296 }
1297 }
1298
1299 req->analysers &= ~an_bit;
1300 req->analyse_exp = TICK_ETERNITY;
1301 return 1;
1302}
1303
1304/* This stream analyser works on a response. It applies all store rules on it
1305 * then returns 1. The data must already be present in the buffer otherwise
1306 * they won't match. It always returns 1.
1307 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001308static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001309{
1310 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001311 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001312 struct sticking_rule *rule;
1313 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001314 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001315
Willy Tarreau87b09662015-04-03 00:22:06 +02001316 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001317 now_ms, __FUNCTION__,
1318 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001319 rep,
1320 rep->rex, rep->wex,
1321 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001322 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001323 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001324
1325 list_for_each_entry(rule, &px->storersp_rules, list) {
1326 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001327
Willy Tarreau9667a802013-12-09 12:52:13 +01001328 /* Only the first stick store-response of each table is applied
1329 * and other ones are ignored. The purpose is to allow complex
1330 * configurations which look for multiple entries by decreasing
1331 * order of precision and to stop at the first which matches.
1332 * An example could be a store of a set-cookie value, with a
1333 * fallback to a parameter found in a 302 redirect.
1334 *
1335 * The store-response rules are not allowed to override the
1336 * store-request rules for the same table, but they may coexist.
1337 * Thus we can have up to one store-request entry and one store-
1338 * response entry for the same table at any time.
1339 */
1340 for (i = nbreq; i < s->store_count; i++) {
1341 if (rule->table.t == s->store[i].table)
1342 break;
1343 }
1344
1345 /* skip existing entries for this table */
1346 if (i < s->store_count)
1347 continue;
1348
Emeric Brun1d33b292010-01-04 15:47:17 +01001349 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001350 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001351 ret = acl_pass(ret);
1352 if (rule->cond->pol == ACL_COND_UNLESS)
1353 ret = !ret;
1354 }
1355
1356 if (ret) {
1357 struct stktable_key *key;
1358
Willy Tarreau192252e2015-04-04 01:47:55 +02001359 key = stktable_fetch_key(rule->table.t, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->expr, NULL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001360 if (!key)
1361 continue;
1362
Willy Tarreau37e340c2013-12-06 23:05:21 +01001363 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001364 struct stksess *ts;
1365
1366 ts = stksess_new(rule->table.t, key);
1367 if (ts) {
1368 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001369 s->store[s->store_count++].ts = ts;
1370 }
1371 }
1372 }
1373 }
1374
1375 /* process store request and store response */
1376 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001377 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001378 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001379
Willy Tarreauc93cd162014-05-13 15:54:22 +02001380 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001381 stksess_free(s->store[i].table, s->store[i].ts);
1382 s->store[i].ts = NULL;
1383 continue;
1384 }
1385
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001386 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1387 if (ts) {
1388 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001389 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001390 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001391 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001392 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001393 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001394
1395 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001396 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001397 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001398 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001399 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001400
1401 rep->analysers &= ~an_bit;
1402 rep->analyse_exp = TICK_ETERNITY;
1403 return 1;
1404}
1405
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001406/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001407 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001408 */
1409#define UPDATE_ANALYSERS(real, list, back, flag) { \
1410 list = (((list) & ~(flag)) | ~(back)) & (real); \
1411 back = real; \
1412 if (!(list)) \
1413 break; \
1414 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1415 continue; \
1416}
1417
Willy Tarreau87b09662015-04-03 00:22:06 +02001418/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001419 * then puts it back to the wait queue in a clean state, or cleans up its
1420 * resources if it must be deleted. Returns in <next> the date the task wants
1421 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1422 * nothing too many times, the request and response buffers flags are monitored
1423 * and each function is called only if at least another function has changed at
1424 * least one flag it is interested in.
1425 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001426struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001427{
Willy Tarreau827aee92011-03-10 16:55:02 +01001428 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001429 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001430 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001431 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001432 unsigned int rq_prod_last, rq_cons_last;
1433 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001434 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001435 struct channel *req, *res;
1436 struct stream_interface *si_f, *si_b;
1437
1438 req = &s->req;
1439 res = &s->res;
1440
1441 si_f = &s->si[0];
1442 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001443
1444 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001445 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001446
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001447 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001448 if (s->txn)
1449 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001450
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001451 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001452 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1453 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001454
1455 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001456 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1457 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001458
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001459 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001460 si_f->flags |= SI_FL_DONT_WAKE;
1461 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001462
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001463 /* 1a: Check for low level timeouts if needed. We just set a flag on
1464 * stream interfaces when their timeouts have expired.
1465 */
1466 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001467 stream_int_check_timeouts(si_f);
1468 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001469
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001470 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001471 * for future reads or writes. Note: this will also concern upper layers
1472 * but we do not touch any other flag. We must be careful and correctly
1473 * detect state changes when calling them.
1474 */
1475
Willy Tarreau8f128b42014-11-28 15:07:47 +01001476 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001477
Willy Tarreau8f128b42014-11-28 15:07:47 +01001478 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1479 si_b->flags |= SI_FL_NOLINGER;
1480 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001481 }
1482
Willy Tarreau8f128b42014-11-28 15:07:47 +01001483 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1484 if (si_f->flags & SI_FL_NOHALF)
1485 si_f->flags |= SI_FL_NOLINGER;
1486 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001487 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001488
Willy Tarreau8f128b42014-11-28 15:07:47 +01001489 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001490
Willy Tarreau8f128b42014-11-28 15:07:47 +01001491 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1492 si_f->flags |= SI_FL_NOLINGER;
1493 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001494 }
1495
Willy Tarreau8f128b42014-11-28 15:07:47 +01001496 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1497 if (si_b->flags & SI_FL_NOHALF)
1498 si_b->flags |= SI_FL_NOLINGER;
1499 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001500 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001501
1502 /* Once in a while we're woken up because the task expires. But
1503 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001504 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001505 * timeout needs to be refreshed.
1506 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001507 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001508 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1509 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001510 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001511 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1512 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001513 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001514
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001515 /* below we may emit error messages so we have to ensure that we have
1516 * our buffers properly allocated.
1517 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001518 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001519 /* No buffer available, we've been subscribed to the list of
1520 * buffer waiters, let's wait for our turn.
1521 */
1522 goto update_exp_and_leave;
1523 }
1524
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001525 /* 1b: check for low-level errors reported at the stream interface.
1526 * First we check if it's a retryable error (in which case we don't
1527 * want to tell the buffer). Otherwise we report the error one level
1528 * upper by setting flags into the buffers. Note that the side towards
1529 * the client cannot have connect (hence retryable) errors. Also, the
1530 * connection setup code must be able to deal with any type of abort.
1531 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001532 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001533 if (unlikely(si_f->flags & SI_FL_ERR)) {
1534 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1535 si_shutr(si_f);
1536 si_shutw(si_f);
1537 stream_int_report_error(si_f);
1538 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001539 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001540 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001541 if (srv)
1542 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001543 if (!(s->flags & SF_ERR_MASK))
1544 s->flags |= SF_ERR_CLICL;
1545 if (!(s->flags & SF_FINST_MASK))
1546 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001547 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001548 }
1549 }
1550
Willy Tarreau8f128b42014-11-28 15:07:47 +01001551 if (unlikely(si_b->flags & SI_FL_ERR)) {
1552 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1553 si_shutr(si_b);
1554 si_shutw(si_b);
1555 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001556 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001557 if (srv)
1558 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001559 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001560 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001561 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001562 if (srv)
1563 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001564 if (!(s->flags & SF_ERR_MASK))
1565 s->flags |= SF_ERR_SRVCL;
1566 if (!(s->flags & SF_FINST_MASK))
1567 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001568 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001569 }
1570 /* note: maybe we should process connection errors here ? */
1571 }
1572
Willy Tarreau8f128b42014-11-28 15:07:47 +01001573 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001574 /* we were trying to establish a connection on the server side,
1575 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1576 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001577 if (unlikely(!sess_update_st_con_tcp(s)))
1578 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001579 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001580 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001581
1582 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1583 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1584 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1585 */
1586 }
1587
Willy Tarreau8f128b42014-11-28 15:07:47 +01001588 rq_prod_last = si_f->state;
1589 rq_cons_last = si_b->state;
1590 rp_cons_last = si_f->state;
1591 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001592
1593 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001594 /* Check for connection closure */
1595
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001596 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001597 "[%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 +01001598 now_ms, __FUNCTION__, __LINE__,
1599 t,
1600 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001601 req, res,
1602 req->rex, res->wex,
1603 req->flags, res->flags,
1604 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1605 si_f->err_type, si_b->err_type,
1606 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001607
1608 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001609 if (unlikely(si_f->state == SI_ST_DIS))
1610 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001611
1612 /* When a server-side connection is released, we have to count it and
1613 * check for pending connections on this server.
1614 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001615 if (unlikely(si_b->state == SI_ST_DIS)) {
1616 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001617 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001618 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001619 if (s->flags & SF_CURR_SESS) {
1620 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001621 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001622 }
1623 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001624 if (may_dequeue_tasks(srv, s->be))
1625 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001626 }
1627 }
1628
1629 /*
1630 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1631 * at this point.
1632 */
1633
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001634 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001635 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001636 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1637 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1638 si_f->state != rq_prod_last ||
1639 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001640 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001641 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001642
Willy Tarreau8f128b42014-11-28 15:07:47 +01001643 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001644 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001645 unsigned int ana_list;
1646 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001647
Willy Tarreau90deb182010-01-07 00:20:41 +01001648 /* it's up to the analysers to stop new connections,
1649 * disable reading or closing. Note: if an analyser
1650 * disables any of these bits, it is responsible for
1651 * enabling them again when it disables itself, so
1652 * that other analysers are called in similar conditions.
1653 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001654 channel_auto_read(req);
1655 channel_auto_connect(req);
1656 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001657
1658 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001659 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001660 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001661 * the list when not needed. Any analyser may return 0
1662 * to break out of the loop, either because of missing
1663 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001664 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001665 * analyser, and we may loop again if other analysers
1666 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001667 *
1668 * We build a list of analysers to run. We evaluate all
1669 * of these analysers in the order of the lower bit to
1670 * the higher bit. This ordering is very important.
1671 * An analyser will often add/remove other analysers,
1672 * including itself. Any changes to itself have no effect
1673 * on the loop. If it removes any other analysers, we
1674 * want those analysers not to be called anymore during
1675 * this loop. If it adds an analyser that is located
1676 * after itself, we want it to be scheduled for being
1677 * processed during the loop. If it adds an analyser
1678 * which is located before it, we want it to switch to
1679 * it immediately, even if it has already been called
1680 * once but removed since.
1681 *
1682 * In order to achieve this, we compare the analyser
1683 * list after the call with a copy of it before the
1684 * call. The work list is fed with analyser bits that
1685 * appeared during the call. Then we compare previous
1686 * work list with the new one, and check the bits that
1687 * appeared. If the lowest of these bits is lower than
1688 * the current bit, it means we have enabled a previous
1689 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001690 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001691
Willy Tarreau8f128b42014-11-28 15:07:47 +01001692 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001693 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001694 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001695
Willy Tarreaufb356202010-08-03 14:02:05 +02001696 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001697 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001698 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001699 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001700 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001701
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001702 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001703 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001704 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001705 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001706 }
1707
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001708 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001709 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001710 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001711 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001712 }
1713
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001714 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001715 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001716 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001717 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001718 }
1719
Willy Tarreaufb356202010-08-03 14:02:05 +02001720 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001721 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001722 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001723 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001724 }
1725
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001726 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001727 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001728 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001729 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001730 }
1731
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001732 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001733 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001734 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001735 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001736 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001737
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001738 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001739 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001740 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001741 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001742 }
1743
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001744 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001745 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001746 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001747 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001748 }
1749
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001750 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001751 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001752 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001753 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001754 }
Emeric Brun647caf12009-06-30 17:57:00 +02001755
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001756 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001757 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001758 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001759 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001760 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001761
Emeric Brun1d33b292010-01-04 15:47:17 +01001762 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001763 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001764 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001765 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001766 }
1767
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001768 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001769 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001770 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001771 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001772 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001773 break;
1774 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001775 }
Willy Tarreau84455332009-03-15 22:34:05 +01001776
Willy Tarreau8f128b42014-11-28 15:07:47 +01001777 rq_prod_last = si_f->state;
1778 rq_cons_last = si_b->state;
1779 req->flags &= ~CF_WAKE_ONCE;
1780 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001781
Willy Tarreau8f128b42014-11-28 15:07:47 +01001782 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001783 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001784 }
1785
Willy Tarreau576507f2010-01-07 00:09:04 +01001786 /* we'll monitor the request analysers while parsing the response,
1787 * because some response analysers may indirectly enable new request
1788 * analysers (eg: HTTP keep-alive).
1789 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001790 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001791
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001792 resync_response:
1793 /* Analyse response */
1794
Willy Tarreau8f128b42014-11-28 15:07:47 +01001795 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1796 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1797 si_f->state != rp_cons_last ||
1798 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001799 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001800 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001801
Willy Tarreau8f128b42014-11-28 15:07:47 +01001802 if ((res->flags & CF_MASK_ANALYSER) &&
1803 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001804 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1805 * for some free space in the response buffer, so we might need to call
1806 * it when something changes in the response buffer, but still we pass
1807 * it the request buffer. Note that the SI state might very well still
1808 * be zero due to us returning a flow of redirects!
1809 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001810 res->analysers &= ~AN_REQ_ALL;
1811 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001812 }
1813
Willy Tarreau8f128b42014-11-28 15:07:47 +01001814 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001815 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001816 unsigned int ana_list;
1817 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001818
Willy Tarreau90deb182010-01-07 00:20:41 +01001819 /* it's up to the analysers to stop disable reading or
1820 * closing. Note: if an analyser disables any of these
1821 * bits, it is responsible for enabling them again when
1822 * it disables itself, so that other analysers are called
1823 * in similar conditions.
1824 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001825 channel_auto_read(res);
1826 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001827
1828 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001829 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001830 * to MSB. The analysers must remove themselves from
1831 * the list when not needed. Any analyser may return 0
1832 * to break out of the loop, either because of missing
1833 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001834 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001835 * analyser, and we may loop again if other analysers
1836 * are added in the middle.
1837 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001838
Willy Tarreau8f128b42014-11-28 15:07:47 +01001839 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001840 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001841 /* Warning! ensure that analysers are always placed in ascending order! */
1842
Emeric Brun97679e72010-09-23 17:56:44 +02001843 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001844 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001845 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001846 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001847 }
1848
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001849 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001850 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001851 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001852 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001853 }
1854
Emeric Brun1d33b292010-01-04 15:47:17 +01001855 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001856 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001857 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001858 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001859 }
1860
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001861 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001862 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001863 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001864 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001865 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001866
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001867 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001868 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001870 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001871 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001872 break;
1873 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001874 }
1875
Willy Tarreau8f128b42014-11-28 15:07:47 +01001876 rp_cons_last = si_f->state;
1877 rp_prod_last = si_b->state;
1878 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001879
Willy Tarreau8f128b42014-11-28 15:07:47 +01001880 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001881 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001882 }
1883
Willy Tarreau576507f2010-01-07 00:09:04 +01001884 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001885 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001886 goto resync_request;
1887
Willy Tarreau8f128b42014-11-28 15:07:47 +01001888 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001889 goto resync_request;
1890
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001891 /* FIXME: here we should call protocol handlers which rely on
1892 * both buffers.
1893 */
1894
1895
1896 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001897 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001898 * we're just in a data phase here since it means we have not
1899 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001900 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001901 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001902 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001903 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001904 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001905 req->analysers = 0;
1906 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001907 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001908 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001909 if (srv)
1910 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001911 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001912 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001913 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001914 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001915 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001916 if (srv)
1917 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001918 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001919 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001920 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001921 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001922 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001923 if (srv)
1924 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001925 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001926 }
1927 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001928 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001929 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001930 if (srv)
1931 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001932 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001933 }
Willy Tarreau84455332009-03-15 22:34:05 +01001934 sess_set_term_flags(s);
1935 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001936 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001937 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001938 res->analysers = 0;
1939 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001940 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001941 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001942 if (srv)
1943 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001944 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001945 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001946 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001947 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001948 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001949 if (srv)
1950 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001951 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001952 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001953 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001954 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001955 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001956 if (srv)
1957 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001958 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001959 }
1960 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001961 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001962 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001963 if (srv)
1964 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001965 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001966 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001967 sess_set_term_flags(s);
1968 }
Willy Tarreau84455332009-03-15 22:34:05 +01001969 }
1970
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001971 /*
1972 * Here we take care of forwarding unhandled data. This also includes
1973 * connection establishments and shutdown requests.
1974 */
1975
1976
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001977 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001978 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001979 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001980 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001981 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001982 if (unlikely(!req->analysers &&
1983 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
1984 (si_f->state >= SI_ST_EST) &&
1985 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01001986 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001987 * attached to it. If any data are left in, we'll permit them to
1988 * move.
1989 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001990 channel_auto_read(req);
1991 channel_auto_connect(req);
1992 channel_auto_close(req);
1993 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001994
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001995 /* We'll let data flow between the producer (if still connected)
1996 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001997 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001998 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
1999 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002000 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002001
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002002 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002003 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2004 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002005 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002006 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2007 (objt_conn(si_b->end) && __objt_conn(si_b->end)->xprt && __objt_conn(si_b->end)->xprt->snd_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002008 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002009 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2010 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002011 (req->flags & CF_STREAMER_FAST)))) {
2012 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002013 }
2014
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002015 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002016 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002017
2018 /*
2019 * Now forward all shutdown requests between both sides of the buffer
2020 */
2021
Willy Tarreau520d95e2009-09-19 21:04:57 +02002022 /* first, let's check if the request buffer needs to shutdown(write), which may
2023 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002024 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2025 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002026 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002027 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002028 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002029 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002030 if (tick_isset(sess->fe->timeout.clientfin)) {
2031 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002032 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002033 }
2034 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002035
2036 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002037 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2038 channel_is_empty(req))) {
2039 if (req->flags & CF_READ_ERROR)
2040 si_b->flags |= SI_FL_NOLINGER;
2041 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002042 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002043 res->rto = s->be->timeout.serverfin;
2044 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002045 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002046 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002047
2048 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002049 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2050 !req->analysers))
2051 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002052
2053 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002054 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2055 if (si_f->flags & SI_FL_NOHALF)
2056 si_f->flags |= SI_FL_NOLINGER;
2057 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002058 if (tick_isset(sess->fe->timeout.clientfin)) {
2059 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002060 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002061 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002062 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002063
Willy Tarreau520d95e2009-09-19 21:04:57 +02002064 /* it's possible that an upper layer has requested a connection setup or abort.
2065 * There are 2 situations where we decide to establish a new connection :
2066 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002067 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002068 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002069 if (si_b->state == SI_ST_INI) {
2070 if (!(req->flags & CF_SHUTW)) {
2071 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002072 /* If we have an appctx, there is no connect method, so we
2073 * immediately switch to the connected state, otherwise we
2074 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002075 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002076 si_b->state = SI_ST_REQ; /* new connection requested */
2077 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002078 }
Willy Tarreau73201222009-08-16 18:27:24 +02002079 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002080 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002081 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2082 channel_shutw_now(req); /* fix buffer flags upon abort */
2083 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002084 }
Willy Tarreau92795622009-03-06 12:51:23 +01002085 }
2086
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002087
2088 /* we may have a pending connection request, or a connection waiting
2089 * for completion.
2090 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002091 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002092 do {
2093 /* nb: step 1 might switch from QUE to ASS, but we first want
2094 * to give a chance to step 2 to perform a redirect if needed.
2095 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002096 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002097 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002098 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002099 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002100
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002101 /* applets directly go to the ESTABLISHED state. Similarly,
2102 * servers experience the same fate when their connection
2103 * is reused.
2104 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002105 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002106 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002107
Willy Tarreaub44c8732013-12-31 23:16:50 +01002108 /* Now we can add the server name to a header (if requested) */
2109 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002110 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002111 (s->be->server_id_hdr_name != NULL) &&
2112 (s->be->mode == PR_MODE_HTTP) &&
2113 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002114 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002115 }
2116
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002117 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002118 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002119 http_perform_server_redirect(s, si_b);
2120 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002121 }
2122
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002123 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002124 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002125 goto resync_stream_interface;
2126
Willy Tarreau815a9b22010-07-27 17:15:12 +02002127 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002128 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002129 goto resync_request;
2130
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002131 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002132
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002133 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002134 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002135 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002136 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002137 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002138 if (unlikely(!res->analysers &&
2139 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2140 (si_b->state >= SI_ST_EST) &&
2141 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002142 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002143 * attached to it. If any data are left in, we'll permit them to
2144 * move.
2145 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002146 channel_auto_read(res);
2147 channel_auto_close(res);
2148 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002149
2150 /* We'll let data flow between the producer (if still connected)
2151 * to the consumer.
2152 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002153 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2154 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002155
2156 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002157 * tunnel timeout set, use it now. Note that we must respect
2158 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002159 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002160 if (!req->analysers && s->be->timeout.tunnel) {
2161 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002162 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002163
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002164 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2165 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002166 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2167 res->rto = s->be->timeout.serverfin;
2168 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2169 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002170 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2171 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002172
Willy Tarreau8f128b42014-11-28 15:07:47 +01002173 req->rex = tick_add(now_ms, req->rto);
2174 req->wex = tick_add(now_ms, req->wto);
2175 res->rex = tick_add(now_ms, res->rto);
2176 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002177 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002178 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002179
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002180 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002181 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2182 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002183 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002184 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2185 (objt_conn(si_b->end) && __objt_conn(si_b->end)->xprt && __objt_conn(si_b->end)->xprt->rcv_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002186 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002187 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2188 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002189 (res->flags & CF_STREAMER_FAST)))) {
2190 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002191 }
2192
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002193 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002194 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002195
2196 /*
2197 * Now forward all shutdown requests between both sides of the buffer
2198 */
2199
2200 /*
2201 * FIXME: this is probably where we should produce error responses.
2202 */
2203
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002204 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002205 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002206 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002207 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002208 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002209 req->wto = s->be->timeout.serverfin;
2210 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002211 }
2212 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002213
2214 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002215 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2216 channel_is_empty(res))) {
2217 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002218 if (tick_isset(sess->fe->timeout.clientfin)) {
2219 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002220 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002221 }
2222 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002223
2224 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002225 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2226 !res->analysers)
2227 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002228
2229 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002230 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2231 if (si_b->flags & SI_FL_NOHALF)
2232 si_b->flags |= SI_FL_NOLINGER;
2233 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002234 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002235 req->wto = s->be->timeout.serverfin;
2236 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002237 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002238 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002239
Willy Tarreau8f128b42014-11-28 15:07:47 +01002240 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002241 goto resync_stream_interface;
2242
Willy Tarreau8f128b42014-11-28 15:07:47 +01002243 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002244 goto resync_request;
2245
Willy Tarreau8f128b42014-11-28 15:07:47 +01002246 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002247 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002248
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002249 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002250 si_f->flags &= ~SI_FL_DONT_WAKE;
2251 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002252
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002253 /* This is needed only when debugging is enabled, to indicate
2254 * client-side or server-side close. Please note that in the unlikely
2255 * event where both sides would close at once, the sequence is reported
2256 * on the server side first.
2257 */
2258 if (unlikely((global.mode & MODE_DEBUG) &&
2259 (!(global.mode & MODE_QUIET) ||
2260 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002261 if (si_b->state == SI_ST_CLO &&
2262 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002263 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002264 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002265 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2266 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002267 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002268 }
2269
Willy Tarreau8f128b42014-11-28 15:07:47 +01002270 if (si_f->state == SI_ST_CLO &&
2271 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002272 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002273 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002274 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2275 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002276 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002277 }
2278 }
2279
Willy Tarreau8f128b42014-11-28 15:07:47 +01002280 if (likely((si_f->state != SI_ST_CLO) ||
2281 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002282
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002283 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002284 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002285
Willy Tarreau8f128b42014-11-28 15:07:47 +01002286 if (si_f->state == SI_ST_EST && obj_type(si_f->end) != OBJ_TYPE_APPCTX)
2287 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002288
Willy Tarreau8f128b42014-11-28 15:07:47 +01002289 if (si_b->state == SI_ST_EST && obj_type(si_b->end) != OBJ_TYPE_APPCTX)
2290 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002291
Willy Tarreau8f128b42014-11-28 15:07:47 +01002292 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2293 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2294 si_f->prev_state = si_f->state;
2295 si_b->prev_state = si_b->state;
2296 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2297 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002298
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002299 /* Trick: if a request is being waiting for the server to respond,
2300 * and if we know the server can timeout, we don't want the timeout
2301 * to expire on the client side first, but we're still interested
2302 * in passing data from the client to the server (eg: POST). Thus,
2303 * we can cancel the client's request timeout if the server's
2304 * request timeout is set and the server has not yet sent a response.
2305 */
2306
Willy Tarreau8f128b42014-11-28 15:07:47 +01002307 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2308 (tick_isset(req->wex) || tick_isset(res->rex))) {
2309 req->flags |= CF_READ_NOEXP;
2310 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002311 }
2312
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002313 /* When any of the stream interfaces is attached to an applet,
2314 * we have to call it here. Note that this one may wake the
2315 * task up again. If at least one applet was called, the current
2316 * task might have been woken up, in which case we don't want it
2317 * to be requeued to the wait queue but rather to the run queue
2318 * to run ASAP. The bitwise "or" in the condition ensures that
2319 * both functions are always called and that we wake up if at
2320 * least one did something.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002321 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002322 if ((si_applet_call(si_b) | si_applet_call(si_f)) != 0) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002323 if (task_in_rq(t)) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002324 t->expire = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02002325 stream_release_buffers(s);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002326 return t;
2327 }
2328 }
2329
Willy Tarreau798f4322012-11-08 14:49:17 +01002330 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002331 t->expire = tick_first(tick_first(req->rex, req->wex),
2332 tick_first(res->rex, res->wex));
2333 if (req->analysers)
2334 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002335
Willy Tarreau8f128b42014-11-28 15:07:47 +01002336 if (si_f->exp)
2337 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002338
Willy Tarreau8f128b42014-11-28 15:07:47 +01002339 if (si_b->exp)
2340 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002341
2342#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002343 fprintf(stderr,
2344 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2345 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
Willy Tarreau8f128b42014-11-28 15:07:47 +01002346 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2347 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002348#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002349
2350#ifdef DEBUG_DEV
2351 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002352 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002353 ABORT_NOW();
2354#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002355 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002356 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002357 }
2358
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002359 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002360 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002361 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002362 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002363 if (sess->listener) {
2364 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002365 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002366 sess->listener->nbconn--;
2367 if (sess->listener->state == LI_FULL)
2368 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002369
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002370 /* Dequeues all of the listeners waiting for a resource */
2371 if (!LIST_ISEMPTY(&global_listener_queue))
2372 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002373
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002374 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2375 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2376 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002377 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002378
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002379 if (unlikely((global.mode & MODE_DEBUG) &&
2380 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002381 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002382 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002383 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2384 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002385 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002386 }
2387
2388 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002389 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002390
Willy Tarreaueee5b512015-04-03 23:46:31 +02002391 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002392 int n;
2393
Willy Tarreaueee5b512015-04-03 23:46:31 +02002394 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002395 if (n < 1 || n > 5)
2396 n = 0;
2397
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002398 if (sess->fe->mode == PR_MODE_HTTP) {
2399 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002400 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002401 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002402 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002403 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002404 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002405 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002406 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002407 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002408 s->be->be_counters.p.http.comp_rsp++;
2409 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002410 }
2411
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002412 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002413 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002414 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002415 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002416 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002417 }
2418
Willy Tarreau87b09662015-04-03 00:22:06 +02002419 /* update time stats for this stream */
2420 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002421
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002422 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002423 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002424 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002425 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002426 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002427}
2428
Willy Tarreau87b09662015-04-03 00:22:06 +02002429/* Update the stream's backend and server time stats */
2430void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002431{
2432 int t_request;
2433 int t_queue;
2434 int t_connect;
2435 int t_data;
2436 int t_close;
2437 struct server *srv;
2438
2439 t_request = 0;
2440 t_queue = s->logs.t_queue;
2441 t_connect = s->logs.t_connect;
2442 t_close = s->logs.t_close;
2443 t_data = s->logs.t_data;
2444
2445 if (s->be->mode != PR_MODE_HTTP)
2446 t_data = t_connect;
2447
2448 if (t_connect < 0 || t_data < 0)
2449 return;
2450
2451 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2452 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2453
2454 t_data -= t_connect;
2455 t_connect -= t_queue;
2456 t_queue -= t_request;
2457
2458 srv = objt_server(s->target);
2459 if (srv) {
2460 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2461 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2462 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2463 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2464 }
2465 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2466 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2467 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2468 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2469}
2470
Willy Tarreau7c669d72008-06-20 15:04:11 +02002471/*
2472 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002473 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002474 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002475 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002476 * server.
2477 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002478void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002479{
2480 if (sess->srv_conn == newsrv)
2481 return;
2482
2483 if (sess->srv_conn) {
2484 sess->srv_conn->served--;
2485 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2486 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002487 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002488 }
2489
2490 if (newsrv) {
2491 newsrv->served++;
2492 if (newsrv->proxy->lbprm.server_take_conn)
2493 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002494 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002495 }
2496}
2497
Willy Tarreau84455332009-03-15 22:34:05 +01002498/* Handle server-side errors for default protocols. It is called whenever a a
2499 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002500 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002501 * them. It's installed as ->srv_error for the server-side stream_interface.
2502 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002503void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002504{
2505 int err_type = si->err_type;
2506 int err = 0, fin = 0;
2507
2508 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002509 err = SF_ERR_CLICL;
2510 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002511 }
2512 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002513 err = SF_ERR_CLICL;
2514 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002515 }
2516 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002517 err = SF_ERR_SRVTO;
2518 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002519 }
2520 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002521 err = SF_ERR_SRVCL;
2522 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002523 }
2524 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002525 err = SF_ERR_SRVTO;
2526 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002527 }
2528 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002529 err = SF_ERR_SRVCL;
2530 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002531 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002532 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002533 err = SF_ERR_RESOURCE;
2534 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002535 }
Willy Tarreau84455332009-03-15 22:34:05 +01002536 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002537 err = SF_ERR_INTERNAL;
2538 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002539 }
2540
Willy Tarreaue7dff022015-04-03 01:14:29 +02002541 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002542 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002543 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002544 s->flags |= fin;
2545}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002546
Willy Tarreaue7dff022015-04-03 01:14:29 +02002547/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002548void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002549{
Willy Tarreau87b09662015-04-03 00:22:06 +02002550 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002551 return;
2552
Willy Tarreau87b09662015-04-03 00:22:06 +02002553 channel_shutw_now(&stream->req);
2554 channel_shutr_now(&stream->res);
2555 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002556 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002557 stream->flags |= why;
2558 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002559}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002560
Willy Tarreau8b22a712010-06-18 17:46:06 +02002561/************************************************************************/
2562/* All supported ACL keywords must be declared here. */
2563/************************************************************************/
2564
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002565/* Returns a pointer to a stkctr depending on the fetch keyword name.
2566 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002567 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002568 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002569 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002570 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002571 * NULL may be returned if the designated stkctr is not tracked. For
2572 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2573 * passed. When present, the currently tracked key is then looked up
2574 * in the specified table instead of the current table. The purpose is
2575 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002576 * multiple tables). <strm> is allowed to be NULL, in which case only
2577 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002578 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002579struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002580smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002581{
2582 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002583 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002584 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002585 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002586 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002587
Willy Tarreau0f791d42013-07-23 19:56:43 +02002588 if (num == '_' - '0') {
2589 /* sc_* variant, args[0] = ctr# (mandatory) */
2590 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002591 if (num >= MAX_SESS_STKCTR)
2592 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002593 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002594 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002595 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002596 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002597
2598 if (!conn)
2599 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002600
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002601 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002602 if (!key)
2603 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002604
Willy Tarreaua65536c2013-07-22 22:40:11 +02002605 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002606 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002607 return &stkctr;
2608 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002609
2610 /* Here, <num> contains the counter number from 0 to 9 for
2611 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002612 * args[arg] is the first optional argument. We first lookup the
2613 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002614 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002615
2616 stkptr = &strm->stkctr[num];
2617 if (!strm || !stkctr_entry(stkptr)) {
2618 stkptr = &sess->stkctr[num];
2619 if (!stkctr_entry(stkptr))
2620 return NULL;
2621 }
2622
2623 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002624 if (!stksess)
2625 return NULL;
2626
Willy Tarreau0f791d42013-07-23 19:56:43 +02002627 if (unlikely(args[arg].type == ARGT_TAB)) {
2628 /* an alternate table was specified, let's look up the same key there */
2629 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002630 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002631 return &stkctr;
2632 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002633 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002634}
2635
Willy Tarreau87b09662015-04-03 00:22:06 +02002636/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002637 * currently being tracked or not.
2638 * Supports being called as "sc[0-9]_tracked" only.
2639 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002640static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002641smp_fetch_sc_tracked(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02002642 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002643{
2644 smp->flags = SMP_F_VOL_TEST;
2645 smp->type = SMP_T_BOOL;
Willy Tarreau192252e2015-04-04 01:47:55 +02002646 smp->data.uint = !!smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002647 return 1;
2648}
2649
Willy Tarreau87b09662015-04-03 00:22:06 +02002650/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002651 * frontend counters or from the src.
2652 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2653 * zero is returned if the key is new.
2654 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002655static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002656smp_fetch_sc_get_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002657 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002658{
Willy Tarreau192252e2015-04-04 01:47:55 +02002659 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002660
2661 if (!stkctr)
2662 return 0;
2663
Willy Tarreau37406352012-04-23 16:16:37 +02002664 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002665 smp->type = SMP_T_UINT;
2666 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002667
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002668 if (stkctr_entry(stkctr) != NULL) {
2669 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002670 if (!ptr)
2671 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002672 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002673 }
2674 return 1;
2675}
2676
Willy Tarreau87b09662015-04-03 00:22:06 +02002677/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002678 * tracked frontend counters or from the src.
2679 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2680 * Value zero is returned if the key is new.
2681 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002682static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002683smp_fetch_sc_gpc0_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002684 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002685{
Willy Tarreau192252e2015-04-04 01:47:55 +02002686 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002687
2688 if (!stkctr)
2689 return 0;
2690
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002691 smp->flags = SMP_F_VOL_TEST;
2692 smp->type = SMP_T_UINT;
2693 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002694 if (stkctr_entry(stkctr) != NULL) {
2695 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002696 if (!ptr)
2697 return 0; /* parameter not stored */
2698 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002699 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002700 }
2701 return 1;
2702}
2703
Willy Tarreau87b09662015-04-03 00:22:06 +02002704/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002705 * frontend counters and return it into temp integer.
2706 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002707 */
2708static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002709smp_fetch_sc_inc_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002710 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002711{
Willy Tarreau192252e2015-04-04 01:47:55 +02002712 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002713
2714 if (!stkctr)
2715 return 0;
2716
Willy Tarreau37406352012-04-23 16:16:37 +02002717 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002718 smp->type = SMP_T_UINT;
2719 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002720 if (stkctr_entry(stkctr) != NULL) {
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002721 void *ptr;
2722
2723 /* First, update gpc0_rate if it's tracked. Second, update its
2724 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2725 */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002726 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002727 if (ptr) {
2728 update_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002729 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002730 smp->data.uint = (&stktable_data_cast(ptr, gpc0_rate))->curr_ctr;
2731 }
2732
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002733 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002734 if (ptr)
2735 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
2736
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002737 }
2738 return 1;
2739}
2740
Willy Tarreau87b09662015-04-03 00:22:06 +02002741/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002742 * frontend counters and return its previous value into temp integer.
2743 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002744 */
2745static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002746smp_fetch_sc_clr_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002747 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002748{
Willy Tarreau192252e2015-04-04 01:47:55 +02002749 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002750
2751 if (!stkctr)
2752 return 0;
2753
Willy Tarreau37406352012-04-23 16:16:37 +02002754 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002755 smp->type = SMP_T_UINT;
2756 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002757 if (stkctr_entry(stkctr) != NULL) {
2758 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002759 if (!ptr)
2760 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002761 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002762 stktable_data_cast(ptr, gpc0) = 0;
2763 }
2764 return 1;
2765}
2766
Willy Tarreau87b09662015-04-03 00:22:06 +02002767/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002768 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2769 * "src_conn_cnt" only.
2770 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002771static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002772smp_fetch_sc_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002773 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002774{
Willy Tarreau192252e2015-04-04 01:47:55 +02002775 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002776
2777 if (!stkctr)
2778 return 0;
2779
Willy Tarreau37406352012-04-23 16:16:37 +02002780 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002781 smp->type = SMP_T_UINT;
2782 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002783 if (stkctr_entry(stkctr) != NULL) {
2784 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002785 if (!ptr)
2786 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002787 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002788 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002789 return 1;
2790}
2791
Willy Tarreau87b09662015-04-03 00:22:06 +02002792/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002793 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2794 * only.
2795 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002796static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002797smp_fetch_sc_conn_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002798 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002799{
Willy Tarreau192252e2015-04-04 01:47:55 +02002800 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002801
2802 if (!stkctr)
2803 return 0;
2804
Willy Tarreau37406352012-04-23 16:16:37 +02002805 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002806 smp->type = SMP_T_UINT;
2807 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002808 if (stkctr_entry(stkctr) != NULL) {
2809 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002810 if (!ptr)
2811 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002812 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002813 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002814 }
2815 return 1;
2816}
2817
Willy Tarreau87b09662015-04-03 00:22:06 +02002818/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002819 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002820 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002821 */
2822static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002823smp_fetch_src_updt_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002824 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002825{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002826 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002827 struct stksess *ts;
2828 struct stktable_key *key;
2829 void *ptr;
2830
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002831 if (!conn)
2832 return 0;
2833
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002834 key = addr_to_stktable_key(&conn->addr.from, px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002835 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002836 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002837
Willy Tarreau24e32d82012-04-23 23:55:44 +02002838 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002839
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002840 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2841 /* entry does not exist and could not be created */
2842 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002843
2844 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2845 if (!ptr)
2846 return 0; /* parameter not stored in this table */
2847
Willy Tarreauf853c462012-04-23 18:53:56 +02002848 smp->type = SMP_T_UINT;
2849 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002850 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002851 return 1;
2852}
2853
Willy Tarreau87b09662015-04-03 00:22:06 +02002854/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002855 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2856 * "src_conn_cur" only.
2857 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002858static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002859smp_fetch_sc_conn_cur(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002860 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002861{
Willy Tarreau192252e2015-04-04 01:47:55 +02002862 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002863
2864 if (!stkctr)
2865 return 0;
2866
Willy Tarreau37406352012-04-23 16:16:37 +02002867 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002868 smp->type = SMP_T_UINT;
2869 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002870 if (stkctr_entry(stkctr) != NULL) {
2871 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002872 if (!ptr)
2873 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002874 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002875 }
2876 return 1;
2877}
2878
Willy Tarreau87b09662015-04-03 00:22:06 +02002879/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02002880 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2881 * "src_sess_cnt" only.
2882 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002883static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002884smp_fetch_sc_sess_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002885 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002886{
Willy Tarreau192252e2015-04-04 01:47:55 +02002887 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02002888
2889 if (!stkctr)
2890 return 0;
2891
Willy Tarreau37406352012-04-23 16:16:37 +02002892 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002893 smp->type = SMP_T_UINT;
2894 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002895 if (stkctr_entry(stkctr) != NULL) {
2896 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002897 if (!ptr)
2898 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002899 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002900 }
2901 return 1;
2902}
2903
Willy Tarreau87b09662015-04-03 00:22:06 +02002904/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002905 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2906 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002907static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002908smp_fetch_sc_sess_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002909 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002910{
Willy Tarreau192252e2015-04-04 01:47:55 +02002911 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002912
2913 if (!stkctr)
2914 return 0;
2915
Willy Tarreau37406352012-04-23 16:16:37 +02002916 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002917 smp->type = SMP_T_UINT;
2918 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002919 if (stkctr_entry(stkctr) != NULL) {
2920 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002921 if (!ptr)
2922 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002923 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002924 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002925 }
2926 return 1;
2927}
2928
Willy Tarreau87b09662015-04-03 00:22:06 +02002929/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02002930 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2931 * "src_http_req_cnt" only.
2932 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002933static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002934smp_fetch_sc_http_req_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002935 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002936{
Willy Tarreau192252e2015-04-04 01:47:55 +02002937 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02002938
2939 if (!stkctr)
2940 return 0;
2941
Willy Tarreau37406352012-04-23 16:16:37 +02002942 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002943 smp->type = SMP_T_UINT;
2944 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002945 if (stkctr_entry(stkctr) != NULL) {
2946 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002947 if (!ptr)
2948 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002949 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002950 }
2951 return 1;
2952}
2953
Willy Tarreau87b09662015-04-03 00:22:06 +02002954/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02002955 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2956 * "src_http_req_rate" only.
2957 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002958static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002959smp_fetch_sc_http_req_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002960 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002961{
Willy Tarreau192252e2015-04-04 01:47:55 +02002962 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02002963
2964 if (!stkctr)
2965 return 0;
2966
Willy Tarreau37406352012-04-23 16:16:37 +02002967 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002968 smp->type = SMP_T_UINT;
2969 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002970 if (stkctr_entry(stkctr) != NULL) {
2971 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002972 if (!ptr)
2973 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002974 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02002975 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002976 }
2977 return 1;
2978}
2979
Willy Tarreau87b09662015-04-03 00:22:06 +02002980/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02002981 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2982 * "src_http_err_cnt" only.
2983 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002984static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002985smp_fetch_sc_http_err_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002986 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002987{
Willy Tarreau192252e2015-04-04 01:47:55 +02002988 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02002989
2990 if (!stkctr)
2991 return 0;
2992
Willy Tarreau37406352012-04-23 16:16:37 +02002993 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002994 smp->type = SMP_T_UINT;
2995 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002996 if (stkctr_entry(stkctr) != NULL) {
2997 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002998 if (!ptr)
2999 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003000 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003001 }
3002 return 1;
3003}
3004
Willy Tarreau87b09662015-04-03 00:22:06 +02003005/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003006 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3007 * "src_http_err_rate" only.
3008 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003009static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003010smp_fetch_sc_http_err_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003011 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003012{
Willy Tarreau192252e2015-04-04 01:47:55 +02003013 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003014
3015 if (!stkctr)
3016 return 0;
3017
Willy Tarreau37406352012-04-23 16:16:37 +02003018 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003019 smp->type = SMP_T_UINT;
3020 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003021 if (stkctr_entry(stkctr) != NULL) {
3022 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003023 if (!ptr)
3024 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003025 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003026 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003027 }
3028 return 1;
3029}
3030
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003031/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003032 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003033 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3034 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003035static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003036smp_fetch_sc_kbytes_in(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003037 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003038{
Willy Tarreau192252e2015-04-04 01:47:55 +02003039 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003040
3041 if (!stkctr)
3042 return 0;
3043
Willy Tarreau37406352012-04-23 16:16:37 +02003044 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003045 smp->type = SMP_T_UINT;
3046 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003047 if (stkctr_entry(stkctr) != NULL) {
3048 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003049 if (!ptr)
3050 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003051 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003052 }
3053 return 1;
3054}
3055
Willy Tarreau613fe992013-07-23 17:39:19 +02003056/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003057 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003058 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003059 */
3060static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003061smp_fetch_sc_bytes_in_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003062 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003063{
Willy Tarreau192252e2015-04-04 01:47:55 +02003064 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003065
3066 if (!stkctr)
3067 return 0;
3068
Willy Tarreau37406352012-04-23 16:16:37 +02003069 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003070 smp->type = SMP_T_UINT;
3071 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003072 if (stkctr_entry(stkctr) != NULL) {
3073 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003074 if (!ptr)
3075 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003076 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003077 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003078 }
3079 return 1;
3080}
3081
Willy Tarreau53aea102013-07-23 17:39:02 +02003082/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003083 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003084 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3085 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003086static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003087smp_fetch_sc_kbytes_out(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02003088 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003089{
Willy Tarreau192252e2015-04-04 01:47:55 +02003090 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003091
3092 if (!stkctr)
3093 return 0;
3094
Willy Tarreau37406352012-04-23 16:16:37 +02003095 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003096 smp->type = SMP_T_UINT;
3097 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003098 if (stkctr_entry(stkctr) != NULL) {
3099 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003100 if (!ptr)
3101 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003102 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003103 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003104 return 1;
3105}
3106
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003107/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003108 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003109 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003110 */
3111static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003112smp_fetch_sc_bytes_out_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003113 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003114{
Willy Tarreau192252e2015-04-04 01:47:55 +02003115 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003116
3117 if (!stkctr)
3118 return 0;
3119
Willy Tarreau37406352012-04-23 16:16:37 +02003120 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003121 smp->type = SMP_T_UINT;
3122 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003123 if (stkctr_entry(stkctr) != NULL) {
3124 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003125 if (!ptr)
3126 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003127 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003128 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003129 }
3130 return 1;
3131}
3132
Willy Tarreau87b09662015-04-03 00:22:06 +02003133/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003134 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3135 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003136static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003137smp_fetch_sc_trackers(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003138 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003139{
Willy Tarreau192252e2015-04-04 01:47:55 +02003140 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003141
Willy Tarreau563eef42013-07-23 18:32:02 +02003142 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003143 return 0;
3144
Willy Tarreau563eef42013-07-23 18:32:02 +02003145 smp->flags = SMP_F_VOL_TEST;
3146 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003147 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003148 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003149}
3150
Willy Tarreau34db1082012-04-19 17:16:54 +02003151/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003152 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003153 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003154static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003155smp_fetch_table_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003156 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003157{
Willy Tarreau37406352012-04-23 16:16:37 +02003158 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003159 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003160 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003161 return 1;
3162}
3163
Willy Tarreau34db1082012-04-19 17:16:54 +02003164/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003165 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003166 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003167static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003168smp_fetch_table_avl(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003169 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003170{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003171 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003172 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003173 smp->type = SMP_T_UINT;
3174 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003175 return 1;
3176}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003177
Willy Tarreau61612d42012-04-19 18:42:05 +02003178/* Note: must not be declared <const> as its list will be overwritten.
3179 * Please take care of keeping this list alphabetically sorted.
3180 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003181static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003182 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003183}};
3184
Willy Tarreau281c7992013-01-08 01:23:27 +01003185/* Note: must not be declared <const> as its list will be overwritten.
3186 * Please take care of keeping this list alphabetically sorted.
3187 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003188static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003189 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3190 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3191 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3192 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3193 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3194 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3195 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3196 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3197 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3198 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3199 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3200 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3201 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3202 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3203 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3204 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3205 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3206 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3207 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3208 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3209 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3210 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3211 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3212 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3213 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3214 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3215 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3216 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3217 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3218 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3219 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3220 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3221 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3222 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3223 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3224 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3225 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3226 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3227 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3228 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3229 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3230 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3231 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3232 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3233 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3234 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3235 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3236 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3237 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3238 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3239 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3240 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3241 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3242 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3243 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3244 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3245 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3246 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3247 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3248 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3249 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3250 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3251 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3252 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3253 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3254 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3255 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3256 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3257 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3258 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3259 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3260 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3261 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3262 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3263 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3264 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3265 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3266 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3267 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3268 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3269 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3270 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3271 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3272 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3273 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3274 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3275 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3276 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3277 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3278 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3279 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3280 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3281 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3282 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3283 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3284 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003285 { /* END */ },
3286}};
3287
Willy Tarreau8b22a712010-06-18 17:46:06 +02003288__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003289static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003290{
Willy Tarreau281c7992013-01-08 01:23:27 +01003291 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003292 acl_register_keywords(&acl_kws);
3293}
3294
Willy Tarreaubaaee002006-06-26 02:48:02 +02003295/*
3296 * Local variables:
3297 * c-indent-level: 8
3298 * c-basic-offset: 8
3299 * End:
3300 */