blob: 7fa399c6a034713e3035b0a06d1e85aec6acb199 [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 Tarreau2542b532012-08-31 16:01:23 +020073 int ret;
Willy Tarreau20d46a52012-12-09 15:55:40 +010074 int i;
Willy Tarreau2542b532012-08-31 16:01:23 +020075
76 ret = -1; /* assume unrecoverable error by default */
77
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020078 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
79 goto out_return;
80
81 /* minimum stream initialization required for an embryonic stream is
82 * fairly low. We need very little to execute L4 ACLs, then we need a
83 * task to make the client-side connection live on its own.
84 * - flags
85 * - stick-entry tracking
86 */
87 s->flags = 0;
88 s->logs.logwait = p->to_log;
89 s->logs.level = 0;
90
91 /* Initialise the current rule list pointer to NULL. We are sure that
92 * any rulelist match the NULL pointer.
93 */
94 s->current_rule_list = NULL;
95
96 memset(s->stkctr, 0, sizeof(s->stkctr));
97
98 s->sess = sess;
99 s->si[0].flags = SI_FL_NONE;
100 s->si[1].flags = SI_FL_ISBACK;
101
102 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
103 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
104 s->uniq_id = global.req_count++;
105
Willy Tarreau87b09662015-04-03 00:22:06 +0200106 /* OK, we're keeping the stream, so let's properly initialize the stream */
107 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200108 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100109 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200110
Willy Tarreaue7dff022015-04-03 01:14:29 +0200111 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200112 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200113
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200114 s->task = t;
Willy Tarreau2542b532012-08-31 16:01:23 +0200115 t->process = l->handler;
116 t->context = s;
117 t->expire = TICK_ETERNITY;
118
Willy Tarreau87b09662015-04-03 00:22:06 +0200119 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200120 * This changes later when switching rules are executed or
121 * when the default backend is assigned.
122 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200123 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200124 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100125 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200126 s->req_cap = NULL;
127 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200128
Willy Tarreau87b09662015-04-03 00:22:06 +0200129 /* Let's count a stream now */
Willy Tarreaub36b4242010-06-04 20:59:39 +0200130 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200131
Willy Tarreaub4c84932013-07-23 19:15:30 +0200132 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200133 void *ptr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200134 struct stkctr *stkctr = &sess->stkctr[i];
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200135
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200136 if (!stkctr_entry(stkctr))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100137 continue;
138
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200139 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200140 if (ptr)
141 stktable_data_cast(ptr, sess_cnt)++;
142
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200143 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200144 if (ptr)
145 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200146 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200147 }
148
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200149 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100150 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200151 si_set_state(&s->si[0], SI_ST_EST);
152
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200153 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200154 if (conn)
155 si_attach_conn(&s->si[0], conn);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200156
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200157 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200158 s->si[0].flags |= SI_FL_INDEP_STR;
159
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200160 /* pre-initialize the other side's stream interface to an INIT state. The
161 * callbacks will be initialized before attempting to connect.
162 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100163 si_reset(&s->si[1]);
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200164 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200165
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200166 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200167 s->si[1].flags |= SI_FL_INDEP_STR;
168
Willy Tarreau87b09662015-04-03 00:22:06 +0200169 stream_init_srv_conn(s);
Willy Tarreau10b688f2015-03-13 16:43:12 +0100170 s->target = l->default_target; /* used by peers and CLI */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200171 s->pend_pos = NULL;
172
173 /* init store persistence */
174 s->store_count = 0;
175
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100176 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100177 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200178
179 /* activate default analysers enabled for this listener */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100180 s->req.analysers = l->analysers;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200181
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100182 s->req.wto = TICK_ETERNITY;
183 s->req.rto = TICK_ETERNITY;
184 s->req.rex = TICK_ETERNITY;
185 s->req.wex = TICK_ETERNITY;
186 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200187
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100188 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100189 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100190 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200191
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200192 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100193 s->req.flags |= CF_NEVER_WAIT;
194 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200195 }
196
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100197 s->res.rto = TICK_ETERNITY;
198 s->res.wto = TICK_ETERNITY;
199 s->res.rex = TICK_ETERNITY;
200 s->res.wex = TICK_ETERNITY;
201 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200202
Willy Tarreaueee5b512015-04-03 23:46:31 +0200203 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100204
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100205 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100206
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200207 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200208 if (conn)
209 conn_data_want_recv(conn);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200210
Willy Tarreauf2b98742015-04-05 01:30:42 +0200211 /* FIXME: we shouldn't restrict ourselves to connections but for now
212 * the only ->accept() only works with sessions.
213 */
214 if (conn && p->accept && (ret = p->accept(s)) <= 0) {
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100215 /* Either we had an unrecoverable error (<0) or work is
216 * finished (=0, eg: monitoring), in both situations,
217 * we can release everything and close.
218 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100219 goto out_free_strm;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200220 }
221
Willy Tarreauf2b98742015-04-05 01:30:42 +0200222 if (conn) {
223 /* if logs require transport layer information, note it on the connection */
224 if (s->logs.logwait & LW_XPRT)
225 conn->flags |= CO_FL_XPRT_TRACKED;
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200226
Willy Tarreauf2b98742015-04-05 01:30:42 +0200227 /* we want the connection handler to notify the stream interface about updates. */
228 conn->flags |= CO_FL_WAKE_DATA;
229 }
Willy Tarreau2542b532012-08-31 16:01:23 +0200230
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200231 /* it is important not to call the wakeup function directly but to
232 * pass through task_wakeup(), because this one knows how to apply
233 * priorities to tasks.
234 */
235 task_wakeup(t, TASK_WOKEN_INIT);
236 return 1;
237
238 /* Error unrolling */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100239 out_free_strm:
Willy Tarreau3b246412014-11-25 17:10:33 +0100240 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200241 pool_free2(pool2_stream, s);
242 out_return:
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100243 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200244}
245
Willy Tarreaubaaee002006-06-26 02:48:02 +0200246/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200247 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200248 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200249static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200251 struct session *sess = strm_sess(s);
252 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100253 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200254 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200255 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100256
Willy Tarreaubaaee002006-06-26 02:48:02 +0200257 if (s->pend_pos)
258 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100259
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100260 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200261 if (s->flags & SF_CURR_SESS) {
262 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100263 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100264 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100265 if (may_dequeue_tasks(objt_server(s->target), s->be))
266 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100267 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100268
Willy Tarreau7c669d72008-06-20 15:04:11 +0200269 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200270 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200271 * it should normally be only the same as the one above,
272 * so this should not happen in fact.
273 */
274 sess_change_server(s, NULL);
275 }
276
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100277 if (s->req.pipe)
278 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100279
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100280 if (s->res.pipe)
281 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100282
Willy Tarreaubf883e02014-11-25 21:10:35 +0100283 /* We may still be present in the buffer wait queue */
284 if (!LIST_ISEMPTY(&s->buffer_wait)) {
285 LIST_DEL(&s->buffer_wait);
286 LIST_INIT(&s->buffer_wait);
287 }
288
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100289 b_drop(&s->req.buf);
290 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100291 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200292 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200293
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100294 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200295 if (s->txn)
296 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100297
Willy Tarreau1e954912012-10-12 17:50:05 +0200298 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200299 if (cli_conn)
300 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200301
Willy Tarreaua4cda672010-06-06 18:28:49 +0200302 for (i = 0; i < s->store_count; i++) {
303 if (!s->store[i].ts)
304 continue;
305 stksess_free(s->store[i].table, s->store[i].ts);
306 s->store[i].ts = NULL;
307 }
308
Willy Tarreaueee5b512015-04-03 23:46:31 +0200309 if (s->txn) {
310 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
311 pool_free2(pool2_http_txn, s->txn);
312 s->txn = NULL;
313 }
314
Willy Tarreau92fb9832007-10-16 17:34:28 +0200315 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200316 pool_free2(fe->rsp_cap_pool, s->res_cap);
317 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100319
Willy Tarreau87b09662015-04-03 00:22:06 +0200320 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200321
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100322 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100323 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200324 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100325 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100326 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100327 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200328 if (s->list.n != &streams)
329 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100330 bref->ref = s->list.n;
331 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100332 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200333 si_release_endpoint(&s->si[1]);
334 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200335
336 /* FIXME: for now we have a 1:1 relation between stream and session so
337 * the stream must free the session.
338 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200339 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200340 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200341
342 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200343 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200344 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200345 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200346 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200347 pool_flush2(pool2_requri);
348 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200349 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200350 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100351 pool_flush2(pool2_connection);
352 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200353 pool_flush2(fe->req_cap_pool);
354 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200355 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200356}
357
Willy Tarreau78955f42014-12-28 13:09:02 +0100358/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
359 * that it's not the last available buffer or it's the response buffer. Unless
360 * the buffer is the response buffer, an extra control is made so that we always
361 * keep <tune.buffers.reserved> buffers available after this allocation. To be
362 * called at the beginning of recv() callbacks to ensure that the required
363 * buffers are properly allocated. Returns 0 in case of failure, non-zero
364 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100365 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200366int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100367{
Willy Tarreau87b09662015-04-03 00:22:06 +0200368 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100369 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100370 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100371
Willy Tarreau78955f42014-12-28 13:09:02 +0100372 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100373 margin = global.tune.reserved_bufs;
374
Willy Tarreau78955f42014-12-28 13:09:02 +0100375 s = chn_sess(chn);
376
377 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100378 if (b)
379 return 1;
380
Willy Tarreaubf883e02014-11-25 21:10:35 +0100381 if (LIST_ISEMPTY(&s->buffer_wait))
382 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100383 return 0;
384}
385
Willy Tarreau87b09662015-04-03 00:22:06 +0200386/* Allocates a work buffer for stream <s>. It is meant to be called inside
387 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaua24adf02014-11-27 01:11:56 +0100388 * to work fine. For a regular connection, only the response is needed so that
389 * an error message may be built and returned. In case where the initiator is
390 * an applet (eg: peers), then we need to allocate the request buffer for the
391 * applet to be able to send its data (in this case a response is not needed).
392 * Request buffers are never picked from the reserved pool, but response
393 * buffers may be allocated from the reserve. This is critical to ensure that
394 * a response may always flow and will never block a server from releasing a
395 * connection. Returns 0 in case of failure, non-zero otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100396 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200397int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100398{
Willy Tarreaua24adf02014-11-27 01:11:56 +0100399 int margin;
400 struct buffer **buf;
401
402 if (objt_appctx(s->si[0].end)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100403 buf = &s->req.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100404 margin = global.tune.reserved_bufs;
405 }
406 else {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100407 buf = &s->res.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100408 margin = 0;
409 }
410
Willy Tarreaubf883e02014-11-25 21:10:35 +0100411 if (!LIST_ISEMPTY(&s->buffer_wait)) {
412 LIST_DEL(&s->buffer_wait);
413 LIST_INIT(&s->buffer_wait);
414 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100415
Willy Tarreaua24adf02014-11-27 01:11:56 +0100416 if (b_alloc_margin(buf, margin))
Willy Tarreau656859d2014-11-25 19:46:36 +0100417 return 1;
418
Willy Tarreaubf883e02014-11-25 21:10:35 +0100419 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100420 return 0;
421}
422
423/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100424 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200425 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100426 * a single buffer, so it makes sense to try to wake two up when two buffers
427 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100428 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200429void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100430{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100431 if (s->req.buf->size && buffer_empty(s->req.buf))
432 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100433
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100434 if (s->res.buf->size && buffer_empty(s->res.buf))
435 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100436
Willy Tarreaubf883e02014-11-25 21:10:35 +0100437 /* if we're certain to have at least 1 buffer available, and there is
438 * someone waiting, we can wake up a waiter and offer them.
439 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100440 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200441 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100442}
443
Willy Tarreau87b09662015-04-03 00:22:06 +0200444/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100445 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200446 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100447 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200448void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100449{
Willy Tarreau87b09662015-04-03 00:22:06 +0200450 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100451
452 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100453 if (rqlimit <= run_queue)
454 break;
455
Willy Tarreaubf883e02014-11-25 21:10:35 +0100456 if (sess->task->state & TASK_RUNNING)
457 continue;
458
459 LIST_DEL(&sess->buffer_wait);
460 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100461 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100462 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100463}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200464
465/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200466int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200467{
Willy Tarreau87b09662015-04-03 00:22:06 +0200468 LIST_INIT(&streams);
469 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
470 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200471}
472
Willy Tarreau87b09662015-04-03 00:22:06 +0200473void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100474{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200475 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100476 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100477 void *ptr;
478 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100479
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100480 bytes = s->req.total - s->logs.bytes_in;
481 s->logs.bytes_in = s->req.total;
482 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200483 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100484
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100485 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100486
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100487 if (objt_server(s->target))
488 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200489
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200490 if (sess->listener && sess->listener->counters)
491 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200492
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100493 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200494 struct stkctr *stkctr = &s->stkctr[i];
495
496 if (!stkctr_entry(stkctr)) {
497 stkctr = &sess->stkctr[i];
498 if (!stkctr_entry(stkctr))
499 continue;
500 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200501
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200502 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100503 if (ptr)
504 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200505
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200506 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100507 if (ptr)
508 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200509 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100510 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100511 }
512
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100513 bytes = s->res.total - s->logs.bytes_out;
514 s->logs.bytes_out = s->res.total;
515 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200516 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100517
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100518 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100519
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100520 if (objt_server(s->target))
521 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200522
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200523 if (sess->listener && sess->listener->counters)
524 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200525
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100526 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200527 struct stkctr *stkctr = &s->stkctr[i];
528
529 if (!stkctr_entry(stkctr)) {
530 stkctr = &sess->stkctr[i];
531 if (!stkctr_entry(stkctr))
532 continue;
533 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200534
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200535 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100536 if (ptr)
537 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200538
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200539 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100540 if (ptr)
541 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200542 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100543 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100544 }
545}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100547/* This function is called with (si->state == SI_ST_CON) meaning that a
548 * connection was attempted and that the file descriptor is already allocated.
549 * We must check for establishment, error and abort. Possible output states
550 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
551 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200552 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100553 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200554static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100555{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100556 struct stream_interface *si = &s->si[1];
557 struct channel *req = &s->req;
558 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200559 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100560
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100561 /* If we got an error, or if nothing happened and the connection timed
562 * out, we must give up. The CER state handler will take care of retry
563 * attempts and error reports.
564 */
565 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100566 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100567 /* Some data were sent past the connection establishment,
568 * so we need to pretend we're established to log correctly
569 * and let later states handle the failure.
570 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100571 si->state = SI_ST_EST;
572 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100573 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100574 return 1;
575 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100576 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100577 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100578
Willy Tarreauf79c8172013-10-21 16:30:56 +0200579 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100580
581 if (si->err_type)
582 return 0;
583
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100584 if (si->flags & SI_FL_ERR)
585 si->err_type = SI_ET_CONN_ERR;
586 else
587 si->err_type = SI_ET_CONN_TO;
588 return 0;
589 }
590
591 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100592 if (!(req->flags & CF_WRITE_PARTIAL) &&
593 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200594 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
595 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100596 s->be->options & PR_O_ABRT_CLOSE)))) {
597 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200598 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100599 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100600 if (s->srv_error)
601 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100602 return 1;
603 }
604
605 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200606 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100607 return 1;
608
609 /* OK, this means that a connection succeeded. The caller will be
610 * responsible for handling the transition from CON to EST.
611 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100612 si->state = SI_ST_EST;
613 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100614 return 1;
615}
616
617/* This function is called with (si->state == SI_ST_CER) meaning that a
618 * previous connection attempt has failed and that the file descriptor
619 * has already been released. Possible causes include asynchronous error
620 * notification and time out. Possible output states are SI_ST_CLO when
621 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
622 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
623 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
624 * marked as in error state. It returns 0.
625 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200626static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100627{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100628 struct stream_interface *si = &s->si[1];
629
Willy Tarreau87b09662015-04-03 00:22:06 +0200630 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100631 if (objt_server(s->target)) {
632 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100633
Willy Tarreaue7dff022015-04-03 01:14:29 +0200634 if (s->flags & SF_CURR_SESS) {
635 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100636 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100637 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100638 }
639
640 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200641 si->conn_retries--;
642 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100643 if (!si->err_type) {
644 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100645 }
646
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100647 if (objt_server(s->target))
648 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100649 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100650 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100651 if (may_dequeue_tasks(objt_server(s->target), s->be))
652 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100653
654 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200655 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100656 s->req.flags |= CF_WRITE_ERROR;
657 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100658
659 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100660 if (s->srv_error)
661 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100662 return 0;
663 }
664
665 /* If the "redispatch" option is set on the backend, we are allowed to
666 * retry on another server for the last retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200667 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100668 * bit to ignore any persistence cookie. We won't count a retry nor a
669 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200670 * If the connection is not persistent, the balancing algorithm is not
671 * determinist (round robin) and there is more than one active server,
672 * we accept to perform an immediate redispatch without waiting since
673 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100674 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200675 if (objt_server(s->target) &&
676 (si->conn_retries == 0 ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200677 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Willy Tarreau33a14e52014-06-13 17:49:40 +0200678 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200679 s->be->options & PR_O_REDISP && !(s->flags & SF_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100680 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100681 if (may_dequeue_tasks(objt_server(s->target), s->be))
682 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100683
Willy Tarreaue7dff022015-04-03 01:14:29 +0200684 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100685 si->state = SI_ST_REQ;
686 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100687 if (objt_server(s->target))
688 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100689 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100690 si->state = SI_ST_ASS;
691 }
692
693 if (si->flags & SI_FL_ERR) {
694 /* The error was an asynchronous connection error, and we will
695 * likely have to retry connecting to the same server, most
696 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200697 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100698 */
699
Willy Tarreaub0290662014-06-13 17:04:44 +0200700 int delay = 1000;
701
702 if (s->be->timeout.connect && s->be->timeout.connect < delay)
703 delay = s->be->timeout.connect;
704
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100705 if (!si->err_type)
706 si->err_type = SI_ET_CONN_ERR;
707
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200708 /* only wait when we're retrying on the same server */
709 if (si->state == SI_ST_ASS ||
710 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
711 (s->be->srv_act <= 1)) {
712 si->state = SI_ST_TAR;
713 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
714 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100715 return 0;
716 }
717 return 0;
718}
719
720/*
721 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200722 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200723 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100724 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200725static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100726{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100727 struct stream_interface *si = &s->si[1];
728 struct channel *req = &s->req;
729 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100730
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100731 /* First, centralize the timers information */
732 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
733 si->exp = TICK_ETERNITY;
734
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100735 if (objt_server(s->target))
736 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100737
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100738 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739 /* if the user wants to log as soon as possible, without counting
740 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200741 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100742 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100743 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100745 }
746 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100747 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100748 }
749
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200750 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200751 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200752 if (req->flags & CF_WAKE_CONNECT) {
753 req->flags |= CF_WAKE_ONCE;
754 req->flags &= ~CF_WAKE_CONNECT;
755 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200756 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200757 /* real connections have timeouts */
758 req->wto = s->be->timeout.server;
759 rep->rto = s->be->timeout.server;
760 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100761 req->wex = TICK_ETERNITY;
762}
763
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100764/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
765 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100766 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
767 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
768 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100769 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200770static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100771{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100772 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100773 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100774 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100775
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100776 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 +0100777 now_ms, __FUNCTION__,
778 s,
Willy Tarreau103197d2014-11-28 15:26:12 +0100779 req, s->rep,
780 req->rex, s->res.wex,
781 req->flags, s->res.flags,
782 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 +0100783
784 if (si->state == SI_ST_ASS) {
785 /* Server assigned to connection request, we have to try to connect now */
786 int conn_err;
787
788 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100789 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100790
Willy Tarreaue7dff022015-04-03 01:14:29 +0200791 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100792 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100793 if (srv)
794 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500795 if (srv)
796 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100797 return;
798 }
799
800 /* We have received a synchronous error. We might have to
801 * abort, retry immediately or redispatch.
802 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200803 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100804 if (!si->err_type) {
805 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100806 }
807
Willy Tarreau827aee92011-03-10 16:55:02 +0100808 if (srv)
809 srv_inc_sess_ctr(srv);
810 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500811 srv_set_sess_last(srv);
812 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100813 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100814 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100815
Willy Tarreau87b09662015-04-03 00:22:06 +0200816 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100817 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100818 if (may_dequeue_tasks(srv, s->be))
819 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100820
821 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200822 si_shutr(si);
823 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100824 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100825
826 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
827
Willy Tarreau87b09662015-04-03 00:22:06 +0200828 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100829 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100830 if (s->srv_error)
831 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100832 return;
833 }
834
835 /* We are facing a retryable error, but we don't want to run a
836 * turn-around now, as the problem is likely a source port
837 * allocation problem, so we want to retry now.
838 */
839 si->state = SI_ST_CER;
840 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100841 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100842 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
843 return;
844 }
845 else if (si->state == SI_ST_QUE) {
846 /* connection request was queued, check for any update */
847 if (!s->pend_pos) {
848 /* The connection is not in the queue anymore. Either
849 * we have a server connection slot available and we
850 * go directly to the assigned state, or we need to
851 * load-balance first and go to the INI state.
852 */
853 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200854 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100855 si->state = SI_ST_REQ;
856 else {
857 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
858 si->state = SI_ST_ASS;
859 }
860 return;
861 }
862
863 /* Connection request still in queue... */
864 if (si->flags & SI_FL_EXP) {
865 /* ... and timeout expired */
866 si->exp = TICK_ETERNITY;
867 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100868 if (srv)
869 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100870 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200871 si_shutr(si);
872 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100873 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100874 if (!si->err_type)
875 si->err_type = SI_ET_QUEUE_TO;
876 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100877 if (s->srv_error)
878 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100879 return;
880 }
881
882 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100883 if ((req->flags & (CF_READ_ERROR)) ||
884 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
885 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100886 /* give up */
887 si->exp = TICK_ETERNITY;
888 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200889 si_shutr(si);
890 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100891 si->err_type |= SI_ET_QUEUE_ABRT;
892 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100893 if (s->srv_error)
894 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100895 return;
896 }
897
898 /* Nothing changed */
899 return;
900 }
901 else if (si->state == SI_ST_TAR) {
902 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100903 if ((req->flags & (CF_READ_ERROR)) ||
904 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
905 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100906 /* give up */
907 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200908 si_shutr(si);
909 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100910 si->err_type |= SI_ET_CONN_ABRT;
911 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100912 if (s->srv_error)
913 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100914 return;
915 }
916
917 if (!(si->flags & SI_FL_EXP))
918 return; /* still in turn-around */
919
920 si->exp = TICK_ETERNITY;
921
Willy Tarreau87b09662015-04-03 00:22:06 +0200922 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100923 * marked "assigned".
924 * FIXME: Should we force a redispatch attempt when the server is down ?
925 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200926 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100927 si->state = SI_ST_ASS;
928 else
929 si->state = SI_ST_REQ;
930 return;
931 }
932}
933
Willy Tarreau87b09662015-04-03 00:22:06 +0200934/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900935 * also counts a failed request if the server state has not reached the request
936 * stage.
937 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200938static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900939{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200940 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900941 if (s->si[1].state < SI_ST_REQ) {
942
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200943 strm_fe(s)->fe_counters.failed_req++;
944 if (strm_li(s)->counters)
945 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900946
Willy Tarreaue7dff022015-04-03 01:14:29 +0200947 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900948 }
949 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200950 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900951 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200952 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900953 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200954 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900955 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200956 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900957 }
958}
959
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100960/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100961 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
962 * a real connection to a server, indicating that a server has been assigned,
963 * or SI_ST_EST for a successful connection to an applet. It may also return
964 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100965 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200966static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100967{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100968 struct stream_interface *si = &s->si[1];
969
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100970 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 +0100971 now_ms, __FUNCTION__,
972 s,
973 s->req, s->rep,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100974 s->req.rex, s->res.wex,
975 s->req.flags, s->res.flags,
Willy Tarreau350f4872014-11-28 14:42:25 +0100976 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 +0100977
978 if (si->state != SI_ST_REQ)
979 return;
980
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100981 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
982 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100983 struct appctx *appctx = objt_appctx(si->end);
984
985 if (!appctx || appctx->applet != __objt_applet(s->target))
986 appctx = stream_int_register_handler(si, objt_applet(s->target));
987
988 if (!appctx) {
989 /* No more memory, let's immediately abort. Force the
990 * error code to ignore the ERR_LOCAL which is not a
991 * real error.
992 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200993 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100994
995 si_shutr(si);
996 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100997 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +0100998 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100999 si->state = SI_ST_CLO;
1000 if (s->srv_error)
1001 s->srv_error(s, si);
1002 return;
1003 }
1004
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001005 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001006 si->state = SI_ST_EST;
1007 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001008 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001009 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001010 return;
1011 }
1012
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001013 /* Try to assign a server */
1014 if (srv_redispatch_connect(s) != 0) {
1015 /* We did not get a server. Either we queued the
1016 * connection request, or we encountered an error.
1017 */
1018 if (si->state == SI_ST_QUE)
1019 return;
1020
1021 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001022 si_shutr(si);
1023 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001024 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001025 if (!si->err_type)
1026 si->err_type = SI_ET_CONN_OTHER;
1027 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001028 if (s->srv_error)
1029 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001030 return;
1031 }
1032
1033 /* The server is assigned */
1034 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1035 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001036 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001037}
1038
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001039/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001040 * if appropriate. The default_backend rule is also considered, then the
1041 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001042 * It returns 1 if the processing can continue on next analysers, or zero if it
1043 * either needs more data or wants to immediately abort the request.
1044 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001045static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001046{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001047 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001048 struct session *sess = s->sess;
1049 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001050
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001051 req->analysers &= ~an_bit;
1052 req->analyse_exp = TICK_ETERNITY;
1053
Willy Tarreau87b09662015-04-03 00:22:06 +02001054 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 +02001055 now_ms, __FUNCTION__,
1056 s,
1057 req,
1058 req->rex, req->wex,
1059 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001060 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001061 req->analysers);
1062
1063 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001064 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001065 struct switching_rule *rule;
1066
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001067 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001068 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001069
Willy Tarreauf51658d2014-04-23 01:21:56 +02001070 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001071 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001072 ret = acl_pass(ret);
1073 if (rule->cond->pol == ACL_COND_UNLESS)
1074 ret = !ret;
1075 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001076
1077 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001078 /* If the backend name is dynamic, try to resolve the name.
1079 * If we can't resolve the name, or if any error occurs, break
1080 * the loop and fallback to the default backend.
1081 */
1082 struct proxy *backend;
1083
1084 if (rule->dynamic) {
1085 struct chunk *tmp = get_trash_chunk();
1086 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1087 break;
1088 backend = findproxy(tmp->str, PR_CAP_BE);
1089 if (!backend)
1090 break;
1091 }
1092 else
1093 backend = rule->be.backend;
1094
Willy Tarreau87b09662015-04-03 00:22:06 +02001095 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001096 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001097 break;
1098 }
1099 }
1100
1101 /* To ensure correct connection accounting on the backend, we
1102 * have to assign one if it was not set (eg: a listen). This
1103 * measure also takes care of correctly setting the default
1104 * backend if any.
1105 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001106 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001107 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001108 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001109 }
1110
Willy Tarreaufb356202010-08-03 14:02:05 +02001111 /* 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 +02001112 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001113 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1114 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001115 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001116
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001117 /* 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 +02001118 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001119 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001120 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001121 int ret = 1;
1122
1123 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001124 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 +01001125 ret = acl_pass(ret);
1126 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1127 ret = !ret;
1128 }
1129
1130 if (ret) {
1131 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001132 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001133 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001134 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001135 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001136 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001137 break;
1138 }
1139 }
1140
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001141 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001142
1143 sw_failed:
1144 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001145 channel_abort(&s->req);
1146 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001147
Willy Tarreaue7dff022015-04-03 01:14:29 +02001148 if (!(s->flags & SF_ERR_MASK))
1149 s->flags |= SF_ERR_RESOURCE;
1150 if (!(s->flags & SF_FINST_MASK))
1151 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001152
Willy Tarreaueee5b512015-04-03 23:46:31 +02001153 if (s->txn)
1154 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001155 s->req.analysers = 0;
1156 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001157 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001158}
1159
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001160/* This stream analyser works on a request. It applies all use-server rules on
1161 * it then returns 1. The data must already be present in the buffer otherwise
1162 * they won't match. It always returns 1.
1163 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001164static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001165{
1166 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001167 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001168 struct server_rule *rule;
1169
Willy Tarreau87b09662015-04-03 00:22:06 +02001170 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 +02001171 now_ms, __FUNCTION__,
1172 s,
1173 req,
1174 req->rex, req->wex,
1175 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001176 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001177 req->analysers);
1178
Willy Tarreaue7dff022015-04-03 01:14:29 +02001179 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001180 list_for_each_entry(rule, &px->server_rules, list) {
1181 int ret;
1182
Willy Tarreau192252e2015-04-04 01:47:55 +02001183 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001184 ret = acl_pass(ret);
1185 if (rule->cond->pol == ACL_COND_UNLESS)
1186 ret = !ret;
1187
1188 if (ret) {
1189 struct server *srv = rule->srv.ptr;
1190
Willy Tarreau892337c2014-05-13 23:41:20 +02001191 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001192 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001193 (s->flags & SF_FORCE_PRST)) {
1194 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001195 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001196 break;
1197 }
1198 /* if the server is not UP, let's go on with next rules
1199 * just in case another one is suited.
1200 */
1201 }
1202 }
1203 }
1204
1205 req->analysers &= ~an_bit;
1206 req->analyse_exp = TICK_ETERNITY;
1207 return 1;
1208}
1209
Emeric Brun1d33b292010-01-04 15:47:17 +01001210/* This stream analyser works on a request. It applies all sticking rules on
1211 * it then returns 1. The data must already be present in the buffer otherwise
1212 * they won't match. It always returns 1.
1213 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001214static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001215{
1216 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001217 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001218 struct sticking_rule *rule;
1219
Willy Tarreau87b09662015-04-03 00:22:06 +02001220 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 +01001221 now_ms, __FUNCTION__,
1222 s,
1223 req,
1224 req->rex, req->wex,
1225 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001226 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001227 req->analysers);
1228
1229 list_for_each_entry(rule, &px->sticking_rules, list) {
1230 int ret = 1 ;
1231 int i;
1232
Willy Tarreau9667a802013-12-09 12:52:13 +01001233 /* Only the first stick store-request of each table is applied
1234 * and other ones are ignored. The purpose is to allow complex
1235 * configurations which look for multiple entries by decreasing
1236 * order of precision and to stop at the first which matches.
1237 * An example could be a store of the IP address from an HTTP
1238 * header first, then from the source if not found.
1239 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001240 for (i = 0; i < s->store_count; i++) {
1241 if (rule->table.t == s->store[i].table)
1242 break;
1243 }
1244
1245 if (i != s->store_count)
1246 continue;
1247
1248 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001249 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001250 ret = acl_pass(ret);
1251 if (rule->cond->pol == ACL_COND_UNLESS)
1252 ret = !ret;
1253 }
1254
1255 if (ret) {
1256 struct stktable_key *key;
1257
Willy Tarreau192252e2015-04-04 01:47:55 +02001258 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 +01001259 if (!key)
1260 continue;
1261
1262 if (rule->flags & STK_IS_MATCH) {
1263 struct stksess *ts;
1264
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001265 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001266 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001267 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001268 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001269
1270 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001271 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1272 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001273 if (node) {
1274 struct server *srv;
1275
1276 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001277 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001278 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001279 (s->flags & SF_FORCE_PRST)) {
1280 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001281 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001282 }
1283 }
1284 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001285 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001286 }
1287 }
1288 if (rule->flags & STK_IS_STORE) {
1289 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1290 struct stksess *ts;
1291
1292 ts = stksess_new(rule->table.t, key);
1293 if (ts) {
1294 s->store[s->store_count].table = rule->table.t;
1295 s->store[s->store_count++].ts = ts;
1296 }
1297 }
1298 }
1299 }
1300 }
1301
1302 req->analysers &= ~an_bit;
1303 req->analyse_exp = TICK_ETERNITY;
1304 return 1;
1305}
1306
1307/* This stream analyser works on a response. It applies all store rules on it
1308 * then returns 1. The data must already be present in the buffer otherwise
1309 * they won't match. It always returns 1.
1310 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001311static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001312{
1313 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001314 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001315 struct sticking_rule *rule;
1316 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001317 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001318
Willy Tarreau87b09662015-04-03 00:22:06 +02001319 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 +01001320 now_ms, __FUNCTION__,
1321 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001322 rep,
1323 rep->rex, rep->wex,
1324 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001325 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001326 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001327
1328 list_for_each_entry(rule, &px->storersp_rules, list) {
1329 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001330
Willy Tarreau9667a802013-12-09 12:52:13 +01001331 /* Only the first stick store-response of each table is applied
1332 * and other ones are ignored. The purpose is to allow complex
1333 * configurations which look for multiple entries by decreasing
1334 * order of precision and to stop at the first which matches.
1335 * An example could be a store of a set-cookie value, with a
1336 * fallback to a parameter found in a 302 redirect.
1337 *
1338 * The store-response rules are not allowed to override the
1339 * store-request rules for the same table, but they may coexist.
1340 * Thus we can have up to one store-request entry and one store-
1341 * response entry for the same table at any time.
1342 */
1343 for (i = nbreq; i < s->store_count; i++) {
1344 if (rule->table.t == s->store[i].table)
1345 break;
1346 }
1347
1348 /* skip existing entries for this table */
1349 if (i < s->store_count)
1350 continue;
1351
Emeric Brun1d33b292010-01-04 15:47:17 +01001352 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001353 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001354 ret = acl_pass(ret);
1355 if (rule->cond->pol == ACL_COND_UNLESS)
1356 ret = !ret;
1357 }
1358
1359 if (ret) {
1360 struct stktable_key *key;
1361
Willy Tarreau192252e2015-04-04 01:47:55 +02001362 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 +01001363 if (!key)
1364 continue;
1365
Willy Tarreau37e340c2013-12-06 23:05:21 +01001366 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001367 struct stksess *ts;
1368
1369 ts = stksess_new(rule->table.t, key);
1370 if (ts) {
1371 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001372 s->store[s->store_count++].ts = ts;
1373 }
1374 }
1375 }
1376 }
1377
1378 /* process store request and store response */
1379 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001380 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001381 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001382
Willy Tarreauc93cd162014-05-13 15:54:22 +02001383 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001384 stksess_free(s->store[i].table, s->store[i].ts);
1385 s->store[i].ts = NULL;
1386 continue;
1387 }
1388
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001389 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1390 if (ts) {
1391 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001392 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001393 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001394 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001395 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001396 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001397
1398 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001399 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001400 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001401 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001402 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001403
1404 rep->analysers &= ~an_bit;
1405 rep->analyse_exp = TICK_ETERNITY;
1406 return 1;
1407}
1408
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001409/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001410 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001411 */
1412#define UPDATE_ANALYSERS(real, list, back, flag) { \
1413 list = (((list) & ~(flag)) | ~(back)) & (real); \
1414 back = real; \
1415 if (!(list)) \
1416 break; \
1417 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1418 continue; \
1419}
1420
Willy Tarreau87b09662015-04-03 00:22:06 +02001421/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001422 * then puts it back to the wait queue in a clean state, or cleans up its
1423 * resources if it must be deleted. Returns in <next> the date the task wants
1424 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1425 * nothing too many times, the request and response buffers flags are monitored
1426 * and each function is called only if at least another function has changed at
1427 * least one flag it is interested in.
1428 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001429struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001430{
Willy Tarreau827aee92011-03-10 16:55:02 +01001431 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001432 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001433 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001434 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001435 unsigned int rq_prod_last, rq_cons_last;
1436 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001437 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001438 struct channel *req, *res;
1439 struct stream_interface *si_f, *si_b;
1440
1441 req = &s->req;
1442 res = &s->res;
1443
1444 si_f = &s->si[0];
1445 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001446
1447 //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 +01001448 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001449
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001450 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001451 if (s->txn)
1452 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001453
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001454 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001455 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1456 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001457
1458 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001459 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1460 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001461
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001462 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001463 si_f->flags |= SI_FL_DONT_WAKE;
1464 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001465
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001466 /* 1a: Check for low level timeouts if needed. We just set a flag on
1467 * stream interfaces when their timeouts have expired.
1468 */
1469 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001470 stream_int_check_timeouts(si_f);
1471 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001472
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001473 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001474 * for future reads or writes. Note: this will also concern upper layers
1475 * but we do not touch any other flag. We must be careful and correctly
1476 * detect state changes when calling them.
1477 */
1478
Willy Tarreau8f128b42014-11-28 15:07:47 +01001479 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001480
Willy Tarreau8f128b42014-11-28 15:07:47 +01001481 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1482 si_b->flags |= SI_FL_NOLINGER;
1483 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001484 }
1485
Willy Tarreau8f128b42014-11-28 15:07:47 +01001486 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1487 if (si_f->flags & SI_FL_NOHALF)
1488 si_f->flags |= SI_FL_NOLINGER;
1489 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001490 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001491
Willy Tarreau8f128b42014-11-28 15:07:47 +01001492 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001493
Willy Tarreau8f128b42014-11-28 15:07:47 +01001494 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1495 si_f->flags |= SI_FL_NOLINGER;
1496 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001497 }
1498
Willy Tarreau8f128b42014-11-28 15:07:47 +01001499 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1500 if (si_b->flags & SI_FL_NOHALF)
1501 si_b->flags |= SI_FL_NOLINGER;
1502 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001503 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001504
1505 /* Once in a while we're woken up because the task expires. But
1506 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001507 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001508 * timeout needs to be refreshed.
1509 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001510 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001511 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1512 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001513 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001514 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1515 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001516 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001517
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001518 /* below we may emit error messages so we have to ensure that we have
1519 * our buffers properly allocated.
1520 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001521 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001522 /* No buffer available, we've been subscribed to the list of
1523 * buffer waiters, let's wait for our turn.
1524 */
1525 goto update_exp_and_leave;
1526 }
1527
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001528 /* 1b: check for low-level errors reported at the stream interface.
1529 * First we check if it's a retryable error (in which case we don't
1530 * want to tell the buffer). Otherwise we report the error one level
1531 * upper by setting flags into the buffers. Note that the side towards
1532 * the client cannot have connect (hence retryable) errors. Also, the
1533 * connection setup code must be able to deal with any type of abort.
1534 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001535 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001536 if (unlikely(si_f->flags & SI_FL_ERR)) {
1537 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1538 si_shutr(si_f);
1539 si_shutw(si_f);
1540 stream_int_report_error(si_f);
1541 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001542 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001543 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001544 if (srv)
1545 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001546 if (!(s->flags & SF_ERR_MASK))
1547 s->flags |= SF_ERR_CLICL;
1548 if (!(s->flags & SF_FINST_MASK))
1549 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001550 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001551 }
1552 }
1553
Willy Tarreau8f128b42014-11-28 15:07:47 +01001554 if (unlikely(si_b->flags & SI_FL_ERR)) {
1555 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1556 si_shutr(si_b);
1557 si_shutw(si_b);
1558 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001559 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001560 if (srv)
1561 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001562 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001563 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001564 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001565 if (srv)
1566 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001567 if (!(s->flags & SF_ERR_MASK))
1568 s->flags |= SF_ERR_SRVCL;
1569 if (!(s->flags & SF_FINST_MASK))
1570 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001571 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001572 }
1573 /* note: maybe we should process connection errors here ? */
1574 }
1575
Willy Tarreau8f128b42014-11-28 15:07:47 +01001576 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001577 /* we were trying to establish a connection on the server side,
1578 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1579 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001580 if (unlikely(!sess_update_st_con_tcp(s)))
1581 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001582 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001583 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001584
1585 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1586 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1587 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1588 */
1589 }
1590
Willy Tarreau8f128b42014-11-28 15:07:47 +01001591 rq_prod_last = si_f->state;
1592 rq_cons_last = si_b->state;
1593 rp_cons_last = si_f->state;
1594 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001595
1596 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001597 /* Check for connection closure */
1598
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001599 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001600 "[%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 +01001601 now_ms, __FUNCTION__, __LINE__,
1602 t,
1603 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001604 req, res,
1605 req->rex, res->wex,
1606 req->flags, res->flags,
1607 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1608 si_f->err_type, si_b->err_type,
1609 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001610
1611 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001612 if (unlikely(si_f->state == SI_ST_DIS))
1613 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001614
1615 /* When a server-side connection is released, we have to count it and
1616 * check for pending connections on this server.
1617 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001618 if (unlikely(si_b->state == SI_ST_DIS)) {
1619 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001620 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001621 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001622 if (s->flags & SF_CURR_SESS) {
1623 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001624 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001625 }
1626 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001627 if (may_dequeue_tasks(srv, s->be))
1628 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001629 }
1630 }
1631
1632 /*
1633 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1634 * at this point.
1635 */
1636
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001637 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001638 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001639 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1640 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1641 si_f->state != rq_prod_last ||
1642 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001643 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001644 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001645
Willy Tarreau8f128b42014-11-28 15:07:47 +01001646 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001647 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001648 unsigned int ana_list;
1649 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001650
Willy Tarreau90deb182010-01-07 00:20:41 +01001651 /* it's up to the analysers to stop new connections,
1652 * disable reading or closing. Note: if an analyser
1653 * disables any of these bits, it is responsible for
1654 * enabling them again when it disables itself, so
1655 * that other analysers are called in similar conditions.
1656 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001657 channel_auto_read(req);
1658 channel_auto_connect(req);
1659 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001660
1661 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001662 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001663 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001664 * the list when not needed. Any analyser may return 0
1665 * to break out of the loop, either because of missing
1666 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001667 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001668 * analyser, and we may loop again if other analysers
1669 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001670 *
1671 * We build a list of analysers to run. We evaluate all
1672 * of these analysers in the order of the lower bit to
1673 * the higher bit. This ordering is very important.
1674 * An analyser will often add/remove other analysers,
1675 * including itself. Any changes to itself have no effect
1676 * on the loop. If it removes any other analysers, we
1677 * want those analysers not to be called anymore during
1678 * this loop. If it adds an analyser that is located
1679 * after itself, we want it to be scheduled for being
1680 * processed during the loop. If it adds an analyser
1681 * which is located before it, we want it to switch to
1682 * it immediately, even if it has already been called
1683 * once but removed since.
1684 *
1685 * In order to achieve this, we compare the analyser
1686 * list after the call with a copy of it before the
1687 * call. The work list is fed with analyser bits that
1688 * appeared during the call. Then we compare previous
1689 * work list with the new one, and check the bits that
1690 * appeared. If the lowest of these bits is lower than
1691 * the current bit, it means we have enabled a previous
1692 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001693 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001694
Willy Tarreau8f128b42014-11-28 15:07:47 +01001695 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001696 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001697 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001698
Willy Tarreaufb356202010-08-03 14:02:05 +02001699 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001700 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001701 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001702 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001703 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001704
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001705 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001706 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001707 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001708 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001709 }
1710
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001711 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001712 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001713 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001714 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001715 }
1716
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001717 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001718 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001719 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001720 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001721 }
1722
Willy Tarreaufb356202010-08-03 14:02:05 +02001723 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001724 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001725 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001726 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001727 }
1728
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001729 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001730 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001731 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001732 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001733 }
1734
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001735 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001736 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001737 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001738 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001739 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001740
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001741 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001742 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001743 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001744 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001745 }
1746
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001747 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001748 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001749 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001750 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001751 }
1752
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001753 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001754 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001755 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001756 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001757 }
Emeric Brun647caf12009-06-30 17:57:00 +02001758
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001759 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001760 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001761 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001762 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001763 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001764
Emeric Brun1d33b292010-01-04 15:47:17 +01001765 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001766 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001767 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001768 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001769 }
1770
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001771 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001772 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001773 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001774 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001775 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001776 break;
1777 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001778 }
Willy Tarreau84455332009-03-15 22:34:05 +01001779
Willy Tarreau8f128b42014-11-28 15:07:47 +01001780 rq_prod_last = si_f->state;
1781 rq_cons_last = si_b->state;
1782 req->flags &= ~CF_WAKE_ONCE;
1783 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001784
Willy Tarreau8f128b42014-11-28 15:07:47 +01001785 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001786 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001787 }
1788
Willy Tarreau576507f2010-01-07 00:09:04 +01001789 /* we'll monitor the request analysers while parsing the response,
1790 * because some response analysers may indirectly enable new request
1791 * analysers (eg: HTTP keep-alive).
1792 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001793 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001794
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001795 resync_response:
1796 /* Analyse response */
1797
Willy Tarreau8f128b42014-11-28 15:07:47 +01001798 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1799 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1800 si_f->state != rp_cons_last ||
1801 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001802 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001803 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001804
Willy Tarreau8f128b42014-11-28 15:07:47 +01001805 if ((res->flags & CF_MASK_ANALYSER) &&
1806 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001807 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1808 * for some free space in the response buffer, so we might need to call
1809 * it when something changes in the response buffer, but still we pass
1810 * it the request buffer. Note that the SI state might very well still
1811 * be zero due to us returning a flow of redirects!
1812 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001813 res->analysers &= ~AN_REQ_ALL;
1814 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001815 }
1816
Willy Tarreau8f128b42014-11-28 15:07:47 +01001817 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001818 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001819 unsigned int ana_list;
1820 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001821
Willy Tarreau90deb182010-01-07 00:20:41 +01001822 /* it's up to the analysers to stop disable reading or
1823 * closing. Note: if an analyser disables any of these
1824 * bits, it is responsible for enabling them again when
1825 * it disables itself, so that other analysers are called
1826 * in similar conditions.
1827 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001828 channel_auto_read(res);
1829 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001830
1831 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001832 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001833 * to MSB. The analysers must remove themselves from
1834 * the list when not needed. Any analyser may return 0
1835 * to break out of the loop, either because of missing
1836 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001837 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001838 * analyser, and we may loop again if other analysers
1839 * are added in the middle.
1840 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001841
Willy Tarreau8f128b42014-11-28 15:07:47 +01001842 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001843 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001844 /* Warning! ensure that analysers are always placed in ascending order! */
1845
Emeric Brun97679e72010-09-23 17:56:44 +02001846 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001847 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001848 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001849 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001850 }
1851
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001852 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001853 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001854 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001855 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001856 }
1857
Emeric Brun1d33b292010-01-04 15:47:17 +01001858 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001859 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001860 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001861 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001862 }
1863
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001864 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001865 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001866 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001867 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001868 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001870 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001871 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001872 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001873 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001874 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001875 break;
1876 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001877 }
1878
Willy Tarreau8f128b42014-11-28 15:07:47 +01001879 rp_cons_last = si_f->state;
1880 rp_prod_last = si_b->state;
1881 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001882
Willy Tarreau8f128b42014-11-28 15:07:47 +01001883 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001884 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001885 }
1886
Willy Tarreau576507f2010-01-07 00:09:04 +01001887 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001888 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001889 goto resync_request;
1890
Willy Tarreau8f128b42014-11-28 15:07:47 +01001891 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001892 goto resync_request;
1893
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001894 /* FIXME: here we should call protocol handlers which rely on
1895 * both buffers.
1896 */
1897
1898
1899 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001900 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001901 * we're just in a data phase here since it means we have not
1902 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001903 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001904 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001905 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001906 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001907 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001908 req->analysers = 0;
1909 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001910 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001911 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001912 if (srv)
1913 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001914 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001915 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001916 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001917 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001918 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001919 if (srv)
1920 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001921 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001922 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001923 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001924 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001925 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001926 if (srv)
1927 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001928 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001929 }
1930 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001931 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001932 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001933 if (srv)
1934 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001935 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001936 }
Willy Tarreau84455332009-03-15 22:34:05 +01001937 sess_set_term_flags(s);
1938 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001939 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001940 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001941 res->analysers = 0;
1942 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001943 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001944 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001945 if (srv)
1946 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001947 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001948 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001949 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001950 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001951 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001952 if (srv)
1953 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001954 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001955 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001956 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001957 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001958 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001959 if (srv)
1960 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001961 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001962 }
1963 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001964 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001965 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001966 if (srv)
1967 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001968 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001969 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001970 sess_set_term_flags(s);
1971 }
Willy Tarreau84455332009-03-15 22:34:05 +01001972 }
1973
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001974 /*
1975 * Here we take care of forwarding unhandled data. This also includes
1976 * connection establishments and shutdown requests.
1977 */
1978
1979
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001980 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001981 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001982 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001983 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001984 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001985 if (unlikely(!req->analysers &&
1986 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
1987 (si_f->state >= SI_ST_EST) &&
1988 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01001989 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001990 * attached to it. If any data are left in, we'll permit them to
1991 * move.
1992 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001993 channel_auto_read(req);
1994 channel_auto_connect(req);
1995 channel_auto_close(req);
1996 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001997
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001998 /* We'll let data flow between the producer (if still connected)
1999 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002000 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002001 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2002 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002003 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002004
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002005 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002006 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2007 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002008 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002009 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2010 (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 +01002011 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002012 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2013 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002014 (req->flags & CF_STREAMER_FAST)))) {
2015 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002016 }
2017
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002018 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002019 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002020
2021 /*
2022 * Now forward all shutdown requests between both sides of the buffer
2023 */
2024
Willy Tarreau520d95e2009-09-19 21:04:57 +02002025 /* first, let's check if the request buffer needs to shutdown(write), which may
2026 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002027 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2028 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002029 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002030 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002031 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002032 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002033 if (tick_isset(sess->fe->timeout.clientfin)) {
2034 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002035 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002036 }
2037 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002038
2039 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002040 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2041 channel_is_empty(req))) {
2042 if (req->flags & CF_READ_ERROR)
2043 si_b->flags |= SI_FL_NOLINGER;
2044 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002045 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002046 res->rto = s->be->timeout.serverfin;
2047 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002048 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002049 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002050
2051 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002052 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2053 !req->analysers))
2054 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002055
2056 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002057 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2058 if (si_f->flags & SI_FL_NOHALF)
2059 si_f->flags |= SI_FL_NOLINGER;
2060 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002061 if (tick_isset(sess->fe->timeout.clientfin)) {
2062 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002063 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002064 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002065 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002066
Willy Tarreau520d95e2009-09-19 21:04:57 +02002067 /* it's possible that an upper layer has requested a connection setup or abort.
2068 * There are 2 situations where we decide to establish a new connection :
2069 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002070 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002071 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002072 if (si_b->state == SI_ST_INI) {
2073 if (!(req->flags & CF_SHUTW)) {
2074 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002075 /* If we have an appctx, there is no connect method, so we
2076 * immediately switch to the connected state, otherwise we
2077 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002078 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002079 si_b->state = SI_ST_REQ; /* new connection requested */
2080 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002081 }
Willy Tarreau73201222009-08-16 18:27:24 +02002082 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002083 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002084 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2085 channel_shutw_now(req); /* fix buffer flags upon abort */
2086 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002087 }
Willy Tarreau92795622009-03-06 12:51:23 +01002088 }
2089
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002090
2091 /* we may have a pending connection request, or a connection waiting
2092 * for completion.
2093 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002094 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002095 do {
2096 /* nb: step 1 might switch from QUE to ASS, but we first want
2097 * to give a chance to step 2 to perform a redirect if needed.
2098 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002099 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002100 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002101 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002102 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002103
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002104 /* applets directly go to the ESTABLISHED state. Similarly,
2105 * servers experience the same fate when their connection
2106 * is reused.
2107 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002108 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002109 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002110
Willy Tarreaub44c8732013-12-31 23:16:50 +01002111 /* Now we can add the server name to a header (if requested) */
2112 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002113 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002114 (s->be->server_id_hdr_name != NULL) &&
2115 (s->be->mode == PR_MODE_HTTP) &&
2116 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002117 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002118 }
2119
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002120 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002121 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002122 http_perform_server_redirect(s, si_b);
2123 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002124 }
2125
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002126 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002127 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002128 goto resync_stream_interface;
2129
Willy Tarreau815a9b22010-07-27 17:15:12 +02002130 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002131 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002132 goto resync_request;
2133
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002134 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002135
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002136 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002137 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002138 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002139 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002140 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002141 if (unlikely(!res->analysers &&
2142 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2143 (si_b->state >= SI_ST_EST) &&
2144 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002145 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002146 * attached to it. If any data are left in, we'll permit them to
2147 * move.
2148 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002149 channel_auto_read(res);
2150 channel_auto_close(res);
2151 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002152
2153 /* We'll let data flow between the producer (if still connected)
2154 * to the consumer.
2155 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002156 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2157 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002158
2159 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002160 * tunnel timeout set, use it now. Note that we must respect
2161 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002162 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002163 if (!req->analysers && s->be->timeout.tunnel) {
2164 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002165 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002166
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002167 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2168 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002169 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2170 res->rto = s->be->timeout.serverfin;
2171 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2172 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002173 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2174 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002175
Willy Tarreau8f128b42014-11-28 15:07:47 +01002176 req->rex = tick_add(now_ms, req->rto);
2177 req->wex = tick_add(now_ms, req->wto);
2178 res->rex = tick_add(now_ms, res->rto);
2179 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002180 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002181 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002182
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002183 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002184 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2185 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002186 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002187 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2188 (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 +01002189 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002190 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2191 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002192 (res->flags & CF_STREAMER_FAST)))) {
2193 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002194 }
2195
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002196 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002197 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002198
2199 /*
2200 * Now forward all shutdown requests between both sides of the buffer
2201 */
2202
2203 /*
2204 * FIXME: this is probably where we should produce error responses.
2205 */
2206
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002207 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002208 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002209 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002210 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002211 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002212 req->wto = s->be->timeout.serverfin;
2213 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002214 }
2215 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002216
2217 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002218 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2219 channel_is_empty(res))) {
2220 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002221 if (tick_isset(sess->fe->timeout.clientfin)) {
2222 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002223 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002224 }
2225 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002226
2227 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002228 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2229 !res->analysers)
2230 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002231
2232 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002233 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2234 if (si_b->flags & SI_FL_NOHALF)
2235 si_b->flags |= SI_FL_NOLINGER;
2236 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002237 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002238 req->wto = s->be->timeout.serverfin;
2239 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002240 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002241 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002242
Willy Tarreau8f128b42014-11-28 15:07:47 +01002243 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002244 goto resync_stream_interface;
2245
Willy Tarreau8f128b42014-11-28 15:07:47 +01002246 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002247 goto resync_request;
2248
Willy Tarreau8f128b42014-11-28 15:07:47 +01002249 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002250 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002251
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002252 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002253 si_f->flags &= ~SI_FL_DONT_WAKE;
2254 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002255
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002256 /* This is needed only when debugging is enabled, to indicate
2257 * client-side or server-side close. Please note that in the unlikely
2258 * event where both sides would close at once, the sequence is reported
2259 * on the server side first.
2260 */
2261 if (unlikely((global.mode & MODE_DEBUG) &&
2262 (!(global.mode & MODE_QUIET) ||
2263 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002264 if (si_b->state == SI_ST_CLO &&
2265 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002266 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002267 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002268 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2269 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002270 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002271 }
2272
Willy Tarreau8f128b42014-11-28 15:07:47 +01002273 if (si_f->state == SI_ST_CLO &&
2274 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002275 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002276 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002277 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2278 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002279 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002280 }
2281 }
2282
Willy Tarreau8f128b42014-11-28 15:07:47 +01002283 if (likely((si_f->state != SI_ST_CLO) ||
2284 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002285
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002286 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002287 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002288
Willy Tarreau8f128b42014-11-28 15:07:47 +01002289 if (si_f->state == SI_ST_EST && obj_type(si_f->end) != OBJ_TYPE_APPCTX)
2290 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002291
Willy Tarreau8f128b42014-11-28 15:07:47 +01002292 if (si_b->state == SI_ST_EST && obj_type(si_b->end) != OBJ_TYPE_APPCTX)
2293 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002294
Willy Tarreau8f128b42014-11-28 15:07:47 +01002295 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2296 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2297 si_f->prev_state = si_f->state;
2298 si_b->prev_state = si_b->state;
2299 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2300 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002301
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002302 /* Trick: if a request is being waiting for the server to respond,
2303 * and if we know the server can timeout, we don't want the timeout
2304 * to expire on the client side first, but we're still interested
2305 * in passing data from the client to the server (eg: POST). Thus,
2306 * we can cancel the client's request timeout if the server's
2307 * request timeout is set and the server has not yet sent a response.
2308 */
2309
Willy Tarreau8f128b42014-11-28 15:07:47 +01002310 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2311 (tick_isset(req->wex) || tick_isset(res->rex))) {
2312 req->flags |= CF_READ_NOEXP;
2313 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002314 }
2315
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002316 /* When any of the stream interfaces is attached to an applet,
2317 * we have to call it here. Note that this one may wake the
2318 * task up again. If at least one applet was called, the current
2319 * task might have been woken up, in which case we don't want it
2320 * to be requeued to the wait queue but rather to the run queue
2321 * to run ASAP. The bitwise "or" in the condition ensures that
2322 * both functions are always called and that we wake up if at
2323 * least one did something.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002324 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002325 if ((si_applet_call(si_b) | si_applet_call(si_f)) != 0) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002326 if (task_in_rq(t)) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002327 t->expire = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02002328 stream_release_buffers(s);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002329 return t;
2330 }
2331 }
2332
Willy Tarreau798f4322012-11-08 14:49:17 +01002333 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002334 t->expire = tick_first(tick_first(req->rex, req->wex),
2335 tick_first(res->rex, res->wex));
2336 if (req->analysers)
2337 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002338
Willy Tarreau8f128b42014-11-28 15:07:47 +01002339 if (si_f->exp)
2340 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002341
Willy Tarreau8f128b42014-11-28 15:07:47 +01002342 if (si_b->exp)
2343 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002344
2345#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002346 fprintf(stderr,
2347 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2348 " 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 +01002349 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2350 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002351#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002352
2353#ifdef DEBUG_DEV
2354 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002355 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002356 ABORT_NOW();
2357#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002358 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002359 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002360 }
2361
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002362 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002363 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002364 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002365 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002366 if (sess->listener) {
2367 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002368 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002369 sess->listener->nbconn--;
2370 if (sess->listener->state == LI_FULL)
2371 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002372
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002373 /* Dequeues all of the listeners waiting for a resource */
2374 if (!LIST_ISEMPTY(&global_listener_queue))
2375 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002376
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002377 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2378 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2379 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002380 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002381
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002382 if (unlikely((global.mode & MODE_DEBUG) &&
2383 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002384 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002385 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002386 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2387 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002388 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002389 }
2390
2391 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002392 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002393
Willy Tarreaueee5b512015-04-03 23:46:31 +02002394 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002395 int n;
2396
Willy Tarreaueee5b512015-04-03 23:46:31 +02002397 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002398 if (n < 1 || n > 5)
2399 n = 0;
2400
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002401 if (sess->fe->mode == PR_MODE_HTTP) {
2402 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002403 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002404 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002405 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002406 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002407 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002408 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002409 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002410 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002411 s->be->be_counters.p.http.comp_rsp++;
2412 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002413 }
2414
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002415 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002416 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002417 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002418 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002419 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002420 }
2421
Willy Tarreau87b09662015-04-03 00:22:06 +02002422 /* update time stats for this stream */
2423 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002424
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002425 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002426 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002427 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002428 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002429 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002430}
2431
Willy Tarreau87b09662015-04-03 00:22:06 +02002432/* Update the stream's backend and server time stats */
2433void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002434{
2435 int t_request;
2436 int t_queue;
2437 int t_connect;
2438 int t_data;
2439 int t_close;
2440 struct server *srv;
2441
2442 t_request = 0;
2443 t_queue = s->logs.t_queue;
2444 t_connect = s->logs.t_connect;
2445 t_close = s->logs.t_close;
2446 t_data = s->logs.t_data;
2447
2448 if (s->be->mode != PR_MODE_HTTP)
2449 t_data = t_connect;
2450
2451 if (t_connect < 0 || t_data < 0)
2452 return;
2453
2454 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2455 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2456
2457 t_data -= t_connect;
2458 t_connect -= t_queue;
2459 t_queue -= t_request;
2460
2461 srv = objt_server(s->target);
2462 if (srv) {
2463 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2464 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2465 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2466 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2467 }
2468 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2469 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2470 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2471 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2472}
2473
Willy Tarreau7c669d72008-06-20 15:04:11 +02002474/*
2475 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002476 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002477 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002478 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002479 * server.
2480 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002481void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002482{
2483 if (sess->srv_conn == newsrv)
2484 return;
2485
2486 if (sess->srv_conn) {
2487 sess->srv_conn->served--;
2488 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2489 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002490 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002491 }
2492
2493 if (newsrv) {
2494 newsrv->served++;
2495 if (newsrv->proxy->lbprm.server_take_conn)
2496 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002497 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002498 }
2499}
2500
Willy Tarreau84455332009-03-15 22:34:05 +01002501/* Handle server-side errors for default protocols. It is called whenever a a
2502 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002503 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002504 * them. It's installed as ->srv_error for the server-side stream_interface.
2505 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002506void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002507{
2508 int err_type = si->err_type;
2509 int err = 0, fin = 0;
2510
2511 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002512 err = SF_ERR_CLICL;
2513 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002514 }
2515 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002516 err = SF_ERR_CLICL;
2517 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002518 }
2519 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002520 err = SF_ERR_SRVTO;
2521 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002522 }
2523 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002524 err = SF_ERR_SRVCL;
2525 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002526 }
2527 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002528 err = SF_ERR_SRVTO;
2529 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002530 }
2531 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002532 err = SF_ERR_SRVCL;
2533 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002534 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002535 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002536 err = SF_ERR_RESOURCE;
2537 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002538 }
Willy Tarreau84455332009-03-15 22:34:05 +01002539 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002540 err = SF_ERR_INTERNAL;
2541 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002542 }
2543
Willy Tarreaue7dff022015-04-03 01:14:29 +02002544 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002545 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002546 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002547 s->flags |= fin;
2548}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002549
Willy Tarreaue7dff022015-04-03 01:14:29 +02002550/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002551void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002552{
Willy Tarreau87b09662015-04-03 00:22:06 +02002553 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002554 return;
2555
Willy Tarreau87b09662015-04-03 00:22:06 +02002556 channel_shutw_now(&stream->req);
2557 channel_shutr_now(&stream->res);
2558 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002559 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002560 stream->flags |= why;
2561 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002562}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002563
Willy Tarreau8b22a712010-06-18 17:46:06 +02002564/************************************************************************/
2565/* All supported ACL keywords must be declared here. */
2566/************************************************************************/
2567
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002568/* Returns a pointer to a stkctr depending on the fetch keyword name.
2569 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002570 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002571 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002572 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002573 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002574 * NULL may be returned if the designated stkctr is not tracked. For
2575 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2576 * passed. When present, the currently tracked key is then looked up
2577 * in the specified table instead of the current table. The purpose is
2578 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002579 * multiple tables). <strm> is allowed to be NULL, in which case only
2580 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002581 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002582struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002583smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002584{
2585 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002586 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002587 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002588 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002589 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002590
Willy Tarreau0f791d42013-07-23 19:56:43 +02002591 if (num == '_' - '0') {
2592 /* sc_* variant, args[0] = ctr# (mandatory) */
2593 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002594 if (num >= MAX_SESS_STKCTR)
2595 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002596 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002597 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002598 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002599 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002600
2601 if (!conn)
2602 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002603
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002604 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002605 if (!key)
2606 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002607
Willy Tarreaua65536c2013-07-22 22:40:11 +02002608 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002609 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002610 return &stkctr;
2611 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002612
2613 /* Here, <num> contains the counter number from 0 to 9 for
2614 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002615 * args[arg] is the first optional argument. We first lookup the
2616 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002617 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002618
2619 stkptr = &strm->stkctr[num];
2620 if (!strm || !stkctr_entry(stkptr)) {
2621 stkptr = &sess->stkctr[num];
2622 if (!stkctr_entry(stkptr))
2623 return NULL;
2624 }
2625
2626 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002627 if (!stksess)
2628 return NULL;
2629
Willy Tarreau0f791d42013-07-23 19:56:43 +02002630 if (unlikely(args[arg].type == ARGT_TAB)) {
2631 /* an alternate table was specified, let's look up the same key there */
2632 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002633 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002634 return &stkctr;
2635 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002636 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002637}
2638
Willy Tarreau87b09662015-04-03 00:22:06 +02002639/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002640 * currently being tracked or not.
2641 * Supports being called as "sc[0-9]_tracked" only.
2642 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002643static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002644smp_fetch_sc_tracked(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02002645 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002646{
2647 smp->flags = SMP_F_VOL_TEST;
2648 smp->type = SMP_T_BOOL;
Willy Tarreau192252e2015-04-04 01:47:55 +02002649 smp->data.uint = !!smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002650 return 1;
2651}
2652
Willy Tarreau87b09662015-04-03 00:22:06 +02002653/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002654 * frontend counters or from the src.
2655 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2656 * zero is returned if the key is new.
2657 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002658static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002659smp_fetch_sc_get_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002660 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002661{
Willy Tarreau192252e2015-04-04 01:47:55 +02002662 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002663
2664 if (!stkctr)
2665 return 0;
2666
Willy Tarreau37406352012-04-23 16:16:37 +02002667 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002668 smp->type = SMP_T_UINT;
2669 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002670
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002671 if (stkctr_entry(stkctr) != NULL) {
2672 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002673 if (!ptr)
2674 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002675 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002676 }
2677 return 1;
2678}
2679
Willy Tarreau87b09662015-04-03 00:22:06 +02002680/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002681 * tracked frontend counters or from the src.
2682 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2683 * Value zero is returned if the key is new.
2684 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002685static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002686smp_fetch_sc_gpc0_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002687 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002688{
Willy Tarreau192252e2015-04-04 01:47:55 +02002689 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002690
2691 if (!stkctr)
2692 return 0;
2693
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002694 smp->flags = SMP_F_VOL_TEST;
2695 smp->type = SMP_T_UINT;
2696 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002697 if (stkctr_entry(stkctr) != NULL) {
2698 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002699 if (!ptr)
2700 return 0; /* parameter not stored */
2701 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002702 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002703 }
2704 return 1;
2705}
2706
Willy Tarreau87b09662015-04-03 00:22:06 +02002707/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002708 * frontend counters and return it into temp integer.
2709 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002710 */
2711static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002712smp_fetch_sc_inc_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002713 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002714{
Willy Tarreau192252e2015-04-04 01:47:55 +02002715 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002716
2717 if (!stkctr)
2718 return 0;
2719
Willy Tarreau37406352012-04-23 16:16:37 +02002720 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002721 smp->type = SMP_T_UINT;
2722 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002723 if (stkctr_entry(stkctr) != NULL) {
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002724 void *ptr;
2725
2726 /* First, update gpc0_rate if it's tracked. Second, update its
2727 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2728 */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002729 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002730 if (ptr) {
2731 update_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002732 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002733 smp->data.uint = (&stktable_data_cast(ptr, gpc0_rate))->curr_ctr;
2734 }
2735
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002736 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002737 if (ptr)
2738 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
2739
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002740 }
2741 return 1;
2742}
2743
Willy Tarreau87b09662015-04-03 00:22:06 +02002744/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002745 * frontend counters and return its previous value into temp integer.
2746 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002747 */
2748static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002749smp_fetch_sc_clr_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002750 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002751{
Willy Tarreau192252e2015-04-04 01:47:55 +02002752 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002753
2754 if (!stkctr)
2755 return 0;
2756
Willy Tarreau37406352012-04-23 16:16:37 +02002757 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002758 smp->type = SMP_T_UINT;
2759 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002760 if (stkctr_entry(stkctr) != NULL) {
2761 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002762 if (!ptr)
2763 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002764 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002765 stktable_data_cast(ptr, gpc0) = 0;
2766 }
2767 return 1;
2768}
2769
Willy Tarreau87b09662015-04-03 00:22:06 +02002770/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002771 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2772 * "src_conn_cnt" only.
2773 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002774static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002775smp_fetch_sc_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002776 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002777{
Willy Tarreau192252e2015-04-04 01:47:55 +02002778 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002779
2780 if (!stkctr)
2781 return 0;
2782
Willy Tarreau37406352012-04-23 16:16:37 +02002783 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002784 smp->type = SMP_T_UINT;
2785 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002786 if (stkctr_entry(stkctr) != NULL) {
2787 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002788 if (!ptr)
2789 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002790 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002791 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002792 return 1;
2793}
2794
Willy Tarreau87b09662015-04-03 00:22:06 +02002795/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002796 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2797 * only.
2798 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002799static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002800smp_fetch_sc_conn_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002801 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002802{
Willy Tarreau192252e2015-04-04 01:47:55 +02002803 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002804
2805 if (!stkctr)
2806 return 0;
2807
Willy Tarreau37406352012-04-23 16:16:37 +02002808 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002809 smp->type = SMP_T_UINT;
2810 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002811 if (stkctr_entry(stkctr) != NULL) {
2812 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002813 if (!ptr)
2814 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002815 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002816 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002817 }
2818 return 1;
2819}
2820
Willy Tarreau87b09662015-04-03 00:22:06 +02002821/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002822 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002823 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002824 */
2825static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002826smp_fetch_src_updt_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002827 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002828{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002829 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002830 struct stksess *ts;
2831 struct stktable_key *key;
2832 void *ptr;
2833
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002834 if (!conn)
2835 return 0;
2836
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002837 key = addr_to_stktable_key(&conn->addr.from, px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002838 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002839 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002840
Willy Tarreau24e32d82012-04-23 23:55:44 +02002841 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002842
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002843 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2844 /* entry does not exist and could not be created */
2845 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002846
2847 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2848 if (!ptr)
2849 return 0; /* parameter not stored in this table */
2850
Willy Tarreauf853c462012-04-23 18:53:56 +02002851 smp->type = SMP_T_UINT;
2852 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002853 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002854 return 1;
2855}
2856
Willy Tarreau87b09662015-04-03 00:22:06 +02002857/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002858 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2859 * "src_conn_cur" only.
2860 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002861static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002862smp_fetch_sc_conn_cur(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002863 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002864{
Willy Tarreau192252e2015-04-04 01:47:55 +02002865 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002866
2867 if (!stkctr)
2868 return 0;
2869
Willy Tarreau37406352012-04-23 16:16:37 +02002870 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002871 smp->type = SMP_T_UINT;
2872 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002873 if (stkctr_entry(stkctr) != NULL) {
2874 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002875 if (!ptr)
2876 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002877 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002878 }
2879 return 1;
2880}
2881
Willy Tarreau87b09662015-04-03 00:22:06 +02002882/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02002883 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2884 * "src_sess_cnt" only.
2885 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002886static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002887smp_fetch_sc_sess_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002888 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002889{
Willy Tarreau192252e2015-04-04 01:47:55 +02002890 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02002891
2892 if (!stkctr)
2893 return 0;
2894
Willy Tarreau37406352012-04-23 16:16:37 +02002895 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002896 smp->type = SMP_T_UINT;
2897 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002898 if (stkctr_entry(stkctr) != NULL) {
2899 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002900 if (!ptr)
2901 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002902 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002903 }
2904 return 1;
2905}
2906
Willy Tarreau87b09662015-04-03 00:22:06 +02002907/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002908 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2909 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002910static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002911smp_fetch_sc_sess_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002912 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002913{
Willy Tarreau192252e2015-04-04 01:47:55 +02002914 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002915
2916 if (!stkctr)
2917 return 0;
2918
Willy Tarreau37406352012-04-23 16:16:37 +02002919 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002920 smp->type = SMP_T_UINT;
2921 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002922 if (stkctr_entry(stkctr) != NULL) {
2923 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002924 if (!ptr)
2925 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002926 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002927 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002928 }
2929 return 1;
2930}
2931
Willy Tarreau87b09662015-04-03 00:22:06 +02002932/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02002933 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2934 * "src_http_req_cnt" only.
2935 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002936static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002937smp_fetch_sc_http_req_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002938 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002939{
Willy Tarreau192252e2015-04-04 01:47:55 +02002940 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02002941
2942 if (!stkctr)
2943 return 0;
2944
Willy Tarreau37406352012-04-23 16:16:37 +02002945 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002946 smp->type = SMP_T_UINT;
2947 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002948 if (stkctr_entry(stkctr) != NULL) {
2949 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002950 if (!ptr)
2951 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002952 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002953 }
2954 return 1;
2955}
2956
Willy Tarreau87b09662015-04-03 00:22:06 +02002957/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02002958 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2959 * "src_http_req_rate" only.
2960 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002961static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002962smp_fetch_sc_http_req_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002963 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002964{
Willy Tarreau192252e2015-04-04 01:47:55 +02002965 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02002966
2967 if (!stkctr)
2968 return 0;
2969
Willy Tarreau37406352012-04-23 16:16:37 +02002970 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002971 smp->type = SMP_T_UINT;
2972 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002973 if (stkctr_entry(stkctr) != NULL) {
2974 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002975 if (!ptr)
2976 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002977 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02002978 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002979 }
2980 return 1;
2981}
2982
Willy Tarreau87b09662015-04-03 00:22:06 +02002983/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02002984 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2985 * "src_http_err_cnt" only.
2986 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002987static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002988smp_fetch_sc_http_err_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002989 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002990{
Willy Tarreau192252e2015-04-04 01:47:55 +02002991 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02002992
2993 if (!stkctr)
2994 return 0;
2995
Willy Tarreau37406352012-04-23 16:16:37 +02002996 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002997 smp->type = SMP_T_UINT;
2998 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002999 if (stkctr_entry(stkctr) != NULL) {
3000 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003001 if (!ptr)
3002 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003003 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003004 }
3005 return 1;
3006}
3007
Willy Tarreau87b09662015-04-03 00:22:06 +02003008/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003009 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3010 * "src_http_err_rate" only.
3011 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003012static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003013smp_fetch_sc_http_err_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003014 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003015{
Willy Tarreau192252e2015-04-04 01:47:55 +02003016 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003017
3018 if (!stkctr)
3019 return 0;
3020
Willy Tarreau37406352012-04-23 16:16:37 +02003021 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003022 smp->type = SMP_T_UINT;
3023 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003024 if (stkctr_entry(stkctr) != NULL) {
3025 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003026 if (!ptr)
3027 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003028 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003029 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003030 }
3031 return 1;
3032}
3033
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003034/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003035 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003036 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3037 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003038static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003039smp_fetch_sc_kbytes_in(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003040 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003041{
Willy Tarreau192252e2015-04-04 01:47:55 +02003042 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003043
3044 if (!stkctr)
3045 return 0;
3046
Willy Tarreau37406352012-04-23 16:16:37 +02003047 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003048 smp->type = SMP_T_UINT;
3049 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003050 if (stkctr_entry(stkctr) != NULL) {
3051 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003052 if (!ptr)
3053 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003054 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003055 }
3056 return 1;
3057}
3058
Willy Tarreau613fe992013-07-23 17:39:19 +02003059/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003060 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003061 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003062 */
3063static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003064smp_fetch_sc_bytes_in_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003065 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003066{
Willy Tarreau192252e2015-04-04 01:47:55 +02003067 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003068
3069 if (!stkctr)
3070 return 0;
3071
Willy Tarreau37406352012-04-23 16:16:37 +02003072 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003073 smp->type = SMP_T_UINT;
3074 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003075 if (stkctr_entry(stkctr) != NULL) {
3076 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003077 if (!ptr)
3078 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003079 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003080 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003081 }
3082 return 1;
3083}
3084
Willy Tarreau53aea102013-07-23 17:39:02 +02003085/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003086 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003087 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3088 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003089static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003090smp_fetch_sc_kbytes_out(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02003091 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003092{
Willy Tarreau192252e2015-04-04 01:47:55 +02003093 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003094
3095 if (!stkctr)
3096 return 0;
3097
Willy Tarreau37406352012-04-23 16:16:37 +02003098 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003099 smp->type = SMP_T_UINT;
3100 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003101 if (stkctr_entry(stkctr) != NULL) {
3102 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003103 if (!ptr)
3104 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003105 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003106 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003107 return 1;
3108}
3109
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003110/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003111 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003112 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003113 */
3114static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003115smp_fetch_sc_bytes_out_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003116 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003117{
Willy Tarreau192252e2015-04-04 01:47:55 +02003118 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003119
3120 if (!stkctr)
3121 return 0;
3122
Willy Tarreau37406352012-04-23 16:16:37 +02003123 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003124 smp->type = SMP_T_UINT;
3125 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003126 if (stkctr_entry(stkctr) != NULL) {
3127 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003128 if (!ptr)
3129 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003130 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003131 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003132 }
3133 return 1;
3134}
3135
Willy Tarreau87b09662015-04-03 00:22:06 +02003136/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003137 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3138 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003139static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003140smp_fetch_sc_trackers(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003141 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003142{
Willy Tarreau192252e2015-04-04 01:47:55 +02003143 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003144
Willy Tarreau563eef42013-07-23 18:32:02 +02003145 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003146 return 0;
3147
Willy Tarreau563eef42013-07-23 18:32:02 +02003148 smp->flags = SMP_F_VOL_TEST;
3149 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003150 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003151 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003152}
3153
Willy Tarreau34db1082012-04-19 17:16:54 +02003154/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003155 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003156 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003157static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003158smp_fetch_table_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003159 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003160{
Willy Tarreau37406352012-04-23 16:16:37 +02003161 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003162 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003163 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003164 return 1;
3165}
3166
Willy Tarreau34db1082012-04-19 17:16:54 +02003167/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003168 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003169 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003170static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003171smp_fetch_table_avl(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003172 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003173{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003174 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003175 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003176 smp->type = SMP_T_UINT;
3177 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003178 return 1;
3179}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003180
Willy Tarreau61612d42012-04-19 18:42:05 +02003181/* Note: must not be declared <const> as its list will be overwritten.
3182 * Please take care of keeping this list alphabetically sorted.
3183 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003184static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003185 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003186}};
3187
Willy Tarreau281c7992013-01-08 01:23:27 +01003188/* Note: must not be declared <const> as its list will be overwritten.
3189 * Please take care of keeping this list alphabetically sorted.
3190 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003191static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003192 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3193 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3194 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3195 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3196 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3197 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3198 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3199 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3200 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3201 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3202 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3203 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3204 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3205 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3206 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3207 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3208 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3209 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3210 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3211 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3212 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3213 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3214 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3215 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3216 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3217 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3218 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3219 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3220 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3221 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3222 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3223 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3224 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3225 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3226 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3227 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3228 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3229 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3230 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3231 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3232 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3233 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3234 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3235 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3236 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3237 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3238 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3239 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3240 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3241 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3242 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3243 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3244 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3245 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3246 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3247 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3248 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3249 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3250 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3251 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3252 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3253 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3254 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3255 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3256 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3257 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3258 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3259 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3260 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3261 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3262 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3263 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3264 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3265 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3266 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3267 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3268 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3269 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3270 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3271 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3272 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3273 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3274 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3275 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3276 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3277 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3278 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3279 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3280 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3281 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3282 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3283 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3284 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3285 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3286 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3287 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003288 { /* END */ },
3289}};
3290
Willy Tarreau8b22a712010-06-18 17:46:06 +02003291__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003292static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003293{
Willy Tarreau281c7992013-01-08 01:23:27 +01003294 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003295 acl_register_keywords(&acl_kws);
3296}
3297
Willy Tarreaubaaee002006-06-26 02:48:02 +02003298/*
3299 * Local variables:
3300 * c-indent-level: 8
3301 * c-basic-offset: 8
3302 * End:
3303 */