blob: 0d9a202063e2dce1e24c0c670a3d7acf9f785ded [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002 * Stream management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreaud28c3532012-04-19 19:28:33 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <stdlib.h>
Willy Tarreau81f9aa32010-06-01 17:45:26 +020014#include <unistd.h>
15#include <fcntl.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020018#include <common/buffer.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020019#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010023#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020025#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020026#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010027#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020028#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010029#include <proto/checks.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020030#include <proto/connection.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020031#include <proto/dumpstats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020032#include <proto/fd.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020033#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020034#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010035#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010036#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020037#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020038#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020039#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020040#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020041#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010042#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010043#include <proto/proto_http.h>
44#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020045#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020048#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010049#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010050#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010051#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052
Willy Tarreau87b09662015-04-03 00:22:06 +020053struct pool_head *pool2_stream;
54struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
Willy Tarreau87b09662015-04-03 00:22:06 +020056/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010057struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
58
Willy Tarreau9903f0e2015-04-04 18:50:31 +020059/* This function is called from the session handler which detects the end of
Willy Tarreau87b09662015-04-03 00:22:06 +020060 * handshake, in order to complete initialization of a valid stream. It must
Willy Tarreau9903f0e2015-04-04 18:50:31 +020061 * be called with an embryonic session. It returns a positive value upon
Willy Tarreau2542b532012-08-31 16:01:23 +020062 * success, 0 if the connection can be ignored, or a negative value upon
63 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreaub4f98092014-05-08 21:06:11 +020064 * The client-side end point is assumed to be a connection, whose pointer is
Willy Tarreau1f52bb22015-04-04 14:28:46 +020065 * taken from sess->origin which is assumed to be valid.
Willy Tarreau2542b532012-08-31 16:01:23 +020066 */
Willy Tarreau9903f0e2015-04-04 18:50:31 +020067int stream_accept_session(struct session *sess, struct task *t)
Willy Tarreau2542b532012-08-31 16:01:23 +020068{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020069 struct stream *s;
Willy Tarreaufb0afa72015-04-03 14:46:27 +020070 struct listener *l = sess->listener;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020071 struct proxy *p = sess->fe;
Willy Tarreau1f52bb22015-04-04 14:28:46 +020072 struct connection *conn = __objt_conn(sess->origin);
Willy Tarreau2542b532012-08-31 16:01:23 +020073 int ret;
Willy Tarreau20d46a52012-12-09 15:55:40 +010074 int i;
Willy Tarreau2542b532012-08-31 16:01:23 +020075
76 ret = -1; /* assume unrecoverable error by default */
77
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020078 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
79 goto out_return;
80
81 /* minimum stream initialization required for an embryonic stream is
82 * fairly low. We need very little to execute L4 ACLs, then we need a
83 * task to make the client-side connection live on its own.
84 * - flags
85 * - stick-entry tracking
86 */
87 s->flags = 0;
88 s->logs.logwait = p->to_log;
89 s->logs.level = 0;
90
91 /* Initialise the current rule list pointer to NULL. We are sure that
92 * any rulelist match the NULL pointer.
93 */
94 s->current_rule_list = NULL;
95
96 memset(s->stkctr, 0, sizeof(s->stkctr));
97
98 s->sess = sess;
99 s->si[0].flags = SI_FL_NONE;
100 s->si[1].flags = SI_FL_ISBACK;
101
102 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
103 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
104 s->uniq_id = global.req_count++;
105
Willy Tarreau87b09662015-04-03 00:22:06 +0200106 /* OK, we're keeping the stream, so let's properly initialize the stream */
107 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200108 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100109 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200110
Willy Tarreaue7dff022015-04-03 01:14:29 +0200111 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200112 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200113
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200114 s->task = t;
Willy Tarreau2542b532012-08-31 16:01:23 +0200115 t->process = l->handler;
116 t->context = s;
117 t->expire = TICK_ETERNITY;
118
Willy Tarreau87b09662015-04-03 00:22:06 +0200119 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200120 * This changes later when switching rules are executed or
121 * when the default backend is assigned.
122 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200123 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200124 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100125 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200126 s->req_cap = NULL;
127 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200128
Willy Tarreau87b09662015-04-03 00:22:06 +0200129 /* Let's count a stream now */
Willy Tarreaub36b4242010-06-04 20:59:39 +0200130 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200131
Willy Tarreaub4c84932013-07-23 19:15:30 +0200132 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200133 void *ptr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200134 struct stkctr *stkctr = &sess->stkctr[i];
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200135
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200136 if (!stkctr_entry(stkctr))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100137 continue;
138
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200139 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200140 if (ptr)
141 stktable_data_cast(ptr, sess_cnt)++;
142
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200143 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200144 if (ptr)
145 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200146 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200147 }
148
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200149 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100150 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200151 si_set_state(&s->si[0], SI_ST_EST);
152
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200153 /* attach the incoming connection to the stream interface now. */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200154 si_attach_conn(&s->si[0], conn);
155
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200156 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200157 s->si[0].flags |= SI_FL_INDEP_STR;
158
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200159 /* pre-initialize the other side's stream interface to an INIT state. The
160 * callbacks will be initialized before attempting to connect.
161 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100162 si_reset(&s->si[1]);
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200163 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200164
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200165 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200166 s->si[1].flags |= SI_FL_INDEP_STR;
167
Willy Tarreau87b09662015-04-03 00:22:06 +0200168 stream_init_srv_conn(s);
Willy Tarreau10b688f2015-03-13 16:43:12 +0100169 s->target = l->default_target; /* used by peers and CLI */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200170 s->pend_pos = NULL;
171
172 /* init store persistence */
173 s->store_count = 0;
174
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100175 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100176 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200177
178 /* activate default analysers enabled for this listener */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100179 s->req.analysers = l->analysers;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200180
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100181 s->req.wto = TICK_ETERNITY;
182 s->req.rto = TICK_ETERNITY;
183 s->req.rex = TICK_ETERNITY;
184 s->req.wex = TICK_ETERNITY;
185 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200186
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100187 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100188 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100189 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200190
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200191 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100192 s->req.flags |= CF_NEVER_WAIT;
193 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200194 }
195
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100196 s->res.rto = TICK_ETERNITY;
197 s->res.wto = TICK_ETERNITY;
198 s->res.rex = TICK_ETERNITY;
199 s->res.wex = TICK_ETERNITY;
200 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200201
Willy Tarreaueee5b512015-04-03 23:46:31 +0200202 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100203
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100204 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100205
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200206 /* finish initialization of the accepted file descriptor */
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200207 conn_data_want_recv(conn);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200208
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100209 if (p->accept && (ret = p->accept(s)) <= 0) {
210 /* Either we had an unrecoverable error (<0) or work is
211 * finished (=0, eg: monitoring), in both situations,
212 * we can release everything and close.
213 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100214 goto out_free_strm;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200215 }
216
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200217 /* if logs require transport layer information, note it on the connection */
218 if (s->logs.logwait & LW_XPRT)
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200219 conn->flags |= CO_FL_XPRT_TRACKED;
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200220
Willy Tarreau2542b532012-08-31 16:01:23 +0200221 /* we want the connection handler to notify the stream interface about updates. */
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200222 conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200223
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200224 /* it is important not to call the wakeup function directly but to
225 * pass through task_wakeup(), because this one knows how to apply
226 * priorities to tasks.
227 */
228 task_wakeup(t, TASK_WOKEN_INIT);
229 return 1;
230
231 /* Error unrolling */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100232 out_free_strm:
Willy Tarreau3b246412014-11-25 17:10:33 +0100233 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200234 pool_free2(pool2_stream, s);
235 out_return:
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100236 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200237}
238
Willy Tarreaubaaee002006-06-26 02:48:02 +0200239/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200240 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200242static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200244 struct session *sess = strm_sess(s);
245 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100246 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200247 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200248 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100249
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250 if (s->pend_pos)
251 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100252
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100253 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200254 if (s->flags & SF_CURR_SESS) {
255 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100256 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100257 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100258 if (may_dequeue_tasks(objt_server(s->target), s->be))
259 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100260 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100261
Willy Tarreau7c669d72008-06-20 15:04:11 +0200262 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200263 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200264 * it should normally be only the same as the one above,
265 * so this should not happen in fact.
266 */
267 sess_change_server(s, NULL);
268 }
269
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100270 if (s->req.pipe)
271 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100272
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100273 if (s->res.pipe)
274 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100275
Willy Tarreaubf883e02014-11-25 21:10:35 +0100276 /* We may still be present in the buffer wait queue */
277 if (!LIST_ISEMPTY(&s->buffer_wait)) {
278 LIST_DEL(&s->buffer_wait);
279 LIST_INIT(&s->buffer_wait);
280 }
281
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100282 b_drop(&s->req.buf);
283 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100284 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200285 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200286
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100287 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200288 if (s->txn)
289 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100290
Willy Tarreau1e954912012-10-12 17:50:05 +0200291 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200292 if (cli_conn)
293 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200294
Willy Tarreaua4cda672010-06-06 18:28:49 +0200295 for (i = 0; i < s->store_count; i++) {
296 if (!s->store[i].ts)
297 continue;
298 stksess_free(s->store[i].table, s->store[i].ts);
299 s->store[i].ts = NULL;
300 }
301
Willy Tarreaueee5b512015-04-03 23:46:31 +0200302 if (s->txn) {
303 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
304 pool_free2(pool2_http_txn, s->txn);
305 s->txn = NULL;
306 }
307
Willy Tarreau92fb9832007-10-16 17:34:28 +0200308 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200309 pool_free2(fe->rsp_cap_pool, s->res_cap);
310 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200311 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100312
Willy Tarreau87b09662015-04-03 00:22:06 +0200313 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200314
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100315 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100316 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200317 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100318 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100319 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100320 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200321 if (s->list.n != &streams)
322 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100323 bref->ref = s->list.n;
324 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100325 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200326 si_release_endpoint(&s->si[1]);
327 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200328
329 /* FIXME: for now we have a 1:1 relation between stream and session so
330 * the stream must free the session.
331 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200332 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200333 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200334
335 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200336 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200337 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200338 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200339 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200340 pool_flush2(pool2_requri);
341 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200342 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200343 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100344 pool_flush2(pool2_connection);
345 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200346 pool_flush2(fe->req_cap_pool);
347 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200348 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200349}
350
Willy Tarreau78955f42014-12-28 13:09:02 +0100351/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
352 * that it's not the last available buffer or it's the response buffer. Unless
353 * the buffer is the response buffer, an extra control is made so that we always
354 * keep <tune.buffers.reserved> buffers available after this allocation. To be
355 * called at the beginning of recv() callbacks to ensure that the required
356 * buffers are properly allocated. Returns 0 in case of failure, non-zero
357 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100358 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200359int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100360{
Willy Tarreau87b09662015-04-03 00:22:06 +0200361 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100362 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100363 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100364
Willy Tarreau78955f42014-12-28 13:09:02 +0100365 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100366 margin = global.tune.reserved_bufs;
367
Willy Tarreau78955f42014-12-28 13:09:02 +0100368 s = chn_sess(chn);
369
370 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100371 if (b)
372 return 1;
373
Willy Tarreaubf883e02014-11-25 21:10:35 +0100374 if (LIST_ISEMPTY(&s->buffer_wait))
375 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100376 return 0;
377}
378
Willy Tarreau87b09662015-04-03 00:22:06 +0200379/* Allocates a work buffer for stream <s>. It is meant to be called inside
380 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaua24adf02014-11-27 01:11:56 +0100381 * to work fine. For a regular connection, only the response is needed so that
382 * an error message may be built and returned. In case where the initiator is
383 * an applet (eg: peers), then we need to allocate the request buffer for the
384 * applet to be able to send its data (in this case a response is not needed).
385 * Request buffers are never picked from the reserved pool, but response
386 * buffers may be allocated from the reserve. This is critical to ensure that
387 * a response may always flow and will never block a server from releasing a
388 * connection. Returns 0 in case of failure, non-zero otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100389 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200390int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100391{
Willy Tarreaua24adf02014-11-27 01:11:56 +0100392 int margin;
393 struct buffer **buf;
394
395 if (objt_appctx(s->si[0].end)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100396 buf = &s->req.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100397 margin = global.tune.reserved_bufs;
398 }
399 else {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100400 buf = &s->res.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100401 margin = 0;
402 }
403
Willy Tarreaubf883e02014-11-25 21:10:35 +0100404 if (!LIST_ISEMPTY(&s->buffer_wait)) {
405 LIST_DEL(&s->buffer_wait);
406 LIST_INIT(&s->buffer_wait);
407 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100408
Willy Tarreaua24adf02014-11-27 01:11:56 +0100409 if (b_alloc_margin(buf, margin))
Willy Tarreau656859d2014-11-25 19:46:36 +0100410 return 1;
411
Willy Tarreaubf883e02014-11-25 21:10:35 +0100412 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100413 return 0;
414}
415
416/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100417 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200418 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100419 * a single buffer, so it makes sense to try to wake two up when two buffers
420 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100421 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200422void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100423{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100424 if (s->req.buf->size && buffer_empty(s->req.buf))
425 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100426
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100427 if (s->res.buf->size && buffer_empty(s->res.buf))
428 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100429
Willy Tarreaubf883e02014-11-25 21:10:35 +0100430 /* if we're certain to have at least 1 buffer available, and there is
431 * someone waiting, we can wake up a waiter and offer them.
432 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100433 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200434 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100435}
436
Willy Tarreau87b09662015-04-03 00:22:06 +0200437/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100438 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200439 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100440 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200441void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100442{
Willy Tarreau87b09662015-04-03 00:22:06 +0200443 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100444
445 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100446 if (rqlimit <= run_queue)
447 break;
448
Willy Tarreaubf883e02014-11-25 21:10:35 +0100449 if (sess->task->state & TASK_RUNNING)
450 continue;
451
452 LIST_DEL(&sess->buffer_wait);
453 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100454 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100455 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100456}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200457
458/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200459int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200460{
Willy Tarreau87b09662015-04-03 00:22:06 +0200461 LIST_INIT(&streams);
462 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
463 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464}
465
Willy Tarreau87b09662015-04-03 00:22:06 +0200466void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100467{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200468 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100469 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100470 void *ptr;
471 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100472
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100473 bytes = s->req.total - s->logs.bytes_in;
474 s->logs.bytes_in = s->req.total;
475 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200476 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100477
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100478 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100479
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100480 if (objt_server(s->target))
481 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200482
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200483 if (sess->listener && sess->listener->counters)
484 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200485
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100486 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200487 struct stkctr *stkctr = &s->stkctr[i];
488
489 if (!stkctr_entry(stkctr)) {
490 stkctr = &sess->stkctr[i];
491 if (!stkctr_entry(stkctr))
492 continue;
493 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200494
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200495 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100496 if (ptr)
497 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200498
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200499 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100500 if (ptr)
501 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200502 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100503 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100504 }
505
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100506 bytes = s->res.total - s->logs.bytes_out;
507 s->logs.bytes_out = s->res.total;
508 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200509 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100510
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100511 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100512
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100513 if (objt_server(s->target))
514 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200515
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200516 if (sess->listener && sess->listener->counters)
517 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200518
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100519 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200520 struct stkctr *stkctr = &s->stkctr[i];
521
522 if (!stkctr_entry(stkctr)) {
523 stkctr = &sess->stkctr[i];
524 if (!stkctr_entry(stkctr))
525 continue;
526 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200527
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200528 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100529 if (ptr)
530 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200531
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200532 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100533 if (ptr)
534 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200535 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100536 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100537 }
538}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200539
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100540/* This function is called with (si->state == SI_ST_CON) meaning that a
541 * connection was attempted and that the file descriptor is already allocated.
542 * We must check for establishment, error and abort. Possible output states
543 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
544 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200545 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100546 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200547static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100548{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100549 struct stream_interface *si = &s->si[1];
550 struct channel *req = &s->req;
551 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200552 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100553
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100554 /* If we got an error, or if nothing happened and the connection timed
555 * out, we must give up. The CER state handler will take care of retry
556 * attempts and error reports.
557 */
558 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100559 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100560 /* Some data were sent past the connection establishment,
561 * so we need to pretend we're established to log correctly
562 * and let later states handle the failure.
563 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100564 si->state = SI_ST_EST;
565 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100566 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100567 return 1;
568 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100569 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100570 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100571
Willy Tarreauf79c8172013-10-21 16:30:56 +0200572 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100573
574 if (si->err_type)
575 return 0;
576
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100577 if (si->flags & SI_FL_ERR)
578 si->err_type = SI_ET_CONN_ERR;
579 else
580 si->err_type = SI_ET_CONN_TO;
581 return 0;
582 }
583
584 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100585 if (!(req->flags & CF_WRITE_PARTIAL) &&
586 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200587 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
588 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100589 s->be->options & PR_O_ABRT_CLOSE)))) {
590 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200591 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100592 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100593 if (s->srv_error)
594 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100595 return 1;
596 }
597
598 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200599 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100600 return 1;
601
602 /* OK, this means that a connection succeeded. The caller will be
603 * responsible for handling the transition from CON to EST.
604 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100605 si->state = SI_ST_EST;
606 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100607 return 1;
608}
609
610/* This function is called with (si->state == SI_ST_CER) meaning that a
611 * previous connection attempt has failed and that the file descriptor
612 * has already been released. Possible causes include asynchronous error
613 * notification and time out. Possible output states are SI_ST_CLO when
614 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
615 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
616 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
617 * marked as in error state. It returns 0.
618 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200619static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100620{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100621 struct stream_interface *si = &s->si[1];
622
Willy Tarreau87b09662015-04-03 00:22:06 +0200623 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100624 if (objt_server(s->target)) {
625 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100626
Willy Tarreaue7dff022015-04-03 01:14:29 +0200627 if (s->flags & SF_CURR_SESS) {
628 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100629 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100630 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100631 }
632
633 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200634 si->conn_retries--;
635 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100636 if (!si->err_type) {
637 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100638 }
639
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100640 if (objt_server(s->target))
641 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100642 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100643 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100644 if (may_dequeue_tasks(objt_server(s->target), s->be))
645 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100646
647 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200648 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100649 s->req.flags |= CF_WRITE_ERROR;
650 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100651
652 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100653 if (s->srv_error)
654 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100655 return 0;
656 }
657
658 /* If the "redispatch" option is set on the backend, we are allowed to
659 * retry on another server for the last retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200660 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100661 * bit to ignore any persistence cookie. We won't count a retry nor a
662 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200663 * If the connection is not persistent, the balancing algorithm is not
664 * determinist (round robin) and there is more than one active server,
665 * we accept to perform an immediate redispatch without waiting since
666 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100667 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200668 if (objt_server(s->target) &&
669 (si->conn_retries == 0 ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200670 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Willy Tarreau33a14e52014-06-13 17:49:40 +0200671 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200672 s->be->options & PR_O_REDISP && !(s->flags & SF_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100673 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100674 if (may_dequeue_tasks(objt_server(s->target), s->be))
675 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100676
Willy Tarreaue7dff022015-04-03 01:14:29 +0200677 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100678 si->state = SI_ST_REQ;
679 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100680 if (objt_server(s->target))
681 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100682 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100683 si->state = SI_ST_ASS;
684 }
685
686 if (si->flags & SI_FL_ERR) {
687 /* The error was an asynchronous connection error, and we will
688 * likely have to retry connecting to the same server, most
689 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200690 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100691 */
692
Willy Tarreaub0290662014-06-13 17:04:44 +0200693 int delay = 1000;
694
695 if (s->be->timeout.connect && s->be->timeout.connect < delay)
696 delay = s->be->timeout.connect;
697
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100698 if (!si->err_type)
699 si->err_type = SI_ET_CONN_ERR;
700
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200701 /* only wait when we're retrying on the same server */
702 if (si->state == SI_ST_ASS ||
703 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
704 (s->be->srv_act <= 1)) {
705 si->state = SI_ST_TAR;
706 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
707 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100708 return 0;
709 }
710 return 0;
711}
712
713/*
714 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200715 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200716 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100717 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200718static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100719{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100720 struct stream_interface *si = &s->si[1];
721 struct channel *req = &s->req;
722 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100723
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100724 /* First, centralize the timers information */
725 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
726 si->exp = TICK_ETERNITY;
727
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100728 if (objt_server(s->target))
729 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100730
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100731 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100732 /* if the user wants to log as soon as possible, without counting
733 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200734 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100735 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100736 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100738 }
739 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100740 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100741 }
742
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200743 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200744 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200745 if (req->flags & CF_WAKE_CONNECT) {
746 req->flags |= CF_WAKE_ONCE;
747 req->flags &= ~CF_WAKE_CONNECT;
748 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200749 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200750 /* real connections have timeouts */
751 req->wto = s->be->timeout.server;
752 rep->rto = s->be->timeout.server;
753 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100754 req->wex = TICK_ETERNITY;
755}
756
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100757/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
758 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100759 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
760 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
761 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100762 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200763static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100764{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100765 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100766 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100767 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100768
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100769 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 +0100770 now_ms, __FUNCTION__,
771 s,
Willy Tarreau103197d2014-11-28 15:26:12 +0100772 req, s->rep,
773 req->rex, s->res.wex,
774 req->flags, s->res.flags,
775 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 +0100776
777 if (si->state == SI_ST_ASS) {
778 /* Server assigned to connection request, we have to try to connect now */
779 int conn_err;
780
781 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100782 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100783
Willy Tarreaue7dff022015-04-03 01:14:29 +0200784 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100785 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100786 if (srv)
787 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500788 if (srv)
789 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100790 return;
791 }
792
793 /* We have received a synchronous error. We might have to
794 * abort, retry immediately or redispatch.
795 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200796 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100797 if (!si->err_type) {
798 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100799 }
800
Willy Tarreau827aee92011-03-10 16:55:02 +0100801 if (srv)
802 srv_inc_sess_ctr(srv);
803 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500804 srv_set_sess_last(srv);
805 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100806 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100807 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100808
Willy Tarreau87b09662015-04-03 00:22:06 +0200809 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100810 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100811 if (may_dequeue_tasks(srv, s->be))
812 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100813
814 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200815 si_shutr(si);
816 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100817 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100818
819 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
820
Willy Tarreau87b09662015-04-03 00:22:06 +0200821 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100822 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100823 if (s->srv_error)
824 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100825 return;
826 }
827
828 /* We are facing a retryable error, but we don't want to run a
829 * turn-around now, as the problem is likely a source port
830 * allocation problem, so we want to retry now.
831 */
832 si->state = SI_ST_CER;
833 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100834 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100835 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
836 return;
837 }
838 else if (si->state == SI_ST_QUE) {
839 /* connection request was queued, check for any update */
840 if (!s->pend_pos) {
841 /* The connection is not in the queue anymore. Either
842 * we have a server connection slot available and we
843 * go directly to the assigned state, or we need to
844 * load-balance first and go to the INI state.
845 */
846 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200847 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100848 si->state = SI_ST_REQ;
849 else {
850 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
851 si->state = SI_ST_ASS;
852 }
853 return;
854 }
855
856 /* Connection request still in queue... */
857 if (si->flags & SI_FL_EXP) {
858 /* ... and timeout expired */
859 si->exp = TICK_ETERNITY;
860 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100861 if (srv)
862 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100863 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200864 si_shutr(si);
865 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100866 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100867 if (!si->err_type)
868 si->err_type = SI_ET_QUEUE_TO;
869 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100870 if (s->srv_error)
871 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100872 return;
873 }
874
875 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100876 if ((req->flags & (CF_READ_ERROR)) ||
877 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
878 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100879 /* give up */
880 si->exp = TICK_ETERNITY;
881 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200882 si_shutr(si);
883 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100884 si->err_type |= SI_ET_QUEUE_ABRT;
885 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100886 if (s->srv_error)
887 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100888 return;
889 }
890
891 /* Nothing changed */
892 return;
893 }
894 else if (si->state == SI_ST_TAR) {
895 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100896 if ((req->flags & (CF_READ_ERROR)) ||
897 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
898 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100899 /* give up */
900 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200901 si_shutr(si);
902 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100903 si->err_type |= SI_ET_CONN_ABRT;
904 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100905 if (s->srv_error)
906 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100907 return;
908 }
909
910 if (!(si->flags & SI_FL_EXP))
911 return; /* still in turn-around */
912
913 si->exp = TICK_ETERNITY;
914
Willy Tarreau87b09662015-04-03 00:22:06 +0200915 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100916 * marked "assigned".
917 * FIXME: Should we force a redispatch attempt when the server is down ?
918 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200919 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100920 si->state = SI_ST_ASS;
921 else
922 si->state = SI_ST_REQ;
923 return;
924 }
925}
926
Willy Tarreau87b09662015-04-03 00:22:06 +0200927/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900928 * also counts a failed request if the server state has not reached the request
929 * stage.
930 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200931static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900932{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200933 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900934 if (s->si[1].state < SI_ST_REQ) {
935
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200936 strm_fe(s)->fe_counters.failed_req++;
937 if (strm_li(s)->counters)
938 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900939
Willy Tarreaue7dff022015-04-03 01:14:29 +0200940 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900941 }
942 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200943 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900944 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200945 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900946 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200947 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900948 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200949 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900950 }
951}
952
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100953/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100954 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
955 * a real connection to a server, indicating that a server has been assigned,
956 * or SI_ST_EST for a successful connection to an applet. It may also return
957 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100958 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200959static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100960{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100961 struct stream_interface *si = &s->si[1];
962
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100963 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 +0100964 now_ms, __FUNCTION__,
965 s,
966 s->req, s->rep,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100967 s->req.rex, s->res.wex,
968 s->req.flags, s->res.flags,
Willy Tarreau350f4872014-11-28 14:42:25 +0100969 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 +0100970
971 if (si->state != SI_ST_REQ)
972 return;
973
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100974 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
975 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100976 struct appctx *appctx = objt_appctx(si->end);
977
978 if (!appctx || appctx->applet != __objt_applet(s->target))
979 appctx = stream_int_register_handler(si, objt_applet(s->target));
980
981 if (!appctx) {
982 /* No more memory, let's immediately abort. Force the
983 * error code to ignore the ERR_LOCAL which is not a
984 * real error.
985 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200986 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100987
988 si_shutr(si);
989 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100990 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +0100991 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100992 si->state = SI_ST_CLO;
993 if (s->srv_error)
994 s->srv_error(s, si);
995 return;
996 }
997
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100998 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100999 si->state = SI_ST_EST;
1000 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001001 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001002 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001003 return;
1004 }
1005
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001006 /* Try to assign a server */
1007 if (srv_redispatch_connect(s) != 0) {
1008 /* We did not get a server. Either we queued the
1009 * connection request, or we encountered an error.
1010 */
1011 if (si->state == SI_ST_QUE)
1012 return;
1013
1014 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001015 si_shutr(si);
1016 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001017 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001018 if (!si->err_type)
1019 si->err_type = SI_ET_CONN_OTHER;
1020 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001021 if (s->srv_error)
1022 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001023 return;
1024 }
1025
1026 /* The server is assigned */
1027 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1028 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001029 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001030}
1031
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001032/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001033 * if appropriate. The default_backend rule is also considered, then the
1034 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001035 * It returns 1 if the processing can continue on next analysers, or zero if it
1036 * either needs more data or wants to immediately abort the request.
1037 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001038static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001039{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001040 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001041 struct session *sess = s->sess;
1042 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001043
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001044 req->analysers &= ~an_bit;
1045 req->analyse_exp = TICK_ETERNITY;
1046
Willy Tarreau87b09662015-04-03 00:22:06 +02001047 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 +02001048 now_ms, __FUNCTION__,
1049 s,
1050 req,
1051 req->rex, req->wex,
1052 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001053 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001054 req->analysers);
1055
1056 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001057 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001058 struct switching_rule *rule;
1059
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001060 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001061 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001062
Willy Tarreauf51658d2014-04-23 01:21:56 +02001063 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001064 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001065 ret = acl_pass(ret);
1066 if (rule->cond->pol == ACL_COND_UNLESS)
1067 ret = !ret;
1068 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001069
1070 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001071 /* If the backend name is dynamic, try to resolve the name.
1072 * If we can't resolve the name, or if any error occurs, break
1073 * the loop and fallback to the default backend.
1074 */
1075 struct proxy *backend;
1076
1077 if (rule->dynamic) {
1078 struct chunk *tmp = get_trash_chunk();
1079 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1080 break;
1081 backend = findproxy(tmp->str, PR_CAP_BE);
1082 if (!backend)
1083 break;
1084 }
1085 else
1086 backend = rule->be.backend;
1087
Willy Tarreau87b09662015-04-03 00:22:06 +02001088 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001089 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001090 break;
1091 }
1092 }
1093
1094 /* To ensure correct connection accounting on the backend, we
1095 * have to assign one if it was not set (eg: a listen). This
1096 * measure also takes care of correctly setting the default
1097 * backend if any.
1098 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001099 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001100 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001101 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001102 }
1103
Willy Tarreaufb356202010-08-03 14:02:05 +02001104 /* 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 +02001105 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001106 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1107 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001108 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001109
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001110 /* 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 +02001111 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001112 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001113 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001114 int ret = 1;
1115
1116 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001117 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 +01001118 ret = acl_pass(ret);
1119 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1120 ret = !ret;
1121 }
1122
1123 if (ret) {
1124 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001125 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001126 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001127 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001128 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001129 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001130 break;
1131 }
1132 }
1133
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001134 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001135
1136 sw_failed:
1137 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001138 channel_abort(&s->req);
1139 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001140
Willy Tarreaue7dff022015-04-03 01:14:29 +02001141 if (!(s->flags & SF_ERR_MASK))
1142 s->flags |= SF_ERR_RESOURCE;
1143 if (!(s->flags & SF_FINST_MASK))
1144 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001145
Willy Tarreaueee5b512015-04-03 23:46:31 +02001146 if (s->txn)
1147 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001148 s->req.analysers = 0;
1149 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001150 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001151}
1152
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001153/* This stream analyser works on a request. It applies all use-server rules on
1154 * it then returns 1. The data must already be present in the buffer otherwise
1155 * they won't match. It always returns 1.
1156 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001157static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001158{
1159 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001160 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001161 struct server_rule *rule;
1162
Willy Tarreau87b09662015-04-03 00:22:06 +02001163 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 +02001164 now_ms, __FUNCTION__,
1165 s,
1166 req,
1167 req->rex, req->wex,
1168 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001169 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001170 req->analysers);
1171
Willy Tarreaue7dff022015-04-03 01:14:29 +02001172 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001173 list_for_each_entry(rule, &px->server_rules, list) {
1174 int ret;
1175
Willy Tarreau192252e2015-04-04 01:47:55 +02001176 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001177 ret = acl_pass(ret);
1178 if (rule->cond->pol == ACL_COND_UNLESS)
1179 ret = !ret;
1180
1181 if (ret) {
1182 struct server *srv = rule->srv.ptr;
1183
Willy Tarreau892337c2014-05-13 23:41:20 +02001184 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001185 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001186 (s->flags & SF_FORCE_PRST)) {
1187 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001188 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001189 break;
1190 }
1191 /* if the server is not UP, let's go on with next rules
1192 * just in case another one is suited.
1193 */
1194 }
1195 }
1196 }
1197
1198 req->analysers &= ~an_bit;
1199 req->analyse_exp = TICK_ETERNITY;
1200 return 1;
1201}
1202
Emeric Brun1d33b292010-01-04 15:47:17 +01001203/* This stream analyser works on a request. It applies all sticking rules on
1204 * it then returns 1. The data must already be present in the buffer otherwise
1205 * they won't match. It always returns 1.
1206 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001207static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001208{
1209 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001210 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001211 struct sticking_rule *rule;
1212
Willy Tarreau87b09662015-04-03 00:22:06 +02001213 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 +01001214 now_ms, __FUNCTION__,
1215 s,
1216 req,
1217 req->rex, req->wex,
1218 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001219 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001220 req->analysers);
1221
1222 list_for_each_entry(rule, &px->sticking_rules, list) {
1223 int ret = 1 ;
1224 int i;
1225
Willy Tarreau9667a802013-12-09 12:52:13 +01001226 /* Only the first stick store-request of each table is applied
1227 * and other ones are ignored. The purpose is to allow complex
1228 * configurations which look for multiple entries by decreasing
1229 * order of precision and to stop at the first which matches.
1230 * An example could be a store of the IP address from an HTTP
1231 * header first, then from the source if not found.
1232 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001233 for (i = 0; i < s->store_count; i++) {
1234 if (rule->table.t == s->store[i].table)
1235 break;
1236 }
1237
1238 if (i != s->store_count)
1239 continue;
1240
1241 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001242 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001243 ret = acl_pass(ret);
1244 if (rule->cond->pol == ACL_COND_UNLESS)
1245 ret = !ret;
1246 }
1247
1248 if (ret) {
1249 struct stktable_key *key;
1250
Willy Tarreau192252e2015-04-04 01:47:55 +02001251 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 +01001252 if (!key)
1253 continue;
1254
1255 if (rule->flags & STK_IS_MATCH) {
1256 struct stksess *ts;
1257
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001258 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001259 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001260 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001261 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001262
1263 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001264 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1265 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001266 if (node) {
1267 struct server *srv;
1268
1269 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001270 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001271 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001272 (s->flags & SF_FORCE_PRST)) {
1273 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001274 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001275 }
1276 }
1277 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001278 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001279 }
1280 }
1281 if (rule->flags & STK_IS_STORE) {
1282 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1283 struct stksess *ts;
1284
1285 ts = stksess_new(rule->table.t, key);
1286 if (ts) {
1287 s->store[s->store_count].table = rule->table.t;
1288 s->store[s->store_count++].ts = ts;
1289 }
1290 }
1291 }
1292 }
1293 }
1294
1295 req->analysers &= ~an_bit;
1296 req->analyse_exp = TICK_ETERNITY;
1297 return 1;
1298}
1299
1300/* This stream analyser works on a response. It applies all store rules on it
1301 * then returns 1. The data must already be present in the buffer otherwise
1302 * they won't match. It always returns 1.
1303 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001304static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001305{
1306 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001307 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001308 struct sticking_rule *rule;
1309 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001310 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001311
Willy Tarreau87b09662015-04-03 00:22:06 +02001312 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 +01001313 now_ms, __FUNCTION__,
1314 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001315 rep,
1316 rep->rex, rep->wex,
1317 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001318 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001319 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001320
1321 list_for_each_entry(rule, &px->storersp_rules, list) {
1322 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001323
Willy Tarreau9667a802013-12-09 12:52:13 +01001324 /* Only the first stick store-response of each table is applied
1325 * and other ones are ignored. The purpose is to allow complex
1326 * configurations which look for multiple entries by decreasing
1327 * order of precision and to stop at the first which matches.
1328 * An example could be a store of a set-cookie value, with a
1329 * fallback to a parameter found in a 302 redirect.
1330 *
1331 * The store-response rules are not allowed to override the
1332 * store-request rules for the same table, but they may coexist.
1333 * Thus we can have up to one store-request entry and one store-
1334 * response entry for the same table at any time.
1335 */
1336 for (i = nbreq; i < s->store_count; i++) {
1337 if (rule->table.t == s->store[i].table)
1338 break;
1339 }
1340
1341 /* skip existing entries for this table */
1342 if (i < s->store_count)
1343 continue;
1344
Emeric Brun1d33b292010-01-04 15:47:17 +01001345 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001346 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001347 ret = acl_pass(ret);
1348 if (rule->cond->pol == ACL_COND_UNLESS)
1349 ret = !ret;
1350 }
1351
1352 if (ret) {
1353 struct stktable_key *key;
1354
Willy Tarreau192252e2015-04-04 01:47:55 +02001355 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 +01001356 if (!key)
1357 continue;
1358
Willy Tarreau37e340c2013-12-06 23:05:21 +01001359 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001360 struct stksess *ts;
1361
1362 ts = stksess_new(rule->table.t, key);
1363 if (ts) {
1364 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001365 s->store[s->store_count++].ts = ts;
1366 }
1367 }
1368 }
1369 }
1370
1371 /* process store request and store response */
1372 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001373 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001374 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001375
Willy Tarreauc93cd162014-05-13 15:54:22 +02001376 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001377 stksess_free(s->store[i].table, s->store[i].ts);
1378 s->store[i].ts = NULL;
1379 continue;
1380 }
1381
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001382 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1383 if (ts) {
1384 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001385 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001386 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001387 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001388 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001389 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001390
1391 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001392 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001393 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001394 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001395 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001396
1397 rep->analysers &= ~an_bit;
1398 rep->analyse_exp = TICK_ETERNITY;
1399 return 1;
1400}
1401
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001402/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001403 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001404 */
1405#define UPDATE_ANALYSERS(real, list, back, flag) { \
1406 list = (((list) & ~(flag)) | ~(back)) & (real); \
1407 back = real; \
1408 if (!(list)) \
1409 break; \
1410 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1411 continue; \
1412}
1413
Willy Tarreau87b09662015-04-03 00:22:06 +02001414/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001415 * then puts it back to the wait queue in a clean state, or cleans up its
1416 * resources if it must be deleted. Returns in <next> the date the task wants
1417 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1418 * nothing too many times, the request and response buffers flags are monitored
1419 * and each function is called only if at least another function has changed at
1420 * least one flag it is interested in.
1421 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001422struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001423{
Willy Tarreau827aee92011-03-10 16:55:02 +01001424 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001425 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001426 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001427 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001428 unsigned int rq_prod_last, rq_cons_last;
1429 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001430 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001431 struct channel *req, *res;
1432 struct stream_interface *si_f, *si_b;
1433
1434 req = &s->req;
1435 res = &s->res;
1436
1437 si_f = &s->si[0];
1438 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001439
1440 //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 +01001441 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001442
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001443 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001444 if (s->txn)
1445 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001446
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001447 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001448 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1449 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001450
1451 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001452 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1453 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001454
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001455 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001456 si_f->flags |= SI_FL_DONT_WAKE;
1457 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001458
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001459 /* 1a: Check for low level timeouts if needed. We just set a flag on
1460 * stream interfaces when their timeouts have expired.
1461 */
1462 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001463 stream_int_check_timeouts(si_f);
1464 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001465
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001466 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001467 * for future reads or writes. Note: this will also concern upper layers
1468 * but we do not touch any other flag. We must be careful and correctly
1469 * detect state changes when calling them.
1470 */
1471
Willy Tarreau8f128b42014-11-28 15:07:47 +01001472 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001473
Willy Tarreau8f128b42014-11-28 15:07:47 +01001474 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1475 si_b->flags |= SI_FL_NOLINGER;
1476 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001477 }
1478
Willy Tarreau8f128b42014-11-28 15:07:47 +01001479 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1480 if (si_f->flags & SI_FL_NOHALF)
1481 si_f->flags |= SI_FL_NOLINGER;
1482 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001483 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001484
Willy Tarreau8f128b42014-11-28 15:07:47 +01001485 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001486
Willy Tarreau8f128b42014-11-28 15:07:47 +01001487 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1488 si_f->flags |= SI_FL_NOLINGER;
1489 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001490 }
1491
Willy Tarreau8f128b42014-11-28 15:07:47 +01001492 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1493 if (si_b->flags & SI_FL_NOHALF)
1494 si_b->flags |= SI_FL_NOLINGER;
1495 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001496 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001497
1498 /* Once in a while we're woken up because the task expires. But
1499 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001500 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001501 * timeout needs to be refreshed.
1502 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001503 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001504 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1505 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001506 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001507 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1508 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001509 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001510
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001511 /* below we may emit error messages so we have to ensure that we have
1512 * our buffers properly allocated.
1513 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001514 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001515 /* No buffer available, we've been subscribed to the list of
1516 * buffer waiters, let's wait for our turn.
1517 */
1518 goto update_exp_and_leave;
1519 }
1520
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001521 /* 1b: check for low-level errors reported at the stream interface.
1522 * First we check if it's a retryable error (in which case we don't
1523 * want to tell the buffer). Otherwise we report the error one level
1524 * upper by setting flags into the buffers. Note that the side towards
1525 * the client cannot have connect (hence retryable) errors. Also, the
1526 * connection setup code must be able to deal with any type of abort.
1527 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001528 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001529 if (unlikely(si_f->flags & SI_FL_ERR)) {
1530 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1531 si_shutr(si_f);
1532 si_shutw(si_f);
1533 stream_int_report_error(si_f);
1534 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001535 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001536 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001537 if (srv)
1538 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001539 if (!(s->flags & SF_ERR_MASK))
1540 s->flags |= SF_ERR_CLICL;
1541 if (!(s->flags & SF_FINST_MASK))
1542 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001543 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001544 }
1545 }
1546
Willy Tarreau8f128b42014-11-28 15:07:47 +01001547 if (unlikely(si_b->flags & SI_FL_ERR)) {
1548 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1549 si_shutr(si_b);
1550 si_shutw(si_b);
1551 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001552 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001553 if (srv)
1554 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001555 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001556 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001557 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001558 if (srv)
1559 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001560 if (!(s->flags & SF_ERR_MASK))
1561 s->flags |= SF_ERR_SRVCL;
1562 if (!(s->flags & SF_FINST_MASK))
1563 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001564 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001565 }
1566 /* note: maybe we should process connection errors here ? */
1567 }
1568
Willy Tarreau8f128b42014-11-28 15:07:47 +01001569 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001570 /* we were trying to establish a connection on the server side,
1571 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1572 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001573 if (unlikely(!sess_update_st_con_tcp(s)))
1574 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001575 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001576 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001577
1578 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1579 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1580 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1581 */
1582 }
1583
Willy Tarreau8f128b42014-11-28 15:07:47 +01001584 rq_prod_last = si_f->state;
1585 rq_cons_last = si_b->state;
1586 rp_cons_last = si_f->state;
1587 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001588
1589 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001590 /* Check for connection closure */
1591
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001592 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001593 "[%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 +01001594 now_ms, __FUNCTION__, __LINE__,
1595 t,
1596 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001597 req, res,
1598 req->rex, res->wex,
1599 req->flags, res->flags,
1600 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1601 si_f->err_type, si_b->err_type,
1602 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001603
1604 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001605 if (unlikely(si_f->state == SI_ST_DIS))
1606 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001607
1608 /* When a server-side connection is released, we have to count it and
1609 * check for pending connections on this server.
1610 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001611 if (unlikely(si_b->state == SI_ST_DIS)) {
1612 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001613 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001614 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001615 if (s->flags & SF_CURR_SESS) {
1616 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001617 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001618 }
1619 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001620 if (may_dequeue_tasks(srv, s->be))
1621 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001622 }
1623 }
1624
1625 /*
1626 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1627 * at this point.
1628 */
1629
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001630 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001631 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001632 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1633 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1634 si_f->state != rq_prod_last ||
1635 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001636 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001637 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001638
Willy Tarreau8f128b42014-11-28 15:07:47 +01001639 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001640 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001641 unsigned int ana_list;
1642 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001643
Willy Tarreau90deb182010-01-07 00:20:41 +01001644 /* it's up to the analysers to stop new connections,
1645 * disable reading or closing. Note: if an analyser
1646 * disables any of these bits, it is responsible for
1647 * enabling them again when it disables itself, so
1648 * that other analysers are called in similar conditions.
1649 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001650 channel_auto_read(req);
1651 channel_auto_connect(req);
1652 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001653
1654 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001655 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001656 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001657 * the list when not needed. Any analyser may return 0
1658 * to break out of the loop, either because of missing
1659 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001660 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001661 * analyser, and we may loop again if other analysers
1662 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001663 *
1664 * We build a list of analysers to run. We evaluate all
1665 * of these analysers in the order of the lower bit to
1666 * the higher bit. This ordering is very important.
1667 * An analyser will often add/remove other analysers,
1668 * including itself. Any changes to itself have no effect
1669 * on the loop. If it removes any other analysers, we
1670 * want those analysers not to be called anymore during
1671 * this loop. If it adds an analyser that is located
1672 * after itself, we want it to be scheduled for being
1673 * processed during the loop. If it adds an analyser
1674 * which is located before it, we want it to switch to
1675 * it immediately, even if it has already been called
1676 * once but removed since.
1677 *
1678 * In order to achieve this, we compare the analyser
1679 * list after the call with a copy of it before the
1680 * call. The work list is fed with analyser bits that
1681 * appeared during the call. Then we compare previous
1682 * work list with the new one, and check the bits that
1683 * appeared. If the lowest of these bits is lower than
1684 * the current bit, it means we have enabled a previous
1685 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001686 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001687
Willy Tarreau8f128b42014-11-28 15:07:47 +01001688 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001689 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001690 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001691
Willy Tarreaufb356202010-08-03 14:02:05 +02001692 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001693 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001694 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001695 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001696 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001697
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001698 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001699 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001700 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001701 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001702 }
1703
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001704 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001705 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001706 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001707 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001708 }
1709
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001710 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001711 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001712 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001713 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001714 }
1715
Willy Tarreaufb356202010-08-03 14:02:05 +02001716 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001717 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001718 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001719 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001720 }
1721
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001722 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001723 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001724 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001725 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001726 }
1727
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001728 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001729 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001730 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001731 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001732 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001733
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001734 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001735 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001736 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001737 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001738 }
1739
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001740 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001741 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001742 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001743 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001744 }
1745
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001746 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001747 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001748 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001749 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001750 }
Emeric Brun647caf12009-06-30 17:57:00 +02001751
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001752 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001753 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001754 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001755 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001756 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001757
Emeric Brun1d33b292010-01-04 15:47:17 +01001758 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001759 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001760 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001761 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001762 }
1763
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001764 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001765 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001766 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001767 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001768 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001769 break;
1770 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001771 }
Willy Tarreau84455332009-03-15 22:34:05 +01001772
Willy Tarreau8f128b42014-11-28 15:07:47 +01001773 rq_prod_last = si_f->state;
1774 rq_cons_last = si_b->state;
1775 req->flags &= ~CF_WAKE_ONCE;
1776 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001777
Willy Tarreau8f128b42014-11-28 15:07:47 +01001778 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001779 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001780 }
1781
Willy Tarreau576507f2010-01-07 00:09:04 +01001782 /* we'll monitor the request analysers while parsing the response,
1783 * because some response analysers may indirectly enable new request
1784 * analysers (eg: HTTP keep-alive).
1785 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001786 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001787
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001788 resync_response:
1789 /* Analyse response */
1790
Willy Tarreau8f128b42014-11-28 15:07:47 +01001791 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1792 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1793 si_f->state != rp_cons_last ||
1794 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001795 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001796 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001797
Willy Tarreau8f128b42014-11-28 15:07:47 +01001798 if ((res->flags & CF_MASK_ANALYSER) &&
1799 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001800 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1801 * for some free space in the response buffer, so we might need to call
1802 * it when something changes in the response buffer, but still we pass
1803 * it the request buffer. Note that the SI state might very well still
1804 * be zero due to us returning a flow of redirects!
1805 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001806 res->analysers &= ~AN_REQ_ALL;
1807 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001808 }
1809
Willy Tarreau8f128b42014-11-28 15:07:47 +01001810 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001811 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001812 unsigned int ana_list;
1813 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001814
Willy Tarreau90deb182010-01-07 00:20:41 +01001815 /* it's up to the analysers to stop disable reading or
1816 * closing. Note: if an analyser disables any of these
1817 * bits, it is responsible for enabling them again when
1818 * it disables itself, so that other analysers are called
1819 * in similar conditions.
1820 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001821 channel_auto_read(res);
1822 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001823
1824 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001825 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001826 * to MSB. The analysers must remove themselves from
1827 * the list when not needed. Any analyser may return 0
1828 * to break out of the loop, either because of missing
1829 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001830 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001831 * analyser, and we may loop again if other analysers
1832 * are added in the middle.
1833 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001834
Willy Tarreau8f128b42014-11-28 15:07:47 +01001835 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001836 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001837 /* Warning! ensure that analysers are always placed in ascending order! */
1838
Emeric Brun97679e72010-09-23 17:56:44 +02001839 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001840 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001841 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001842 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001843 }
1844
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001845 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001846 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001847 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001848 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001849 }
1850
Emeric Brun1d33b292010-01-04 15:47:17 +01001851 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001852 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001853 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001854 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001855 }
1856
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001857 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001858 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001859 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001860 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001861 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001862
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001863 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001864 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001865 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001866 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001867 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001868 break;
1869 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001870 }
1871
Willy Tarreau8f128b42014-11-28 15:07:47 +01001872 rp_cons_last = si_f->state;
1873 rp_prod_last = si_b->state;
1874 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001875
Willy Tarreau8f128b42014-11-28 15:07:47 +01001876 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001877 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001878 }
1879
Willy Tarreau576507f2010-01-07 00:09:04 +01001880 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001881 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001882 goto resync_request;
1883
Willy Tarreau8f128b42014-11-28 15:07:47 +01001884 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001885 goto resync_request;
1886
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001887 /* FIXME: here we should call protocol handlers which rely on
1888 * both buffers.
1889 */
1890
1891
1892 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001893 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001894 * we're just in a data phase here since it means we have not
1895 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001896 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001897 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001898 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001899 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001900 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001901 req->analysers = 0;
1902 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001903 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001904 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001905 if (srv)
1906 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001907 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001908 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001909 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001910 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001911 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001912 if (srv)
1913 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001914 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001915 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001916 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001917 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001918 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001919 if (srv)
1920 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001921 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001922 }
1923 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001924 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001925 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001926 if (srv)
1927 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001928 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001929 }
Willy Tarreau84455332009-03-15 22:34:05 +01001930 sess_set_term_flags(s);
1931 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001932 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001933 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001934 res->analysers = 0;
1935 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001936 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001937 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001938 if (srv)
1939 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001940 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001941 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001942 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001943 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001944 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001945 if (srv)
1946 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001947 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001948 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001949 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001950 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001951 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001952 if (srv)
1953 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001954 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001955 }
1956 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001957 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001958 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001959 if (srv)
1960 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001961 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001962 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001963 sess_set_term_flags(s);
1964 }
Willy Tarreau84455332009-03-15 22:34:05 +01001965 }
1966
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001967 /*
1968 * Here we take care of forwarding unhandled data. This also includes
1969 * connection establishments and shutdown requests.
1970 */
1971
1972
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001973 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001974 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001975 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001976 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001977 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001978 if (unlikely(!req->analysers &&
1979 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
1980 (si_f->state >= SI_ST_EST) &&
1981 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01001982 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001983 * attached to it. If any data are left in, we'll permit them to
1984 * move.
1985 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001986 channel_auto_read(req);
1987 channel_auto_connect(req);
1988 channel_auto_close(req);
1989 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001990
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001991 /* We'll let data flow between the producer (if still connected)
1992 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001993 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001994 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
1995 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001996 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001997
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001998 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001999 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2000 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002001 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002002 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2003 (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 +01002004 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002005 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2006 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002007 (req->flags & CF_STREAMER_FAST)))) {
2008 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002009 }
2010
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002011 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002012 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002013
2014 /*
2015 * Now forward all shutdown requests between both sides of the buffer
2016 */
2017
Willy Tarreau520d95e2009-09-19 21:04:57 +02002018 /* first, let's check if the request buffer needs to shutdown(write), which may
2019 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002020 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2021 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002022 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002023 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002024 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002025 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002026 if (tick_isset(sess->fe->timeout.clientfin)) {
2027 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002028 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002029 }
2030 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002031
2032 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002033 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2034 channel_is_empty(req))) {
2035 if (req->flags & CF_READ_ERROR)
2036 si_b->flags |= SI_FL_NOLINGER;
2037 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002038 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002039 res->rto = s->be->timeout.serverfin;
2040 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002041 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002042 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002043
2044 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002045 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2046 !req->analysers))
2047 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002048
2049 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002050 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2051 if (si_f->flags & SI_FL_NOHALF)
2052 si_f->flags |= SI_FL_NOLINGER;
2053 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002054 if (tick_isset(sess->fe->timeout.clientfin)) {
2055 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002056 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002057 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002058 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002059
Willy Tarreau520d95e2009-09-19 21:04:57 +02002060 /* it's possible that an upper layer has requested a connection setup or abort.
2061 * There are 2 situations where we decide to establish a new connection :
2062 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002063 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002064 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002065 if (si_b->state == SI_ST_INI) {
2066 if (!(req->flags & CF_SHUTW)) {
2067 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002068 /* If we have an appctx, there is no connect method, so we
2069 * immediately switch to the connected state, otherwise we
2070 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002071 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002072 si_b->state = SI_ST_REQ; /* new connection requested */
2073 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002074 }
Willy Tarreau73201222009-08-16 18:27:24 +02002075 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002076 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002077 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2078 channel_shutw_now(req); /* fix buffer flags upon abort */
2079 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002080 }
Willy Tarreau92795622009-03-06 12:51:23 +01002081 }
2082
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002083
2084 /* we may have a pending connection request, or a connection waiting
2085 * for completion.
2086 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002087 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002088 do {
2089 /* nb: step 1 might switch from QUE to ASS, but we first want
2090 * to give a chance to step 2 to perform a redirect if needed.
2091 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002092 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002093 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002094 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002095 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002096
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002097 /* applets directly go to the ESTABLISHED state. Similarly,
2098 * servers experience the same fate when their connection
2099 * is reused.
2100 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002101 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002102 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002103
Willy Tarreaub44c8732013-12-31 23:16:50 +01002104 /* Now we can add the server name to a header (if requested) */
2105 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002106 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002107 (s->be->server_id_hdr_name != NULL) &&
2108 (s->be->mode == PR_MODE_HTTP) &&
2109 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002110 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002111 }
2112
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002113 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002114 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002115 http_perform_server_redirect(s, si_b);
2116 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002117 }
2118
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002119 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002120 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002121 goto resync_stream_interface;
2122
Willy Tarreau815a9b22010-07-27 17:15:12 +02002123 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002124 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002125 goto resync_request;
2126
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002127 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002128
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002129 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002130 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002131 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002132 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002133 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002134 if (unlikely(!res->analysers &&
2135 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2136 (si_b->state >= SI_ST_EST) &&
2137 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002138 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002139 * attached to it. If any data are left in, we'll permit them to
2140 * move.
2141 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002142 channel_auto_read(res);
2143 channel_auto_close(res);
2144 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002145
2146 /* We'll let data flow between the producer (if still connected)
2147 * to the consumer.
2148 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002149 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2150 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002151
2152 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002153 * tunnel timeout set, use it now. Note that we must respect
2154 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002155 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002156 if (!req->analysers && s->be->timeout.tunnel) {
2157 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002158 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002159
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002160 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2161 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002162 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2163 res->rto = s->be->timeout.serverfin;
2164 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2165 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002166 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2167 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002168
Willy Tarreau8f128b42014-11-28 15:07:47 +01002169 req->rex = tick_add(now_ms, req->rto);
2170 req->wex = tick_add(now_ms, req->wto);
2171 res->rex = tick_add(now_ms, res->rto);
2172 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002173 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002174 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002175
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002176 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002177 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2178 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002179 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002180 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2181 (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 +01002182 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002183 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2184 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002185 (res->flags & CF_STREAMER_FAST)))) {
2186 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002187 }
2188
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002189 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002190 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002191
2192 /*
2193 * Now forward all shutdown requests between both sides of the buffer
2194 */
2195
2196 /*
2197 * FIXME: this is probably where we should produce error responses.
2198 */
2199
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002200 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002201 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002202 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002203 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002204 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002205 req->wto = s->be->timeout.serverfin;
2206 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002207 }
2208 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002209
2210 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002211 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2212 channel_is_empty(res))) {
2213 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002214 if (tick_isset(sess->fe->timeout.clientfin)) {
2215 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002216 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002217 }
2218 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002219
2220 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002221 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2222 !res->analysers)
2223 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002224
2225 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002226 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2227 if (si_b->flags & SI_FL_NOHALF)
2228 si_b->flags |= SI_FL_NOLINGER;
2229 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002230 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002231 req->wto = s->be->timeout.serverfin;
2232 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002233 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002234 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002235
Willy Tarreau8f128b42014-11-28 15:07:47 +01002236 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002237 goto resync_stream_interface;
2238
Willy Tarreau8f128b42014-11-28 15:07:47 +01002239 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002240 goto resync_request;
2241
Willy Tarreau8f128b42014-11-28 15:07:47 +01002242 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002243 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002244
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002245 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002246 si_f->flags &= ~SI_FL_DONT_WAKE;
2247 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002248
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002249 /* This is needed only when debugging is enabled, to indicate
2250 * client-side or server-side close. Please note that in the unlikely
2251 * event where both sides would close at once, the sequence is reported
2252 * on the server side first.
2253 */
2254 if (unlikely((global.mode & MODE_DEBUG) &&
2255 (!(global.mode & MODE_QUIET) ||
2256 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002257 if (si_b->state == SI_ST_CLO &&
2258 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002259 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002260 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002261 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2262 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002263 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002264 }
2265
Willy Tarreau8f128b42014-11-28 15:07:47 +01002266 if (si_f->state == SI_ST_CLO &&
2267 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002268 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002269 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002270 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2271 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002272 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002273 }
2274 }
2275
Willy Tarreau8f128b42014-11-28 15:07:47 +01002276 if (likely((si_f->state != SI_ST_CLO) ||
2277 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002278
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002279 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002280 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002281
Willy Tarreau8f128b42014-11-28 15:07:47 +01002282 if (si_f->state == SI_ST_EST && obj_type(si_f->end) != OBJ_TYPE_APPCTX)
2283 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002284
Willy Tarreau8f128b42014-11-28 15:07:47 +01002285 if (si_b->state == SI_ST_EST && obj_type(si_b->end) != OBJ_TYPE_APPCTX)
2286 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002287
Willy Tarreau8f128b42014-11-28 15:07:47 +01002288 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2289 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2290 si_f->prev_state = si_f->state;
2291 si_b->prev_state = si_b->state;
2292 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2293 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002294
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002295 /* Trick: if a request is being waiting for the server to respond,
2296 * and if we know the server can timeout, we don't want the timeout
2297 * to expire on the client side first, but we're still interested
2298 * in passing data from the client to the server (eg: POST). Thus,
2299 * we can cancel the client's request timeout if the server's
2300 * request timeout is set and the server has not yet sent a response.
2301 */
2302
Willy Tarreau8f128b42014-11-28 15:07:47 +01002303 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2304 (tick_isset(req->wex) || tick_isset(res->rex))) {
2305 req->flags |= CF_READ_NOEXP;
2306 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002307 }
2308
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002309 /* When any of the stream interfaces is attached to an applet,
2310 * we have to call it here. Note that this one may wake the
2311 * task up again. If at least one applet was called, the current
2312 * task might have been woken up, in which case we don't want it
2313 * to be requeued to the wait queue but rather to the run queue
2314 * to run ASAP. The bitwise "or" in the condition ensures that
2315 * both functions are always called and that we wake up if at
2316 * least one did something.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002317 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002318 if ((si_applet_call(si_b) | si_applet_call(si_f)) != 0) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002319 if (task_in_rq(t)) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002320 t->expire = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02002321 stream_release_buffers(s);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002322 return t;
2323 }
2324 }
2325
Willy Tarreau798f4322012-11-08 14:49:17 +01002326 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002327 t->expire = tick_first(tick_first(req->rex, req->wex),
2328 tick_first(res->rex, res->wex));
2329 if (req->analysers)
2330 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002331
Willy Tarreau8f128b42014-11-28 15:07:47 +01002332 if (si_f->exp)
2333 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002334
Willy Tarreau8f128b42014-11-28 15:07:47 +01002335 if (si_b->exp)
2336 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002337
2338#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002339 fprintf(stderr,
2340 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2341 " 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 +01002342 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2343 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002344#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002345
2346#ifdef DEBUG_DEV
2347 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002348 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002349 ABORT_NOW();
2350#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002351 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002352 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002353 }
2354
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002355 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002356 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002357 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002358 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002359 if (sess->listener) {
2360 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002361 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002362 sess->listener->nbconn--;
2363 if (sess->listener->state == LI_FULL)
2364 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002365
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002366 /* Dequeues all of the listeners waiting for a resource */
2367 if (!LIST_ISEMPTY(&global_listener_queue))
2368 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002369
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002370 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2371 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2372 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002373 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002374
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002375 if (unlikely((global.mode & MODE_DEBUG) &&
2376 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002377 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002378 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002379 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2380 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002381 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002382 }
2383
2384 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002385 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002386
Willy Tarreaueee5b512015-04-03 23:46:31 +02002387 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002388 int n;
2389
Willy Tarreaueee5b512015-04-03 23:46:31 +02002390 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002391 if (n < 1 || n > 5)
2392 n = 0;
2393
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002394 if (sess->fe->mode == PR_MODE_HTTP) {
2395 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002396 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002397 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002398 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002399 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002400 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002401 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002402 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002403 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002404 s->be->be_counters.p.http.comp_rsp++;
2405 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002406 }
2407
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002408 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002409 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002410 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002411 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002412 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002413 }
2414
Willy Tarreau87b09662015-04-03 00:22:06 +02002415 /* update time stats for this stream */
2416 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002417
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002418 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002419 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002420 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002421 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002422 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002423}
2424
Willy Tarreau87b09662015-04-03 00:22:06 +02002425/* Update the stream's backend and server time stats */
2426void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002427{
2428 int t_request;
2429 int t_queue;
2430 int t_connect;
2431 int t_data;
2432 int t_close;
2433 struct server *srv;
2434
2435 t_request = 0;
2436 t_queue = s->logs.t_queue;
2437 t_connect = s->logs.t_connect;
2438 t_close = s->logs.t_close;
2439 t_data = s->logs.t_data;
2440
2441 if (s->be->mode != PR_MODE_HTTP)
2442 t_data = t_connect;
2443
2444 if (t_connect < 0 || t_data < 0)
2445 return;
2446
2447 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2448 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2449
2450 t_data -= t_connect;
2451 t_connect -= t_queue;
2452 t_queue -= t_request;
2453
2454 srv = objt_server(s->target);
2455 if (srv) {
2456 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2457 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2458 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2459 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2460 }
2461 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2462 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2463 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2464 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2465}
2466
Willy Tarreau7c669d72008-06-20 15:04:11 +02002467/*
2468 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002469 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002470 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002471 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002472 * server.
2473 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002474void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002475{
2476 if (sess->srv_conn == newsrv)
2477 return;
2478
2479 if (sess->srv_conn) {
2480 sess->srv_conn->served--;
2481 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2482 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002483 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002484 }
2485
2486 if (newsrv) {
2487 newsrv->served++;
2488 if (newsrv->proxy->lbprm.server_take_conn)
2489 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002490 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002491 }
2492}
2493
Willy Tarreau84455332009-03-15 22:34:05 +01002494/* Handle server-side errors for default protocols. It is called whenever a a
2495 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002496 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002497 * them. It's installed as ->srv_error for the server-side stream_interface.
2498 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002499void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002500{
2501 int err_type = si->err_type;
2502 int err = 0, fin = 0;
2503
2504 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002505 err = SF_ERR_CLICL;
2506 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002507 }
2508 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002509 err = SF_ERR_CLICL;
2510 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002511 }
2512 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002513 err = SF_ERR_SRVTO;
2514 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002515 }
2516 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002517 err = SF_ERR_SRVCL;
2518 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002519 }
2520 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002521 err = SF_ERR_SRVTO;
2522 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002523 }
2524 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002525 err = SF_ERR_SRVCL;
2526 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002527 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002528 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002529 err = SF_ERR_RESOURCE;
2530 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002531 }
Willy Tarreau84455332009-03-15 22:34:05 +01002532 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002533 err = SF_ERR_INTERNAL;
2534 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002535 }
2536
Willy Tarreaue7dff022015-04-03 01:14:29 +02002537 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002538 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002539 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002540 s->flags |= fin;
2541}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002542
Willy Tarreaue7dff022015-04-03 01:14:29 +02002543/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002544void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002545{
Willy Tarreau87b09662015-04-03 00:22:06 +02002546 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002547 return;
2548
Willy Tarreau87b09662015-04-03 00:22:06 +02002549 channel_shutw_now(&stream->req);
2550 channel_shutr_now(&stream->res);
2551 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002552 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002553 stream->flags |= why;
2554 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002555}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002556
Willy Tarreau8b22a712010-06-18 17:46:06 +02002557/************************************************************************/
2558/* All supported ACL keywords must be declared here. */
2559/************************************************************************/
2560
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002561/* Returns a pointer to a stkctr depending on the fetch keyword name.
2562 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002563 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002564 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002565 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002566 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002567 * NULL may be returned if the designated stkctr is not tracked. For
2568 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2569 * passed. When present, the currently tracked key is then looked up
2570 * in the specified table instead of the current table. The purpose is
2571 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002572 * multiple tables). <strm> is allowed to be NULL, in which case only
2573 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002574 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002575struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002576smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002577{
2578 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002579 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002580 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002581 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002582 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002583
Willy Tarreau0f791d42013-07-23 19:56:43 +02002584 if (num == '_' - '0') {
2585 /* sc_* variant, args[0] = ctr# (mandatory) */
2586 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002587 if (num >= MAX_SESS_STKCTR)
2588 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002589 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002590 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002591 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002592 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002593
2594 if (!conn)
2595 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002596
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002597 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002598 if (!key)
2599 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002600
Willy Tarreaua65536c2013-07-22 22:40:11 +02002601 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002602 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002603 return &stkctr;
2604 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002605
2606 /* Here, <num> contains the counter number from 0 to 9 for
2607 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002608 * args[arg] is the first optional argument. We first lookup the
2609 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002610 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002611
2612 stkptr = &strm->stkctr[num];
2613 if (!strm || !stkctr_entry(stkptr)) {
2614 stkptr = &sess->stkctr[num];
2615 if (!stkctr_entry(stkptr))
2616 return NULL;
2617 }
2618
2619 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002620 if (!stksess)
2621 return NULL;
2622
Willy Tarreau0f791d42013-07-23 19:56:43 +02002623 if (unlikely(args[arg].type == ARGT_TAB)) {
2624 /* an alternate table was specified, let's look up the same key there */
2625 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002626 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002627 return &stkctr;
2628 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002629 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002630}
2631
Willy Tarreau87b09662015-04-03 00:22:06 +02002632/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002633 * currently being tracked or not.
2634 * Supports being called as "sc[0-9]_tracked" only.
2635 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002636static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002637smp_fetch_sc_tracked(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02002638 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002639{
2640 smp->flags = SMP_F_VOL_TEST;
2641 smp->type = SMP_T_BOOL;
Willy Tarreau192252e2015-04-04 01:47:55 +02002642 smp->data.uint = !!smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002643 return 1;
2644}
2645
Willy Tarreau87b09662015-04-03 00:22:06 +02002646/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002647 * frontend counters or from the src.
2648 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2649 * zero is returned if the key is new.
2650 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002651static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002652smp_fetch_sc_get_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002653 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002654{
Willy Tarreau192252e2015-04-04 01:47:55 +02002655 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002656
2657 if (!stkctr)
2658 return 0;
2659
Willy Tarreau37406352012-04-23 16:16:37 +02002660 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002661 smp->type = SMP_T_UINT;
2662 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002663
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002664 if (stkctr_entry(stkctr) != NULL) {
2665 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002666 if (!ptr)
2667 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002668 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002669 }
2670 return 1;
2671}
2672
Willy Tarreau87b09662015-04-03 00:22:06 +02002673/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002674 * tracked frontend counters or from the src.
2675 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2676 * Value zero is returned if the key is new.
2677 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002678static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002679smp_fetch_sc_gpc0_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002680 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002681{
Willy Tarreau192252e2015-04-04 01:47:55 +02002682 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002683
2684 if (!stkctr)
2685 return 0;
2686
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002687 smp->flags = SMP_F_VOL_TEST;
2688 smp->type = SMP_T_UINT;
2689 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002690 if (stkctr_entry(stkctr) != NULL) {
2691 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002692 if (!ptr)
2693 return 0; /* parameter not stored */
2694 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002695 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002696 }
2697 return 1;
2698}
2699
Willy Tarreau87b09662015-04-03 00:22:06 +02002700/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002701 * frontend counters and return it into temp integer.
2702 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002703 */
2704static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002705smp_fetch_sc_inc_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002706 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002707{
Willy Tarreau192252e2015-04-04 01:47:55 +02002708 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002709
2710 if (!stkctr)
2711 return 0;
2712
Willy Tarreau37406352012-04-23 16:16:37 +02002713 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002714 smp->type = SMP_T_UINT;
2715 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002716 if (stkctr_entry(stkctr) != NULL) {
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002717 void *ptr;
2718
2719 /* First, update gpc0_rate if it's tracked. Second, update its
2720 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2721 */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002722 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002723 if (ptr) {
2724 update_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002725 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002726 smp->data.uint = (&stktable_data_cast(ptr, gpc0_rate))->curr_ctr;
2727 }
2728
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002729 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002730 if (ptr)
2731 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
2732
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002733 }
2734 return 1;
2735}
2736
Willy Tarreau87b09662015-04-03 00:22:06 +02002737/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002738 * frontend counters and return its previous value into temp integer.
2739 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002740 */
2741static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002742smp_fetch_sc_clr_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002743 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002744{
Willy Tarreau192252e2015-04-04 01:47:55 +02002745 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002746
2747 if (!stkctr)
2748 return 0;
2749
Willy Tarreau37406352012-04-23 16:16:37 +02002750 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002751 smp->type = SMP_T_UINT;
2752 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002753 if (stkctr_entry(stkctr) != NULL) {
2754 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002755 if (!ptr)
2756 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002757 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002758 stktable_data_cast(ptr, gpc0) = 0;
2759 }
2760 return 1;
2761}
2762
Willy Tarreau87b09662015-04-03 00:22:06 +02002763/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002764 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2765 * "src_conn_cnt" only.
2766 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002767static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002768smp_fetch_sc_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002769 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002770{
Willy Tarreau192252e2015-04-04 01:47:55 +02002771 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002772
2773 if (!stkctr)
2774 return 0;
2775
Willy Tarreau37406352012-04-23 16:16:37 +02002776 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002777 smp->type = SMP_T_UINT;
2778 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002779 if (stkctr_entry(stkctr) != NULL) {
2780 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002781 if (!ptr)
2782 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002783 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002784 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002785 return 1;
2786}
2787
Willy Tarreau87b09662015-04-03 00:22:06 +02002788/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002789 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2790 * only.
2791 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002792static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002793smp_fetch_sc_conn_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002794 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002795{
Willy Tarreau192252e2015-04-04 01:47:55 +02002796 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002797
2798 if (!stkctr)
2799 return 0;
2800
Willy Tarreau37406352012-04-23 16:16:37 +02002801 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002802 smp->type = SMP_T_UINT;
2803 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002804 if (stkctr_entry(stkctr) != NULL) {
2805 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002806 if (!ptr)
2807 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002808 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002809 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002810 }
2811 return 1;
2812}
2813
Willy Tarreau87b09662015-04-03 00:22:06 +02002814/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002815 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002816 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002817 */
2818static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002819smp_fetch_src_updt_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002820 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002821{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002822 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002823 struct stksess *ts;
2824 struct stktable_key *key;
2825 void *ptr;
2826
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002827 if (!conn)
2828 return 0;
2829
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002830 key = addr_to_stktable_key(&conn->addr.from, px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002831 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002832 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002833
Willy Tarreau24e32d82012-04-23 23:55:44 +02002834 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002835
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002836 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2837 /* entry does not exist and could not be created */
2838 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002839
2840 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2841 if (!ptr)
2842 return 0; /* parameter not stored in this table */
2843
Willy Tarreauf853c462012-04-23 18:53:56 +02002844 smp->type = SMP_T_UINT;
2845 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002846 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002847 return 1;
2848}
2849
Willy Tarreau87b09662015-04-03 00:22:06 +02002850/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002851 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2852 * "src_conn_cur" only.
2853 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002854static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002855smp_fetch_sc_conn_cur(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002856 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002857{
Willy Tarreau192252e2015-04-04 01:47:55 +02002858 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002859
2860 if (!stkctr)
2861 return 0;
2862
Willy Tarreau37406352012-04-23 16:16:37 +02002863 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002864 smp->type = SMP_T_UINT;
2865 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002866 if (stkctr_entry(stkctr) != NULL) {
2867 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002868 if (!ptr)
2869 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002870 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002871 }
2872 return 1;
2873}
2874
Willy Tarreau87b09662015-04-03 00:22:06 +02002875/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02002876 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2877 * "src_sess_cnt" only.
2878 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002879static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002880smp_fetch_sc_sess_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002881 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002882{
Willy Tarreau192252e2015-04-04 01:47:55 +02002883 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02002884
2885 if (!stkctr)
2886 return 0;
2887
Willy Tarreau37406352012-04-23 16:16:37 +02002888 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002889 smp->type = SMP_T_UINT;
2890 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002891 if (stkctr_entry(stkctr) != NULL) {
2892 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002893 if (!ptr)
2894 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002895 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002896 }
2897 return 1;
2898}
2899
Willy Tarreau87b09662015-04-03 00:22:06 +02002900/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002901 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2902 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002903static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002904smp_fetch_sc_sess_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002905 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002906{
Willy Tarreau192252e2015-04-04 01:47:55 +02002907 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002908
2909 if (!stkctr)
2910 return 0;
2911
Willy Tarreau37406352012-04-23 16:16:37 +02002912 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002913 smp->type = SMP_T_UINT;
2914 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002915 if (stkctr_entry(stkctr) != NULL) {
2916 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002917 if (!ptr)
2918 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002919 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002920 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002921 }
2922 return 1;
2923}
2924
Willy Tarreau87b09662015-04-03 00:22:06 +02002925/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02002926 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2927 * "src_http_req_cnt" only.
2928 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002929static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002930smp_fetch_sc_http_req_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002931 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002932{
Willy Tarreau192252e2015-04-04 01:47:55 +02002933 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02002934
2935 if (!stkctr)
2936 return 0;
2937
Willy Tarreau37406352012-04-23 16:16:37 +02002938 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002939 smp->type = SMP_T_UINT;
2940 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002941 if (stkctr_entry(stkctr) != NULL) {
2942 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002943 if (!ptr)
2944 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002945 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002946 }
2947 return 1;
2948}
2949
Willy Tarreau87b09662015-04-03 00:22:06 +02002950/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02002951 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2952 * "src_http_req_rate" only.
2953 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002954static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002955smp_fetch_sc_http_req_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002956 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002957{
Willy Tarreau192252e2015-04-04 01:47:55 +02002958 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02002959
2960 if (!stkctr)
2961 return 0;
2962
Willy Tarreau37406352012-04-23 16:16:37 +02002963 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002964 smp->type = SMP_T_UINT;
2965 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002966 if (stkctr_entry(stkctr) != NULL) {
2967 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002968 if (!ptr)
2969 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002970 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02002971 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002972 }
2973 return 1;
2974}
2975
Willy Tarreau87b09662015-04-03 00:22:06 +02002976/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02002977 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2978 * "src_http_err_cnt" only.
2979 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002980static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002981smp_fetch_sc_http_err_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002982 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002983{
Willy Tarreau192252e2015-04-04 01:47:55 +02002984 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02002985
2986 if (!stkctr)
2987 return 0;
2988
Willy Tarreau37406352012-04-23 16:16:37 +02002989 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002990 smp->type = SMP_T_UINT;
2991 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002992 if (stkctr_entry(stkctr) != NULL) {
2993 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002994 if (!ptr)
2995 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002996 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002997 }
2998 return 1;
2999}
3000
Willy Tarreau87b09662015-04-03 00:22:06 +02003001/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003002 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3003 * "src_http_err_rate" only.
3004 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003005static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003006smp_fetch_sc_http_err_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003007 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003008{
Willy Tarreau192252e2015-04-04 01:47:55 +02003009 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003010
3011 if (!stkctr)
3012 return 0;
3013
Willy Tarreau37406352012-04-23 16:16:37 +02003014 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003015 smp->type = SMP_T_UINT;
3016 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003017 if (stkctr_entry(stkctr) != NULL) {
3018 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003019 if (!ptr)
3020 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003021 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003022 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003023 }
3024 return 1;
3025}
3026
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003027/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003028 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003029 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3030 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003031static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003032smp_fetch_sc_kbytes_in(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003033 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003034{
Willy Tarreau192252e2015-04-04 01:47:55 +02003035 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003036
3037 if (!stkctr)
3038 return 0;
3039
Willy Tarreau37406352012-04-23 16:16:37 +02003040 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003041 smp->type = SMP_T_UINT;
3042 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003043 if (stkctr_entry(stkctr) != NULL) {
3044 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003045 if (!ptr)
3046 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003047 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003048 }
3049 return 1;
3050}
3051
Willy Tarreau613fe992013-07-23 17:39:19 +02003052/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003053 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003054 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003055 */
3056static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003057smp_fetch_sc_bytes_in_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003058 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003059{
Willy Tarreau192252e2015-04-04 01:47:55 +02003060 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003061
3062 if (!stkctr)
3063 return 0;
3064
Willy Tarreau37406352012-04-23 16:16:37 +02003065 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003066 smp->type = SMP_T_UINT;
3067 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003068 if (stkctr_entry(stkctr) != NULL) {
3069 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003070 if (!ptr)
3071 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003072 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003073 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003074 }
3075 return 1;
3076}
3077
Willy Tarreau53aea102013-07-23 17:39:02 +02003078/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003079 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003080 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3081 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003082static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003083smp_fetch_sc_kbytes_out(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02003084 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003085{
Willy Tarreau192252e2015-04-04 01:47:55 +02003086 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003087
3088 if (!stkctr)
3089 return 0;
3090
Willy Tarreau37406352012-04-23 16:16:37 +02003091 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003092 smp->type = SMP_T_UINT;
3093 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003094 if (stkctr_entry(stkctr) != NULL) {
3095 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003096 if (!ptr)
3097 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003098 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003099 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003100 return 1;
3101}
3102
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003103/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003104 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003105 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003106 */
3107static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003108smp_fetch_sc_bytes_out_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003109 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003110{
Willy Tarreau192252e2015-04-04 01:47:55 +02003111 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003112
3113 if (!stkctr)
3114 return 0;
3115
Willy Tarreau37406352012-04-23 16:16:37 +02003116 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003117 smp->type = SMP_T_UINT;
3118 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003119 if (stkctr_entry(stkctr) != NULL) {
3120 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003121 if (!ptr)
3122 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003123 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003124 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003125 }
3126 return 1;
3127}
3128
Willy Tarreau87b09662015-04-03 00:22:06 +02003129/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003130 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3131 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003132static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003133smp_fetch_sc_trackers(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003134 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003135{
Willy Tarreau192252e2015-04-04 01:47:55 +02003136 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003137
Willy Tarreau563eef42013-07-23 18:32:02 +02003138 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003139 return 0;
3140
Willy Tarreau563eef42013-07-23 18:32:02 +02003141 smp->flags = SMP_F_VOL_TEST;
3142 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003143 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003144 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003145}
3146
Willy Tarreau34db1082012-04-19 17:16:54 +02003147/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003148 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003149 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003150static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003151smp_fetch_table_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003152 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003153{
Willy Tarreau37406352012-04-23 16:16:37 +02003154 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003155 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003156 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003157 return 1;
3158}
3159
Willy Tarreau34db1082012-04-19 17:16:54 +02003160/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003161 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003162 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003163static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003164smp_fetch_table_avl(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003165 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003166{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003167 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003168 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003169 smp->type = SMP_T_UINT;
3170 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003171 return 1;
3172}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003173
Willy Tarreau61612d42012-04-19 18:42:05 +02003174/* Note: must not be declared <const> as its list will be overwritten.
3175 * Please take care of keeping this list alphabetically sorted.
3176 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003177static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003178 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003179}};
3180
Willy Tarreau281c7992013-01-08 01:23:27 +01003181/* Note: must not be declared <const> as its list will be overwritten.
3182 * Please take care of keeping this list alphabetically sorted.
3183 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003184static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003185 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3186 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3187 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3188 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3189 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3190 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3191 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3192 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3193 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3194 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3195 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3196 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3197 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3198 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3199 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3200 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3201 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3202 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3203 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3204 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3205 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3206 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3207 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3208 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3209 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3210 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3211 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3212 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3213 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3214 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3215 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3216 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3217 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3218 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3219 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3220 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3221 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3222 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3223 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3224 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3225 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3226 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3227 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3228 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3229 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3230 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3231 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3232 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3233 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3234 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3235 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3236 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3237 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3238 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3239 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3240 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3241 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3242 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3243 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3244 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3245 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3246 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3247 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3248 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3249 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3250 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3251 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3252 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3253 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3254 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3255 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3256 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3257 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3258 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3259 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3260 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3261 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3262 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3263 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3264 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3265 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3266 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3267 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3268 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3269 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3270 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3271 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3272 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3273 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3274 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3275 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3276 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3277 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3278 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3279 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3280 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003281 { /* END */ },
3282}};
3283
Willy Tarreau8b22a712010-06-18 17:46:06 +02003284__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003285static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003286{
Willy Tarreau281c7992013-01-08 01:23:27 +01003287 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003288 acl_register_keywords(&acl_kws);
3289}
3290
Willy Tarreaubaaee002006-06-26 02:48:02 +02003291/*
3292 * Local variables:
3293 * c-indent-level: 8
3294 * c-basic-offset: 8
3295 * End:
3296 */