blob: 863075b0ba4c9a722cf4b3742aed05bd1d69c1de [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 Tarreau02d86382015-04-05 12:00:52 +020061 * be called with an embryonic session. It returns the pointer to the newly
62 * created stream, or NULL in case of fatal error. For now the client-side
63 * end point is taken from the session's origin, which must be valid.
Willy Tarreaud1769b82015-04-06 00:25:48 +020064 * The task's context is set to the new stream, and its function is set to
65 * process_stream(). Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +020066 */
Willy Tarreau02d86382015-04-05 12:00:52 +020067struct stream *stream_new(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 Tarreauf2b98742015-04-05 01:30:42 +020070 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau470280e2015-04-05 01:33:13 +020071 struct appctx *appctx = objt_appctx(sess->origin);
Willy Tarreau20d46a52012-12-09 15:55:40 +010072 int i;
Willy Tarreau2542b532012-08-31 16:01:23 +020073
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020074 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020075 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020076
77 /* minimum stream initialization required for an embryonic stream is
78 * fairly low. We need very little to execute L4 ACLs, then we need a
79 * task to make the client-side connection live on its own.
80 * - flags
81 * - stick-entry tracking
82 */
83 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020084 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020085 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020086 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
87 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
88 tv_zero(&s->logs.tv_request);
89 s->logs.t_queue = -1;
90 s->logs.t_connect = -1;
91 s->logs.t_data = -1;
92 s->logs.t_close = 0;
93 s->logs.bytes_in = s->logs.bytes_out = 0;
94 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
95 s->logs.srv_queue_size = 0; /* we will get this number soon */
96
97 /* default logging function */
98 s->do_log = strm_log;
99
100 /* default error reporting function, may be changed by analysers */
101 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200102
103 /* Initialise the current rule list pointer to NULL. We are sure that
104 * any rulelist match the NULL pointer.
105 */
106 s->current_rule_list = NULL;
107
108 memset(s->stkctr, 0, sizeof(s->stkctr));
109
110 s->sess = sess;
111 s->si[0].flags = SI_FL_NONE;
112 s->si[1].flags = SI_FL_ISBACK;
113
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200114 s->uniq_id = global.req_count++;
115
Willy Tarreau87b09662015-04-03 00:22:06 +0200116 /* OK, we're keeping the stream, so let's properly initialize the stream */
117 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200118 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100119 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200120
Willy Tarreaue7dff022015-04-03 01:14:29 +0200121 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200122 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200123
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200124 s->task = t;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200125 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200126 t->context = s;
127 t->expire = TICK_ETERNITY;
128
Willy Tarreau87b09662015-04-03 00:22:06 +0200129 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200130 * This changes later when switching rules are executed or
131 * when the default backend is assigned.
132 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200133 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200134 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100135 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200136 s->req_cap = NULL;
137 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200138
Willy Tarreau87b09662015-04-03 00:22:06 +0200139 /* Let's count a stream now */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200140 if (conn)
141 proxy_inc_fe_sess_ctr(sess->listener, sess->fe);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200142
Willy Tarreaub4c84932013-07-23 19:15:30 +0200143 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200144 void *ptr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200145 struct stkctr *stkctr = &sess->stkctr[i];
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200146
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200147 if (!stkctr_entry(stkctr))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100148 continue;
149
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200150 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200151 if (ptr)
152 stktable_data_cast(ptr, sess_cnt)++;
153
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200154 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200155 if (ptr)
156 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200157 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200158 }
159
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200160 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100161 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200162 si_set_state(&s->si[0], SI_ST_EST);
163
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200164 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200165 if (conn)
166 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200167 else if (appctx)
168 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200169
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200170 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200171 s->si[0].flags |= SI_FL_INDEP_STR;
172
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200173 /* pre-initialize the other side's stream interface to an INIT state. The
174 * callbacks will be initialized before attempting to connect.
175 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100176 si_reset(&s->si[1]);
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200177 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200178
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200179 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200180 s->si[1].flags |= SI_FL_INDEP_STR;
181
Willy Tarreau87b09662015-04-03 00:22:06 +0200182 stream_init_srv_conn(s);
Willy Tarreaud1769b82015-04-06 00:25:48 +0200183 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200184 s->pend_pos = NULL;
185
186 /* init store persistence */
187 s->store_count = 0;
188
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100189 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100190 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200191 s->req.analysers = 0;
192 channel_auto_connect(&s->req); /* don't wait to establish connection */
193 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200194
195 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100196 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100197 s->req.rex = TICK_ETERNITY;
198 s->req.wex = TICK_ETERNITY;
199 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200200
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100201 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100202 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100203 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200204
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200205 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100206 s->req.flags |= CF_NEVER_WAIT;
207 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200208 }
209
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200210 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100211 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100212 s->res.rex = TICK_ETERNITY;
213 s->res.wex = TICK_ETERNITY;
214 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200215
Willy Tarreaueee5b512015-04-03 23:46:31 +0200216 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100217
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100218 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100219
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200220 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200221 if (conn)
222 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200223 else if (appctx)
224 s->si[0].flags |= SI_FL_WAIT_DATA;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200225
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200226 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200227 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200228
Willy Tarreauf2b98742015-04-05 01:30:42 +0200229 if (conn) {
230 /* if logs require transport layer information, note it on the connection */
231 if (s->logs.logwait & LW_XPRT)
232 conn->flags |= CO_FL_XPRT_TRACKED;
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200233
Willy Tarreauf2b98742015-04-05 01:30:42 +0200234 /* we want the connection handler to notify the stream interface about updates. */
235 conn->flags |= CO_FL_WAKE_DATA;
236 }
Willy Tarreau2542b532012-08-31 16:01:23 +0200237
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200238 /* it is important not to call the wakeup function directly but to
239 * pass through task_wakeup(), because this one knows how to apply
240 * priorities to tasks.
241 */
242 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200243 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200244
245 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200246 out_fail_accept:
Willy Tarreau3b246412014-11-25 17:10:33 +0100247 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200248 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200249 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200250}
251
Willy Tarreaubaaee002006-06-26 02:48:02 +0200252/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200253 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200254 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200255static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200257 struct session *sess = strm_sess(s);
258 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100259 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200260 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200261 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100262
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263 if (s->pend_pos)
264 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100265
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100266 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200267 if (s->flags & SF_CURR_SESS) {
268 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100269 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100270 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100271 if (may_dequeue_tasks(objt_server(s->target), s->be))
272 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100273 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100274
Willy Tarreau7c669d72008-06-20 15:04:11 +0200275 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200276 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200277 * it should normally be only the same as the one above,
278 * so this should not happen in fact.
279 */
280 sess_change_server(s, NULL);
281 }
282
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100283 if (s->req.pipe)
284 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100285
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100286 if (s->res.pipe)
287 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100288
Willy Tarreaubf883e02014-11-25 21:10:35 +0100289 /* We may still be present in the buffer wait queue */
290 if (!LIST_ISEMPTY(&s->buffer_wait)) {
291 LIST_DEL(&s->buffer_wait);
292 LIST_INIT(&s->buffer_wait);
293 }
294
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100295 b_drop(&s->req.buf);
296 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100297 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200298 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200299
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100300 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200301 if (s->txn)
302 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100303
Willy Tarreau1e954912012-10-12 17:50:05 +0200304 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200305 if (cli_conn)
306 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200307
Willy Tarreaua4cda672010-06-06 18:28:49 +0200308 for (i = 0; i < s->store_count; i++) {
309 if (!s->store[i].ts)
310 continue;
311 stksess_free(s->store[i].table, s->store[i].ts);
312 s->store[i].ts = NULL;
313 }
314
Willy Tarreaueee5b512015-04-03 23:46:31 +0200315 if (s->txn) {
316 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
317 pool_free2(pool2_http_txn, s->txn);
318 s->txn = NULL;
319 }
320
Willy Tarreau92fb9832007-10-16 17:34:28 +0200321 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200322 pool_free2(fe->rsp_cap_pool, s->res_cap);
323 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100325
Willy Tarreau87b09662015-04-03 00:22:06 +0200326 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200327
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100328 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100329 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200330 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100331 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100332 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100333 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200334 if (s->list.n != &streams)
335 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100336 bref->ref = s->list.n;
337 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100338 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200339 si_release_endpoint(&s->si[1]);
340 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200341
342 /* FIXME: for now we have a 1:1 relation between stream and session so
343 * the stream must free the session.
344 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200345 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200346 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200347
348 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200349 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200350 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200351 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200352 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200353 pool_flush2(pool2_requri);
354 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200355 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200356 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100357 pool_flush2(pool2_connection);
358 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200359 pool_flush2(fe->req_cap_pool);
360 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200361 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200362}
363
Willy Tarreau78955f42014-12-28 13:09:02 +0100364/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
365 * that it's not the last available buffer or it's the response buffer. Unless
366 * the buffer is the response buffer, an extra control is made so that we always
367 * keep <tune.buffers.reserved> buffers available after this allocation. To be
368 * called at the beginning of recv() callbacks to ensure that the required
369 * buffers are properly allocated. Returns 0 in case of failure, non-zero
370 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100371 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200372int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100373{
Willy Tarreau87b09662015-04-03 00:22:06 +0200374 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100375 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100376 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100377
Willy Tarreau78955f42014-12-28 13:09:02 +0100378 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100379 margin = global.tune.reserved_bufs;
380
Willy Tarreau78955f42014-12-28 13:09:02 +0100381 s = chn_sess(chn);
382
383 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100384 if (b)
385 return 1;
386
Willy Tarreaubf883e02014-11-25 21:10:35 +0100387 if (LIST_ISEMPTY(&s->buffer_wait))
388 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100389 return 0;
390}
391
Willy Tarreau87b09662015-04-03 00:22:06 +0200392/* Allocates a work buffer for stream <s>. It is meant to be called inside
393 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaua24adf02014-11-27 01:11:56 +0100394 * to work fine. For a regular connection, only the response is needed so that
395 * an error message may be built and returned. In case where the initiator is
396 * an applet (eg: peers), then we need to allocate the request buffer for the
397 * applet to be able to send its data (in this case a response is not needed).
398 * Request buffers are never picked from the reserved pool, but response
399 * buffers may be allocated from the reserve. This is critical to ensure that
400 * a response may always flow and will never block a server from releasing a
401 * connection. Returns 0 in case of failure, non-zero otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100402 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200403int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100404{
Willy Tarreaua24adf02014-11-27 01:11:56 +0100405 int margin;
406 struct buffer **buf;
407
408 if (objt_appctx(s->si[0].end)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100409 buf = &s->req.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100410 margin = global.tune.reserved_bufs;
411 }
412 else {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100413 buf = &s->res.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100414 margin = 0;
415 }
416
Willy Tarreaubf883e02014-11-25 21:10:35 +0100417 if (!LIST_ISEMPTY(&s->buffer_wait)) {
418 LIST_DEL(&s->buffer_wait);
419 LIST_INIT(&s->buffer_wait);
420 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100421
Willy Tarreaua24adf02014-11-27 01:11:56 +0100422 if (b_alloc_margin(buf, margin))
Willy Tarreau656859d2014-11-25 19:46:36 +0100423 return 1;
424
Willy Tarreaubf883e02014-11-25 21:10:35 +0100425 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100426 return 0;
427}
428
429/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100430 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200431 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100432 * a single buffer, so it makes sense to try to wake two up when two buffers
433 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100434 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200435void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100436{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100437 if (s->req.buf->size && buffer_empty(s->req.buf))
438 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100439
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100440 if (s->res.buf->size && buffer_empty(s->res.buf))
441 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100442
Willy Tarreaubf883e02014-11-25 21:10:35 +0100443 /* if we're certain to have at least 1 buffer available, and there is
444 * someone waiting, we can wake up a waiter and offer them.
445 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100446 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200447 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100448}
449
Willy Tarreau87b09662015-04-03 00:22:06 +0200450/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100451 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200452 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100453 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200454void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100455{
Willy Tarreau87b09662015-04-03 00:22:06 +0200456 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100457
458 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100459 if (rqlimit <= run_queue)
460 break;
461
Willy Tarreaubf883e02014-11-25 21:10:35 +0100462 if (sess->task->state & TASK_RUNNING)
463 continue;
464
465 LIST_DEL(&sess->buffer_wait);
466 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100467 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100468 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100469}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200470
471/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200472int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200473{
Willy Tarreau87b09662015-04-03 00:22:06 +0200474 LIST_INIT(&streams);
475 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
476 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200477}
478
Willy Tarreau87b09662015-04-03 00:22:06 +0200479void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100480{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200481 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100482 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100483 void *ptr;
484 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100485
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100486 bytes = s->req.total - s->logs.bytes_in;
487 s->logs.bytes_in = s->req.total;
488 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200489 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100490
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100491 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100492
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100493 if (objt_server(s->target))
494 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200495
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200496 if (sess->listener && sess->listener->counters)
497 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200498
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100499 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200500 struct stkctr *stkctr = &s->stkctr[i];
501
502 if (!stkctr_entry(stkctr)) {
503 stkctr = &sess->stkctr[i];
504 if (!stkctr_entry(stkctr))
505 continue;
506 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200507
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200508 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100509 if (ptr)
510 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200511
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200512 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100513 if (ptr)
514 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200515 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100516 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100517 }
518
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100519 bytes = s->res.total - s->logs.bytes_out;
520 s->logs.bytes_out = s->res.total;
521 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200522 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100523
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100524 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100525
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100526 if (objt_server(s->target))
527 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200528
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200529 if (sess->listener && sess->listener->counters)
530 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200531
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100532 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200533 struct stkctr *stkctr = &s->stkctr[i];
534
535 if (!stkctr_entry(stkctr)) {
536 stkctr = &sess->stkctr[i];
537 if (!stkctr_entry(stkctr))
538 continue;
539 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200540
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200541 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100542 if (ptr)
543 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200544
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200545 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100546 if (ptr)
547 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200548 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100549 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100550 }
551}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100553/* This function is called with (si->state == SI_ST_CON) meaning that a
554 * connection was attempted and that the file descriptor is already allocated.
555 * We must check for establishment, error and abort. Possible output states
556 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
557 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200558 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100559 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200560static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100561{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100562 struct stream_interface *si = &s->si[1];
563 struct channel *req = &s->req;
564 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200565 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100566
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100567 /* If we got an error, or if nothing happened and the connection timed
568 * out, we must give up. The CER state handler will take care of retry
569 * attempts and error reports.
570 */
571 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100572 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100573 /* Some data were sent past the connection establishment,
574 * so we need to pretend we're established to log correctly
575 * and let later states handle the failure.
576 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100577 si->state = SI_ST_EST;
578 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100579 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100580 return 1;
581 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100582 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100583 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100584
Willy Tarreauf79c8172013-10-21 16:30:56 +0200585 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100586
587 if (si->err_type)
588 return 0;
589
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100590 if (si->flags & SI_FL_ERR)
591 si->err_type = SI_ET_CONN_ERR;
592 else
593 si->err_type = SI_ET_CONN_TO;
594 return 0;
595 }
596
597 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100598 if (!(req->flags & CF_WRITE_PARTIAL) &&
599 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200600 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
601 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100602 s->be->options & PR_O_ABRT_CLOSE)))) {
603 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200604 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100605 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100606 if (s->srv_error)
607 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100608 return 1;
609 }
610
611 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200612 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100613 return 1;
614
615 /* OK, this means that a connection succeeded. The caller will be
616 * responsible for handling the transition from CON to EST.
617 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100618 si->state = SI_ST_EST;
619 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100620 return 1;
621}
622
623/* This function is called with (si->state == SI_ST_CER) meaning that a
624 * previous connection attempt has failed and that the file descriptor
625 * has already been released. Possible causes include asynchronous error
626 * notification and time out. Possible output states are SI_ST_CLO when
627 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
628 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
629 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
630 * marked as in error state. It returns 0.
631 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200632static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100633{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100634 struct stream_interface *si = &s->si[1];
635
Willy Tarreau87b09662015-04-03 00:22:06 +0200636 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100637 if (objt_server(s->target)) {
638 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100639
Willy Tarreaue7dff022015-04-03 01:14:29 +0200640 if (s->flags & SF_CURR_SESS) {
641 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100642 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100643 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644 }
645
646 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200647 si->conn_retries--;
648 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100649 if (!si->err_type) {
650 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100651 }
652
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100653 if (objt_server(s->target))
654 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100655 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100656 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100657 if (may_dequeue_tasks(objt_server(s->target), s->be))
658 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100659
660 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200661 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100662 s->req.flags |= CF_WRITE_ERROR;
663 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100664
665 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100666 if (s->srv_error)
667 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100668 return 0;
669 }
670
671 /* If the "redispatch" option is set on the backend, we are allowed to
672 * retry on another server for the last retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200673 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100674 * bit to ignore any persistence cookie. We won't count a retry nor a
675 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200676 * If the connection is not persistent, the balancing algorithm is not
677 * determinist (round robin) and there is more than one active server,
678 * we accept to perform an immediate redispatch without waiting since
679 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100680 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200681 if (objt_server(s->target) &&
682 (si->conn_retries == 0 ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200683 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Willy Tarreau33a14e52014-06-13 17:49:40 +0200684 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200685 s->be->options & PR_O_REDISP && !(s->flags & SF_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100686 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100687 if (may_dequeue_tasks(objt_server(s->target), s->be))
688 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100689
Willy Tarreaue7dff022015-04-03 01:14:29 +0200690 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100691 si->state = SI_ST_REQ;
692 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100693 if (objt_server(s->target))
694 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100695 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100696 si->state = SI_ST_ASS;
697 }
698
699 if (si->flags & SI_FL_ERR) {
700 /* The error was an asynchronous connection error, and we will
701 * likely have to retry connecting to the same server, most
702 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200703 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100704 */
705
Willy Tarreaub0290662014-06-13 17:04:44 +0200706 int delay = 1000;
707
708 if (s->be->timeout.connect && s->be->timeout.connect < delay)
709 delay = s->be->timeout.connect;
710
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100711 if (!si->err_type)
712 si->err_type = SI_ET_CONN_ERR;
713
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200714 /* only wait when we're retrying on the same server */
715 if (si->state == SI_ST_ASS ||
716 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
717 (s->be->srv_act <= 1)) {
718 si->state = SI_ST_TAR;
719 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
720 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100721 return 0;
722 }
723 return 0;
724}
725
726/*
727 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200728 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200729 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100730 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200731static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100732{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100733 struct stream_interface *si = &s->si[1];
734 struct channel *req = &s->req;
735 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100737 /* First, centralize the timers information */
738 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
739 si->exp = TICK_ETERNITY;
740
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100741 if (objt_server(s->target))
742 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100743
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100745 /* if the user wants to log as soon as possible, without counting
746 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200747 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100748 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100749 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100750 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751 }
752 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100753 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100754 }
755
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200756 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200757 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200758 if (req->flags & CF_WAKE_CONNECT) {
759 req->flags |= CF_WAKE_ONCE;
760 req->flags &= ~CF_WAKE_CONNECT;
761 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200762 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200763 /* real connections have timeouts */
764 req->wto = s->be->timeout.server;
765 rep->rto = s->be->timeout.server;
766 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100767 req->wex = TICK_ETERNITY;
768}
769
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100770/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
771 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100772 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
773 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
774 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100775 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200776static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100777{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100778 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100779 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100780 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100781
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100782 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 +0100783 now_ms, __FUNCTION__,
784 s,
Willy Tarreau103197d2014-11-28 15:26:12 +0100785 req, s->rep,
786 req->rex, s->res.wex,
787 req->flags, s->res.flags,
788 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 +0100789
790 if (si->state == SI_ST_ASS) {
791 /* Server assigned to connection request, we have to try to connect now */
792 int conn_err;
793
794 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100795 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100796
Willy Tarreaue7dff022015-04-03 01:14:29 +0200797 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100798 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100799 if (srv)
800 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500801 if (srv)
802 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100803 return;
804 }
805
806 /* We have received a synchronous error. We might have to
807 * abort, retry immediately or redispatch.
808 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200809 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100810 if (!si->err_type) {
811 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100812 }
813
Willy Tarreau827aee92011-03-10 16:55:02 +0100814 if (srv)
815 srv_inc_sess_ctr(srv);
816 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500817 srv_set_sess_last(srv);
818 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100819 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100820 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100821
Willy Tarreau87b09662015-04-03 00:22:06 +0200822 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100823 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100824 if (may_dequeue_tasks(srv, s->be))
825 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100826
827 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200828 si_shutr(si);
829 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100830 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100831
832 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
833
Willy Tarreau87b09662015-04-03 00:22:06 +0200834 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100835 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100836 if (s->srv_error)
837 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100838 return;
839 }
840
841 /* We are facing a retryable error, but we don't want to run a
842 * turn-around now, as the problem is likely a source port
843 * allocation problem, so we want to retry now.
844 */
845 si->state = SI_ST_CER;
846 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100847 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100848 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
849 return;
850 }
851 else if (si->state == SI_ST_QUE) {
852 /* connection request was queued, check for any update */
853 if (!s->pend_pos) {
854 /* The connection is not in the queue anymore. Either
855 * we have a server connection slot available and we
856 * go directly to the assigned state, or we need to
857 * load-balance first and go to the INI state.
858 */
859 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200860 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100861 si->state = SI_ST_REQ;
862 else {
863 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
864 si->state = SI_ST_ASS;
865 }
866 return;
867 }
868
869 /* Connection request still in queue... */
870 if (si->flags & SI_FL_EXP) {
871 /* ... and timeout expired */
872 si->exp = TICK_ETERNITY;
873 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100874 if (srv)
875 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100876 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200877 si_shutr(si);
878 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100879 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100880 if (!si->err_type)
881 si->err_type = SI_ET_QUEUE_TO;
882 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100883 if (s->srv_error)
884 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100885 return;
886 }
887
888 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100889 if ((req->flags & (CF_READ_ERROR)) ||
890 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
891 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100892 /* give up */
893 si->exp = TICK_ETERNITY;
894 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200895 si_shutr(si);
896 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100897 si->err_type |= SI_ET_QUEUE_ABRT;
898 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100899 if (s->srv_error)
900 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100901 return;
902 }
903
904 /* Nothing changed */
905 return;
906 }
907 else if (si->state == SI_ST_TAR) {
908 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100909 if ((req->flags & (CF_READ_ERROR)) ||
910 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
911 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100912 /* give up */
913 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200914 si_shutr(si);
915 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100916 si->err_type |= SI_ET_CONN_ABRT;
917 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100918 if (s->srv_error)
919 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100920 return;
921 }
922
923 if (!(si->flags & SI_FL_EXP))
924 return; /* still in turn-around */
925
926 si->exp = TICK_ETERNITY;
927
Willy Tarreau87b09662015-04-03 00:22:06 +0200928 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100929 * marked "assigned".
930 * FIXME: Should we force a redispatch attempt when the server is down ?
931 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200932 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100933 si->state = SI_ST_ASS;
934 else
935 si->state = SI_ST_REQ;
936 return;
937 }
938}
939
Willy Tarreau87b09662015-04-03 00:22:06 +0200940/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900941 * also counts a failed request if the server state has not reached the request
942 * stage.
943 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200944static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900945{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200946 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900947 if (s->si[1].state < SI_ST_REQ) {
948
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200949 strm_fe(s)->fe_counters.failed_req++;
950 if (strm_li(s)->counters)
951 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900952
Willy Tarreaue7dff022015-04-03 01:14:29 +0200953 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900954 }
955 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200956 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900957 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200958 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900959 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200960 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900961 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200962 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900963 }
964}
965
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100966/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100967 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
968 * a real connection to a server, indicating that a server has been assigned,
969 * or SI_ST_EST for a successful connection to an applet. It may also return
970 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100971 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200972static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100973{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100974 struct stream_interface *si = &s->si[1];
975
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100976 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 +0100977 now_ms, __FUNCTION__,
978 s,
979 s->req, s->rep,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100980 s->req.rex, s->res.wex,
981 s->req.flags, s->res.flags,
Willy Tarreau350f4872014-11-28 14:42:25 +0100982 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 +0100983
984 if (si->state != SI_ST_REQ)
985 return;
986
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100987 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
988 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100989 struct appctx *appctx = objt_appctx(si->end);
990
991 if (!appctx || appctx->applet != __objt_applet(s->target))
992 appctx = stream_int_register_handler(si, objt_applet(s->target));
993
994 if (!appctx) {
995 /* No more memory, let's immediately abort. Force the
996 * error code to ignore the ERR_LOCAL which is not a
997 * real error.
998 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200999 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001000
1001 si_shutr(si);
1002 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001003 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001004 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001005 si->state = SI_ST_CLO;
1006 if (s->srv_error)
1007 s->srv_error(s, si);
1008 return;
1009 }
1010
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001011 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001012 si->state = SI_ST_EST;
1013 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001014 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001015 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001016 return;
1017 }
1018
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001019 /* Try to assign a server */
1020 if (srv_redispatch_connect(s) != 0) {
1021 /* We did not get a server. Either we queued the
1022 * connection request, or we encountered an error.
1023 */
1024 if (si->state == SI_ST_QUE)
1025 return;
1026
1027 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001028 si_shutr(si);
1029 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001030 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001031 if (!si->err_type)
1032 si->err_type = SI_ET_CONN_OTHER;
1033 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001034 if (s->srv_error)
1035 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001036 return;
1037 }
1038
1039 /* The server is assigned */
1040 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1041 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001042 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001043}
1044
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001045/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001046 * if appropriate. The default_backend rule is also considered, then the
1047 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001048 * It returns 1 if the processing can continue on next analysers, or zero if it
1049 * either needs more data or wants to immediately abort the request.
1050 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001051static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001052{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001053 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001054 struct session *sess = s->sess;
1055 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001056
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001057 req->analysers &= ~an_bit;
1058 req->analyse_exp = TICK_ETERNITY;
1059
Willy Tarreau87b09662015-04-03 00:22:06 +02001060 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 +02001061 now_ms, __FUNCTION__,
1062 s,
1063 req,
1064 req->rex, req->wex,
1065 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001066 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001067 req->analysers);
1068
1069 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001070 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001071 struct switching_rule *rule;
1072
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001073 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001074 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001075
Willy Tarreauf51658d2014-04-23 01:21:56 +02001076 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001077 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001078 ret = acl_pass(ret);
1079 if (rule->cond->pol == ACL_COND_UNLESS)
1080 ret = !ret;
1081 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001082
1083 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001084 /* If the backend name is dynamic, try to resolve the name.
1085 * If we can't resolve the name, or if any error occurs, break
1086 * the loop and fallback to the default backend.
1087 */
1088 struct proxy *backend;
1089
1090 if (rule->dynamic) {
1091 struct chunk *tmp = get_trash_chunk();
1092 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1093 break;
1094 backend = findproxy(tmp->str, PR_CAP_BE);
1095 if (!backend)
1096 break;
1097 }
1098 else
1099 backend = rule->be.backend;
1100
Willy Tarreau87b09662015-04-03 00:22:06 +02001101 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001102 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001103 break;
1104 }
1105 }
1106
1107 /* To ensure correct connection accounting on the backend, we
1108 * have to assign one if it was not set (eg: a listen). This
1109 * measure also takes care of correctly setting the default
1110 * backend if any.
1111 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001112 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001113 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001114 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001115 }
1116
Willy Tarreaufb356202010-08-03 14:02:05 +02001117 /* 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 +02001118 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001119 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1120 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001121 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001122
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001123 /* 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 +02001124 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001125 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001126 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001127 int ret = 1;
1128
1129 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001130 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 +01001131 ret = acl_pass(ret);
1132 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1133 ret = !ret;
1134 }
1135
1136 if (ret) {
1137 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001138 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001139 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001140 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001141 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001142 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001143 break;
1144 }
1145 }
1146
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001147 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001148
1149 sw_failed:
1150 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001151 channel_abort(&s->req);
1152 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001153
Willy Tarreaue7dff022015-04-03 01:14:29 +02001154 if (!(s->flags & SF_ERR_MASK))
1155 s->flags |= SF_ERR_RESOURCE;
1156 if (!(s->flags & SF_FINST_MASK))
1157 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001158
Willy Tarreaueee5b512015-04-03 23:46:31 +02001159 if (s->txn)
1160 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001161 s->req.analysers = 0;
1162 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001163 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001164}
1165
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001166/* This stream analyser works on a request. It applies all use-server rules on
1167 * it then returns 1. The data must already be present in the buffer otherwise
1168 * they won't match. It always returns 1.
1169 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001170static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001171{
1172 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001173 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001174 struct server_rule *rule;
1175
Willy Tarreau87b09662015-04-03 00:22:06 +02001176 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 +02001177 now_ms, __FUNCTION__,
1178 s,
1179 req,
1180 req->rex, req->wex,
1181 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001182 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001183 req->analysers);
1184
Willy Tarreaue7dff022015-04-03 01:14:29 +02001185 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001186 list_for_each_entry(rule, &px->server_rules, list) {
1187 int ret;
1188
Willy Tarreau192252e2015-04-04 01:47:55 +02001189 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001190 ret = acl_pass(ret);
1191 if (rule->cond->pol == ACL_COND_UNLESS)
1192 ret = !ret;
1193
1194 if (ret) {
1195 struct server *srv = rule->srv.ptr;
1196
Willy Tarreau892337c2014-05-13 23:41:20 +02001197 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001198 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001199 (s->flags & SF_FORCE_PRST)) {
1200 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001201 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001202 break;
1203 }
1204 /* if the server is not UP, let's go on with next rules
1205 * just in case another one is suited.
1206 */
1207 }
1208 }
1209 }
1210
1211 req->analysers &= ~an_bit;
1212 req->analyse_exp = TICK_ETERNITY;
1213 return 1;
1214}
1215
Emeric Brun1d33b292010-01-04 15:47:17 +01001216/* This stream analyser works on a request. It applies all sticking rules on
1217 * it then returns 1. The data must already be present in the buffer otherwise
1218 * they won't match. It always returns 1.
1219 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001220static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001221{
1222 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001223 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001224 struct sticking_rule *rule;
1225
Willy Tarreau87b09662015-04-03 00:22:06 +02001226 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 +01001227 now_ms, __FUNCTION__,
1228 s,
1229 req,
1230 req->rex, req->wex,
1231 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001232 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001233 req->analysers);
1234
1235 list_for_each_entry(rule, &px->sticking_rules, list) {
1236 int ret = 1 ;
1237 int i;
1238
Willy Tarreau9667a802013-12-09 12:52:13 +01001239 /* Only the first stick store-request of each table is applied
1240 * and other ones are ignored. The purpose is to allow complex
1241 * configurations which look for multiple entries by decreasing
1242 * order of precision and to stop at the first which matches.
1243 * An example could be a store of the IP address from an HTTP
1244 * header first, then from the source if not found.
1245 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001246 for (i = 0; i < s->store_count; i++) {
1247 if (rule->table.t == s->store[i].table)
1248 break;
1249 }
1250
1251 if (i != s->store_count)
1252 continue;
1253
1254 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001255 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001256 ret = acl_pass(ret);
1257 if (rule->cond->pol == ACL_COND_UNLESS)
1258 ret = !ret;
1259 }
1260
1261 if (ret) {
1262 struct stktable_key *key;
1263
Willy Tarreau192252e2015-04-04 01:47:55 +02001264 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 +01001265 if (!key)
1266 continue;
1267
1268 if (rule->flags & STK_IS_MATCH) {
1269 struct stksess *ts;
1270
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001271 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001272 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001273 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001274 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001275
1276 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001277 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1278 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001279 if (node) {
1280 struct server *srv;
1281
1282 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001283 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001284 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001285 (s->flags & SF_FORCE_PRST)) {
1286 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001287 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001288 }
1289 }
1290 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001291 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001292 }
1293 }
1294 if (rule->flags & STK_IS_STORE) {
1295 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1296 struct stksess *ts;
1297
1298 ts = stksess_new(rule->table.t, key);
1299 if (ts) {
1300 s->store[s->store_count].table = rule->table.t;
1301 s->store[s->store_count++].ts = ts;
1302 }
1303 }
1304 }
1305 }
1306 }
1307
1308 req->analysers &= ~an_bit;
1309 req->analyse_exp = TICK_ETERNITY;
1310 return 1;
1311}
1312
1313/* This stream analyser works on a response. It applies all store rules on it
1314 * then returns 1. The data must already be present in the buffer otherwise
1315 * they won't match. It always returns 1.
1316 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001317static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001318{
1319 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001320 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001321 struct sticking_rule *rule;
1322 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001323 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001324
Willy Tarreau87b09662015-04-03 00:22:06 +02001325 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 +01001326 now_ms, __FUNCTION__,
1327 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001328 rep,
1329 rep->rex, rep->wex,
1330 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001331 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001332 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001333
1334 list_for_each_entry(rule, &px->storersp_rules, list) {
1335 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001336
Willy Tarreau9667a802013-12-09 12:52:13 +01001337 /* Only the first stick store-response of each table is applied
1338 * and other ones are ignored. The purpose is to allow complex
1339 * configurations which look for multiple entries by decreasing
1340 * order of precision and to stop at the first which matches.
1341 * An example could be a store of a set-cookie value, with a
1342 * fallback to a parameter found in a 302 redirect.
1343 *
1344 * The store-response rules are not allowed to override the
1345 * store-request rules for the same table, but they may coexist.
1346 * Thus we can have up to one store-request entry and one store-
1347 * response entry for the same table at any time.
1348 */
1349 for (i = nbreq; i < s->store_count; i++) {
1350 if (rule->table.t == s->store[i].table)
1351 break;
1352 }
1353
1354 /* skip existing entries for this table */
1355 if (i < s->store_count)
1356 continue;
1357
Emeric Brun1d33b292010-01-04 15:47:17 +01001358 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001359 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001360 ret = acl_pass(ret);
1361 if (rule->cond->pol == ACL_COND_UNLESS)
1362 ret = !ret;
1363 }
1364
1365 if (ret) {
1366 struct stktable_key *key;
1367
Willy Tarreau192252e2015-04-04 01:47:55 +02001368 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 +01001369 if (!key)
1370 continue;
1371
Willy Tarreau37e340c2013-12-06 23:05:21 +01001372 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001373 struct stksess *ts;
1374
1375 ts = stksess_new(rule->table.t, key);
1376 if (ts) {
1377 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001378 s->store[s->store_count++].ts = ts;
1379 }
1380 }
1381 }
1382 }
1383
1384 /* process store request and store response */
1385 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001386 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001387 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001388
Willy Tarreauc93cd162014-05-13 15:54:22 +02001389 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001390 stksess_free(s->store[i].table, s->store[i].ts);
1391 s->store[i].ts = NULL;
1392 continue;
1393 }
1394
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001395 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1396 if (ts) {
1397 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001398 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001399 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001400 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001401 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001402 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001403
1404 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001405 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001406 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001407 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001408 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001409
1410 rep->analysers &= ~an_bit;
1411 rep->analyse_exp = TICK_ETERNITY;
1412 return 1;
1413}
1414
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001415/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001416 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001417 */
1418#define UPDATE_ANALYSERS(real, list, back, flag) { \
1419 list = (((list) & ~(flag)) | ~(back)) & (real); \
1420 back = real; \
1421 if (!(list)) \
1422 break; \
1423 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1424 continue; \
1425}
1426
Willy Tarreau87b09662015-04-03 00:22:06 +02001427/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001428 * then puts it back to the wait queue in a clean state, or cleans up its
1429 * resources if it must be deleted. Returns in <next> the date the task wants
1430 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1431 * nothing too many times, the request and response buffers flags are monitored
1432 * and each function is called only if at least another function has changed at
1433 * least one flag it is interested in.
1434 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001435struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001436{
Willy Tarreau827aee92011-03-10 16:55:02 +01001437 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001438 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001439 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001440 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001441 unsigned int rq_prod_last, rq_cons_last;
1442 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001443 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001444 struct channel *req, *res;
1445 struct stream_interface *si_f, *si_b;
1446
1447 req = &s->req;
1448 res = &s->res;
1449
1450 si_f = &s->si[0];
1451 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001452
1453 //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 +01001454 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001455
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001456 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001457 if (s->txn)
1458 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001459
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001460 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001461 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1462 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001463
1464 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001465 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1466 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001467
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001468 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001469 si_f->flags |= SI_FL_DONT_WAKE;
1470 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001471
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001472 /* 1a: Check for low level timeouts if needed. We just set a flag on
1473 * stream interfaces when their timeouts have expired.
1474 */
1475 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001476 stream_int_check_timeouts(si_f);
1477 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001478
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001479 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001480 * for future reads or writes. Note: this will also concern upper layers
1481 * but we do not touch any other flag. We must be careful and correctly
1482 * detect state changes when calling them.
1483 */
1484
Willy Tarreau8f128b42014-11-28 15:07:47 +01001485 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001486
Willy Tarreau8f128b42014-11-28 15:07:47 +01001487 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1488 si_b->flags |= SI_FL_NOLINGER;
1489 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001490 }
1491
Willy Tarreau8f128b42014-11-28 15:07:47 +01001492 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1493 if (si_f->flags & SI_FL_NOHALF)
1494 si_f->flags |= SI_FL_NOLINGER;
1495 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001496 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001497
Willy Tarreau8f128b42014-11-28 15:07:47 +01001498 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001499
Willy Tarreau8f128b42014-11-28 15:07:47 +01001500 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1501 si_f->flags |= SI_FL_NOLINGER;
1502 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001503 }
1504
Willy Tarreau8f128b42014-11-28 15:07:47 +01001505 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1506 if (si_b->flags & SI_FL_NOHALF)
1507 si_b->flags |= SI_FL_NOLINGER;
1508 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001509 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001510
1511 /* Once in a while we're woken up because the task expires. But
1512 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001513 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001514 * timeout needs to be refreshed.
1515 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001516 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001517 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1518 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001519 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001520 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1521 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001522 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001523
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001524 /* below we may emit error messages so we have to ensure that we have
1525 * our buffers properly allocated.
1526 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001527 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001528 /* No buffer available, we've been subscribed to the list of
1529 * buffer waiters, let's wait for our turn.
1530 */
1531 goto update_exp_and_leave;
1532 }
1533
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001534 /* 1b: check for low-level errors reported at the stream interface.
1535 * First we check if it's a retryable error (in which case we don't
1536 * want to tell the buffer). Otherwise we report the error one level
1537 * upper by setting flags into the buffers. Note that the side towards
1538 * the client cannot have connect (hence retryable) errors. Also, the
1539 * connection setup code must be able to deal with any type of abort.
1540 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001541 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001542 if (unlikely(si_f->flags & SI_FL_ERR)) {
1543 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1544 si_shutr(si_f);
1545 si_shutw(si_f);
1546 stream_int_report_error(si_f);
1547 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001548 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001549 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001550 if (srv)
1551 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001552 if (!(s->flags & SF_ERR_MASK))
1553 s->flags |= SF_ERR_CLICL;
1554 if (!(s->flags & SF_FINST_MASK))
1555 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001556 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001557 }
1558 }
1559
Willy Tarreau8f128b42014-11-28 15:07:47 +01001560 if (unlikely(si_b->flags & SI_FL_ERR)) {
1561 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1562 si_shutr(si_b);
1563 si_shutw(si_b);
1564 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001565 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001566 if (srv)
1567 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001568 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001569 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001570 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001571 if (srv)
1572 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001573 if (!(s->flags & SF_ERR_MASK))
1574 s->flags |= SF_ERR_SRVCL;
1575 if (!(s->flags & SF_FINST_MASK))
1576 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001577 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001578 }
1579 /* note: maybe we should process connection errors here ? */
1580 }
1581
Willy Tarreau8f128b42014-11-28 15:07:47 +01001582 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001583 /* we were trying to establish a connection on the server side,
1584 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1585 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001586 if (unlikely(!sess_update_st_con_tcp(s)))
1587 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001588 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001589 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001590
1591 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1592 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1593 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1594 */
1595 }
1596
Willy Tarreau8f128b42014-11-28 15:07:47 +01001597 rq_prod_last = si_f->state;
1598 rq_cons_last = si_b->state;
1599 rp_cons_last = si_f->state;
1600 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001601
1602 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001603 /* Check for connection closure */
1604
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001605 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001606 "[%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 +01001607 now_ms, __FUNCTION__, __LINE__,
1608 t,
1609 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001610 req, res,
1611 req->rex, res->wex,
1612 req->flags, res->flags,
1613 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1614 si_f->err_type, si_b->err_type,
1615 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001616
1617 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001618 if (unlikely(si_f->state == SI_ST_DIS))
1619 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001620
1621 /* When a server-side connection is released, we have to count it and
1622 * check for pending connections on this server.
1623 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001624 if (unlikely(si_b->state == SI_ST_DIS)) {
1625 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001626 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001627 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001628 if (s->flags & SF_CURR_SESS) {
1629 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001630 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001631 }
1632 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001633 if (may_dequeue_tasks(srv, s->be))
1634 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001635 }
1636 }
1637
1638 /*
1639 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1640 * at this point.
1641 */
1642
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001643 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001644 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001645 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1646 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1647 si_f->state != rq_prod_last ||
1648 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001649 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001650 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001651
Willy Tarreau8f128b42014-11-28 15:07:47 +01001652 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001653 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001654 unsigned int ana_list;
1655 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001656
Willy Tarreau90deb182010-01-07 00:20:41 +01001657 /* it's up to the analysers to stop new connections,
1658 * disable reading or closing. Note: if an analyser
1659 * disables any of these bits, it is responsible for
1660 * enabling them again when it disables itself, so
1661 * that other analysers are called in similar conditions.
1662 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001663 channel_auto_read(req);
1664 channel_auto_connect(req);
1665 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001666
1667 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001668 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001669 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001670 * the list when not needed. Any analyser may return 0
1671 * to break out of the loop, either because of missing
1672 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001673 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001674 * analyser, and we may loop again if other analysers
1675 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001676 *
1677 * We build a list of analysers to run. We evaluate all
1678 * of these analysers in the order of the lower bit to
1679 * the higher bit. This ordering is very important.
1680 * An analyser will often add/remove other analysers,
1681 * including itself. Any changes to itself have no effect
1682 * on the loop. If it removes any other analysers, we
1683 * want those analysers not to be called anymore during
1684 * this loop. If it adds an analyser that is located
1685 * after itself, we want it to be scheduled for being
1686 * processed during the loop. If it adds an analyser
1687 * which is located before it, we want it to switch to
1688 * it immediately, even if it has already been called
1689 * once but removed since.
1690 *
1691 * In order to achieve this, we compare the analyser
1692 * list after the call with a copy of it before the
1693 * call. The work list is fed with analyser bits that
1694 * appeared during the call. Then we compare previous
1695 * work list with the new one, and check the bits that
1696 * appeared. If the lowest of these bits is lower than
1697 * the current bit, it means we have enabled a previous
1698 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001699 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001700
Willy Tarreau8f128b42014-11-28 15:07:47 +01001701 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001702 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001703 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001704
Willy Tarreaufb356202010-08-03 14:02:05 +02001705 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001706 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001707 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001708 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001709 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001710
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001711 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001712 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001713 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001714 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001715 }
1716
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001717 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001718 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
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_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001721 }
1722
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001723 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001724 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001725 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001726 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001727 }
1728
Willy Tarreaufb356202010-08-03 14:02:05 +02001729 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001730 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001731 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001732 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001733 }
1734
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001735 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001736 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001737 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001738 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001739 }
1740
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001741 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001742 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001743 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001744 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001745 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001746
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001747 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001748 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001749 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001750 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001751 }
1752
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001753 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001754 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001755 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001756 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001757 }
1758
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001759 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001760 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001761 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001762 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001763 }
Emeric Brun647caf12009-06-30 17:57:00 +02001764
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001765 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001766 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001767 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001768 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001769 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001770
Emeric Brun1d33b292010-01-04 15:47:17 +01001771 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001772 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001773 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001774 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001775 }
1776
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001777 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001778 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001779 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001780 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001781 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001782 break;
1783 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001784 }
Willy Tarreau84455332009-03-15 22:34:05 +01001785
Willy Tarreau8f128b42014-11-28 15:07:47 +01001786 rq_prod_last = si_f->state;
1787 rq_cons_last = si_b->state;
1788 req->flags &= ~CF_WAKE_ONCE;
1789 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001790
Willy Tarreau8f128b42014-11-28 15:07:47 +01001791 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001792 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001793 }
1794
Willy Tarreau576507f2010-01-07 00:09:04 +01001795 /* we'll monitor the request analysers while parsing the response,
1796 * because some response analysers may indirectly enable new request
1797 * analysers (eg: HTTP keep-alive).
1798 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001799 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001800
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001801 resync_response:
1802 /* Analyse response */
1803
Willy Tarreau8f128b42014-11-28 15:07:47 +01001804 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1805 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1806 si_f->state != rp_cons_last ||
1807 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001808 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001809 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001810
Willy Tarreau8f128b42014-11-28 15:07:47 +01001811 if ((res->flags & CF_MASK_ANALYSER) &&
1812 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001813 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1814 * for some free space in the response buffer, so we might need to call
1815 * it when something changes in the response buffer, but still we pass
1816 * it the request buffer. Note that the SI state might very well still
1817 * be zero due to us returning a flow of redirects!
1818 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001819 res->analysers &= ~AN_REQ_ALL;
1820 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001821 }
1822
Willy Tarreau8f128b42014-11-28 15:07:47 +01001823 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001824 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001825 unsigned int ana_list;
1826 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001827
Willy Tarreau90deb182010-01-07 00:20:41 +01001828 /* it's up to the analysers to stop disable reading or
1829 * closing. Note: if an analyser disables any of these
1830 * bits, it is responsible for enabling them again when
1831 * it disables itself, so that other analysers are called
1832 * in similar conditions.
1833 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001834 channel_auto_read(res);
1835 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001836
1837 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001838 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001839 * to MSB. The analysers must remove themselves from
1840 * the list when not needed. Any analyser may return 0
1841 * to break out of the loop, either because of missing
1842 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001843 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001844 * analyser, and we may loop again if other analysers
1845 * are added in the middle.
1846 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001847
Willy Tarreau8f128b42014-11-28 15:07:47 +01001848 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001849 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001850 /* Warning! ensure that analysers are always placed in ascending order! */
1851
Emeric Brun97679e72010-09-23 17:56:44 +02001852 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001853 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001854 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001855 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001856 }
1857
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001858 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001859 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001860 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001861 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001862 }
1863
Emeric Brun1d33b292010-01-04 15:47:17 +01001864 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001865 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001866 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001867 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001868 }
1869
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001870 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001871 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001872 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001873 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001874 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001875
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001876 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001877 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001878 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001879 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001880 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001881 break;
1882 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001883 }
1884
Willy Tarreau8f128b42014-11-28 15:07:47 +01001885 rp_cons_last = si_f->state;
1886 rp_prod_last = si_b->state;
1887 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001888
Willy Tarreau8f128b42014-11-28 15:07:47 +01001889 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001890 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001891 }
1892
Willy Tarreau576507f2010-01-07 00:09:04 +01001893 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001894 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001895 goto resync_request;
1896
Willy Tarreau8f128b42014-11-28 15:07:47 +01001897 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001898 goto resync_request;
1899
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001900 /* FIXME: here we should call protocol handlers which rely on
1901 * both buffers.
1902 */
1903
1904
1905 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001906 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001907 * we're just in a data phase here since it means we have not
1908 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001909 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001910 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001911 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001912 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001913 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001914 req->analysers = 0;
1915 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001916 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001917 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001918 if (srv)
1919 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001920 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001921 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001922 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001923 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001924 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001925 if (srv)
1926 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001927 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001928 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001929 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001930 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001931 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001932 if (srv)
1933 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001934 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001935 }
1936 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001937 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001938 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001939 if (srv)
1940 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001941 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001942 }
Willy Tarreau84455332009-03-15 22:34:05 +01001943 sess_set_term_flags(s);
1944 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001945 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001946 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001947 res->analysers = 0;
1948 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001949 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001950 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001951 if (srv)
1952 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001953 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001954 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001955 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001956 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001957 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001958 if (srv)
1959 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001960 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001961 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001962 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001963 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001964 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001965 if (srv)
1966 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001967 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001968 }
1969 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001970 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001971 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001972 if (srv)
1973 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001974 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001975 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001976 sess_set_term_flags(s);
1977 }
Willy Tarreau84455332009-03-15 22:34:05 +01001978 }
1979
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001980 /*
1981 * Here we take care of forwarding unhandled data. This also includes
1982 * connection establishments and shutdown requests.
1983 */
1984
1985
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001986 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001987 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001988 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001989 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001990 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001991 if (unlikely(!req->analysers &&
1992 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
1993 (si_f->state >= SI_ST_EST) &&
1994 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01001995 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001996 * attached to it. If any data are left in, we'll permit them to
1997 * move.
1998 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001999 channel_auto_read(req);
2000 channel_auto_connect(req);
2001 channel_auto_close(req);
2002 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002003
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002004 /* We'll let data flow between the producer (if still connected)
2005 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002006 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002007 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2008 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002009 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002010
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002011 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002012 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2013 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002014 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002015 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2016 (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 +01002017 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002018 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2019 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002020 (req->flags & CF_STREAMER_FAST)))) {
2021 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002022 }
2023
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002024 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002025 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002026
2027 /*
2028 * Now forward all shutdown requests between both sides of the buffer
2029 */
2030
Willy Tarreau520d95e2009-09-19 21:04:57 +02002031 /* first, let's check if the request buffer needs to shutdown(write), which may
2032 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002033 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2034 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002035 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002036 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002037 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002038 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002039 if (tick_isset(sess->fe->timeout.clientfin)) {
2040 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002041 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002042 }
2043 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002044
2045 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002046 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2047 channel_is_empty(req))) {
2048 if (req->flags & CF_READ_ERROR)
2049 si_b->flags |= SI_FL_NOLINGER;
2050 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002051 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002052 res->rto = s->be->timeout.serverfin;
2053 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002054 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002055 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002056
2057 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002058 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2059 !req->analysers))
2060 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002061
2062 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002063 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2064 if (si_f->flags & SI_FL_NOHALF)
2065 si_f->flags |= SI_FL_NOLINGER;
2066 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002067 if (tick_isset(sess->fe->timeout.clientfin)) {
2068 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002069 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002070 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002071 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002072
Willy Tarreau520d95e2009-09-19 21:04:57 +02002073 /* it's possible that an upper layer has requested a connection setup or abort.
2074 * There are 2 situations where we decide to establish a new connection :
2075 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002076 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002077 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002078 if (si_b->state == SI_ST_INI) {
2079 if (!(req->flags & CF_SHUTW)) {
2080 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002081 /* If we have an appctx, there is no connect method, so we
2082 * immediately switch to the connected state, otherwise we
2083 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002084 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002085 si_b->state = SI_ST_REQ; /* new connection requested */
2086 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002087 }
Willy Tarreau73201222009-08-16 18:27:24 +02002088 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002089 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002090 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2091 channel_shutw_now(req); /* fix buffer flags upon abort */
2092 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002093 }
Willy Tarreau92795622009-03-06 12:51:23 +01002094 }
2095
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002096
2097 /* we may have a pending connection request, or a connection waiting
2098 * for completion.
2099 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002100 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002101 do {
2102 /* nb: step 1 might switch from QUE to ASS, but we first want
2103 * to give a chance to step 2 to perform a redirect if needed.
2104 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002105 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002106 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002107 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002108 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002109
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002110 /* applets directly go to the ESTABLISHED state. Similarly,
2111 * servers experience the same fate when their connection
2112 * is reused.
2113 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002114 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002115 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002116
Willy Tarreaub44c8732013-12-31 23:16:50 +01002117 /* Now we can add the server name to a header (if requested) */
2118 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002119 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002120 (s->be->server_id_hdr_name != NULL) &&
2121 (s->be->mode == PR_MODE_HTTP) &&
2122 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002123 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002124 }
2125
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002126 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002127 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002128 http_perform_server_redirect(s, si_b);
2129 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002130 }
2131
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002132 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002133 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002134 goto resync_stream_interface;
2135
Willy Tarreau815a9b22010-07-27 17:15:12 +02002136 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002137 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002138 goto resync_request;
2139
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002140 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002141
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002142 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002143 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002144 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002145 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002146 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002147 if (unlikely(!res->analysers &&
2148 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2149 (si_b->state >= SI_ST_EST) &&
2150 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002151 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002152 * attached to it. If any data are left in, we'll permit them to
2153 * move.
2154 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002155 channel_auto_read(res);
2156 channel_auto_close(res);
2157 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002158
2159 /* We'll let data flow between the producer (if still connected)
2160 * to the consumer.
2161 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002162 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2163 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002164
2165 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002166 * tunnel timeout set, use it now. Note that we must respect
2167 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002168 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002169 if (!req->analysers && s->be->timeout.tunnel) {
2170 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002171 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002172
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002173 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2174 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002175 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2176 res->rto = s->be->timeout.serverfin;
2177 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2178 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002179 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2180 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002181
Willy Tarreau8f128b42014-11-28 15:07:47 +01002182 req->rex = tick_add(now_ms, req->rto);
2183 req->wex = tick_add(now_ms, req->wto);
2184 res->rex = tick_add(now_ms, res->rto);
2185 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002186 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002187 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002188
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002189 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002190 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2191 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002192 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002193 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2194 (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 +01002195 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002196 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2197 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002198 (res->flags & CF_STREAMER_FAST)))) {
2199 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002200 }
2201
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002202 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002203 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002204
2205 /*
2206 * Now forward all shutdown requests between both sides of the buffer
2207 */
2208
2209 /*
2210 * FIXME: this is probably where we should produce error responses.
2211 */
2212
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002213 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002214 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002215 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002216 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002217 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002218 req->wto = s->be->timeout.serverfin;
2219 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002220 }
2221 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002222
2223 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002224 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2225 channel_is_empty(res))) {
2226 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002227 if (tick_isset(sess->fe->timeout.clientfin)) {
2228 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002229 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002230 }
2231 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002232
2233 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002234 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2235 !res->analysers)
2236 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002237
2238 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002239 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2240 if (si_b->flags & SI_FL_NOHALF)
2241 si_b->flags |= SI_FL_NOLINGER;
2242 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002243 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002244 req->wto = s->be->timeout.serverfin;
2245 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002246 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002247 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002248
Willy Tarreau8f128b42014-11-28 15:07:47 +01002249 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002250 goto resync_stream_interface;
2251
Willy Tarreau8f128b42014-11-28 15:07:47 +01002252 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002253 goto resync_request;
2254
Willy Tarreau8f128b42014-11-28 15:07:47 +01002255 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002256 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002257
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002258 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002259 si_f->flags &= ~SI_FL_DONT_WAKE;
2260 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002261
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002262 /* This is needed only when debugging is enabled, to indicate
2263 * client-side or server-side close. Please note that in the unlikely
2264 * event where both sides would close at once, the sequence is reported
2265 * on the server side first.
2266 */
2267 if (unlikely((global.mode & MODE_DEBUG) &&
2268 (!(global.mode & MODE_QUIET) ||
2269 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002270 if (si_b->state == SI_ST_CLO &&
2271 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002272 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002273 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002274 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2275 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002276 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002277 }
2278
Willy Tarreau8f128b42014-11-28 15:07:47 +01002279 if (si_f->state == SI_ST_CLO &&
2280 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002281 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002282 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002283 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2284 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002285 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002286 }
2287 }
2288
Willy Tarreau8f128b42014-11-28 15:07:47 +01002289 if (likely((si_f->state != SI_ST_CLO) ||
2290 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002291
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002292 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002293 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002294
Willy Tarreau8f128b42014-11-28 15:07:47 +01002295 if (si_f->state == SI_ST_EST && obj_type(si_f->end) != OBJ_TYPE_APPCTX)
2296 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002297
Willy Tarreau8f128b42014-11-28 15:07:47 +01002298 if (si_b->state == SI_ST_EST && obj_type(si_b->end) != OBJ_TYPE_APPCTX)
2299 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002300
Willy Tarreau8f128b42014-11-28 15:07:47 +01002301 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2302 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2303 si_f->prev_state = si_f->state;
2304 si_b->prev_state = si_b->state;
2305 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2306 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002307
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002308 /* Trick: if a request is being waiting for the server to respond,
2309 * and if we know the server can timeout, we don't want the timeout
2310 * to expire on the client side first, but we're still interested
2311 * in passing data from the client to the server (eg: POST). Thus,
2312 * we can cancel the client's request timeout if the server's
2313 * request timeout is set and the server has not yet sent a response.
2314 */
2315
Willy Tarreau8f128b42014-11-28 15:07:47 +01002316 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2317 (tick_isset(req->wex) || tick_isset(res->rex))) {
2318 req->flags |= CF_READ_NOEXP;
2319 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002320 }
2321
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002322 /* When any of the stream interfaces is attached to an applet,
2323 * we have to call it here. Note that this one may wake the
2324 * task up again. If at least one applet was called, the current
2325 * task might have been woken up, in which case we don't want it
2326 * to be requeued to the wait queue but rather to the run queue
2327 * to run ASAP. The bitwise "or" in the condition ensures that
2328 * both functions are always called and that we wake up if at
2329 * least one did something.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002330 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002331 if ((si_applet_call(si_b) | si_applet_call(si_f)) != 0) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002332 if (task_in_rq(t)) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002333 t->expire = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02002334 stream_release_buffers(s);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002335 return t;
2336 }
2337 }
2338
Willy Tarreau798f4322012-11-08 14:49:17 +01002339 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002340 t->expire = tick_first(tick_first(req->rex, req->wex),
2341 tick_first(res->rex, res->wex));
2342 if (req->analysers)
2343 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002344
Willy Tarreau8f128b42014-11-28 15:07:47 +01002345 if (si_f->exp)
2346 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002347
Willy Tarreau8f128b42014-11-28 15:07:47 +01002348 if (si_b->exp)
2349 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002350
2351#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002352 fprintf(stderr,
2353 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2354 " 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 +01002355 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2356 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002357#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002358
2359#ifdef DEBUG_DEV
2360 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002361 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002362 ABORT_NOW();
2363#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002364 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002365 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002366 }
2367
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002368 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002369 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002370 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002371 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002372 if (sess->listener) {
2373 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002374 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002375 sess->listener->nbconn--;
2376 if (sess->listener->state == LI_FULL)
2377 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002378
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002379 /* Dequeues all of the listeners waiting for a resource */
2380 if (!LIST_ISEMPTY(&global_listener_queue))
2381 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002382
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002383 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2384 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2385 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002386 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002387
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002388 if (unlikely((global.mode & MODE_DEBUG) &&
2389 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002390 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002391 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002392 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2393 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002394 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002395 }
2396
2397 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002398 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002399
Willy Tarreaueee5b512015-04-03 23:46:31 +02002400 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002401 int n;
2402
Willy Tarreaueee5b512015-04-03 23:46:31 +02002403 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002404 if (n < 1 || n > 5)
2405 n = 0;
2406
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002407 if (sess->fe->mode == PR_MODE_HTTP) {
2408 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002409 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002410 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002411 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002412 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002413 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002414 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002415 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002416 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002417 s->be->be_counters.p.http.comp_rsp++;
2418 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002419 }
2420
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002421 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002422 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002423 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002424 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002425 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002426 }
2427
Willy Tarreau87b09662015-04-03 00:22:06 +02002428 /* update time stats for this stream */
2429 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002430
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002431 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002432 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002433 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002434 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002435 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002436}
2437
Willy Tarreau87b09662015-04-03 00:22:06 +02002438/* Update the stream's backend and server time stats */
2439void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002440{
2441 int t_request;
2442 int t_queue;
2443 int t_connect;
2444 int t_data;
2445 int t_close;
2446 struct server *srv;
2447
2448 t_request = 0;
2449 t_queue = s->logs.t_queue;
2450 t_connect = s->logs.t_connect;
2451 t_close = s->logs.t_close;
2452 t_data = s->logs.t_data;
2453
2454 if (s->be->mode != PR_MODE_HTTP)
2455 t_data = t_connect;
2456
2457 if (t_connect < 0 || t_data < 0)
2458 return;
2459
2460 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2461 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2462
2463 t_data -= t_connect;
2464 t_connect -= t_queue;
2465 t_queue -= t_request;
2466
2467 srv = objt_server(s->target);
2468 if (srv) {
2469 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2470 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2471 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2472 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2473 }
2474 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2475 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2476 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2477 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2478}
2479
Willy Tarreau7c669d72008-06-20 15:04:11 +02002480/*
2481 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002482 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002483 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002484 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002485 * server.
2486 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002487void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002488{
2489 if (sess->srv_conn == newsrv)
2490 return;
2491
2492 if (sess->srv_conn) {
2493 sess->srv_conn->served--;
2494 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2495 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002496 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002497 }
2498
2499 if (newsrv) {
2500 newsrv->served++;
2501 if (newsrv->proxy->lbprm.server_take_conn)
2502 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002503 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002504 }
2505}
2506
Willy Tarreau84455332009-03-15 22:34:05 +01002507/* Handle server-side errors for default protocols. It is called whenever a a
2508 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002509 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002510 * them. It's installed as ->srv_error for the server-side stream_interface.
2511 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002512void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002513{
2514 int err_type = si->err_type;
2515 int err = 0, fin = 0;
2516
2517 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002518 err = SF_ERR_CLICL;
2519 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002520 }
2521 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002522 err = SF_ERR_CLICL;
2523 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002524 }
2525 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002526 err = SF_ERR_SRVTO;
2527 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002528 }
2529 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002530 err = SF_ERR_SRVCL;
2531 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002532 }
2533 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002534 err = SF_ERR_SRVTO;
2535 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002536 }
2537 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002538 err = SF_ERR_SRVCL;
2539 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002540 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002541 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002542 err = SF_ERR_RESOURCE;
2543 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002544 }
Willy Tarreau84455332009-03-15 22:34:05 +01002545 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002546 err = SF_ERR_INTERNAL;
2547 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002548 }
2549
Willy Tarreaue7dff022015-04-03 01:14:29 +02002550 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002551 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002552 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002553 s->flags |= fin;
2554}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002555
Willy Tarreaue7dff022015-04-03 01:14:29 +02002556/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002557void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002558{
Willy Tarreau87b09662015-04-03 00:22:06 +02002559 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002560 return;
2561
Willy Tarreau87b09662015-04-03 00:22:06 +02002562 channel_shutw_now(&stream->req);
2563 channel_shutr_now(&stream->res);
2564 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002565 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002566 stream->flags |= why;
2567 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002568}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002569
Willy Tarreau8b22a712010-06-18 17:46:06 +02002570/************************************************************************/
2571/* All supported ACL keywords must be declared here. */
2572/************************************************************************/
2573
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002574/* Returns a pointer to a stkctr depending on the fetch keyword name.
2575 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002576 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002577 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002578 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002579 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002580 * NULL may be returned if the designated stkctr is not tracked. For
2581 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2582 * passed. When present, the currently tracked key is then looked up
2583 * in the specified table instead of the current table. The purpose is
2584 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002585 * multiple tables). <strm> is allowed to be NULL, in which case only
2586 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002587 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002588struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002589smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002590{
2591 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002592 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002593 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002594 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002595 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002596
Willy Tarreau0f791d42013-07-23 19:56:43 +02002597 if (num == '_' - '0') {
2598 /* sc_* variant, args[0] = ctr# (mandatory) */
2599 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002600 if (num >= MAX_SESS_STKCTR)
2601 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002602 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002603 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002604 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002605 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002606
2607 if (!conn)
2608 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002609
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002610 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002611 if (!key)
2612 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002613
Willy Tarreaua65536c2013-07-22 22:40:11 +02002614 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002615 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002616 return &stkctr;
2617 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002618
2619 /* Here, <num> contains the counter number from 0 to 9 for
2620 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002621 * args[arg] is the first optional argument. We first lookup the
2622 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002623 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002624
2625 stkptr = &strm->stkctr[num];
2626 if (!strm || !stkctr_entry(stkptr)) {
2627 stkptr = &sess->stkctr[num];
2628 if (!stkctr_entry(stkptr))
2629 return NULL;
2630 }
2631
2632 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002633 if (!stksess)
2634 return NULL;
2635
Willy Tarreau0f791d42013-07-23 19:56:43 +02002636 if (unlikely(args[arg].type == ARGT_TAB)) {
2637 /* an alternate table was specified, let's look up the same key there */
2638 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002639 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002640 return &stkctr;
2641 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002642 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002643}
2644
Willy Tarreau87b09662015-04-03 00:22:06 +02002645/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002646 * currently being tracked or not.
2647 * Supports being called as "sc[0-9]_tracked" only.
2648 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002649static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002650smp_fetch_sc_tracked(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02002651 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002652{
2653 smp->flags = SMP_F_VOL_TEST;
2654 smp->type = SMP_T_BOOL;
Willy Tarreau192252e2015-04-04 01:47:55 +02002655 smp->data.uint = !!smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002656 return 1;
2657}
2658
Willy Tarreau87b09662015-04-03 00:22:06 +02002659/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002660 * frontend counters or from the src.
2661 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2662 * zero is returned if the key is new.
2663 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002664static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002665smp_fetch_sc_get_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002666 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002667{
Willy Tarreau192252e2015-04-04 01:47:55 +02002668 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002669
2670 if (!stkctr)
2671 return 0;
2672
Willy Tarreau37406352012-04-23 16:16:37 +02002673 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002674 smp->type = SMP_T_UINT;
2675 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002676
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002677 if (stkctr_entry(stkctr) != NULL) {
2678 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002679 if (!ptr)
2680 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002681 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002682 }
2683 return 1;
2684}
2685
Willy Tarreau87b09662015-04-03 00:22:06 +02002686/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002687 * tracked frontend counters or from the src.
2688 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2689 * Value zero is returned if the key is new.
2690 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002691static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002692smp_fetch_sc_gpc0_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002693 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002694{
Willy Tarreau192252e2015-04-04 01:47:55 +02002695 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002696
2697 if (!stkctr)
2698 return 0;
2699
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002700 smp->flags = SMP_F_VOL_TEST;
2701 smp->type = SMP_T_UINT;
2702 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002703 if (stkctr_entry(stkctr) != NULL) {
2704 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002705 if (!ptr)
2706 return 0; /* parameter not stored */
2707 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002708 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002709 }
2710 return 1;
2711}
2712
Willy Tarreau87b09662015-04-03 00:22:06 +02002713/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002714 * frontend counters and return it into temp integer.
2715 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002716 */
2717static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002718smp_fetch_sc_inc_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002719 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002720{
Willy Tarreau192252e2015-04-04 01:47:55 +02002721 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002722
2723 if (!stkctr)
2724 return 0;
2725
Willy Tarreau37406352012-04-23 16:16:37 +02002726 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002727 smp->type = SMP_T_UINT;
2728 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002729 if (stkctr_entry(stkctr) != NULL) {
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002730 void *ptr;
2731
2732 /* First, update gpc0_rate if it's tracked. Second, update its
2733 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2734 */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002735 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002736 if (ptr) {
2737 update_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002738 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002739 smp->data.uint = (&stktable_data_cast(ptr, gpc0_rate))->curr_ctr;
2740 }
2741
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002742 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002743 if (ptr)
2744 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
2745
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002746 }
2747 return 1;
2748}
2749
Willy Tarreau87b09662015-04-03 00:22:06 +02002750/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002751 * frontend counters and return its previous value into temp integer.
2752 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002753 */
2754static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002755smp_fetch_sc_clr_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002756 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002757{
Willy Tarreau192252e2015-04-04 01:47:55 +02002758 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002759
2760 if (!stkctr)
2761 return 0;
2762
Willy Tarreau37406352012-04-23 16:16:37 +02002763 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002764 smp->type = SMP_T_UINT;
2765 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002766 if (stkctr_entry(stkctr) != NULL) {
2767 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002768 if (!ptr)
2769 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002770 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002771 stktable_data_cast(ptr, gpc0) = 0;
2772 }
2773 return 1;
2774}
2775
Willy Tarreau87b09662015-04-03 00:22:06 +02002776/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002777 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2778 * "src_conn_cnt" only.
2779 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002780static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002781smp_fetch_sc_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002782 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002783{
Willy Tarreau192252e2015-04-04 01:47:55 +02002784 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002785
2786 if (!stkctr)
2787 return 0;
2788
Willy Tarreau37406352012-04-23 16:16:37 +02002789 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002790 smp->type = SMP_T_UINT;
2791 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002792 if (stkctr_entry(stkctr) != NULL) {
2793 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002794 if (!ptr)
2795 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002796 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002797 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002798 return 1;
2799}
2800
Willy Tarreau87b09662015-04-03 00:22:06 +02002801/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002802 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2803 * only.
2804 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002805static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002806smp_fetch_sc_conn_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002807 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002808{
Willy Tarreau192252e2015-04-04 01:47:55 +02002809 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002810
2811 if (!stkctr)
2812 return 0;
2813
Willy Tarreau37406352012-04-23 16:16:37 +02002814 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002815 smp->type = SMP_T_UINT;
2816 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002817 if (stkctr_entry(stkctr) != NULL) {
2818 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002819 if (!ptr)
2820 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002821 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002822 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002823 }
2824 return 1;
2825}
2826
Willy Tarreau87b09662015-04-03 00:22:06 +02002827/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002828 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002829 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002830 */
2831static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002832smp_fetch_src_updt_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002833 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002834{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002835 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002836 struct stksess *ts;
2837 struct stktable_key *key;
2838 void *ptr;
2839
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002840 if (!conn)
2841 return 0;
2842
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002843 key = addr_to_stktable_key(&conn->addr.from, px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002844 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002845 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002846
Willy Tarreau24e32d82012-04-23 23:55:44 +02002847 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002848
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002849 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2850 /* entry does not exist and could not be created */
2851 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002852
2853 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2854 if (!ptr)
2855 return 0; /* parameter not stored in this table */
2856
Willy Tarreauf853c462012-04-23 18:53:56 +02002857 smp->type = SMP_T_UINT;
2858 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002859 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002860 return 1;
2861}
2862
Willy Tarreau87b09662015-04-03 00:22:06 +02002863/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002864 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2865 * "src_conn_cur" only.
2866 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002867static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002868smp_fetch_sc_conn_cur(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002869 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002870{
Willy Tarreau192252e2015-04-04 01:47:55 +02002871 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002872
2873 if (!stkctr)
2874 return 0;
2875
Willy Tarreau37406352012-04-23 16:16:37 +02002876 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002877 smp->type = SMP_T_UINT;
2878 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002879 if (stkctr_entry(stkctr) != NULL) {
2880 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002881 if (!ptr)
2882 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002883 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002884 }
2885 return 1;
2886}
2887
Willy Tarreau87b09662015-04-03 00:22:06 +02002888/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02002889 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2890 * "src_sess_cnt" only.
2891 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002892static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002893smp_fetch_sc_sess_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002894 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002895{
Willy Tarreau192252e2015-04-04 01:47:55 +02002896 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02002897
2898 if (!stkctr)
2899 return 0;
2900
Willy Tarreau37406352012-04-23 16:16:37 +02002901 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002902 smp->type = SMP_T_UINT;
2903 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002904 if (stkctr_entry(stkctr) != NULL) {
2905 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002906 if (!ptr)
2907 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002908 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002909 }
2910 return 1;
2911}
2912
Willy Tarreau87b09662015-04-03 00:22:06 +02002913/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002914 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2915 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002916static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002917smp_fetch_sc_sess_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002918 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002919{
Willy Tarreau192252e2015-04-04 01:47:55 +02002920 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002921
2922 if (!stkctr)
2923 return 0;
2924
Willy Tarreau37406352012-04-23 16:16:37 +02002925 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002926 smp->type = SMP_T_UINT;
2927 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002928 if (stkctr_entry(stkctr) != NULL) {
2929 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002930 if (!ptr)
2931 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002932 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002933 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002934 }
2935 return 1;
2936}
2937
Willy Tarreau87b09662015-04-03 00:22:06 +02002938/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02002939 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2940 * "src_http_req_cnt" only.
2941 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002942static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002943smp_fetch_sc_http_req_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002944 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002945{
Willy Tarreau192252e2015-04-04 01:47:55 +02002946 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02002947
2948 if (!stkctr)
2949 return 0;
2950
Willy Tarreau37406352012-04-23 16:16:37 +02002951 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002952 smp->type = SMP_T_UINT;
2953 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002954 if (stkctr_entry(stkctr) != NULL) {
2955 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002956 if (!ptr)
2957 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002958 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002959 }
2960 return 1;
2961}
2962
Willy Tarreau87b09662015-04-03 00:22:06 +02002963/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02002964 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2965 * "src_http_req_rate" only.
2966 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002967static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002968smp_fetch_sc_http_req_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002969 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002970{
Willy Tarreau192252e2015-04-04 01:47:55 +02002971 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02002972
2973 if (!stkctr)
2974 return 0;
2975
Willy Tarreau37406352012-04-23 16:16:37 +02002976 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002977 smp->type = SMP_T_UINT;
2978 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002979 if (stkctr_entry(stkctr) != NULL) {
2980 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002981 if (!ptr)
2982 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002983 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02002984 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002985 }
2986 return 1;
2987}
2988
Willy Tarreau87b09662015-04-03 00:22:06 +02002989/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02002990 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2991 * "src_http_err_cnt" only.
2992 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002993static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002994smp_fetch_sc_http_err_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002995 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002996{
Willy Tarreau192252e2015-04-04 01:47:55 +02002997 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02002998
2999 if (!stkctr)
3000 return 0;
3001
Willy Tarreau37406352012-04-23 16:16:37 +02003002 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003003 smp->type = SMP_T_UINT;
3004 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003005 if (stkctr_entry(stkctr) != NULL) {
3006 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003007 if (!ptr)
3008 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003009 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003010 }
3011 return 1;
3012}
3013
Willy Tarreau87b09662015-04-03 00:22:06 +02003014/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003015 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3016 * "src_http_err_rate" only.
3017 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003018static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003019smp_fetch_sc_http_err_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003020 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003021{
Willy Tarreau192252e2015-04-04 01:47:55 +02003022 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003023
3024 if (!stkctr)
3025 return 0;
3026
Willy Tarreau37406352012-04-23 16:16:37 +02003027 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003028 smp->type = SMP_T_UINT;
3029 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003030 if (stkctr_entry(stkctr) != NULL) {
3031 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003032 if (!ptr)
3033 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003034 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003035 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003036 }
3037 return 1;
3038}
3039
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003040/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003041 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003042 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3043 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003044static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003045smp_fetch_sc_kbytes_in(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003046 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003047{
Willy Tarreau192252e2015-04-04 01:47:55 +02003048 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003049
3050 if (!stkctr)
3051 return 0;
3052
Willy Tarreau37406352012-04-23 16:16:37 +02003053 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003054 smp->type = SMP_T_UINT;
3055 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003056 if (stkctr_entry(stkctr) != NULL) {
3057 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003058 if (!ptr)
3059 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003060 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003061 }
3062 return 1;
3063}
3064
Willy Tarreau613fe992013-07-23 17:39:19 +02003065/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003066 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003067 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003068 */
3069static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003070smp_fetch_sc_bytes_in_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003071 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003072{
Willy Tarreau192252e2015-04-04 01:47:55 +02003073 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003074
3075 if (!stkctr)
3076 return 0;
3077
Willy Tarreau37406352012-04-23 16:16:37 +02003078 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003079 smp->type = SMP_T_UINT;
3080 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003081 if (stkctr_entry(stkctr) != NULL) {
3082 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003083 if (!ptr)
3084 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003085 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003086 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003087 }
3088 return 1;
3089}
3090
Willy Tarreau53aea102013-07-23 17:39:02 +02003091/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003092 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003093 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3094 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003095static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003096smp_fetch_sc_kbytes_out(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02003097 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003098{
Willy Tarreau192252e2015-04-04 01:47:55 +02003099 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003100
3101 if (!stkctr)
3102 return 0;
3103
Willy Tarreau37406352012-04-23 16:16:37 +02003104 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003105 smp->type = SMP_T_UINT;
3106 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003107 if (stkctr_entry(stkctr) != NULL) {
3108 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003109 if (!ptr)
3110 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003111 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003112 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003113 return 1;
3114}
3115
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003116/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003117 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003118 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003119 */
3120static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003121smp_fetch_sc_bytes_out_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003122 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003123{
Willy Tarreau192252e2015-04-04 01:47:55 +02003124 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003125
3126 if (!stkctr)
3127 return 0;
3128
Willy Tarreau37406352012-04-23 16:16:37 +02003129 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003130 smp->type = SMP_T_UINT;
3131 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003132 if (stkctr_entry(stkctr) != NULL) {
3133 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003134 if (!ptr)
3135 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003136 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003137 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003138 }
3139 return 1;
3140}
3141
Willy Tarreau87b09662015-04-03 00:22:06 +02003142/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003143 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3144 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003145static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003146smp_fetch_sc_trackers(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003147 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003148{
Willy Tarreau192252e2015-04-04 01:47:55 +02003149 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003150
Willy Tarreau563eef42013-07-23 18:32:02 +02003151 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003152 return 0;
3153
Willy Tarreau563eef42013-07-23 18:32:02 +02003154 smp->flags = SMP_F_VOL_TEST;
3155 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003156 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003157 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003158}
3159
Willy Tarreau34db1082012-04-19 17:16:54 +02003160/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003161 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003162 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003163static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003164smp_fetch_table_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003165 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003166{
Willy Tarreau37406352012-04-23 16:16:37 +02003167 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003168 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003169 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003170 return 1;
3171}
3172
Willy Tarreau34db1082012-04-19 17:16:54 +02003173/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003174 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003175 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003176static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003177smp_fetch_table_avl(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003178 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003179{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003180 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003181 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003182 smp->type = SMP_T_UINT;
3183 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003184 return 1;
3185}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003186
Willy Tarreau61612d42012-04-19 18:42:05 +02003187/* Note: must not be declared <const> as its list will be overwritten.
3188 * Please take care of keeping this list alphabetically sorted.
3189 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003190static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003191 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003192}};
3193
Willy Tarreau281c7992013-01-08 01:23:27 +01003194/* Note: must not be declared <const> as its list will be overwritten.
3195 * Please take care of keeping this list alphabetically sorted.
3196 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003197static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003198 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3199 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3200 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3201 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3202 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3203 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3204 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3205 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3206 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3207 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3208 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3209 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3210 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3211 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3212 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3213 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3214 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3215 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3216 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3217 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3218 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3219 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3220 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3221 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3222 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3223 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3224 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3225 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3226 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3227 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3228 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3229 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3230 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3231 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3232 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3233 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3234 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3235 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3236 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3237 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3238 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3239 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3240 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3241 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3242 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3243 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3244 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3245 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3246 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3247 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3248 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3249 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3250 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3251 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3252 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3253 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3254 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3255 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3256 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3257 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3258 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3259 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3260 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3261 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3262 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3263 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3264 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3265 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3266 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3267 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3268 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3269 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3270 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3271 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3272 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3273 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3274 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3275 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3276 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3277 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3278 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3279 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3280 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3281 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3282 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3283 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3284 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3285 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3286 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3287 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3288 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3289 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3290 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3291 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3292 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3293 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003294 { /* END */ },
3295}};
3296
Willy Tarreau8b22a712010-06-18 17:46:06 +02003297__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003298static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003299{
Willy Tarreau281c7992013-01-08 01:23:27 +01003300 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003301 acl_register_keywords(&acl_kws);
3302}
3303
Willy Tarreaubaaee002006-06-26 02:48:02 +02003304/*
3305 * Local variables:
3306 * c-indent-level: 8
3307 * c-basic-offset: 8
3308 * End:
3309 */