blob: b28dff7a2c0ae447ca4b37e61dd061ba08cb4191 [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 Tarreau2542b532012-08-31 16:01:23 +020064 */
Willy Tarreau02d86382015-04-05 12:00:52 +020065struct stream *stream_new(struct session *sess, struct task *t)
Willy Tarreau2542b532012-08-31 16:01:23 +020066{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020067 struct stream *s;
Willy Tarreaufb0afa72015-04-03 14:46:27 +020068 struct listener *l = sess->listener;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020069 struct proxy *p = sess->fe;
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;
84 s->logs.logwait = p->to_log;
85 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 Tarreau2542b532012-08-31 16:01:23 +0200125 t->process = l->handler;
126 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 Tarreaub36b4242010-06-04 20:59:39 +0200140 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200141
Willy Tarreaub4c84932013-07-23 19:15:30 +0200142 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200143 void *ptr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200144 struct stkctr *stkctr = &sess->stkctr[i];
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200145
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200146 if (!stkctr_entry(stkctr))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100147 continue;
148
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200149 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200150 if (ptr)
151 stktable_data_cast(ptr, sess_cnt)++;
152
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200153 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200154 if (ptr)
155 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200156 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200157 }
158
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200159 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100160 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200161 si_set_state(&s->si[0], SI_ST_EST);
162
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200163 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200164 if (conn)
165 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200166 else if (appctx)
167 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200168
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200169 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200170 s->si[0].flags |= SI_FL_INDEP_STR;
171
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200172 /* pre-initialize the other side's stream interface to an INIT state. The
173 * callbacks will be initialized before attempting to connect.
174 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100175 si_reset(&s->si[1]);
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200176 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200177
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200178 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200179 s->si[1].flags |= SI_FL_INDEP_STR;
180
Willy Tarreau87b09662015-04-03 00:22:06 +0200181 stream_init_srv_conn(s);
Willy Tarreau10b688f2015-03-13 16:43:12 +0100182 s->target = l->default_target; /* used by peers and CLI */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200183 s->pend_pos = NULL;
184
185 /* init store persistence */
186 s->store_count = 0;
187
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100188 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100189 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200190
191 /* activate default analysers enabled for this listener */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100192 s->req.analysers = l->analysers;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200193
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200194 if (!s->req.analysers) {
195 channel_auto_connect(&s->req); /* don't wait to establish connection */
196 channel_auto_close(&s->req); /* let the producer forward close requests */
197 }
198
199 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100200 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100201 s->req.rex = TICK_ETERNITY;
202 s->req.wex = TICK_ETERNITY;
203 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200204
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100205 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100206 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100207 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200208
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200209 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100210 s->req.flags |= CF_NEVER_WAIT;
211 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200212 }
213
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200214 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100215 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100216 s->res.rex = TICK_ETERNITY;
217 s->res.wex = TICK_ETERNITY;
218 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200219
Willy Tarreaueee5b512015-04-03 23:46:31 +0200220 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100221
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100222 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100223
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200224 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200225 if (conn)
226 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200227 else if (appctx)
228 s->si[0].flags |= SI_FL_WAIT_DATA;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200229
Willy Tarreaue0232f12015-04-05 18:01:06 +0200230 if (p->accept && p->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200231 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200232
Willy Tarreauf2b98742015-04-05 01:30:42 +0200233 if (conn) {
234 /* if logs require transport layer information, note it on the connection */
235 if (s->logs.logwait & LW_XPRT)
236 conn->flags |= CO_FL_XPRT_TRACKED;
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200237
Willy Tarreauf2b98742015-04-05 01:30:42 +0200238 /* we want the connection handler to notify the stream interface about updates. */
239 conn->flags |= CO_FL_WAKE_DATA;
240 }
Willy Tarreau2542b532012-08-31 16:01:23 +0200241
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200242 /* it is important not to call the wakeup function directly but to
243 * pass through task_wakeup(), because this one knows how to apply
244 * priorities to tasks.
245 */
246 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200247 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200248
249 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200250 out_fail_accept:
Willy Tarreau3b246412014-11-25 17:10:33 +0100251 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200252 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200253 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200254}
255
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200257 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200258 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200259static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200260{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200261 struct session *sess = strm_sess(s);
262 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100263 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200264 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200265 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100266
Willy Tarreaubaaee002006-06-26 02:48:02 +0200267 if (s->pend_pos)
268 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100269
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100270 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200271 if (s->flags & SF_CURR_SESS) {
272 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100273 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100274 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100275 if (may_dequeue_tasks(objt_server(s->target), s->be))
276 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100277 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100278
Willy Tarreau7c669d72008-06-20 15:04:11 +0200279 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200280 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200281 * it should normally be only the same as the one above,
282 * so this should not happen in fact.
283 */
284 sess_change_server(s, NULL);
285 }
286
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100287 if (s->req.pipe)
288 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100289
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100290 if (s->res.pipe)
291 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100292
Willy Tarreaubf883e02014-11-25 21:10:35 +0100293 /* We may still be present in the buffer wait queue */
294 if (!LIST_ISEMPTY(&s->buffer_wait)) {
295 LIST_DEL(&s->buffer_wait);
296 LIST_INIT(&s->buffer_wait);
297 }
298
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100299 b_drop(&s->req.buf);
300 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100301 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200302 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200303
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100304 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200305 if (s->txn)
306 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100307
Willy Tarreau1e954912012-10-12 17:50:05 +0200308 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200309 if (cli_conn)
310 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200311
Willy Tarreaua4cda672010-06-06 18:28:49 +0200312 for (i = 0; i < s->store_count; i++) {
313 if (!s->store[i].ts)
314 continue;
315 stksess_free(s->store[i].table, s->store[i].ts);
316 s->store[i].ts = NULL;
317 }
318
Willy Tarreaueee5b512015-04-03 23:46:31 +0200319 if (s->txn) {
320 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
321 pool_free2(pool2_http_txn, s->txn);
322 s->txn = NULL;
323 }
324
Willy Tarreau92fb9832007-10-16 17:34:28 +0200325 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200326 pool_free2(fe->rsp_cap_pool, s->res_cap);
327 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100329
Willy Tarreau87b09662015-04-03 00:22:06 +0200330 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200331
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100332 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100333 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200334 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100335 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100336 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100337 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200338 if (s->list.n != &streams)
339 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100340 bref->ref = s->list.n;
341 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100342 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200343 si_release_endpoint(&s->si[1]);
344 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200345
346 /* FIXME: for now we have a 1:1 relation between stream and session so
347 * the stream must free the session.
348 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200349 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200350 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200351
352 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200353 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200354 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200355 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200356 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200357 pool_flush2(pool2_requri);
358 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200359 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200360 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100361 pool_flush2(pool2_connection);
362 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200363 pool_flush2(fe->req_cap_pool);
364 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200365 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200366}
367
Willy Tarreau78955f42014-12-28 13:09:02 +0100368/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
369 * that it's not the last available buffer or it's the response buffer. Unless
370 * the buffer is the response buffer, an extra control is made so that we always
371 * keep <tune.buffers.reserved> buffers available after this allocation. To be
372 * called at the beginning of recv() callbacks to ensure that the required
373 * buffers are properly allocated. Returns 0 in case of failure, non-zero
374 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100375 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200376int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100377{
Willy Tarreau87b09662015-04-03 00:22:06 +0200378 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100379 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100380 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100381
Willy Tarreau78955f42014-12-28 13:09:02 +0100382 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100383 margin = global.tune.reserved_bufs;
384
Willy Tarreau78955f42014-12-28 13:09:02 +0100385 s = chn_sess(chn);
386
387 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100388 if (b)
389 return 1;
390
Willy Tarreaubf883e02014-11-25 21:10:35 +0100391 if (LIST_ISEMPTY(&s->buffer_wait))
392 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100393 return 0;
394}
395
Willy Tarreau87b09662015-04-03 00:22:06 +0200396/* Allocates a work buffer for stream <s>. It is meant to be called inside
397 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaua24adf02014-11-27 01:11:56 +0100398 * to work fine. For a regular connection, only the response is needed so that
399 * an error message may be built and returned. In case where the initiator is
400 * an applet (eg: peers), then we need to allocate the request buffer for the
401 * applet to be able to send its data (in this case a response is not needed).
402 * Request buffers are never picked from the reserved pool, but response
403 * buffers may be allocated from the reserve. This is critical to ensure that
404 * a response may always flow and will never block a server from releasing a
405 * connection. Returns 0 in case of failure, non-zero otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100406 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200407int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100408{
Willy Tarreaua24adf02014-11-27 01:11:56 +0100409 int margin;
410 struct buffer **buf;
411
412 if (objt_appctx(s->si[0].end)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100413 buf = &s->req.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100414 margin = global.tune.reserved_bufs;
415 }
416 else {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100417 buf = &s->res.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100418 margin = 0;
419 }
420
Willy Tarreaubf883e02014-11-25 21:10:35 +0100421 if (!LIST_ISEMPTY(&s->buffer_wait)) {
422 LIST_DEL(&s->buffer_wait);
423 LIST_INIT(&s->buffer_wait);
424 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100425
Willy Tarreaua24adf02014-11-27 01:11:56 +0100426 if (b_alloc_margin(buf, margin))
Willy Tarreau656859d2014-11-25 19:46:36 +0100427 return 1;
428
Willy Tarreaubf883e02014-11-25 21:10:35 +0100429 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100430 return 0;
431}
432
433/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100434 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200435 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100436 * a single buffer, so it makes sense to try to wake two up when two buffers
437 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100438 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200439void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100440{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100441 if (s->req.buf->size && buffer_empty(s->req.buf))
442 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100443
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100444 if (s->res.buf->size && buffer_empty(s->res.buf))
445 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100446
Willy Tarreaubf883e02014-11-25 21:10:35 +0100447 /* if we're certain to have at least 1 buffer available, and there is
448 * someone waiting, we can wake up a waiter and offer them.
449 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100450 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200451 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100452}
453
Willy Tarreau87b09662015-04-03 00:22:06 +0200454/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100455 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200456 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100457 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200458void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100459{
Willy Tarreau87b09662015-04-03 00:22:06 +0200460 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100461
462 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100463 if (rqlimit <= run_queue)
464 break;
465
Willy Tarreaubf883e02014-11-25 21:10:35 +0100466 if (sess->task->state & TASK_RUNNING)
467 continue;
468
469 LIST_DEL(&sess->buffer_wait);
470 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100471 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100472 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100473}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200474
475/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200476int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200477{
Willy Tarreau87b09662015-04-03 00:22:06 +0200478 LIST_INIT(&streams);
479 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
480 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200481}
482
Willy Tarreau87b09662015-04-03 00:22:06 +0200483void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100484{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200485 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100486 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100487 void *ptr;
488 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100489
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100490 bytes = s->req.total - s->logs.bytes_in;
491 s->logs.bytes_in = s->req.total;
492 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200493 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100494
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100495 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100496
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100497 if (objt_server(s->target))
498 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200499
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200500 if (sess->listener && sess->listener->counters)
501 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200502
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100503 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200504 struct stkctr *stkctr = &s->stkctr[i];
505
506 if (!stkctr_entry(stkctr)) {
507 stkctr = &sess->stkctr[i];
508 if (!stkctr_entry(stkctr))
509 continue;
510 }
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_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100513 if (ptr)
514 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200515
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200516 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100517 if (ptr)
518 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200519 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100520 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100521 }
522
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100523 bytes = s->res.total - s->logs.bytes_out;
524 s->logs.bytes_out = s->res.total;
525 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200526 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100527
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100528 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100529
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100530 if (objt_server(s->target))
531 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200532
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200533 if (sess->listener && sess->listener->counters)
534 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200535
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100536 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200537 struct stkctr *stkctr = &s->stkctr[i];
538
539 if (!stkctr_entry(stkctr)) {
540 stkctr = &sess->stkctr[i];
541 if (!stkctr_entry(stkctr))
542 continue;
543 }
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_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100546 if (ptr)
547 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200548
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200549 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100550 if (ptr)
551 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200552 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100553 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100554 }
555}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200556
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100557/* This function is called with (si->state == SI_ST_CON) meaning that a
558 * connection was attempted and that the file descriptor is already allocated.
559 * We must check for establishment, error and abort. Possible output states
560 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
561 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200562 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100563 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200564static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100565{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100566 struct stream_interface *si = &s->si[1];
567 struct channel *req = &s->req;
568 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200569 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100570
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100571 /* If we got an error, or if nothing happened and the connection timed
572 * out, we must give up. The CER state handler will take care of retry
573 * attempts and error reports.
574 */
575 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100576 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100577 /* Some data were sent past the connection establishment,
578 * so we need to pretend we're established to log correctly
579 * and let later states handle the failure.
580 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100581 si->state = SI_ST_EST;
582 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100583 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100584 return 1;
585 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100586 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100587 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100588
Willy Tarreauf79c8172013-10-21 16:30:56 +0200589 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100590
591 if (si->err_type)
592 return 0;
593
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100594 if (si->flags & SI_FL_ERR)
595 si->err_type = SI_ET_CONN_ERR;
596 else
597 si->err_type = SI_ET_CONN_TO;
598 return 0;
599 }
600
601 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100602 if (!(req->flags & CF_WRITE_PARTIAL) &&
603 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200604 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
605 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100606 s->be->options & PR_O_ABRT_CLOSE)))) {
607 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200608 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100609 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100610 if (s->srv_error)
611 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100612 return 1;
613 }
614
615 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200616 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100617 return 1;
618
619 /* OK, this means that a connection succeeded. The caller will be
620 * responsible for handling the transition from CON to EST.
621 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100622 si->state = SI_ST_EST;
623 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100624 return 1;
625}
626
627/* This function is called with (si->state == SI_ST_CER) meaning that a
628 * previous connection attempt has failed and that the file descriptor
629 * has already been released. Possible causes include asynchronous error
630 * notification and time out. Possible output states are SI_ST_CLO when
631 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
632 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
633 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
634 * marked as in error state. It returns 0.
635 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200636static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100637{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100638 struct stream_interface *si = &s->si[1];
639
Willy Tarreau87b09662015-04-03 00:22:06 +0200640 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100641 if (objt_server(s->target)) {
642 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100643
Willy Tarreaue7dff022015-04-03 01:14:29 +0200644 if (s->flags & SF_CURR_SESS) {
645 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100646 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100647 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100648 }
649
650 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200651 si->conn_retries--;
652 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100653 if (!si->err_type) {
654 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100655 }
656
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100657 if (objt_server(s->target))
658 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100659 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100660 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100661 if (may_dequeue_tasks(objt_server(s->target), s->be))
662 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100663
664 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200665 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100666 s->req.flags |= CF_WRITE_ERROR;
667 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100668
669 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100670 if (s->srv_error)
671 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100672 return 0;
673 }
674
675 /* If the "redispatch" option is set on the backend, we are allowed to
676 * retry on another server for the last retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200677 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100678 * bit to ignore any persistence cookie. We won't count a retry nor a
679 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200680 * If the connection is not persistent, the balancing algorithm is not
681 * determinist (round robin) and there is more than one active server,
682 * we accept to perform an immediate redispatch without waiting since
683 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100684 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200685 if (objt_server(s->target) &&
686 (si->conn_retries == 0 ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200687 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Willy Tarreau33a14e52014-06-13 17:49:40 +0200688 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200689 s->be->options & PR_O_REDISP && !(s->flags & SF_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100690 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100691 if (may_dequeue_tasks(objt_server(s->target), s->be))
692 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100693
Willy Tarreaue7dff022015-04-03 01:14:29 +0200694 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100695 si->state = SI_ST_REQ;
696 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100697 if (objt_server(s->target))
698 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100699 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100700 si->state = SI_ST_ASS;
701 }
702
703 if (si->flags & SI_FL_ERR) {
704 /* The error was an asynchronous connection error, and we will
705 * likely have to retry connecting to the same server, most
706 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200707 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100708 */
709
Willy Tarreaub0290662014-06-13 17:04:44 +0200710 int delay = 1000;
711
712 if (s->be->timeout.connect && s->be->timeout.connect < delay)
713 delay = s->be->timeout.connect;
714
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100715 if (!si->err_type)
716 si->err_type = SI_ET_CONN_ERR;
717
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200718 /* only wait when we're retrying on the same server */
719 if (si->state == SI_ST_ASS ||
720 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
721 (s->be->srv_act <= 1)) {
722 si->state = SI_ST_TAR;
723 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
724 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100725 return 0;
726 }
727 return 0;
728}
729
730/*
731 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200732 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200733 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100734 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200735static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100737 struct stream_interface *si = &s->si[1];
738 struct channel *req = &s->req;
739 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100740
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100741 /* First, centralize the timers information */
742 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
743 si->exp = TICK_ETERNITY;
744
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100745 if (objt_server(s->target))
746 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100747
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100748 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100749 /* if the user wants to log as soon as possible, without counting
750 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200751 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100752 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100753 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100754 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100755 }
756 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100757 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758 }
759
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200760 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200761 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200762 if (req->flags & CF_WAKE_CONNECT) {
763 req->flags |= CF_WAKE_ONCE;
764 req->flags &= ~CF_WAKE_CONNECT;
765 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200766 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200767 /* real connections have timeouts */
768 req->wto = s->be->timeout.server;
769 rep->rto = s->be->timeout.server;
770 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100771 req->wex = TICK_ETERNITY;
772}
773
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100774/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
775 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100776 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
777 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
778 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100779 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200780static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100781{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100782 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100783 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100784 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100785
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100786 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 +0100787 now_ms, __FUNCTION__,
788 s,
Willy Tarreau103197d2014-11-28 15:26:12 +0100789 req, s->rep,
790 req->rex, s->res.wex,
791 req->flags, s->res.flags,
792 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 +0100793
794 if (si->state == SI_ST_ASS) {
795 /* Server assigned to connection request, we have to try to connect now */
796 int conn_err;
797
798 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100799 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100800
Willy Tarreaue7dff022015-04-03 01:14:29 +0200801 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100802 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100803 if (srv)
804 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500805 if (srv)
806 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100807 return;
808 }
809
810 /* We have received a synchronous error. We might have to
811 * abort, retry immediately or redispatch.
812 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200813 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100814 if (!si->err_type) {
815 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100816 }
817
Willy Tarreau827aee92011-03-10 16:55:02 +0100818 if (srv)
819 srv_inc_sess_ctr(srv);
820 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500821 srv_set_sess_last(srv);
822 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100823 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100824 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100825
Willy Tarreau87b09662015-04-03 00:22:06 +0200826 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100827 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100828 if (may_dequeue_tasks(srv, s->be))
829 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100830
831 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200832 si_shutr(si);
833 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100834 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100835
836 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
837
Willy Tarreau87b09662015-04-03 00:22:06 +0200838 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100839 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100840 if (s->srv_error)
841 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100842 return;
843 }
844
845 /* We are facing a retryable error, but we don't want to run a
846 * turn-around now, as the problem is likely a source port
847 * allocation problem, so we want to retry now.
848 */
849 si->state = SI_ST_CER;
850 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100851 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100852 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
853 return;
854 }
855 else if (si->state == SI_ST_QUE) {
856 /* connection request was queued, check for any update */
857 if (!s->pend_pos) {
858 /* The connection is not in the queue anymore. Either
859 * we have a server connection slot available and we
860 * go directly to the assigned state, or we need to
861 * load-balance first and go to the INI state.
862 */
863 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200864 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100865 si->state = SI_ST_REQ;
866 else {
867 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
868 si->state = SI_ST_ASS;
869 }
870 return;
871 }
872
873 /* Connection request still in queue... */
874 if (si->flags & SI_FL_EXP) {
875 /* ... and timeout expired */
876 si->exp = TICK_ETERNITY;
877 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100878 if (srv)
879 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100880 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200881 si_shutr(si);
882 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100883 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100884 if (!si->err_type)
885 si->err_type = SI_ET_QUEUE_TO;
886 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100887 if (s->srv_error)
888 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100889 return;
890 }
891
892 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100893 if ((req->flags & (CF_READ_ERROR)) ||
894 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
895 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100896 /* give up */
897 si->exp = TICK_ETERNITY;
898 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200899 si_shutr(si);
900 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100901 si->err_type |= SI_ET_QUEUE_ABRT;
902 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100903 if (s->srv_error)
904 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100905 return;
906 }
907
908 /* Nothing changed */
909 return;
910 }
911 else if (si->state == SI_ST_TAR) {
912 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100913 if ((req->flags & (CF_READ_ERROR)) ||
914 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
915 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100916 /* give up */
917 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200918 si_shutr(si);
919 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100920 si->err_type |= SI_ET_CONN_ABRT;
921 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100922 if (s->srv_error)
923 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100924 return;
925 }
926
927 if (!(si->flags & SI_FL_EXP))
928 return; /* still in turn-around */
929
930 si->exp = TICK_ETERNITY;
931
Willy Tarreau87b09662015-04-03 00:22:06 +0200932 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100933 * marked "assigned".
934 * FIXME: Should we force a redispatch attempt when the server is down ?
935 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200936 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100937 si->state = SI_ST_ASS;
938 else
939 si->state = SI_ST_REQ;
940 return;
941 }
942}
943
Willy Tarreau87b09662015-04-03 00:22:06 +0200944/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900945 * also counts a failed request if the server state has not reached the request
946 * stage.
947 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200948static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900949{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200950 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900951 if (s->si[1].state < SI_ST_REQ) {
952
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200953 strm_fe(s)->fe_counters.failed_req++;
954 if (strm_li(s)->counters)
955 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900956
Willy Tarreaue7dff022015-04-03 01:14:29 +0200957 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900958 }
959 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200960 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900961 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200962 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900963 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200964 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900965 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200966 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900967 }
968}
969
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100970/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100971 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
972 * a real connection to a server, indicating that a server has been assigned,
973 * or SI_ST_EST for a successful connection to an applet. It may also return
974 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100975 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200976static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100977{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100978 struct stream_interface *si = &s->si[1];
979
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100980 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 +0100981 now_ms, __FUNCTION__,
982 s,
983 s->req, s->rep,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100984 s->req.rex, s->res.wex,
985 s->req.flags, s->res.flags,
Willy Tarreau350f4872014-11-28 14:42:25 +0100986 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 +0100987
988 if (si->state != SI_ST_REQ)
989 return;
990
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100991 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
992 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100993 struct appctx *appctx = objt_appctx(si->end);
994
995 if (!appctx || appctx->applet != __objt_applet(s->target))
996 appctx = stream_int_register_handler(si, objt_applet(s->target));
997
998 if (!appctx) {
999 /* No more memory, let's immediately abort. Force the
1000 * error code to ignore the ERR_LOCAL which is not a
1001 * real error.
1002 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001003 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001004
1005 si_shutr(si);
1006 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001007 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001008 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001009 si->state = SI_ST_CLO;
1010 if (s->srv_error)
1011 s->srv_error(s, si);
1012 return;
1013 }
1014
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001015 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001016 si->state = SI_ST_EST;
1017 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001018 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001019 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001020 return;
1021 }
1022
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001023 /* Try to assign a server */
1024 if (srv_redispatch_connect(s) != 0) {
1025 /* We did not get a server. Either we queued the
1026 * connection request, or we encountered an error.
1027 */
1028 if (si->state == SI_ST_QUE)
1029 return;
1030
1031 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001032 si_shutr(si);
1033 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001034 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001035 if (!si->err_type)
1036 si->err_type = SI_ET_CONN_OTHER;
1037 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001038 if (s->srv_error)
1039 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001040 return;
1041 }
1042
1043 /* The server is assigned */
1044 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1045 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001046 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001047}
1048
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001049/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001050 * if appropriate. The default_backend rule is also considered, then the
1051 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001052 * It returns 1 if the processing can continue on next analysers, or zero if it
1053 * either needs more data or wants to immediately abort the request.
1054 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001055static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001056{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001057 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001058 struct session *sess = s->sess;
1059 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001060
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001061 req->analysers &= ~an_bit;
1062 req->analyse_exp = TICK_ETERNITY;
1063
Willy Tarreau87b09662015-04-03 00:22:06 +02001064 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 +02001065 now_ms, __FUNCTION__,
1066 s,
1067 req,
1068 req->rex, req->wex,
1069 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001070 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001071 req->analysers);
1072
1073 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001074 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001075 struct switching_rule *rule;
1076
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001077 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001078 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001079
Willy Tarreauf51658d2014-04-23 01:21:56 +02001080 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001081 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001082 ret = acl_pass(ret);
1083 if (rule->cond->pol == ACL_COND_UNLESS)
1084 ret = !ret;
1085 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001086
1087 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001088 /* If the backend name is dynamic, try to resolve the name.
1089 * If we can't resolve the name, or if any error occurs, break
1090 * the loop and fallback to the default backend.
1091 */
1092 struct proxy *backend;
1093
1094 if (rule->dynamic) {
1095 struct chunk *tmp = get_trash_chunk();
1096 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1097 break;
1098 backend = findproxy(tmp->str, PR_CAP_BE);
1099 if (!backend)
1100 break;
1101 }
1102 else
1103 backend = rule->be.backend;
1104
Willy Tarreau87b09662015-04-03 00:22:06 +02001105 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001106 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001107 break;
1108 }
1109 }
1110
1111 /* To ensure correct connection accounting on the backend, we
1112 * have to assign one if it was not set (eg: a listen). This
1113 * measure also takes care of correctly setting the default
1114 * backend if any.
1115 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001116 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001117 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001118 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001119 }
1120
Willy Tarreaufb356202010-08-03 14:02:05 +02001121 /* 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 +02001122 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001123 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1124 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001125 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001126
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001127 /* 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 +02001128 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001129 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001130 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001131 int ret = 1;
1132
1133 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001134 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 +01001135 ret = acl_pass(ret);
1136 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1137 ret = !ret;
1138 }
1139
1140 if (ret) {
1141 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001142 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001143 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001144 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001145 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001146 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001147 break;
1148 }
1149 }
1150
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001151 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001152
1153 sw_failed:
1154 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001155 channel_abort(&s->req);
1156 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001157
Willy Tarreaue7dff022015-04-03 01:14:29 +02001158 if (!(s->flags & SF_ERR_MASK))
1159 s->flags |= SF_ERR_RESOURCE;
1160 if (!(s->flags & SF_FINST_MASK))
1161 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001162
Willy Tarreaueee5b512015-04-03 23:46:31 +02001163 if (s->txn)
1164 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001165 s->req.analysers = 0;
1166 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001167 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001168}
1169
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001170/* This stream analyser works on a request. It applies all use-server rules on
1171 * it then returns 1. The data must already be present in the buffer otherwise
1172 * they won't match. It always returns 1.
1173 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001174static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001175{
1176 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001177 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001178 struct server_rule *rule;
1179
Willy Tarreau87b09662015-04-03 00:22:06 +02001180 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 +02001181 now_ms, __FUNCTION__,
1182 s,
1183 req,
1184 req->rex, req->wex,
1185 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001186 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001187 req->analysers);
1188
Willy Tarreaue7dff022015-04-03 01:14:29 +02001189 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001190 list_for_each_entry(rule, &px->server_rules, list) {
1191 int ret;
1192
Willy Tarreau192252e2015-04-04 01:47:55 +02001193 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001194 ret = acl_pass(ret);
1195 if (rule->cond->pol == ACL_COND_UNLESS)
1196 ret = !ret;
1197
1198 if (ret) {
1199 struct server *srv = rule->srv.ptr;
1200
Willy Tarreau892337c2014-05-13 23:41:20 +02001201 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001202 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001203 (s->flags & SF_FORCE_PRST)) {
1204 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001205 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001206 break;
1207 }
1208 /* if the server is not UP, let's go on with next rules
1209 * just in case another one is suited.
1210 */
1211 }
1212 }
1213 }
1214
1215 req->analysers &= ~an_bit;
1216 req->analyse_exp = TICK_ETERNITY;
1217 return 1;
1218}
1219
Emeric Brun1d33b292010-01-04 15:47:17 +01001220/* This stream analyser works on a request. It applies all sticking rules on
1221 * it then returns 1. The data must already be present in the buffer otherwise
1222 * they won't match. It always returns 1.
1223 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001224static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001225{
1226 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001227 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001228 struct sticking_rule *rule;
1229
Willy Tarreau87b09662015-04-03 00:22:06 +02001230 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 +01001231 now_ms, __FUNCTION__,
1232 s,
1233 req,
1234 req->rex, req->wex,
1235 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001236 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001237 req->analysers);
1238
1239 list_for_each_entry(rule, &px->sticking_rules, list) {
1240 int ret = 1 ;
1241 int i;
1242
Willy Tarreau9667a802013-12-09 12:52:13 +01001243 /* Only the first stick store-request of each table is applied
1244 * and other ones are ignored. The purpose is to allow complex
1245 * configurations which look for multiple entries by decreasing
1246 * order of precision and to stop at the first which matches.
1247 * An example could be a store of the IP address from an HTTP
1248 * header first, then from the source if not found.
1249 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001250 for (i = 0; i < s->store_count; i++) {
1251 if (rule->table.t == s->store[i].table)
1252 break;
1253 }
1254
1255 if (i != s->store_count)
1256 continue;
1257
1258 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001259 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001260 ret = acl_pass(ret);
1261 if (rule->cond->pol == ACL_COND_UNLESS)
1262 ret = !ret;
1263 }
1264
1265 if (ret) {
1266 struct stktable_key *key;
1267
Willy Tarreau192252e2015-04-04 01:47:55 +02001268 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 +01001269 if (!key)
1270 continue;
1271
1272 if (rule->flags & STK_IS_MATCH) {
1273 struct stksess *ts;
1274
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001275 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001276 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001277 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001278 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001279
1280 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001281 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1282 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001283 if (node) {
1284 struct server *srv;
1285
1286 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001287 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001288 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001289 (s->flags & SF_FORCE_PRST)) {
1290 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001291 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001292 }
1293 }
1294 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001295 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001296 }
1297 }
1298 if (rule->flags & STK_IS_STORE) {
1299 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1300 struct stksess *ts;
1301
1302 ts = stksess_new(rule->table.t, key);
1303 if (ts) {
1304 s->store[s->store_count].table = rule->table.t;
1305 s->store[s->store_count++].ts = ts;
1306 }
1307 }
1308 }
1309 }
1310 }
1311
1312 req->analysers &= ~an_bit;
1313 req->analyse_exp = TICK_ETERNITY;
1314 return 1;
1315}
1316
1317/* This stream analyser works on a response. It applies all store rules on it
1318 * then returns 1. The data must already be present in the buffer otherwise
1319 * they won't match. It always returns 1.
1320 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001321static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001322{
1323 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001324 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001325 struct sticking_rule *rule;
1326 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001327 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001328
Willy Tarreau87b09662015-04-03 00:22:06 +02001329 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 +01001330 now_ms, __FUNCTION__,
1331 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001332 rep,
1333 rep->rex, rep->wex,
1334 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001335 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001336 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001337
1338 list_for_each_entry(rule, &px->storersp_rules, list) {
1339 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001340
Willy Tarreau9667a802013-12-09 12:52:13 +01001341 /* Only the first stick store-response of each table is applied
1342 * and other ones are ignored. The purpose is to allow complex
1343 * configurations which look for multiple entries by decreasing
1344 * order of precision and to stop at the first which matches.
1345 * An example could be a store of a set-cookie value, with a
1346 * fallback to a parameter found in a 302 redirect.
1347 *
1348 * The store-response rules are not allowed to override the
1349 * store-request rules for the same table, but they may coexist.
1350 * Thus we can have up to one store-request entry and one store-
1351 * response entry for the same table at any time.
1352 */
1353 for (i = nbreq; i < s->store_count; i++) {
1354 if (rule->table.t == s->store[i].table)
1355 break;
1356 }
1357
1358 /* skip existing entries for this table */
1359 if (i < s->store_count)
1360 continue;
1361
Emeric Brun1d33b292010-01-04 15:47:17 +01001362 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001363 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001364 ret = acl_pass(ret);
1365 if (rule->cond->pol == ACL_COND_UNLESS)
1366 ret = !ret;
1367 }
1368
1369 if (ret) {
1370 struct stktable_key *key;
1371
Willy Tarreau192252e2015-04-04 01:47:55 +02001372 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 +01001373 if (!key)
1374 continue;
1375
Willy Tarreau37e340c2013-12-06 23:05:21 +01001376 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001377 struct stksess *ts;
1378
1379 ts = stksess_new(rule->table.t, key);
1380 if (ts) {
1381 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001382 s->store[s->store_count++].ts = ts;
1383 }
1384 }
1385 }
1386 }
1387
1388 /* process store request and store response */
1389 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001390 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001391 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001392
Willy Tarreauc93cd162014-05-13 15:54:22 +02001393 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001394 stksess_free(s->store[i].table, s->store[i].ts);
1395 s->store[i].ts = NULL;
1396 continue;
1397 }
1398
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001399 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1400 if (ts) {
1401 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001402 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001403 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001404 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001405 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001406 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001407
1408 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001409 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001410 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001411 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001412 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001413
1414 rep->analysers &= ~an_bit;
1415 rep->analyse_exp = TICK_ETERNITY;
1416 return 1;
1417}
1418
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001419/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001420 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001421 */
1422#define UPDATE_ANALYSERS(real, list, back, flag) { \
1423 list = (((list) & ~(flag)) | ~(back)) & (real); \
1424 back = real; \
1425 if (!(list)) \
1426 break; \
1427 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1428 continue; \
1429}
1430
Willy Tarreau87b09662015-04-03 00:22:06 +02001431/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001432 * then puts it back to the wait queue in a clean state, or cleans up its
1433 * resources if it must be deleted. Returns in <next> the date the task wants
1434 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1435 * nothing too many times, the request and response buffers flags are monitored
1436 * and each function is called only if at least another function has changed at
1437 * least one flag it is interested in.
1438 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001439struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001440{
Willy Tarreau827aee92011-03-10 16:55:02 +01001441 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001442 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001443 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001444 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001445 unsigned int rq_prod_last, rq_cons_last;
1446 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001447 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001448 struct channel *req, *res;
1449 struct stream_interface *si_f, *si_b;
1450
1451 req = &s->req;
1452 res = &s->res;
1453
1454 si_f = &s->si[0];
1455 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001456
1457 //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 +01001458 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001459
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001460 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001461 if (s->txn)
1462 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001463
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001464 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001465 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1466 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001467
1468 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001469 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1470 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001471
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001472 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001473 si_f->flags |= SI_FL_DONT_WAKE;
1474 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001475
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001476 /* 1a: Check for low level timeouts if needed. We just set a flag on
1477 * stream interfaces when their timeouts have expired.
1478 */
1479 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001480 stream_int_check_timeouts(si_f);
1481 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001482
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001483 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001484 * for future reads or writes. Note: this will also concern upper layers
1485 * but we do not touch any other flag. We must be careful and correctly
1486 * detect state changes when calling them.
1487 */
1488
Willy Tarreau8f128b42014-11-28 15:07:47 +01001489 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001490
Willy Tarreau8f128b42014-11-28 15:07:47 +01001491 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1492 si_b->flags |= SI_FL_NOLINGER;
1493 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001494 }
1495
Willy Tarreau8f128b42014-11-28 15:07:47 +01001496 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1497 if (si_f->flags & SI_FL_NOHALF)
1498 si_f->flags |= SI_FL_NOLINGER;
1499 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001500 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001501
Willy Tarreau8f128b42014-11-28 15:07:47 +01001502 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001503
Willy Tarreau8f128b42014-11-28 15:07:47 +01001504 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1505 si_f->flags |= SI_FL_NOLINGER;
1506 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001507 }
1508
Willy Tarreau8f128b42014-11-28 15:07:47 +01001509 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1510 if (si_b->flags & SI_FL_NOHALF)
1511 si_b->flags |= SI_FL_NOLINGER;
1512 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001513 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001514
1515 /* Once in a while we're woken up because the task expires. But
1516 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001517 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001518 * timeout needs to be refreshed.
1519 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001520 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001521 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1522 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001523 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001524 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1525 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001526 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001527
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001528 /* below we may emit error messages so we have to ensure that we have
1529 * our buffers properly allocated.
1530 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001531 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001532 /* No buffer available, we've been subscribed to the list of
1533 * buffer waiters, let's wait for our turn.
1534 */
1535 goto update_exp_and_leave;
1536 }
1537
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001538 /* 1b: check for low-level errors reported at the stream interface.
1539 * First we check if it's a retryable error (in which case we don't
1540 * want to tell the buffer). Otherwise we report the error one level
1541 * upper by setting flags into the buffers. Note that the side towards
1542 * the client cannot have connect (hence retryable) errors. Also, the
1543 * connection setup code must be able to deal with any type of abort.
1544 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001545 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001546 if (unlikely(si_f->flags & SI_FL_ERR)) {
1547 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1548 si_shutr(si_f);
1549 si_shutw(si_f);
1550 stream_int_report_error(si_f);
1551 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001552 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001553 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001554 if (srv)
1555 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001556 if (!(s->flags & SF_ERR_MASK))
1557 s->flags |= SF_ERR_CLICL;
1558 if (!(s->flags & SF_FINST_MASK))
1559 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001560 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001561 }
1562 }
1563
Willy Tarreau8f128b42014-11-28 15:07:47 +01001564 if (unlikely(si_b->flags & SI_FL_ERR)) {
1565 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1566 si_shutr(si_b);
1567 si_shutw(si_b);
1568 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001569 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001570 if (srv)
1571 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001572 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001573 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001574 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001575 if (srv)
1576 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001577 if (!(s->flags & SF_ERR_MASK))
1578 s->flags |= SF_ERR_SRVCL;
1579 if (!(s->flags & SF_FINST_MASK))
1580 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001581 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001582 }
1583 /* note: maybe we should process connection errors here ? */
1584 }
1585
Willy Tarreau8f128b42014-11-28 15:07:47 +01001586 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001587 /* we were trying to establish a connection on the server side,
1588 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1589 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001590 if (unlikely(!sess_update_st_con_tcp(s)))
1591 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001592 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001593 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001594
1595 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1596 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1597 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1598 */
1599 }
1600
Willy Tarreau8f128b42014-11-28 15:07:47 +01001601 rq_prod_last = si_f->state;
1602 rq_cons_last = si_b->state;
1603 rp_cons_last = si_f->state;
1604 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001605
1606 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001607 /* Check for connection closure */
1608
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001609 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001610 "[%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 +01001611 now_ms, __FUNCTION__, __LINE__,
1612 t,
1613 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001614 req, res,
1615 req->rex, res->wex,
1616 req->flags, res->flags,
1617 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1618 si_f->err_type, si_b->err_type,
1619 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001620
1621 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001622 if (unlikely(si_f->state == SI_ST_DIS))
1623 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001624
1625 /* When a server-side connection is released, we have to count it and
1626 * check for pending connections on this server.
1627 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001628 if (unlikely(si_b->state == SI_ST_DIS)) {
1629 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001630 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001631 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001632 if (s->flags & SF_CURR_SESS) {
1633 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001634 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001635 }
1636 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001637 if (may_dequeue_tasks(srv, s->be))
1638 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001639 }
1640 }
1641
1642 /*
1643 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1644 * at this point.
1645 */
1646
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001647 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001648 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001649 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1650 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1651 si_f->state != rq_prod_last ||
1652 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001653 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001654 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001655
Willy Tarreau8f128b42014-11-28 15:07:47 +01001656 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001657 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001658 unsigned int ana_list;
1659 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001660
Willy Tarreau90deb182010-01-07 00:20:41 +01001661 /* it's up to the analysers to stop new connections,
1662 * disable reading or closing. Note: if an analyser
1663 * disables any of these bits, it is responsible for
1664 * enabling them again when it disables itself, so
1665 * that other analysers are called in similar conditions.
1666 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001667 channel_auto_read(req);
1668 channel_auto_connect(req);
1669 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001670
1671 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001672 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001673 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001674 * the list when not needed. Any analyser may return 0
1675 * to break out of the loop, either because of missing
1676 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001677 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001678 * analyser, and we may loop again if other analysers
1679 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001680 *
1681 * We build a list of analysers to run. We evaluate all
1682 * of these analysers in the order of the lower bit to
1683 * the higher bit. This ordering is very important.
1684 * An analyser will often add/remove other analysers,
1685 * including itself. Any changes to itself have no effect
1686 * on the loop. If it removes any other analysers, we
1687 * want those analysers not to be called anymore during
1688 * this loop. If it adds an analyser that is located
1689 * after itself, we want it to be scheduled for being
1690 * processed during the loop. If it adds an analyser
1691 * which is located before it, we want it to switch to
1692 * it immediately, even if it has already been called
1693 * once but removed since.
1694 *
1695 * In order to achieve this, we compare the analyser
1696 * list after the call with a copy of it before the
1697 * call. The work list is fed with analyser bits that
1698 * appeared during the call. Then we compare previous
1699 * work list with the new one, and check the bits that
1700 * appeared. If the lowest of these bits is lower than
1701 * the current bit, it means we have enabled a previous
1702 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001703 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001704
Willy Tarreau8f128b42014-11-28 15:07:47 +01001705 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001706 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001707 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001708
Willy Tarreaufb356202010-08-03 14:02:05 +02001709 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001710 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001711 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001712 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001713 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001714
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001715 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001716 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001717 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001718 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001719 }
1720
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001721 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001722 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001723 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001724 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001725 }
1726
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001727 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001728 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001729 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001730 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001731 }
1732
Willy Tarreaufb356202010-08-03 14:02:05 +02001733 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001734 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001735 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001736 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001737 }
1738
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001739 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001740 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001741 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001742 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001743 }
1744
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001745 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001746 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001747 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001748 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001749 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001750
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001751 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001752 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001753 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001754 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001755 }
1756
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001757 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001758 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001759 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001760 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001761 }
1762
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001763 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001764 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001765 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001766 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001767 }
Emeric Brun647caf12009-06-30 17:57:00 +02001768
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001769 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001770 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001771 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001772 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001773 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001774
Emeric Brun1d33b292010-01-04 15:47:17 +01001775 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001776 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001777 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001778 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001779 }
1780
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001781 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001782 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001783 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001784 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001785 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001786 break;
1787 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001788 }
Willy Tarreau84455332009-03-15 22:34:05 +01001789
Willy Tarreau8f128b42014-11-28 15:07:47 +01001790 rq_prod_last = si_f->state;
1791 rq_cons_last = si_b->state;
1792 req->flags &= ~CF_WAKE_ONCE;
1793 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001794
Willy Tarreau8f128b42014-11-28 15:07:47 +01001795 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001796 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001797 }
1798
Willy Tarreau576507f2010-01-07 00:09:04 +01001799 /* we'll monitor the request analysers while parsing the response,
1800 * because some response analysers may indirectly enable new request
1801 * analysers (eg: HTTP keep-alive).
1802 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001803 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001804
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001805 resync_response:
1806 /* Analyse response */
1807
Willy Tarreau8f128b42014-11-28 15:07:47 +01001808 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1809 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1810 si_f->state != rp_cons_last ||
1811 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001812 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001813 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001814
Willy Tarreau8f128b42014-11-28 15:07:47 +01001815 if ((res->flags & CF_MASK_ANALYSER) &&
1816 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001817 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1818 * for some free space in the response buffer, so we might need to call
1819 * it when something changes in the response buffer, but still we pass
1820 * it the request buffer. Note that the SI state might very well still
1821 * be zero due to us returning a flow of redirects!
1822 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001823 res->analysers &= ~AN_REQ_ALL;
1824 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001825 }
1826
Willy Tarreau8f128b42014-11-28 15:07:47 +01001827 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001828 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001829 unsigned int ana_list;
1830 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001831
Willy Tarreau90deb182010-01-07 00:20:41 +01001832 /* it's up to the analysers to stop disable reading or
1833 * closing. Note: if an analyser disables any of these
1834 * bits, it is responsible for enabling them again when
1835 * it disables itself, so that other analysers are called
1836 * in similar conditions.
1837 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001838 channel_auto_read(res);
1839 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001840
1841 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001842 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001843 * to MSB. The analysers must remove themselves from
1844 * the list when not needed. Any analyser may return 0
1845 * to break out of the loop, either because of missing
1846 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001847 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001848 * analyser, and we may loop again if other analysers
1849 * are added in the middle.
1850 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001851
Willy Tarreau8f128b42014-11-28 15:07:47 +01001852 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001853 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001854 /* Warning! ensure that analysers are always placed in ascending order! */
1855
Emeric Brun97679e72010-09-23 17:56:44 +02001856 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001857 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001858 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001859 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001860 }
1861
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001862 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001863 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001864 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001865 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001866 }
1867
Emeric Brun1d33b292010-01-04 15:47:17 +01001868 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001869 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001870 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001871 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001872 }
1873
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001874 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001875 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001876 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001877 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001878 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001879
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001880 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001881 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001883 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001884 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001885 break;
1886 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001887 }
1888
Willy Tarreau8f128b42014-11-28 15:07:47 +01001889 rp_cons_last = si_f->state;
1890 rp_prod_last = si_b->state;
1891 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001892
Willy Tarreau8f128b42014-11-28 15:07:47 +01001893 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001894 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001895 }
1896
Willy Tarreau576507f2010-01-07 00:09:04 +01001897 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001898 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001899 goto resync_request;
1900
Willy Tarreau8f128b42014-11-28 15:07:47 +01001901 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001902 goto resync_request;
1903
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001904 /* FIXME: here we should call protocol handlers which rely on
1905 * both buffers.
1906 */
1907
1908
1909 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001910 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001911 * we're just in a data phase here since it means we have not
1912 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001913 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001914 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001915 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001916 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001917 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001918 req->analysers = 0;
1919 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001920 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001921 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001922 if (srv)
1923 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001924 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001925 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001926 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001927 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001928 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001929 if (srv)
1930 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001931 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001932 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001933 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001934 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001935 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001936 if (srv)
1937 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001938 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001939 }
1940 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001941 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001942 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001943 if (srv)
1944 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001945 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001946 }
Willy Tarreau84455332009-03-15 22:34:05 +01001947 sess_set_term_flags(s);
1948 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001949 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001950 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001951 res->analysers = 0;
1952 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001953 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001954 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001955 if (srv)
1956 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001957 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001958 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001959 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001960 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001961 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001962 if (srv)
1963 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001964 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001965 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001966 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001967 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001968 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001969 if (srv)
1970 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001971 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001972 }
1973 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001974 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001975 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001976 if (srv)
1977 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001978 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001979 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001980 sess_set_term_flags(s);
1981 }
Willy Tarreau84455332009-03-15 22:34:05 +01001982 }
1983
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001984 /*
1985 * Here we take care of forwarding unhandled data. This also includes
1986 * connection establishments and shutdown requests.
1987 */
1988
1989
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001990 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001991 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001992 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001993 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001994 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001995 if (unlikely(!req->analysers &&
1996 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
1997 (si_f->state >= SI_ST_EST) &&
1998 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01001999 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002000 * attached to it. If any data are left in, we'll permit them to
2001 * move.
2002 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002003 channel_auto_read(req);
2004 channel_auto_connect(req);
2005 channel_auto_close(req);
2006 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002007
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002008 /* We'll let data flow between the producer (if still connected)
2009 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002010 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002011 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2012 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002013 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002014
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002015 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002016 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2017 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002018 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002019 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2020 (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 +01002021 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002022 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2023 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002024 (req->flags & CF_STREAMER_FAST)))) {
2025 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002026 }
2027
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002028 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002029 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002030
2031 /*
2032 * Now forward all shutdown requests between both sides of the buffer
2033 */
2034
Willy Tarreau520d95e2009-09-19 21:04:57 +02002035 /* first, let's check if the request buffer needs to shutdown(write), which may
2036 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002037 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2038 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002039 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002040 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002041 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002042 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002043 if (tick_isset(sess->fe->timeout.clientfin)) {
2044 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002045 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002046 }
2047 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002048
2049 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002050 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2051 channel_is_empty(req))) {
2052 if (req->flags & CF_READ_ERROR)
2053 si_b->flags |= SI_FL_NOLINGER;
2054 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002055 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002056 res->rto = s->be->timeout.serverfin;
2057 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002058 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002059 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002060
2061 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002062 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2063 !req->analysers))
2064 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002065
2066 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002067 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2068 if (si_f->flags & SI_FL_NOHALF)
2069 si_f->flags |= SI_FL_NOLINGER;
2070 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002071 if (tick_isset(sess->fe->timeout.clientfin)) {
2072 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002073 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002074 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002075 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002076
Willy Tarreau520d95e2009-09-19 21:04:57 +02002077 /* it's possible that an upper layer has requested a connection setup or abort.
2078 * There are 2 situations where we decide to establish a new connection :
2079 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002080 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002081 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002082 if (si_b->state == SI_ST_INI) {
2083 if (!(req->flags & CF_SHUTW)) {
2084 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002085 /* If we have an appctx, there is no connect method, so we
2086 * immediately switch to the connected state, otherwise we
2087 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002088 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002089 si_b->state = SI_ST_REQ; /* new connection requested */
2090 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002091 }
Willy Tarreau73201222009-08-16 18:27:24 +02002092 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002093 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002094 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2095 channel_shutw_now(req); /* fix buffer flags upon abort */
2096 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002097 }
Willy Tarreau92795622009-03-06 12:51:23 +01002098 }
2099
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002100
2101 /* we may have a pending connection request, or a connection waiting
2102 * for completion.
2103 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002104 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002105 do {
2106 /* nb: step 1 might switch from QUE to ASS, but we first want
2107 * to give a chance to step 2 to perform a redirect if needed.
2108 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002109 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002110 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002111 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002112 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002113
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002114 /* applets directly go to the ESTABLISHED state. Similarly,
2115 * servers experience the same fate when their connection
2116 * is reused.
2117 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002118 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002119 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002120
Willy Tarreaub44c8732013-12-31 23:16:50 +01002121 /* Now we can add the server name to a header (if requested) */
2122 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002123 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002124 (s->be->server_id_hdr_name != NULL) &&
2125 (s->be->mode == PR_MODE_HTTP) &&
2126 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002127 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002128 }
2129
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002130 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002131 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002132 http_perform_server_redirect(s, si_b);
2133 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002134 }
2135
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002136 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002137 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002138 goto resync_stream_interface;
2139
Willy Tarreau815a9b22010-07-27 17:15:12 +02002140 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002141 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002142 goto resync_request;
2143
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002144 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002145
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002146 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002147 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002148 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002149 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002150 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002151 if (unlikely(!res->analysers &&
2152 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2153 (si_b->state >= SI_ST_EST) &&
2154 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002155 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002156 * attached to it. If any data are left in, we'll permit them to
2157 * move.
2158 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002159 channel_auto_read(res);
2160 channel_auto_close(res);
2161 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002162
2163 /* We'll let data flow between the producer (if still connected)
2164 * to the consumer.
2165 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002166 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2167 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002168
2169 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002170 * tunnel timeout set, use it now. Note that we must respect
2171 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002172 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002173 if (!req->analysers && s->be->timeout.tunnel) {
2174 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002175 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002176
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002177 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2178 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002179 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2180 res->rto = s->be->timeout.serverfin;
2181 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2182 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002183 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2184 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002185
Willy Tarreau8f128b42014-11-28 15:07:47 +01002186 req->rex = tick_add(now_ms, req->rto);
2187 req->wex = tick_add(now_ms, req->wto);
2188 res->rex = tick_add(now_ms, res->rto);
2189 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002190 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002191 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002192
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002193 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002194 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2195 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002196 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002197 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2198 (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 +01002199 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002200 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2201 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002202 (res->flags & CF_STREAMER_FAST)))) {
2203 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002204 }
2205
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002206 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002207 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002208
2209 /*
2210 * Now forward all shutdown requests between both sides of the buffer
2211 */
2212
2213 /*
2214 * FIXME: this is probably where we should produce error responses.
2215 */
2216
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002217 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002218 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002219 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002220 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002221 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002222 req->wto = s->be->timeout.serverfin;
2223 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002224 }
2225 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002226
2227 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002228 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2229 channel_is_empty(res))) {
2230 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002231 if (tick_isset(sess->fe->timeout.clientfin)) {
2232 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002233 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002234 }
2235 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002236
2237 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002238 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2239 !res->analysers)
2240 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002241
2242 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002243 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2244 if (si_b->flags & SI_FL_NOHALF)
2245 si_b->flags |= SI_FL_NOLINGER;
2246 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002247 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002248 req->wto = s->be->timeout.serverfin;
2249 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002250 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002251 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002252
Willy Tarreau8f128b42014-11-28 15:07:47 +01002253 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002254 goto resync_stream_interface;
2255
Willy Tarreau8f128b42014-11-28 15:07:47 +01002256 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002257 goto resync_request;
2258
Willy Tarreau8f128b42014-11-28 15:07:47 +01002259 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002260 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002261
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002262 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002263 si_f->flags &= ~SI_FL_DONT_WAKE;
2264 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002265
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002266 /* This is needed only when debugging is enabled, to indicate
2267 * client-side or server-side close. Please note that in the unlikely
2268 * event where both sides would close at once, the sequence is reported
2269 * on the server side first.
2270 */
2271 if (unlikely((global.mode & MODE_DEBUG) &&
2272 (!(global.mode & MODE_QUIET) ||
2273 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002274 if (si_b->state == SI_ST_CLO &&
2275 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002276 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002277 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002278 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2279 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002280 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002281 }
2282
Willy Tarreau8f128b42014-11-28 15:07:47 +01002283 if (si_f->state == SI_ST_CLO &&
2284 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002285 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002286 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002287 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2288 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002289 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002290 }
2291 }
2292
Willy Tarreau8f128b42014-11-28 15:07:47 +01002293 if (likely((si_f->state != SI_ST_CLO) ||
2294 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002295
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002296 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002297 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002298
Willy Tarreau8f128b42014-11-28 15:07:47 +01002299 if (si_f->state == SI_ST_EST && obj_type(si_f->end) != OBJ_TYPE_APPCTX)
2300 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002301
Willy Tarreau8f128b42014-11-28 15:07:47 +01002302 if (si_b->state == SI_ST_EST && obj_type(si_b->end) != OBJ_TYPE_APPCTX)
2303 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002304
Willy Tarreau8f128b42014-11-28 15:07:47 +01002305 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2306 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2307 si_f->prev_state = si_f->state;
2308 si_b->prev_state = si_b->state;
2309 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2310 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002311
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002312 /* Trick: if a request is being waiting for the server to respond,
2313 * and if we know the server can timeout, we don't want the timeout
2314 * to expire on the client side first, but we're still interested
2315 * in passing data from the client to the server (eg: POST). Thus,
2316 * we can cancel the client's request timeout if the server's
2317 * request timeout is set and the server has not yet sent a response.
2318 */
2319
Willy Tarreau8f128b42014-11-28 15:07:47 +01002320 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2321 (tick_isset(req->wex) || tick_isset(res->rex))) {
2322 req->flags |= CF_READ_NOEXP;
2323 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002324 }
2325
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002326 /* When any of the stream interfaces is attached to an applet,
2327 * we have to call it here. Note that this one may wake the
2328 * task up again. If at least one applet was called, the current
2329 * task might have been woken up, in which case we don't want it
2330 * to be requeued to the wait queue but rather to the run queue
2331 * to run ASAP. The bitwise "or" in the condition ensures that
2332 * both functions are always called and that we wake up if at
2333 * least one did something.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002334 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002335 if ((si_applet_call(si_b) | si_applet_call(si_f)) != 0) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002336 if (task_in_rq(t)) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002337 t->expire = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02002338 stream_release_buffers(s);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002339 return t;
2340 }
2341 }
2342
Willy Tarreau798f4322012-11-08 14:49:17 +01002343 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002344 t->expire = tick_first(tick_first(req->rex, req->wex),
2345 tick_first(res->rex, res->wex));
2346 if (req->analysers)
2347 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002348
Willy Tarreau8f128b42014-11-28 15:07:47 +01002349 if (si_f->exp)
2350 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002351
Willy Tarreau8f128b42014-11-28 15:07:47 +01002352 if (si_b->exp)
2353 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002354
2355#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002356 fprintf(stderr,
2357 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2358 " 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 +01002359 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2360 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002361#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002362
2363#ifdef DEBUG_DEV
2364 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002365 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002366 ABORT_NOW();
2367#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002368 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002369 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002370 }
2371
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002372 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002373 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002374 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002375 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002376 if (sess->listener) {
2377 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002378 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002379 sess->listener->nbconn--;
2380 if (sess->listener->state == LI_FULL)
2381 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002382
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002383 /* Dequeues all of the listeners waiting for a resource */
2384 if (!LIST_ISEMPTY(&global_listener_queue))
2385 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002386
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002387 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2388 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2389 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002390 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002391
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002392 if (unlikely((global.mode & MODE_DEBUG) &&
2393 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002394 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002395 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002396 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2397 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002398 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002399 }
2400
2401 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002402 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002403
Willy Tarreaueee5b512015-04-03 23:46:31 +02002404 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002405 int n;
2406
Willy Tarreaueee5b512015-04-03 23:46:31 +02002407 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002408 if (n < 1 || n > 5)
2409 n = 0;
2410
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002411 if (sess->fe->mode == PR_MODE_HTTP) {
2412 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002413 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002414 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002415 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002416 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002417 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002418 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002419 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002420 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002421 s->be->be_counters.p.http.comp_rsp++;
2422 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002423 }
2424
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002425 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002426 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002427 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002428 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002429 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002430 }
2431
Willy Tarreau87b09662015-04-03 00:22:06 +02002432 /* update time stats for this stream */
2433 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002434
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002435 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002436 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002437 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002438 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002439 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002440}
2441
Willy Tarreau87b09662015-04-03 00:22:06 +02002442/* Update the stream's backend and server time stats */
2443void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002444{
2445 int t_request;
2446 int t_queue;
2447 int t_connect;
2448 int t_data;
2449 int t_close;
2450 struct server *srv;
2451
2452 t_request = 0;
2453 t_queue = s->logs.t_queue;
2454 t_connect = s->logs.t_connect;
2455 t_close = s->logs.t_close;
2456 t_data = s->logs.t_data;
2457
2458 if (s->be->mode != PR_MODE_HTTP)
2459 t_data = t_connect;
2460
2461 if (t_connect < 0 || t_data < 0)
2462 return;
2463
2464 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2465 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2466
2467 t_data -= t_connect;
2468 t_connect -= t_queue;
2469 t_queue -= t_request;
2470
2471 srv = objt_server(s->target);
2472 if (srv) {
2473 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2474 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2475 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2476 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2477 }
2478 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2479 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2480 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2481 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2482}
2483
Willy Tarreau7c669d72008-06-20 15:04:11 +02002484/*
2485 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002486 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002487 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002488 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002489 * server.
2490 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002491void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002492{
2493 if (sess->srv_conn == newsrv)
2494 return;
2495
2496 if (sess->srv_conn) {
2497 sess->srv_conn->served--;
2498 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2499 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002500 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002501 }
2502
2503 if (newsrv) {
2504 newsrv->served++;
2505 if (newsrv->proxy->lbprm.server_take_conn)
2506 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002507 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002508 }
2509}
2510
Willy Tarreau84455332009-03-15 22:34:05 +01002511/* Handle server-side errors for default protocols. It is called whenever a a
2512 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002513 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002514 * them. It's installed as ->srv_error for the server-side stream_interface.
2515 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002516void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002517{
2518 int err_type = si->err_type;
2519 int err = 0, fin = 0;
2520
2521 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002522 err = SF_ERR_CLICL;
2523 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002524 }
2525 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002526 err = SF_ERR_CLICL;
2527 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002528 }
2529 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002530 err = SF_ERR_SRVTO;
2531 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002532 }
2533 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002534 err = SF_ERR_SRVCL;
2535 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002536 }
2537 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002538 err = SF_ERR_SRVTO;
2539 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002540 }
2541 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002542 err = SF_ERR_SRVCL;
2543 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002544 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002545 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002546 err = SF_ERR_RESOURCE;
2547 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002548 }
Willy Tarreau84455332009-03-15 22:34:05 +01002549 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002550 err = SF_ERR_INTERNAL;
2551 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002552 }
2553
Willy Tarreaue7dff022015-04-03 01:14:29 +02002554 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002555 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002556 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002557 s->flags |= fin;
2558}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002559
Willy Tarreaue7dff022015-04-03 01:14:29 +02002560/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002561void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002562{
Willy Tarreau87b09662015-04-03 00:22:06 +02002563 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002564 return;
2565
Willy Tarreau87b09662015-04-03 00:22:06 +02002566 channel_shutw_now(&stream->req);
2567 channel_shutr_now(&stream->res);
2568 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002569 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002570 stream->flags |= why;
2571 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002572}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002573
Willy Tarreau8b22a712010-06-18 17:46:06 +02002574/************************************************************************/
2575/* All supported ACL keywords must be declared here. */
2576/************************************************************************/
2577
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002578/* Returns a pointer to a stkctr depending on the fetch keyword name.
2579 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002580 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002581 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002582 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002583 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002584 * NULL may be returned if the designated stkctr is not tracked. For
2585 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2586 * passed. When present, the currently tracked key is then looked up
2587 * in the specified table instead of the current table. The purpose is
2588 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002589 * multiple tables). <strm> is allowed to be NULL, in which case only
2590 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002591 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002592struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002593smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002594{
2595 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002596 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002597 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002598 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002599 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002600
Willy Tarreau0f791d42013-07-23 19:56:43 +02002601 if (num == '_' - '0') {
2602 /* sc_* variant, args[0] = ctr# (mandatory) */
2603 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002604 if (num >= MAX_SESS_STKCTR)
2605 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002606 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002607 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002608 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002609 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002610
2611 if (!conn)
2612 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002613
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002614 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002615 if (!key)
2616 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002617
Willy Tarreaua65536c2013-07-22 22:40:11 +02002618 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002619 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002620 return &stkctr;
2621 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002622
2623 /* Here, <num> contains the counter number from 0 to 9 for
2624 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002625 * args[arg] is the first optional argument. We first lookup the
2626 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002627 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002628
2629 stkptr = &strm->stkctr[num];
2630 if (!strm || !stkctr_entry(stkptr)) {
2631 stkptr = &sess->stkctr[num];
2632 if (!stkctr_entry(stkptr))
2633 return NULL;
2634 }
2635
2636 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002637 if (!stksess)
2638 return NULL;
2639
Willy Tarreau0f791d42013-07-23 19:56:43 +02002640 if (unlikely(args[arg].type == ARGT_TAB)) {
2641 /* an alternate table was specified, let's look up the same key there */
2642 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002643 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002644 return &stkctr;
2645 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002646 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002647}
2648
Willy Tarreau87b09662015-04-03 00:22:06 +02002649/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002650 * currently being tracked or not.
2651 * Supports being called as "sc[0-9]_tracked" only.
2652 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002653static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002654smp_fetch_sc_tracked(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02002655 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002656{
2657 smp->flags = SMP_F_VOL_TEST;
2658 smp->type = SMP_T_BOOL;
Willy Tarreau192252e2015-04-04 01:47:55 +02002659 smp->data.uint = !!smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002660 return 1;
2661}
2662
Willy Tarreau87b09662015-04-03 00:22:06 +02002663/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002664 * frontend counters or from the src.
2665 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2666 * zero is returned if the key is new.
2667 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002668static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002669smp_fetch_sc_get_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002670 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002671{
Willy Tarreau192252e2015-04-04 01:47:55 +02002672 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002673
2674 if (!stkctr)
2675 return 0;
2676
Willy Tarreau37406352012-04-23 16:16:37 +02002677 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002678 smp->type = SMP_T_UINT;
2679 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002680
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002681 if (stkctr_entry(stkctr) != NULL) {
2682 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002683 if (!ptr)
2684 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002685 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002686 }
2687 return 1;
2688}
2689
Willy Tarreau87b09662015-04-03 00:22:06 +02002690/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002691 * tracked frontend counters or from the src.
2692 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2693 * Value zero is returned if the key is new.
2694 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002695static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002696smp_fetch_sc_gpc0_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002697 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002698{
Willy Tarreau192252e2015-04-04 01:47:55 +02002699 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002700
2701 if (!stkctr)
2702 return 0;
2703
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002704 smp->flags = SMP_F_VOL_TEST;
2705 smp->type = SMP_T_UINT;
2706 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002707 if (stkctr_entry(stkctr) != NULL) {
2708 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002709 if (!ptr)
2710 return 0; /* parameter not stored */
2711 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002712 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002713 }
2714 return 1;
2715}
2716
Willy Tarreau87b09662015-04-03 00:22:06 +02002717/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002718 * frontend counters and return it into temp integer.
2719 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002720 */
2721static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002722smp_fetch_sc_inc_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002723 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002724{
Willy Tarreau192252e2015-04-04 01:47:55 +02002725 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002726
2727 if (!stkctr)
2728 return 0;
2729
Willy Tarreau37406352012-04-23 16:16:37 +02002730 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002731 smp->type = SMP_T_UINT;
2732 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002733 if (stkctr_entry(stkctr) != NULL) {
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002734 void *ptr;
2735
2736 /* First, update gpc0_rate if it's tracked. Second, update its
2737 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2738 */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002739 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002740 if (ptr) {
2741 update_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002742 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002743 smp->data.uint = (&stktable_data_cast(ptr, gpc0_rate))->curr_ctr;
2744 }
2745
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002746 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002747 if (ptr)
2748 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
2749
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002750 }
2751 return 1;
2752}
2753
Willy Tarreau87b09662015-04-03 00:22:06 +02002754/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002755 * frontend counters and return its previous value into temp integer.
2756 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002757 */
2758static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002759smp_fetch_sc_clr_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002760 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002761{
Willy Tarreau192252e2015-04-04 01:47:55 +02002762 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002763
2764 if (!stkctr)
2765 return 0;
2766
Willy Tarreau37406352012-04-23 16:16:37 +02002767 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002768 smp->type = SMP_T_UINT;
2769 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002770 if (stkctr_entry(stkctr) != NULL) {
2771 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002772 if (!ptr)
2773 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002774 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002775 stktable_data_cast(ptr, gpc0) = 0;
2776 }
2777 return 1;
2778}
2779
Willy Tarreau87b09662015-04-03 00:22:06 +02002780/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002781 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2782 * "src_conn_cnt" only.
2783 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002784static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002785smp_fetch_sc_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002786 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002787{
Willy Tarreau192252e2015-04-04 01:47:55 +02002788 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002789
2790 if (!stkctr)
2791 return 0;
2792
Willy Tarreau37406352012-04-23 16:16:37 +02002793 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002794 smp->type = SMP_T_UINT;
2795 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002796 if (stkctr_entry(stkctr) != NULL) {
2797 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002798 if (!ptr)
2799 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002800 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002801 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002802 return 1;
2803}
2804
Willy Tarreau87b09662015-04-03 00:22:06 +02002805/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002806 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2807 * only.
2808 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002809static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002810smp_fetch_sc_conn_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002811 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002812{
Willy Tarreau192252e2015-04-04 01:47:55 +02002813 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002814
2815 if (!stkctr)
2816 return 0;
2817
Willy Tarreau37406352012-04-23 16:16:37 +02002818 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002819 smp->type = SMP_T_UINT;
2820 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002821 if (stkctr_entry(stkctr) != NULL) {
2822 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002823 if (!ptr)
2824 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002825 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002826 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002827 }
2828 return 1;
2829}
2830
Willy Tarreau87b09662015-04-03 00:22:06 +02002831/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002832 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002833 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002834 */
2835static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002836smp_fetch_src_updt_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002837 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002838{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002839 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002840 struct stksess *ts;
2841 struct stktable_key *key;
2842 void *ptr;
2843
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002844 if (!conn)
2845 return 0;
2846
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002847 key = addr_to_stktable_key(&conn->addr.from, px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002848 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002849 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002850
Willy Tarreau24e32d82012-04-23 23:55:44 +02002851 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002852
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002853 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2854 /* entry does not exist and could not be created */
2855 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002856
2857 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2858 if (!ptr)
2859 return 0; /* parameter not stored in this table */
2860
Willy Tarreauf853c462012-04-23 18:53:56 +02002861 smp->type = SMP_T_UINT;
2862 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002863 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002864 return 1;
2865}
2866
Willy Tarreau87b09662015-04-03 00:22:06 +02002867/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002868 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2869 * "src_conn_cur" only.
2870 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002871static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002872smp_fetch_sc_conn_cur(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002873 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002874{
Willy Tarreau192252e2015-04-04 01:47:55 +02002875 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002876
2877 if (!stkctr)
2878 return 0;
2879
Willy Tarreau37406352012-04-23 16:16:37 +02002880 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002881 smp->type = SMP_T_UINT;
2882 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002883 if (stkctr_entry(stkctr) != NULL) {
2884 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002885 if (!ptr)
2886 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002887 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002888 }
2889 return 1;
2890}
2891
Willy Tarreau87b09662015-04-03 00:22:06 +02002892/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02002893 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2894 * "src_sess_cnt" only.
2895 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002896static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002897smp_fetch_sc_sess_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002898 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002899{
Willy Tarreau192252e2015-04-04 01:47:55 +02002900 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02002901
2902 if (!stkctr)
2903 return 0;
2904
Willy Tarreau37406352012-04-23 16:16:37 +02002905 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002906 smp->type = SMP_T_UINT;
2907 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002908 if (stkctr_entry(stkctr) != NULL) {
2909 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002910 if (!ptr)
2911 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002912 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002913 }
2914 return 1;
2915}
2916
Willy Tarreau87b09662015-04-03 00:22:06 +02002917/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002918 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2919 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002920static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002921smp_fetch_sc_sess_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002922 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002923{
Willy Tarreau192252e2015-04-04 01:47:55 +02002924 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002925
2926 if (!stkctr)
2927 return 0;
2928
Willy Tarreau37406352012-04-23 16:16:37 +02002929 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002930 smp->type = SMP_T_UINT;
2931 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002932 if (stkctr_entry(stkctr) != NULL) {
2933 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002934 if (!ptr)
2935 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002936 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002937 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002938 }
2939 return 1;
2940}
2941
Willy Tarreau87b09662015-04-03 00:22:06 +02002942/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02002943 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2944 * "src_http_req_cnt" only.
2945 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002946static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002947smp_fetch_sc_http_req_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002948 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002949{
Willy Tarreau192252e2015-04-04 01:47:55 +02002950 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02002951
2952 if (!stkctr)
2953 return 0;
2954
Willy Tarreau37406352012-04-23 16:16:37 +02002955 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002956 smp->type = SMP_T_UINT;
2957 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002958 if (stkctr_entry(stkctr) != NULL) {
2959 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002960 if (!ptr)
2961 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002962 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002963 }
2964 return 1;
2965}
2966
Willy Tarreau87b09662015-04-03 00:22:06 +02002967/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02002968 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2969 * "src_http_req_rate" only.
2970 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002971static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002972smp_fetch_sc_http_req_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002973 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002974{
Willy Tarreau192252e2015-04-04 01:47:55 +02002975 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02002976
2977 if (!stkctr)
2978 return 0;
2979
Willy Tarreau37406352012-04-23 16:16:37 +02002980 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002981 smp->type = SMP_T_UINT;
2982 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002983 if (stkctr_entry(stkctr) != NULL) {
2984 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002985 if (!ptr)
2986 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002987 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02002988 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002989 }
2990 return 1;
2991}
2992
Willy Tarreau87b09662015-04-03 00:22:06 +02002993/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02002994 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2995 * "src_http_err_cnt" only.
2996 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002997static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002998smp_fetch_sc_http_err_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002999 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003000{
Willy Tarreau192252e2015-04-04 01:47:55 +02003001 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003002
3003 if (!stkctr)
3004 return 0;
3005
Willy Tarreau37406352012-04-23 16:16:37 +02003006 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003007 smp->type = SMP_T_UINT;
3008 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003009 if (stkctr_entry(stkctr) != NULL) {
3010 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003011 if (!ptr)
3012 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003013 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003014 }
3015 return 1;
3016}
3017
Willy Tarreau87b09662015-04-03 00:22:06 +02003018/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003019 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3020 * "src_http_err_rate" only.
3021 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003022static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003023smp_fetch_sc_http_err_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003024 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003025{
Willy Tarreau192252e2015-04-04 01:47:55 +02003026 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003027
3028 if (!stkctr)
3029 return 0;
3030
Willy Tarreau37406352012-04-23 16:16:37 +02003031 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003032 smp->type = SMP_T_UINT;
3033 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003034 if (stkctr_entry(stkctr) != NULL) {
3035 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003036 if (!ptr)
3037 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003038 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003039 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003040 }
3041 return 1;
3042}
3043
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003044/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003045 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003046 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3047 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003048static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003049smp_fetch_sc_kbytes_in(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003050 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003051{
Willy Tarreau192252e2015-04-04 01:47:55 +02003052 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003053
3054 if (!stkctr)
3055 return 0;
3056
Willy Tarreau37406352012-04-23 16:16:37 +02003057 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003058 smp->type = SMP_T_UINT;
3059 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003060 if (stkctr_entry(stkctr) != NULL) {
3061 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003062 if (!ptr)
3063 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003064 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003065 }
3066 return 1;
3067}
3068
Willy Tarreau613fe992013-07-23 17:39:19 +02003069/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003070 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003071 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003072 */
3073static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003074smp_fetch_sc_bytes_in_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003075 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003076{
Willy Tarreau192252e2015-04-04 01:47:55 +02003077 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003078
3079 if (!stkctr)
3080 return 0;
3081
Willy Tarreau37406352012-04-23 16:16:37 +02003082 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003083 smp->type = SMP_T_UINT;
3084 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003085 if (stkctr_entry(stkctr) != NULL) {
3086 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003087 if (!ptr)
3088 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003089 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003090 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003091 }
3092 return 1;
3093}
3094
Willy Tarreau53aea102013-07-23 17:39:02 +02003095/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003096 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003097 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3098 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003099static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003100smp_fetch_sc_kbytes_out(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02003101 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003102{
Willy Tarreau192252e2015-04-04 01:47:55 +02003103 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003104
3105 if (!stkctr)
3106 return 0;
3107
Willy Tarreau37406352012-04-23 16:16:37 +02003108 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003109 smp->type = SMP_T_UINT;
3110 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003111 if (stkctr_entry(stkctr) != NULL) {
3112 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003113 if (!ptr)
3114 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003115 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003116 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003117 return 1;
3118}
3119
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003120/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003121 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003122 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003123 */
3124static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003125smp_fetch_sc_bytes_out_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003126 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003127{
Willy Tarreau192252e2015-04-04 01:47:55 +02003128 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003129
3130 if (!stkctr)
3131 return 0;
3132
Willy Tarreau37406352012-04-23 16:16:37 +02003133 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003134 smp->type = SMP_T_UINT;
3135 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003136 if (stkctr_entry(stkctr) != NULL) {
3137 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003138 if (!ptr)
3139 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003140 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003141 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003142 }
3143 return 1;
3144}
3145
Willy Tarreau87b09662015-04-03 00:22:06 +02003146/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003147 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3148 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003149static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003150smp_fetch_sc_trackers(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003151 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003152{
Willy Tarreau192252e2015-04-04 01:47:55 +02003153 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003154
Willy Tarreau563eef42013-07-23 18:32:02 +02003155 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003156 return 0;
3157
Willy Tarreau563eef42013-07-23 18:32:02 +02003158 smp->flags = SMP_F_VOL_TEST;
3159 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003160 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003161 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003162}
3163
Willy Tarreau34db1082012-04-19 17:16:54 +02003164/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003165 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003166 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003167static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003168smp_fetch_table_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003169 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003170{
Willy Tarreau37406352012-04-23 16:16:37 +02003171 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003172 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003173 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003174 return 1;
3175}
3176
Willy Tarreau34db1082012-04-19 17:16:54 +02003177/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003178 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003179 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003180static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003181smp_fetch_table_avl(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003182 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003183{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003184 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003185 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003186 smp->type = SMP_T_UINT;
3187 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003188 return 1;
3189}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003190
Willy Tarreau61612d42012-04-19 18:42:05 +02003191/* Note: must not be declared <const> as its list will be overwritten.
3192 * Please take care of keeping this list alphabetically sorted.
3193 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003194static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003195 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003196}};
3197
Willy Tarreau281c7992013-01-08 01:23:27 +01003198/* Note: must not be declared <const> as its list will be overwritten.
3199 * Please take care of keeping this list alphabetically sorted.
3200 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003201static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003202 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3203 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3204 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3205 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3206 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3207 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3208 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3209 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3210 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3211 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3212 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3213 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3214 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3215 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3216 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3217 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3218 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3219 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3220 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3221 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3222 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3223 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3224 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3225 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3226 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3227 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3228 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3229 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3230 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3231 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3232 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3233 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3234 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3235 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3236 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3237 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3238 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3239 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3240 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3241 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3242 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3243 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3244 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3245 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3246 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3247 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3248 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3249 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3250 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3251 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3252 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3253 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3254 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3255 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3256 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3257 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3258 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3259 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3260 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3261 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3262 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3263 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3264 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3265 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3266 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3267 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3268 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3269 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3270 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3271 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3272 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3273 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3274 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3275 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3276 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3277 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3278 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3279 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3280 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3281 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3282 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3283 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3284 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3285 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3286 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3287 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3288 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3289 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3290 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3291 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3292 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3293 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3294 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3295 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3296 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3297 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003298 { /* END */ },
3299}};
3300
Willy Tarreau8b22a712010-06-18 17:46:06 +02003301__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003302static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003303{
Willy Tarreau281c7992013-01-08 01:23:27 +01003304 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003305 acl_register_keywords(&acl_kws);
3306}
3307
Willy Tarreaubaaee002006-06-26 02:48:02 +02003308/*
3309 * Local variables:
3310 * c-indent-level: 8
3311 * c-basic-offset: 8
3312 * End:
3313 */