blob: 3e2b5b663aa27a16b4d444104514d3ea038c913f [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002 * Stream management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreaud28c3532012-04-19 19:28:33 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <stdlib.h>
Willy Tarreau81f9aa32010-06-01 17:45:26 +020014#include <unistd.h>
15#include <fcntl.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020018#include <common/buffer.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020019#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010023#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020025#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020026#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010027#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020028#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010029#include <proto/checks.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020030#include <proto/connection.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020031#include <proto/dumpstats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020032#include <proto/fd.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020033#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020034#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010035#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010036#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020037#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020038#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020039#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020040#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020041#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010042#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010043#include <proto/proto_http.h>
44#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020045#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020048#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010049#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010050#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010051#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052
Willy Tarreau87b09662015-04-03 00:22:06 +020053struct pool_head *pool2_stream;
54struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
Willy Tarreau87b09662015-04-03 00:22:06 +020056/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010057struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
58
Willy Tarreau9903f0e2015-04-04 18:50:31 +020059/* This function is called from the session handler which detects the end of
Willy Tarreau87b09662015-04-03 00:22:06 +020060 * handshake, in order to complete initialization of a valid stream. It must
Willy Tarreau02d86382015-04-05 12:00:52 +020061 * be called with an embryonic session. It returns the pointer to the newly
62 * created stream, or NULL in case of fatal error. For now the client-side
63 * end point is taken from the session's origin, which must be valid.
Willy Tarreau2542b532012-08-31 16:01:23 +020064 */
Willy Tarreau02d86382015-04-05 12:00:52 +020065struct stream *stream_new(struct session *sess, struct task *t)
Willy Tarreau2542b532012-08-31 16:01:23 +020066{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020067 struct stream *s;
Willy Tarreaufb0afa72015-04-03 14:46:27 +020068 struct listener *l = sess->listener;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020069 struct proxy *p = sess->fe;
Willy Tarreauf2b98742015-04-05 01:30:42 +020070 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau470280e2015-04-05 01:33:13 +020071 struct appctx *appctx = objt_appctx(sess->origin);
Willy Tarreau20d46a52012-12-09 15:55:40 +010072 int i;
Willy Tarreau2542b532012-08-31 16:01:23 +020073
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020074 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020075 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020076
77 /* minimum stream initialization required for an embryonic stream is
78 * fairly low. We need very little to execute L4 ACLs, then we need a
79 * task to make the client-side connection live on its own.
80 * - flags
81 * - stick-entry tracking
82 */
83 s->flags = 0;
84 s->logs.logwait = p->to_log;
85 s->logs.level = 0;
86
87 /* Initialise the current rule list pointer to NULL. We are sure that
88 * any rulelist match the NULL pointer.
89 */
90 s->current_rule_list = NULL;
91
92 memset(s->stkctr, 0, sizeof(s->stkctr));
93
94 s->sess = sess;
95 s->si[0].flags = SI_FL_NONE;
96 s->si[1].flags = SI_FL_ISBACK;
97
98 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
99 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
100 s->uniq_id = global.req_count++;
101
Willy Tarreau87b09662015-04-03 00:22:06 +0200102 /* OK, we're keeping the stream, so let's properly initialize the stream */
103 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200104 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100105 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200106
Willy Tarreaue7dff022015-04-03 01:14:29 +0200107 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200108 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200109
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200110 s->task = t;
Willy Tarreau2542b532012-08-31 16:01:23 +0200111 t->process = l->handler;
112 t->context = s;
113 t->expire = TICK_ETERNITY;
114
Willy Tarreau87b09662015-04-03 00:22:06 +0200115 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200116 * This changes later when switching rules are executed or
117 * when the default backend is assigned.
118 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200119 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200120 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100121 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200122 s->req_cap = NULL;
123 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200124
Willy Tarreau87b09662015-04-03 00:22:06 +0200125 /* Let's count a stream now */
Willy Tarreaub36b4242010-06-04 20:59:39 +0200126 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200127
Willy Tarreaub4c84932013-07-23 19:15:30 +0200128 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200129 void *ptr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200130 struct stkctr *stkctr = &sess->stkctr[i];
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200131
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200132 if (!stkctr_entry(stkctr))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100133 continue;
134
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200135 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200136 if (ptr)
137 stktable_data_cast(ptr, sess_cnt)++;
138
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200139 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200140 if (ptr)
141 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200142 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200143 }
144
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200145 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100146 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200147 si_set_state(&s->si[0], SI_ST_EST);
148
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200149 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200150 if (conn)
151 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200152 else if (appctx)
153 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200154
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200155 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200156 s->si[0].flags |= SI_FL_INDEP_STR;
157
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200158 /* pre-initialize the other side's stream interface to an INIT state. The
159 * callbacks will be initialized before attempting to connect.
160 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100161 si_reset(&s->si[1]);
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200162 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200163
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200164 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200165 s->si[1].flags |= SI_FL_INDEP_STR;
166
Willy Tarreau87b09662015-04-03 00:22:06 +0200167 stream_init_srv_conn(s);
Willy Tarreau10b688f2015-03-13 16:43:12 +0100168 s->target = l->default_target; /* used by peers and CLI */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200169 s->pend_pos = NULL;
170
171 /* init store persistence */
172 s->store_count = 0;
173
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100174 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100175 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200176
177 /* activate default analysers enabled for this listener */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100178 s->req.analysers = l->analysers;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200179
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100180 s->req.wto = TICK_ETERNITY;
181 s->req.rto = TICK_ETERNITY;
182 s->req.rex = TICK_ETERNITY;
183 s->req.wex = TICK_ETERNITY;
184 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200185
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100186 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100187 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100188 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200189
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200190 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100191 s->req.flags |= CF_NEVER_WAIT;
192 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200193 }
194
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100195 s->res.rto = TICK_ETERNITY;
196 s->res.wto = TICK_ETERNITY;
197 s->res.rex = TICK_ETERNITY;
198 s->res.wex = TICK_ETERNITY;
199 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200200
Willy Tarreaueee5b512015-04-03 23:46:31 +0200201 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100202
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100203 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100204
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200205 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200206 if (conn)
207 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200208 else if (appctx)
209 s->si[0].flags |= SI_FL_WAIT_DATA;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200210
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200211 if (conn && p->accept && p->accept(s) < 0)
212 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200213
Willy Tarreauf2b98742015-04-05 01:30:42 +0200214 if (conn) {
215 /* if logs require transport layer information, note it on the connection */
216 if (s->logs.logwait & LW_XPRT)
217 conn->flags |= CO_FL_XPRT_TRACKED;
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200218
Willy Tarreauf2b98742015-04-05 01:30:42 +0200219 /* we want the connection handler to notify the stream interface about updates. */
220 conn->flags |= CO_FL_WAKE_DATA;
221 }
Willy Tarreau2542b532012-08-31 16:01:23 +0200222
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200223 /* it is important not to call the wakeup function directly but to
224 * pass through task_wakeup(), because this one knows how to apply
225 * priorities to tasks.
226 */
227 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200228 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200229
230 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200231 out_fail_accept:
Willy Tarreau3b246412014-11-25 17:10:33 +0100232 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200233 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200234 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200235}
236
Willy Tarreaubaaee002006-06-26 02:48:02 +0200237/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200238 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200239 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200240static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200242 struct session *sess = strm_sess(s);
243 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100244 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200245 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200246 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100247
Willy Tarreaubaaee002006-06-26 02:48:02 +0200248 if (s->pend_pos)
249 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100250
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100251 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200252 if (s->flags & SF_CURR_SESS) {
253 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100254 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100255 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100256 if (may_dequeue_tasks(objt_server(s->target), s->be))
257 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100258 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100259
Willy Tarreau7c669d72008-06-20 15:04:11 +0200260 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200261 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200262 * it should normally be only the same as the one above,
263 * so this should not happen in fact.
264 */
265 sess_change_server(s, NULL);
266 }
267
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100268 if (s->req.pipe)
269 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100270
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100271 if (s->res.pipe)
272 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100273
Willy Tarreaubf883e02014-11-25 21:10:35 +0100274 /* We may still be present in the buffer wait queue */
275 if (!LIST_ISEMPTY(&s->buffer_wait)) {
276 LIST_DEL(&s->buffer_wait);
277 LIST_INIT(&s->buffer_wait);
278 }
279
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100280 b_drop(&s->req.buf);
281 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100282 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200283 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200284
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100285 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200286 if (s->txn)
287 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100288
Willy Tarreau1e954912012-10-12 17:50:05 +0200289 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200290 if (cli_conn)
291 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200292
Willy Tarreaua4cda672010-06-06 18:28:49 +0200293 for (i = 0; i < s->store_count; i++) {
294 if (!s->store[i].ts)
295 continue;
296 stksess_free(s->store[i].table, s->store[i].ts);
297 s->store[i].ts = NULL;
298 }
299
Willy Tarreaueee5b512015-04-03 23:46:31 +0200300 if (s->txn) {
301 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
302 pool_free2(pool2_http_txn, s->txn);
303 s->txn = NULL;
304 }
305
Willy Tarreau92fb9832007-10-16 17:34:28 +0200306 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200307 pool_free2(fe->rsp_cap_pool, s->res_cap);
308 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200309 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100310
Willy Tarreau87b09662015-04-03 00:22:06 +0200311 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200312
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100313 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100314 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200315 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100316 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100317 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100318 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200319 if (s->list.n != &streams)
320 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100321 bref->ref = s->list.n;
322 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100323 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200324 si_release_endpoint(&s->si[1]);
325 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200326
327 /* FIXME: for now we have a 1:1 relation between stream and session so
328 * the stream must free the session.
329 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200330 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200331 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200332
333 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200334 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200335 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200336 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200337 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200338 pool_flush2(pool2_requri);
339 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200340 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200341 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100342 pool_flush2(pool2_connection);
343 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200344 pool_flush2(fe->req_cap_pool);
345 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200346 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200347}
348
Willy Tarreau78955f42014-12-28 13:09:02 +0100349/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
350 * that it's not the last available buffer or it's the response buffer. Unless
351 * the buffer is the response buffer, an extra control is made so that we always
352 * keep <tune.buffers.reserved> buffers available after this allocation. To be
353 * called at the beginning of recv() callbacks to ensure that the required
354 * buffers are properly allocated. Returns 0 in case of failure, non-zero
355 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100356 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200357int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100358{
Willy Tarreau87b09662015-04-03 00:22:06 +0200359 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100360 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100361 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100362
Willy Tarreau78955f42014-12-28 13:09:02 +0100363 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100364 margin = global.tune.reserved_bufs;
365
Willy Tarreau78955f42014-12-28 13:09:02 +0100366 s = chn_sess(chn);
367
368 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100369 if (b)
370 return 1;
371
Willy Tarreaubf883e02014-11-25 21:10:35 +0100372 if (LIST_ISEMPTY(&s->buffer_wait))
373 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100374 return 0;
375}
376
Willy Tarreau87b09662015-04-03 00:22:06 +0200377/* Allocates a work buffer for stream <s>. It is meant to be called inside
378 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaua24adf02014-11-27 01:11:56 +0100379 * to work fine. For a regular connection, only the response is needed so that
380 * an error message may be built and returned. In case where the initiator is
381 * an applet (eg: peers), then we need to allocate the request buffer for the
382 * applet to be able to send its data (in this case a response is not needed).
383 * Request buffers are never picked from the reserved pool, but response
384 * buffers may be allocated from the reserve. This is critical to ensure that
385 * a response may always flow and will never block a server from releasing a
386 * connection. Returns 0 in case of failure, non-zero otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100387 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200388int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100389{
Willy Tarreaua24adf02014-11-27 01:11:56 +0100390 int margin;
391 struct buffer **buf;
392
393 if (objt_appctx(s->si[0].end)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100394 buf = &s->req.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100395 margin = global.tune.reserved_bufs;
396 }
397 else {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100398 buf = &s->res.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100399 margin = 0;
400 }
401
Willy Tarreaubf883e02014-11-25 21:10:35 +0100402 if (!LIST_ISEMPTY(&s->buffer_wait)) {
403 LIST_DEL(&s->buffer_wait);
404 LIST_INIT(&s->buffer_wait);
405 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100406
Willy Tarreaua24adf02014-11-27 01:11:56 +0100407 if (b_alloc_margin(buf, margin))
Willy Tarreau656859d2014-11-25 19:46:36 +0100408 return 1;
409
Willy Tarreaubf883e02014-11-25 21:10:35 +0100410 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100411 return 0;
412}
413
414/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100415 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200416 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100417 * a single buffer, so it makes sense to try to wake two up when two buffers
418 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100419 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200420void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100421{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100422 if (s->req.buf->size && buffer_empty(s->req.buf))
423 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100424
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100425 if (s->res.buf->size && buffer_empty(s->res.buf))
426 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100427
Willy Tarreaubf883e02014-11-25 21:10:35 +0100428 /* if we're certain to have at least 1 buffer available, and there is
429 * someone waiting, we can wake up a waiter and offer them.
430 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100431 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200432 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100433}
434
Willy Tarreau87b09662015-04-03 00:22:06 +0200435/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100436 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200437 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100438 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200439void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100440{
Willy Tarreau87b09662015-04-03 00:22:06 +0200441 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100442
443 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100444 if (rqlimit <= run_queue)
445 break;
446
Willy Tarreaubf883e02014-11-25 21:10:35 +0100447 if (sess->task->state & TASK_RUNNING)
448 continue;
449
450 LIST_DEL(&sess->buffer_wait);
451 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100452 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100453 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100454}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200455
456/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200457int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200458{
Willy Tarreau87b09662015-04-03 00:22:06 +0200459 LIST_INIT(&streams);
460 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
461 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200462}
463
Willy Tarreau87b09662015-04-03 00:22:06 +0200464void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100465{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200466 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100467 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100468 void *ptr;
469 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100470
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100471 bytes = s->req.total - s->logs.bytes_in;
472 s->logs.bytes_in = s->req.total;
473 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200474 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100475
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100476 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100477
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100478 if (objt_server(s->target))
479 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200480
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200481 if (sess->listener && sess->listener->counters)
482 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200483
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100484 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200485 struct stkctr *stkctr = &s->stkctr[i];
486
487 if (!stkctr_entry(stkctr)) {
488 stkctr = &sess->stkctr[i];
489 if (!stkctr_entry(stkctr))
490 continue;
491 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200492
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200493 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100494 if (ptr)
495 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200496
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200497 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100498 if (ptr)
499 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200500 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100501 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100502 }
503
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100504 bytes = s->res.total - s->logs.bytes_out;
505 s->logs.bytes_out = s->res.total;
506 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200507 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100508
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100509 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100510
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100511 if (objt_server(s->target))
512 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200513
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200514 if (sess->listener && sess->listener->counters)
515 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200516
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100517 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200518 struct stkctr *stkctr = &s->stkctr[i];
519
520 if (!stkctr_entry(stkctr)) {
521 stkctr = &sess->stkctr[i];
522 if (!stkctr_entry(stkctr))
523 continue;
524 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200525
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200526 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100527 if (ptr)
528 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200529
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200530 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100531 if (ptr)
532 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200533 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100534 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100535 }
536}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100538/* This function is called with (si->state == SI_ST_CON) meaning that a
539 * connection was attempted and that the file descriptor is already allocated.
540 * We must check for establishment, error and abort. Possible output states
541 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
542 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200543 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100544 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200545static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100546{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100547 struct stream_interface *si = &s->si[1];
548 struct channel *req = &s->req;
549 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200550 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100551
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100552 /* If we got an error, or if nothing happened and the connection timed
553 * out, we must give up. The CER state handler will take care of retry
554 * attempts and error reports.
555 */
556 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100557 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100558 /* Some data were sent past the connection establishment,
559 * so we need to pretend we're established to log correctly
560 * and let later states handle the failure.
561 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100562 si->state = SI_ST_EST;
563 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100564 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100565 return 1;
566 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100567 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100568 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100569
Willy Tarreauf79c8172013-10-21 16:30:56 +0200570 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100571
572 if (si->err_type)
573 return 0;
574
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100575 if (si->flags & SI_FL_ERR)
576 si->err_type = SI_ET_CONN_ERR;
577 else
578 si->err_type = SI_ET_CONN_TO;
579 return 0;
580 }
581
582 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100583 if (!(req->flags & CF_WRITE_PARTIAL) &&
584 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200585 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
586 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100587 s->be->options & PR_O_ABRT_CLOSE)))) {
588 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200589 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100590 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100591 if (s->srv_error)
592 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100593 return 1;
594 }
595
596 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200597 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100598 return 1;
599
600 /* OK, this means that a connection succeeded. The caller will be
601 * responsible for handling the transition from CON to EST.
602 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100603 si->state = SI_ST_EST;
604 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100605 return 1;
606}
607
608/* This function is called with (si->state == SI_ST_CER) meaning that a
609 * previous connection attempt has failed and that the file descriptor
610 * has already been released. Possible causes include asynchronous error
611 * notification and time out. Possible output states are SI_ST_CLO when
612 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
613 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
614 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
615 * marked as in error state. It returns 0.
616 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200617static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100618{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100619 struct stream_interface *si = &s->si[1];
620
Willy Tarreau87b09662015-04-03 00:22:06 +0200621 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100622 if (objt_server(s->target)) {
623 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100624
Willy Tarreaue7dff022015-04-03 01:14:29 +0200625 if (s->flags & SF_CURR_SESS) {
626 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100627 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100628 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100629 }
630
631 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200632 si->conn_retries--;
633 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634 if (!si->err_type) {
635 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100636 }
637
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100638 if (objt_server(s->target))
639 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100640 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100641 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100642 if (may_dequeue_tasks(objt_server(s->target), s->be))
643 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644
645 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200646 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100647 s->req.flags |= CF_WRITE_ERROR;
648 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100649
650 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100651 if (s->srv_error)
652 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100653 return 0;
654 }
655
656 /* If the "redispatch" option is set on the backend, we are allowed to
657 * retry on another server for the last retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200658 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100659 * bit to ignore any persistence cookie. We won't count a retry nor a
660 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200661 * If the connection is not persistent, the balancing algorithm is not
662 * determinist (round robin) and there is more than one active server,
663 * we accept to perform an immediate redispatch without waiting since
664 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100665 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200666 if (objt_server(s->target) &&
667 (si->conn_retries == 0 ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200668 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Willy Tarreau33a14e52014-06-13 17:49:40 +0200669 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200670 s->be->options & PR_O_REDISP && !(s->flags & SF_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100671 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100672 if (may_dequeue_tasks(objt_server(s->target), s->be))
673 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100674
Willy Tarreaue7dff022015-04-03 01:14:29 +0200675 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100676 si->state = SI_ST_REQ;
677 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100678 if (objt_server(s->target))
679 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100680 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681 si->state = SI_ST_ASS;
682 }
683
684 if (si->flags & SI_FL_ERR) {
685 /* The error was an asynchronous connection error, and we will
686 * likely have to retry connecting to the same server, most
687 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200688 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100689 */
690
Willy Tarreaub0290662014-06-13 17:04:44 +0200691 int delay = 1000;
692
693 if (s->be->timeout.connect && s->be->timeout.connect < delay)
694 delay = s->be->timeout.connect;
695
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100696 if (!si->err_type)
697 si->err_type = SI_ET_CONN_ERR;
698
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200699 /* only wait when we're retrying on the same server */
700 if (si->state == SI_ST_ASS ||
701 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
702 (s->be->srv_act <= 1)) {
703 si->state = SI_ST_TAR;
704 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
705 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100706 return 0;
707 }
708 return 0;
709}
710
711/*
712 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200713 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200714 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100715 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200716static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100717{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100718 struct stream_interface *si = &s->si[1];
719 struct channel *req = &s->req;
720 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100721
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100722 /* First, centralize the timers information */
723 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
724 si->exp = TICK_ETERNITY;
725
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100726 if (objt_server(s->target))
727 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100728
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100729 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100730 /* if the user wants to log as soon as possible, without counting
731 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200732 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100733 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100734 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100735 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736 }
737 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100738 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739 }
740
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200741 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200742 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200743 if (req->flags & CF_WAKE_CONNECT) {
744 req->flags |= CF_WAKE_ONCE;
745 req->flags &= ~CF_WAKE_CONNECT;
746 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200747 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200748 /* real connections have timeouts */
749 req->wto = s->be->timeout.server;
750 rep->rto = s->be->timeout.server;
751 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100752 req->wex = TICK_ETERNITY;
753}
754
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100755/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
756 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100757 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
758 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
759 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100760 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200761static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100762{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100763 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100764 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100765 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100766
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100767 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 +0100768 now_ms, __FUNCTION__,
769 s,
Willy Tarreau103197d2014-11-28 15:26:12 +0100770 req, s->rep,
771 req->rex, s->res.wex,
772 req->flags, s->res.flags,
773 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 +0100774
775 if (si->state == SI_ST_ASS) {
776 /* Server assigned to connection request, we have to try to connect now */
777 int conn_err;
778
779 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100780 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100781
Willy Tarreaue7dff022015-04-03 01:14:29 +0200782 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100783 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100784 if (srv)
785 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500786 if (srv)
787 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100788 return;
789 }
790
791 /* We have received a synchronous error. We might have to
792 * abort, retry immediately or redispatch.
793 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200794 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100795 if (!si->err_type) {
796 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100797 }
798
Willy Tarreau827aee92011-03-10 16:55:02 +0100799 if (srv)
800 srv_inc_sess_ctr(srv);
801 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500802 srv_set_sess_last(srv);
803 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100804 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100805 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100806
Willy Tarreau87b09662015-04-03 00:22:06 +0200807 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100808 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100809 if (may_dequeue_tasks(srv, s->be))
810 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100811
812 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200813 si_shutr(si);
814 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100815 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100816
817 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
818
Willy Tarreau87b09662015-04-03 00:22:06 +0200819 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100820 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100821 if (s->srv_error)
822 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100823 return;
824 }
825
826 /* We are facing a retryable error, but we don't want to run a
827 * turn-around now, as the problem is likely a source port
828 * allocation problem, so we want to retry now.
829 */
830 si->state = SI_ST_CER;
831 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100832 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100833 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
834 return;
835 }
836 else if (si->state == SI_ST_QUE) {
837 /* connection request was queued, check for any update */
838 if (!s->pend_pos) {
839 /* The connection is not in the queue anymore. Either
840 * we have a server connection slot available and we
841 * go directly to the assigned state, or we need to
842 * load-balance first and go to the INI state.
843 */
844 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200845 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100846 si->state = SI_ST_REQ;
847 else {
848 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
849 si->state = SI_ST_ASS;
850 }
851 return;
852 }
853
854 /* Connection request still in queue... */
855 if (si->flags & SI_FL_EXP) {
856 /* ... and timeout expired */
857 si->exp = TICK_ETERNITY;
858 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100859 if (srv)
860 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100861 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200862 si_shutr(si);
863 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100864 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100865 if (!si->err_type)
866 si->err_type = SI_ET_QUEUE_TO;
867 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100868 if (s->srv_error)
869 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100870 return;
871 }
872
873 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100874 if ((req->flags & (CF_READ_ERROR)) ||
875 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
876 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100877 /* give up */
878 si->exp = TICK_ETERNITY;
879 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200880 si_shutr(si);
881 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100882 si->err_type |= SI_ET_QUEUE_ABRT;
883 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100884 if (s->srv_error)
885 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100886 return;
887 }
888
889 /* Nothing changed */
890 return;
891 }
892 else if (si->state == SI_ST_TAR) {
893 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100894 if ((req->flags & (CF_READ_ERROR)) ||
895 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
896 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100897 /* give up */
898 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200899 si_shutr(si);
900 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100901 si->err_type |= SI_ET_CONN_ABRT;
902 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100903 if (s->srv_error)
904 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100905 return;
906 }
907
908 if (!(si->flags & SI_FL_EXP))
909 return; /* still in turn-around */
910
911 si->exp = TICK_ETERNITY;
912
Willy Tarreau87b09662015-04-03 00:22:06 +0200913 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100914 * marked "assigned".
915 * FIXME: Should we force a redispatch attempt when the server is down ?
916 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200917 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100918 si->state = SI_ST_ASS;
919 else
920 si->state = SI_ST_REQ;
921 return;
922 }
923}
924
Willy Tarreau87b09662015-04-03 00:22:06 +0200925/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900926 * also counts a failed request if the server state has not reached the request
927 * stage.
928 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200929static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900930{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200931 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900932 if (s->si[1].state < SI_ST_REQ) {
933
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200934 strm_fe(s)->fe_counters.failed_req++;
935 if (strm_li(s)->counters)
936 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900937
Willy Tarreaue7dff022015-04-03 01:14:29 +0200938 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900939 }
940 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200941 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900942 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200943 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900944 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200945 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900946 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200947 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900948 }
949}
950
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100951/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100952 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
953 * a real connection to a server, indicating that a server has been assigned,
954 * or SI_ST_EST for a successful connection to an applet. It may also return
955 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100956 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200957static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100958{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100959 struct stream_interface *si = &s->si[1];
960
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100961 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 +0100962 now_ms, __FUNCTION__,
963 s,
964 s->req, s->rep,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100965 s->req.rex, s->res.wex,
966 s->req.flags, s->res.flags,
Willy Tarreau350f4872014-11-28 14:42:25 +0100967 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 +0100968
969 if (si->state != SI_ST_REQ)
970 return;
971
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100972 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
973 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100974 struct appctx *appctx = objt_appctx(si->end);
975
976 if (!appctx || appctx->applet != __objt_applet(s->target))
977 appctx = stream_int_register_handler(si, objt_applet(s->target));
978
979 if (!appctx) {
980 /* No more memory, let's immediately abort. Force the
981 * error code to ignore the ERR_LOCAL which is not a
982 * real error.
983 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200984 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100985
986 si_shutr(si);
987 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100988 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +0100989 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100990 si->state = SI_ST_CLO;
991 if (s->srv_error)
992 s->srv_error(s, si);
993 return;
994 }
995
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100996 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100997 si->state = SI_ST_EST;
998 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +0200999 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001000 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001001 return;
1002 }
1003
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001004 /* Try to assign a server */
1005 if (srv_redispatch_connect(s) != 0) {
1006 /* We did not get a server. Either we queued the
1007 * connection request, or we encountered an error.
1008 */
1009 if (si->state == SI_ST_QUE)
1010 return;
1011
1012 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001013 si_shutr(si);
1014 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001015 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001016 if (!si->err_type)
1017 si->err_type = SI_ET_CONN_OTHER;
1018 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001019 if (s->srv_error)
1020 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001021 return;
1022 }
1023
1024 /* The server is assigned */
1025 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1026 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001027 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001028}
1029
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001030/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001031 * if appropriate. The default_backend rule is also considered, then the
1032 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001033 * It returns 1 if the processing can continue on next analysers, or zero if it
1034 * either needs more data or wants to immediately abort the request.
1035 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001036static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001037{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001038 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001039 struct session *sess = s->sess;
1040 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001041
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001042 req->analysers &= ~an_bit;
1043 req->analyse_exp = TICK_ETERNITY;
1044
Willy Tarreau87b09662015-04-03 00:22:06 +02001045 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 +02001046 now_ms, __FUNCTION__,
1047 s,
1048 req,
1049 req->rex, req->wex,
1050 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001051 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001052 req->analysers);
1053
1054 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001055 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001056 struct switching_rule *rule;
1057
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001058 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001059 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001060
Willy Tarreauf51658d2014-04-23 01:21:56 +02001061 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001062 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001063 ret = acl_pass(ret);
1064 if (rule->cond->pol == ACL_COND_UNLESS)
1065 ret = !ret;
1066 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001067
1068 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001069 /* If the backend name is dynamic, try to resolve the name.
1070 * If we can't resolve the name, or if any error occurs, break
1071 * the loop and fallback to the default backend.
1072 */
1073 struct proxy *backend;
1074
1075 if (rule->dynamic) {
1076 struct chunk *tmp = get_trash_chunk();
1077 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1078 break;
1079 backend = findproxy(tmp->str, PR_CAP_BE);
1080 if (!backend)
1081 break;
1082 }
1083 else
1084 backend = rule->be.backend;
1085
Willy Tarreau87b09662015-04-03 00:22:06 +02001086 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001087 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001088 break;
1089 }
1090 }
1091
1092 /* To ensure correct connection accounting on the backend, we
1093 * have to assign one if it was not set (eg: a listen). This
1094 * measure also takes care of correctly setting the default
1095 * backend if any.
1096 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001097 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001098 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001099 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001100 }
1101
Willy Tarreaufb356202010-08-03 14:02:05 +02001102 /* 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 +02001103 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001104 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1105 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001106 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001107
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001108 /* 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 +02001109 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001110 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001111 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001112 int ret = 1;
1113
1114 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001115 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 +01001116 ret = acl_pass(ret);
1117 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1118 ret = !ret;
1119 }
1120
1121 if (ret) {
1122 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001123 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001124 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001125 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001126 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001127 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001128 break;
1129 }
1130 }
1131
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001132 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001133
1134 sw_failed:
1135 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001136 channel_abort(&s->req);
1137 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001138
Willy Tarreaue7dff022015-04-03 01:14:29 +02001139 if (!(s->flags & SF_ERR_MASK))
1140 s->flags |= SF_ERR_RESOURCE;
1141 if (!(s->flags & SF_FINST_MASK))
1142 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001143
Willy Tarreaueee5b512015-04-03 23:46:31 +02001144 if (s->txn)
1145 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001146 s->req.analysers = 0;
1147 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001148 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001149}
1150
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001151/* This stream analyser works on a request. It applies all use-server rules on
1152 * it then returns 1. The data must already be present in the buffer otherwise
1153 * they won't match. It always returns 1.
1154 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001155static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001156{
1157 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001158 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001159 struct server_rule *rule;
1160
Willy Tarreau87b09662015-04-03 00:22:06 +02001161 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 +02001162 now_ms, __FUNCTION__,
1163 s,
1164 req,
1165 req->rex, req->wex,
1166 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001167 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001168 req->analysers);
1169
Willy Tarreaue7dff022015-04-03 01:14:29 +02001170 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001171 list_for_each_entry(rule, &px->server_rules, list) {
1172 int ret;
1173
Willy Tarreau192252e2015-04-04 01:47:55 +02001174 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001175 ret = acl_pass(ret);
1176 if (rule->cond->pol == ACL_COND_UNLESS)
1177 ret = !ret;
1178
1179 if (ret) {
1180 struct server *srv = rule->srv.ptr;
1181
Willy Tarreau892337c2014-05-13 23:41:20 +02001182 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001183 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001184 (s->flags & SF_FORCE_PRST)) {
1185 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001186 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001187 break;
1188 }
1189 /* if the server is not UP, let's go on with next rules
1190 * just in case another one is suited.
1191 */
1192 }
1193 }
1194 }
1195
1196 req->analysers &= ~an_bit;
1197 req->analyse_exp = TICK_ETERNITY;
1198 return 1;
1199}
1200
Emeric Brun1d33b292010-01-04 15:47:17 +01001201/* This stream analyser works on a request. It applies all sticking rules on
1202 * it then returns 1. The data must already be present in the buffer otherwise
1203 * they won't match. It always returns 1.
1204 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001205static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001206{
1207 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001208 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001209 struct sticking_rule *rule;
1210
Willy Tarreau87b09662015-04-03 00:22:06 +02001211 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 +01001212 now_ms, __FUNCTION__,
1213 s,
1214 req,
1215 req->rex, req->wex,
1216 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001217 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001218 req->analysers);
1219
1220 list_for_each_entry(rule, &px->sticking_rules, list) {
1221 int ret = 1 ;
1222 int i;
1223
Willy Tarreau9667a802013-12-09 12:52:13 +01001224 /* Only the first stick store-request of each table is applied
1225 * and other ones are ignored. The purpose is to allow complex
1226 * configurations which look for multiple entries by decreasing
1227 * order of precision and to stop at the first which matches.
1228 * An example could be a store of the IP address from an HTTP
1229 * header first, then from the source if not found.
1230 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001231 for (i = 0; i < s->store_count; i++) {
1232 if (rule->table.t == s->store[i].table)
1233 break;
1234 }
1235
1236 if (i != s->store_count)
1237 continue;
1238
1239 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001240 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001241 ret = acl_pass(ret);
1242 if (rule->cond->pol == ACL_COND_UNLESS)
1243 ret = !ret;
1244 }
1245
1246 if (ret) {
1247 struct stktable_key *key;
1248
Willy Tarreau192252e2015-04-04 01:47:55 +02001249 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 +01001250 if (!key)
1251 continue;
1252
1253 if (rule->flags & STK_IS_MATCH) {
1254 struct stksess *ts;
1255
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001256 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001257 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001258 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001259 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001260
1261 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001262 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1263 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001264 if (node) {
1265 struct server *srv;
1266
1267 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001268 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001269 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001270 (s->flags & SF_FORCE_PRST)) {
1271 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001272 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001273 }
1274 }
1275 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001276 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001277 }
1278 }
1279 if (rule->flags & STK_IS_STORE) {
1280 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1281 struct stksess *ts;
1282
1283 ts = stksess_new(rule->table.t, key);
1284 if (ts) {
1285 s->store[s->store_count].table = rule->table.t;
1286 s->store[s->store_count++].ts = ts;
1287 }
1288 }
1289 }
1290 }
1291 }
1292
1293 req->analysers &= ~an_bit;
1294 req->analyse_exp = TICK_ETERNITY;
1295 return 1;
1296}
1297
1298/* This stream analyser works on a response. It applies all store rules on it
1299 * then returns 1. The data must already be present in the buffer otherwise
1300 * they won't match. It always returns 1.
1301 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001302static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001303{
1304 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001305 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001306 struct sticking_rule *rule;
1307 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001308 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001309
Willy Tarreau87b09662015-04-03 00:22:06 +02001310 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 +01001311 now_ms, __FUNCTION__,
1312 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001313 rep,
1314 rep->rex, rep->wex,
1315 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001316 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001317 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001318
1319 list_for_each_entry(rule, &px->storersp_rules, list) {
1320 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001321
Willy Tarreau9667a802013-12-09 12:52:13 +01001322 /* Only the first stick store-response of each table is applied
1323 * and other ones are ignored. The purpose is to allow complex
1324 * configurations which look for multiple entries by decreasing
1325 * order of precision and to stop at the first which matches.
1326 * An example could be a store of a set-cookie value, with a
1327 * fallback to a parameter found in a 302 redirect.
1328 *
1329 * The store-response rules are not allowed to override the
1330 * store-request rules for the same table, but they may coexist.
1331 * Thus we can have up to one store-request entry and one store-
1332 * response entry for the same table at any time.
1333 */
1334 for (i = nbreq; i < s->store_count; i++) {
1335 if (rule->table.t == s->store[i].table)
1336 break;
1337 }
1338
1339 /* skip existing entries for this table */
1340 if (i < s->store_count)
1341 continue;
1342
Emeric Brun1d33b292010-01-04 15:47:17 +01001343 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001344 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001345 ret = acl_pass(ret);
1346 if (rule->cond->pol == ACL_COND_UNLESS)
1347 ret = !ret;
1348 }
1349
1350 if (ret) {
1351 struct stktable_key *key;
1352
Willy Tarreau192252e2015-04-04 01:47:55 +02001353 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 +01001354 if (!key)
1355 continue;
1356
Willy Tarreau37e340c2013-12-06 23:05:21 +01001357 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001358 struct stksess *ts;
1359
1360 ts = stksess_new(rule->table.t, key);
1361 if (ts) {
1362 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001363 s->store[s->store_count++].ts = ts;
1364 }
1365 }
1366 }
1367 }
1368
1369 /* process store request and store response */
1370 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001371 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001372 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001373
Willy Tarreauc93cd162014-05-13 15:54:22 +02001374 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001375 stksess_free(s->store[i].table, s->store[i].ts);
1376 s->store[i].ts = NULL;
1377 continue;
1378 }
1379
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001380 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1381 if (ts) {
1382 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001383 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001384 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001385 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001386 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001387 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001388
1389 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001390 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001391 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001392 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001393 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001394
1395 rep->analysers &= ~an_bit;
1396 rep->analyse_exp = TICK_ETERNITY;
1397 return 1;
1398}
1399
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001400/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001401 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001402 */
1403#define UPDATE_ANALYSERS(real, list, back, flag) { \
1404 list = (((list) & ~(flag)) | ~(back)) & (real); \
1405 back = real; \
1406 if (!(list)) \
1407 break; \
1408 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1409 continue; \
1410}
1411
Willy Tarreau87b09662015-04-03 00:22:06 +02001412/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001413 * then puts it back to the wait queue in a clean state, or cleans up its
1414 * resources if it must be deleted. Returns in <next> the date the task wants
1415 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1416 * nothing too many times, the request and response buffers flags are monitored
1417 * and each function is called only if at least another function has changed at
1418 * least one flag it is interested in.
1419 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001420struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001421{
Willy Tarreau827aee92011-03-10 16:55:02 +01001422 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001423 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001424 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001425 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001426 unsigned int rq_prod_last, rq_cons_last;
1427 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001428 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001429 struct channel *req, *res;
1430 struct stream_interface *si_f, *si_b;
1431
1432 req = &s->req;
1433 res = &s->res;
1434
1435 si_f = &s->si[0];
1436 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001437
1438 //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 +01001439 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001440
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001441 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001442 if (s->txn)
1443 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001444
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001445 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001446 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1447 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001448
1449 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001450 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1451 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001452
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001453 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001454 si_f->flags |= SI_FL_DONT_WAKE;
1455 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001456
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001457 /* 1a: Check for low level timeouts if needed. We just set a flag on
1458 * stream interfaces when their timeouts have expired.
1459 */
1460 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001461 stream_int_check_timeouts(si_f);
1462 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001463
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001464 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001465 * for future reads or writes. Note: this will also concern upper layers
1466 * but we do not touch any other flag. We must be careful and correctly
1467 * detect state changes when calling them.
1468 */
1469
Willy Tarreau8f128b42014-11-28 15:07:47 +01001470 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001471
Willy Tarreau8f128b42014-11-28 15:07:47 +01001472 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1473 si_b->flags |= SI_FL_NOLINGER;
1474 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001475 }
1476
Willy Tarreau8f128b42014-11-28 15:07:47 +01001477 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1478 if (si_f->flags & SI_FL_NOHALF)
1479 si_f->flags |= SI_FL_NOLINGER;
1480 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001481 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001482
Willy Tarreau8f128b42014-11-28 15:07:47 +01001483 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001484
Willy Tarreau8f128b42014-11-28 15:07:47 +01001485 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1486 si_f->flags |= SI_FL_NOLINGER;
1487 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001488 }
1489
Willy Tarreau8f128b42014-11-28 15:07:47 +01001490 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1491 if (si_b->flags & SI_FL_NOHALF)
1492 si_b->flags |= SI_FL_NOLINGER;
1493 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001494 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001495
1496 /* Once in a while we're woken up because the task expires. But
1497 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001498 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001499 * timeout needs to be refreshed.
1500 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001501 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001502 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1503 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001504 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001505 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1506 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001507 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001508
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001509 /* below we may emit error messages so we have to ensure that we have
1510 * our buffers properly allocated.
1511 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001512 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001513 /* No buffer available, we've been subscribed to the list of
1514 * buffer waiters, let's wait for our turn.
1515 */
1516 goto update_exp_and_leave;
1517 }
1518
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001519 /* 1b: check for low-level errors reported at the stream interface.
1520 * First we check if it's a retryable error (in which case we don't
1521 * want to tell the buffer). Otherwise we report the error one level
1522 * upper by setting flags into the buffers. Note that the side towards
1523 * the client cannot have connect (hence retryable) errors. Also, the
1524 * connection setup code must be able to deal with any type of abort.
1525 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001526 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001527 if (unlikely(si_f->flags & SI_FL_ERR)) {
1528 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1529 si_shutr(si_f);
1530 si_shutw(si_f);
1531 stream_int_report_error(si_f);
1532 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001533 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001534 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001535 if (srv)
1536 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001537 if (!(s->flags & SF_ERR_MASK))
1538 s->flags |= SF_ERR_CLICL;
1539 if (!(s->flags & SF_FINST_MASK))
1540 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001541 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001542 }
1543 }
1544
Willy Tarreau8f128b42014-11-28 15:07:47 +01001545 if (unlikely(si_b->flags & SI_FL_ERR)) {
1546 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1547 si_shutr(si_b);
1548 si_shutw(si_b);
1549 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001550 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001551 if (srv)
1552 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001553 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001554 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001555 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001556 if (srv)
1557 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001558 if (!(s->flags & SF_ERR_MASK))
1559 s->flags |= SF_ERR_SRVCL;
1560 if (!(s->flags & SF_FINST_MASK))
1561 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001562 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001563 }
1564 /* note: maybe we should process connection errors here ? */
1565 }
1566
Willy Tarreau8f128b42014-11-28 15:07:47 +01001567 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001568 /* we were trying to establish a connection on the server side,
1569 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1570 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001571 if (unlikely(!sess_update_st_con_tcp(s)))
1572 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001573 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001574 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001575
1576 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1577 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1578 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1579 */
1580 }
1581
Willy Tarreau8f128b42014-11-28 15:07:47 +01001582 rq_prod_last = si_f->state;
1583 rq_cons_last = si_b->state;
1584 rp_cons_last = si_f->state;
1585 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001586
1587 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001588 /* Check for connection closure */
1589
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001590 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001591 "[%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 +01001592 now_ms, __FUNCTION__, __LINE__,
1593 t,
1594 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001595 req, res,
1596 req->rex, res->wex,
1597 req->flags, res->flags,
1598 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1599 si_f->err_type, si_b->err_type,
1600 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001601
1602 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001603 if (unlikely(si_f->state == SI_ST_DIS))
1604 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001605
1606 /* When a server-side connection is released, we have to count it and
1607 * check for pending connections on this server.
1608 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001609 if (unlikely(si_b->state == SI_ST_DIS)) {
1610 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001611 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001612 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001613 if (s->flags & SF_CURR_SESS) {
1614 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001615 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001616 }
1617 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001618 if (may_dequeue_tasks(srv, s->be))
1619 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001620 }
1621 }
1622
1623 /*
1624 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1625 * at this point.
1626 */
1627
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001628 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001629 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001630 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1631 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1632 si_f->state != rq_prod_last ||
1633 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001634 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001635 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001636
Willy Tarreau8f128b42014-11-28 15:07:47 +01001637 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001638 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001639 unsigned int ana_list;
1640 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001641
Willy Tarreau90deb182010-01-07 00:20:41 +01001642 /* it's up to the analysers to stop new connections,
1643 * disable reading or closing. Note: if an analyser
1644 * disables any of these bits, it is responsible for
1645 * enabling them again when it disables itself, so
1646 * that other analysers are called in similar conditions.
1647 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001648 channel_auto_read(req);
1649 channel_auto_connect(req);
1650 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001651
1652 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001653 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001654 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001655 * the list when not needed. Any analyser may return 0
1656 * to break out of the loop, either because of missing
1657 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001658 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001659 * analyser, and we may loop again if other analysers
1660 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001661 *
1662 * We build a list of analysers to run. We evaluate all
1663 * of these analysers in the order of the lower bit to
1664 * the higher bit. This ordering is very important.
1665 * An analyser will often add/remove other analysers,
1666 * including itself. Any changes to itself have no effect
1667 * on the loop. If it removes any other analysers, we
1668 * want those analysers not to be called anymore during
1669 * this loop. If it adds an analyser that is located
1670 * after itself, we want it to be scheduled for being
1671 * processed during the loop. If it adds an analyser
1672 * which is located before it, we want it to switch to
1673 * it immediately, even if it has already been called
1674 * once but removed since.
1675 *
1676 * In order to achieve this, we compare the analyser
1677 * list after the call with a copy of it before the
1678 * call. The work list is fed with analyser bits that
1679 * appeared during the call. Then we compare previous
1680 * work list with the new one, and check the bits that
1681 * appeared. If the lowest of these bits is lower than
1682 * the current bit, it means we have enabled a previous
1683 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001684 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001685
Willy Tarreau8f128b42014-11-28 15:07:47 +01001686 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001687 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001688 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001689
Willy Tarreaufb356202010-08-03 14:02:05 +02001690 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001691 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001692 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001693 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001694 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001695
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001696 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001697 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001698 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001699 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001700 }
1701
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001702 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001703 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001704 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001705 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001706 }
1707
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001708 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001709 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001710 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001711 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001712 }
1713
Willy Tarreaufb356202010-08-03 14:02:05 +02001714 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001715 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001716 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001717 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001718 }
1719
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001720 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001721 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001722 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001723 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001724 }
1725
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001726 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001727 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001728 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001729 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001730 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001731
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001732 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001733 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001734 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001735 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001736 }
1737
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001738 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001739 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001740 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001741 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001742 }
1743
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001744 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001745 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001746 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001747 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001748 }
Emeric Brun647caf12009-06-30 17:57:00 +02001749
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001750 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001751 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001752 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001753 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001754 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001755
Emeric Brun1d33b292010-01-04 15:47:17 +01001756 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001757 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001758 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001759 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001760 }
1761
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001762 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001763 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001764 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001765 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001766 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001767 break;
1768 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001769 }
Willy Tarreau84455332009-03-15 22:34:05 +01001770
Willy Tarreau8f128b42014-11-28 15:07:47 +01001771 rq_prod_last = si_f->state;
1772 rq_cons_last = si_b->state;
1773 req->flags &= ~CF_WAKE_ONCE;
1774 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001775
Willy Tarreau8f128b42014-11-28 15:07:47 +01001776 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001777 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001778 }
1779
Willy Tarreau576507f2010-01-07 00:09:04 +01001780 /* we'll monitor the request analysers while parsing the response,
1781 * because some response analysers may indirectly enable new request
1782 * analysers (eg: HTTP keep-alive).
1783 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001784 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001785
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001786 resync_response:
1787 /* Analyse response */
1788
Willy Tarreau8f128b42014-11-28 15:07:47 +01001789 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1790 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1791 si_f->state != rp_cons_last ||
1792 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001793 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001794 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001795
Willy Tarreau8f128b42014-11-28 15:07:47 +01001796 if ((res->flags & CF_MASK_ANALYSER) &&
1797 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001798 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1799 * for some free space in the response buffer, so we might need to call
1800 * it when something changes in the response buffer, but still we pass
1801 * it the request buffer. Note that the SI state might very well still
1802 * be zero due to us returning a flow of redirects!
1803 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001804 res->analysers &= ~AN_REQ_ALL;
1805 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001806 }
1807
Willy Tarreau8f128b42014-11-28 15:07:47 +01001808 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001809 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001810 unsigned int ana_list;
1811 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001812
Willy Tarreau90deb182010-01-07 00:20:41 +01001813 /* it's up to the analysers to stop disable reading or
1814 * closing. Note: if an analyser disables any of these
1815 * bits, it is responsible for enabling them again when
1816 * it disables itself, so that other analysers are called
1817 * in similar conditions.
1818 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001819 channel_auto_read(res);
1820 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001821
1822 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001823 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001824 * to MSB. The analysers must remove themselves from
1825 * the list when not needed. Any analyser may return 0
1826 * to break out of the loop, either because of missing
1827 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001828 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001829 * analyser, and we may loop again if other analysers
1830 * are added in the middle.
1831 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001832
Willy Tarreau8f128b42014-11-28 15:07:47 +01001833 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001834 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001835 /* Warning! ensure that analysers are always placed in ascending order! */
1836
Emeric Brun97679e72010-09-23 17:56:44 +02001837 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001838 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001839 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001840 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001841 }
1842
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001843 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001844 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001845 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001846 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001847 }
1848
Emeric Brun1d33b292010-01-04 15:47:17 +01001849 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001850 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001851 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001852 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001853 }
1854
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001855 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001856 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001857 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001858 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001859 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001860
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001861 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001862 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001863 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001864 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001865 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001866 break;
1867 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001868 }
1869
Willy Tarreau8f128b42014-11-28 15:07:47 +01001870 rp_cons_last = si_f->state;
1871 rp_prod_last = si_b->state;
1872 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001873
Willy Tarreau8f128b42014-11-28 15:07:47 +01001874 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001875 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001876 }
1877
Willy Tarreau576507f2010-01-07 00:09:04 +01001878 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001879 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001880 goto resync_request;
1881
Willy Tarreau8f128b42014-11-28 15:07:47 +01001882 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001883 goto resync_request;
1884
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001885 /* FIXME: here we should call protocol handlers which rely on
1886 * both buffers.
1887 */
1888
1889
1890 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001891 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001892 * we're just in a data phase here since it means we have not
1893 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001894 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001895 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001896 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001897 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001898 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001899 req->analysers = 0;
1900 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001901 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001902 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001903 if (srv)
1904 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001905 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001906 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001907 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001908 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001909 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001910 if (srv)
1911 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001912 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001913 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001914 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001915 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001916 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001917 if (srv)
1918 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001919 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001920 }
1921 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001922 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001923 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001924 if (srv)
1925 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001926 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001927 }
Willy Tarreau84455332009-03-15 22:34:05 +01001928 sess_set_term_flags(s);
1929 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001930 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001931 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001932 res->analysers = 0;
1933 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001934 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001935 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001936 if (srv)
1937 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001938 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001939 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001940 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001941 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001942 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001943 if (srv)
1944 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001945 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001946 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001947 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001948 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001949 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001950 if (srv)
1951 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001952 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001953 }
1954 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001955 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001956 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001957 if (srv)
1958 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001959 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001960 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001961 sess_set_term_flags(s);
1962 }
Willy Tarreau84455332009-03-15 22:34:05 +01001963 }
1964
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001965 /*
1966 * Here we take care of forwarding unhandled data. This also includes
1967 * connection establishments and shutdown requests.
1968 */
1969
1970
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001971 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001972 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001973 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001974 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001975 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001976 if (unlikely(!req->analysers &&
1977 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
1978 (si_f->state >= SI_ST_EST) &&
1979 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01001980 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001981 * attached to it. If any data are left in, we'll permit them to
1982 * move.
1983 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001984 channel_auto_read(req);
1985 channel_auto_connect(req);
1986 channel_auto_close(req);
1987 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001988
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001989 /* We'll let data flow between the producer (if still connected)
1990 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001991 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001992 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
1993 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001994 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001995
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001996 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001997 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
1998 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001999 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002000 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2001 (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 +01002002 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002003 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2004 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002005 (req->flags & CF_STREAMER_FAST)))) {
2006 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002007 }
2008
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002009 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002010 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002011
2012 /*
2013 * Now forward all shutdown requests between both sides of the buffer
2014 */
2015
Willy Tarreau520d95e2009-09-19 21:04:57 +02002016 /* first, let's check if the request buffer needs to shutdown(write), which may
2017 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002018 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2019 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002020 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002021 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002022 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002023 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002024 if (tick_isset(sess->fe->timeout.clientfin)) {
2025 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002026 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002027 }
2028 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002029
2030 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002031 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2032 channel_is_empty(req))) {
2033 if (req->flags & CF_READ_ERROR)
2034 si_b->flags |= SI_FL_NOLINGER;
2035 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002036 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002037 res->rto = s->be->timeout.serverfin;
2038 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002039 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002040 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002041
2042 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002043 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2044 !req->analysers))
2045 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002046
2047 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002048 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2049 if (si_f->flags & SI_FL_NOHALF)
2050 si_f->flags |= SI_FL_NOLINGER;
2051 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002052 if (tick_isset(sess->fe->timeout.clientfin)) {
2053 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002054 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002055 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002056 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002057
Willy Tarreau520d95e2009-09-19 21:04:57 +02002058 /* it's possible that an upper layer has requested a connection setup or abort.
2059 * There are 2 situations where we decide to establish a new connection :
2060 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002061 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002062 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002063 if (si_b->state == SI_ST_INI) {
2064 if (!(req->flags & CF_SHUTW)) {
2065 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002066 /* If we have an appctx, there is no connect method, so we
2067 * immediately switch to the connected state, otherwise we
2068 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002069 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002070 si_b->state = SI_ST_REQ; /* new connection requested */
2071 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002072 }
Willy Tarreau73201222009-08-16 18:27:24 +02002073 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002074 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002075 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2076 channel_shutw_now(req); /* fix buffer flags upon abort */
2077 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002078 }
Willy Tarreau92795622009-03-06 12:51:23 +01002079 }
2080
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002081
2082 /* we may have a pending connection request, or a connection waiting
2083 * for completion.
2084 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002085 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002086 do {
2087 /* nb: step 1 might switch from QUE to ASS, but we first want
2088 * to give a chance to step 2 to perform a redirect if needed.
2089 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002090 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002091 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002092 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002093 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002094
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002095 /* applets directly go to the ESTABLISHED state. Similarly,
2096 * servers experience the same fate when their connection
2097 * is reused.
2098 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002099 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002100 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002101
Willy Tarreaub44c8732013-12-31 23:16:50 +01002102 /* Now we can add the server name to a header (if requested) */
2103 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002104 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002105 (s->be->server_id_hdr_name != NULL) &&
2106 (s->be->mode == PR_MODE_HTTP) &&
2107 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002108 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002109 }
2110
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002111 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002112 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002113 http_perform_server_redirect(s, si_b);
2114 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002115 }
2116
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002117 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002118 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002119 goto resync_stream_interface;
2120
Willy Tarreau815a9b22010-07-27 17:15:12 +02002121 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002122 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002123 goto resync_request;
2124
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002125 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002126
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002127 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002128 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002129 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002130 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002131 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002132 if (unlikely(!res->analysers &&
2133 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2134 (si_b->state >= SI_ST_EST) &&
2135 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002136 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002137 * attached to it. If any data are left in, we'll permit them to
2138 * move.
2139 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002140 channel_auto_read(res);
2141 channel_auto_close(res);
2142 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002143
2144 /* We'll let data flow between the producer (if still connected)
2145 * to the consumer.
2146 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002147 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2148 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002149
2150 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002151 * tunnel timeout set, use it now. Note that we must respect
2152 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002153 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002154 if (!req->analysers && s->be->timeout.tunnel) {
2155 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002156 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002157
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002158 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2159 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002160 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2161 res->rto = s->be->timeout.serverfin;
2162 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2163 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002164 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2165 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002166
Willy Tarreau8f128b42014-11-28 15:07:47 +01002167 req->rex = tick_add(now_ms, req->rto);
2168 req->wex = tick_add(now_ms, req->wto);
2169 res->rex = tick_add(now_ms, res->rto);
2170 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002171 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002172 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002173
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002174 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002175 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2176 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002177 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002178 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2179 (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 +01002180 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002181 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2182 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002183 (res->flags & CF_STREAMER_FAST)))) {
2184 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002185 }
2186
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002187 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002188 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002189
2190 /*
2191 * Now forward all shutdown requests between both sides of the buffer
2192 */
2193
2194 /*
2195 * FIXME: this is probably where we should produce error responses.
2196 */
2197
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002198 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002199 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002200 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002201 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002202 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002203 req->wto = s->be->timeout.serverfin;
2204 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002205 }
2206 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002207
2208 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002209 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2210 channel_is_empty(res))) {
2211 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002212 if (tick_isset(sess->fe->timeout.clientfin)) {
2213 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002214 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002215 }
2216 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002217
2218 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002219 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2220 !res->analysers)
2221 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002222
2223 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002224 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2225 if (si_b->flags & SI_FL_NOHALF)
2226 si_b->flags |= SI_FL_NOLINGER;
2227 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002228 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002229 req->wto = s->be->timeout.serverfin;
2230 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002231 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002232 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002233
Willy Tarreau8f128b42014-11-28 15:07:47 +01002234 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002235 goto resync_stream_interface;
2236
Willy Tarreau8f128b42014-11-28 15:07:47 +01002237 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002238 goto resync_request;
2239
Willy Tarreau8f128b42014-11-28 15:07:47 +01002240 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002241 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002242
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002243 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002244 si_f->flags &= ~SI_FL_DONT_WAKE;
2245 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002246
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002247 /* This is needed only when debugging is enabled, to indicate
2248 * client-side or server-side close. Please note that in the unlikely
2249 * event where both sides would close at once, the sequence is reported
2250 * on the server side first.
2251 */
2252 if (unlikely((global.mode & MODE_DEBUG) &&
2253 (!(global.mode & MODE_QUIET) ||
2254 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002255 if (si_b->state == SI_ST_CLO &&
2256 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002257 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002258 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002259 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2260 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002261 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002262 }
2263
Willy Tarreau8f128b42014-11-28 15:07:47 +01002264 if (si_f->state == SI_ST_CLO &&
2265 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002266 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002267 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002268 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2269 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002270 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002271 }
2272 }
2273
Willy Tarreau8f128b42014-11-28 15:07:47 +01002274 if (likely((si_f->state != SI_ST_CLO) ||
2275 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002276
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002277 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002278 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002279
Willy Tarreau8f128b42014-11-28 15:07:47 +01002280 if (si_f->state == SI_ST_EST && obj_type(si_f->end) != OBJ_TYPE_APPCTX)
2281 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002282
Willy Tarreau8f128b42014-11-28 15:07:47 +01002283 if (si_b->state == SI_ST_EST && obj_type(si_b->end) != OBJ_TYPE_APPCTX)
2284 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002285
Willy Tarreau8f128b42014-11-28 15:07:47 +01002286 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2287 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2288 si_f->prev_state = si_f->state;
2289 si_b->prev_state = si_b->state;
2290 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2291 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002292
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002293 /* Trick: if a request is being waiting for the server to respond,
2294 * and if we know the server can timeout, we don't want the timeout
2295 * to expire on the client side first, but we're still interested
2296 * in passing data from the client to the server (eg: POST). Thus,
2297 * we can cancel the client's request timeout if the server's
2298 * request timeout is set and the server has not yet sent a response.
2299 */
2300
Willy Tarreau8f128b42014-11-28 15:07:47 +01002301 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2302 (tick_isset(req->wex) || tick_isset(res->rex))) {
2303 req->flags |= CF_READ_NOEXP;
2304 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002305 }
2306
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002307 /* When any of the stream interfaces is attached to an applet,
2308 * we have to call it here. Note that this one may wake the
2309 * task up again. If at least one applet was called, the current
2310 * task might have been woken up, in which case we don't want it
2311 * to be requeued to the wait queue but rather to the run queue
2312 * to run ASAP. The bitwise "or" in the condition ensures that
2313 * both functions are always called and that we wake up if at
2314 * least one did something.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002315 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002316 if ((si_applet_call(si_b) | si_applet_call(si_f)) != 0) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002317 if (task_in_rq(t)) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002318 t->expire = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02002319 stream_release_buffers(s);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002320 return t;
2321 }
2322 }
2323
Willy Tarreau798f4322012-11-08 14:49:17 +01002324 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002325 t->expire = tick_first(tick_first(req->rex, req->wex),
2326 tick_first(res->rex, res->wex));
2327 if (req->analysers)
2328 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002329
Willy Tarreau8f128b42014-11-28 15:07:47 +01002330 if (si_f->exp)
2331 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002332
Willy Tarreau8f128b42014-11-28 15:07:47 +01002333 if (si_b->exp)
2334 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002335
2336#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002337 fprintf(stderr,
2338 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2339 " 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 +01002340 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2341 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002342#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002343
2344#ifdef DEBUG_DEV
2345 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002346 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002347 ABORT_NOW();
2348#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002349 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002350 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002351 }
2352
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002353 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002354 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002355 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002356 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002357 if (sess->listener) {
2358 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002359 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002360 sess->listener->nbconn--;
2361 if (sess->listener->state == LI_FULL)
2362 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002363
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002364 /* Dequeues all of the listeners waiting for a resource */
2365 if (!LIST_ISEMPTY(&global_listener_queue))
2366 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002367
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002368 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2369 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2370 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002371 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002372
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002373 if (unlikely((global.mode & MODE_DEBUG) &&
2374 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002375 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002376 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002377 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2378 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002379 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002380 }
2381
2382 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002383 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002384
Willy Tarreaueee5b512015-04-03 23:46:31 +02002385 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002386 int n;
2387
Willy Tarreaueee5b512015-04-03 23:46:31 +02002388 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002389 if (n < 1 || n > 5)
2390 n = 0;
2391
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002392 if (sess->fe->mode == PR_MODE_HTTP) {
2393 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002394 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002395 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002396 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002397 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002398 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002399 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002400 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002401 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002402 s->be->be_counters.p.http.comp_rsp++;
2403 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002404 }
2405
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002406 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002407 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002408 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002409 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002410 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002411 }
2412
Willy Tarreau87b09662015-04-03 00:22:06 +02002413 /* update time stats for this stream */
2414 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002415
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002416 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002417 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002418 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002419 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002420 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002421}
2422
Willy Tarreau87b09662015-04-03 00:22:06 +02002423/* Update the stream's backend and server time stats */
2424void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002425{
2426 int t_request;
2427 int t_queue;
2428 int t_connect;
2429 int t_data;
2430 int t_close;
2431 struct server *srv;
2432
2433 t_request = 0;
2434 t_queue = s->logs.t_queue;
2435 t_connect = s->logs.t_connect;
2436 t_close = s->logs.t_close;
2437 t_data = s->logs.t_data;
2438
2439 if (s->be->mode != PR_MODE_HTTP)
2440 t_data = t_connect;
2441
2442 if (t_connect < 0 || t_data < 0)
2443 return;
2444
2445 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2446 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2447
2448 t_data -= t_connect;
2449 t_connect -= t_queue;
2450 t_queue -= t_request;
2451
2452 srv = objt_server(s->target);
2453 if (srv) {
2454 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2455 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2456 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2457 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2458 }
2459 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2460 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2461 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2462 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2463}
2464
Willy Tarreau7c669d72008-06-20 15:04:11 +02002465/*
2466 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002467 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002468 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002469 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002470 * server.
2471 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002472void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002473{
2474 if (sess->srv_conn == newsrv)
2475 return;
2476
2477 if (sess->srv_conn) {
2478 sess->srv_conn->served--;
2479 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2480 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002481 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002482 }
2483
2484 if (newsrv) {
2485 newsrv->served++;
2486 if (newsrv->proxy->lbprm.server_take_conn)
2487 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002488 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002489 }
2490}
2491
Willy Tarreau84455332009-03-15 22:34:05 +01002492/* Handle server-side errors for default protocols. It is called whenever a a
2493 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002494 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002495 * them. It's installed as ->srv_error for the server-side stream_interface.
2496 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002497void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002498{
2499 int err_type = si->err_type;
2500 int err = 0, fin = 0;
2501
2502 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002503 err = SF_ERR_CLICL;
2504 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002505 }
2506 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002507 err = SF_ERR_CLICL;
2508 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002509 }
2510 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002511 err = SF_ERR_SRVTO;
2512 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002513 }
2514 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002515 err = SF_ERR_SRVCL;
2516 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002517 }
2518 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002519 err = SF_ERR_SRVTO;
2520 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002521 }
2522 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002523 err = SF_ERR_SRVCL;
2524 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002525 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002526 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002527 err = SF_ERR_RESOURCE;
2528 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002529 }
Willy Tarreau84455332009-03-15 22:34:05 +01002530 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002531 err = SF_ERR_INTERNAL;
2532 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002533 }
2534
Willy Tarreaue7dff022015-04-03 01:14:29 +02002535 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002536 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002537 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002538 s->flags |= fin;
2539}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002540
Willy Tarreaue7dff022015-04-03 01:14:29 +02002541/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002542void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002543{
Willy Tarreau87b09662015-04-03 00:22:06 +02002544 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002545 return;
2546
Willy Tarreau87b09662015-04-03 00:22:06 +02002547 channel_shutw_now(&stream->req);
2548 channel_shutr_now(&stream->res);
2549 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002550 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002551 stream->flags |= why;
2552 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002553}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002554
Willy Tarreau8b22a712010-06-18 17:46:06 +02002555/************************************************************************/
2556/* All supported ACL keywords must be declared here. */
2557/************************************************************************/
2558
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002559/* Returns a pointer to a stkctr depending on the fetch keyword name.
2560 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002561 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002562 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002563 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002564 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002565 * NULL may be returned if the designated stkctr is not tracked. For
2566 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2567 * passed. When present, the currently tracked key is then looked up
2568 * in the specified table instead of the current table. The purpose is
2569 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002570 * multiple tables). <strm> is allowed to be NULL, in which case only
2571 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002572 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002573struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002574smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002575{
2576 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002577 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002578 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002579 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002580 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002581
Willy Tarreau0f791d42013-07-23 19:56:43 +02002582 if (num == '_' - '0') {
2583 /* sc_* variant, args[0] = ctr# (mandatory) */
2584 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002585 if (num >= MAX_SESS_STKCTR)
2586 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002587 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002588 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002589 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002590 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002591
2592 if (!conn)
2593 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002594
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002595 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002596 if (!key)
2597 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002598
Willy Tarreaua65536c2013-07-22 22:40:11 +02002599 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002600 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002601 return &stkctr;
2602 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002603
2604 /* Here, <num> contains the counter number from 0 to 9 for
2605 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002606 * args[arg] is the first optional argument. We first lookup the
2607 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002608 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002609
2610 stkptr = &strm->stkctr[num];
2611 if (!strm || !stkctr_entry(stkptr)) {
2612 stkptr = &sess->stkctr[num];
2613 if (!stkctr_entry(stkptr))
2614 return NULL;
2615 }
2616
2617 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002618 if (!stksess)
2619 return NULL;
2620
Willy Tarreau0f791d42013-07-23 19:56:43 +02002621 if (unlikely(args[arg].type == ARGT_TAB)) {
2622 /* an alternate table was specified, let's look up the same key there */
2623 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002624 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002625 return &stkctr;
2626 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002627 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002628}
2629
Willy Tarreau87b09662015-04-03 00:22:06 +02002630/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002631 * currently being tracked or not.
2632 * Supports being called as "sc[0-9]_tracked" only.
2633 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002634static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002635smp_fetch_sc_tracked(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02002636 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002637{
2638 smp->flags = SMP_F_VOL_TEST;
2639 smp->type = SMP_T_BOOL;
Willy Tarreau192252e2015-04-04 01:47:55 +02002640 smp->data.uint = !!smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002641 return 1;
2642}
2643
Willy Tarreau87b09662015-04-03 00:22:06 +02002644/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002645 * frontend counters or from the src.
2646 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2647 * zero is returned if the key is new.
2648 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002649static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002650smp_fetch_sc_get_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002651 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002652{
Willy Tarreau192252e2015-04-04 01:47:55 +02002653 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002654
2655 if (!stkctr)
2656 return 0;
2657
Willy Tarreau37406352012-04-23 16:16:37 +02002658 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002659 smp->type = SMP_T_UINT;
2660 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002661
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002662 if (stkctr_entry(stkctr) != NULL) {
2663 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002664 if (!ptr)
2665 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002666 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002667 }
2668 return 1;
2669}
2670
Willy Tarreau87b09662015-04-03 00:22:06 +02002671/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002672 * tracked frontend counters or from the src.
2673 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2674 * Value zero is returned if the key is new.
2675 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002676static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002677smp_fetch_sc_gpc0_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002678 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002679{
Willy Tarreau192252e2015-04-04 01:47:55 +02002680 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002681
2682 if (!stkctr)
2683 return 0;
2684
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002685 smp->flags = SMP_F_VOL_TEST;
2686 smp->type = SMP_T_UINT;
2687 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002688 if (stkctr_entry(stkctr) != NULL) {
2689 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002690 if (!ptr)
2691 return 0; /* parameter not stored */
2692 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002693 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002694 }
2695 return 1;
2696}
2697
Willy Tarreau87b09662015-04-03 00:22:06 +02002698/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002699 * frontend counters and return it into temp integer.
2700 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002701 */
2702static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002703smp_fetch_sc_inc_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002704 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002705{
Willy Tarreau192252e2015-04-04 01:47:55 +02002706 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002707
2708 if (!stkctr)
2709 return 0;
2710
Willy Tarreau37406352012-04-23 16:16:37 +02002711 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002712 smp->type = SMP_T_UINT;
2713 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002714 if (stkctr_entry(stkctr) != NULL) {
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002715 void *ptr;
2716
2717 /* First, update gpc0_rate if it's tracked. Second, update its
2718 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2719 */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002720 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002721 if (ptr) {
2722 update_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002723 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002724 smp->data.uint = (&stktable_data_cast(ptr, gpc0_rate))->curr_ctr;
2725 }
2726
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002727 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002728 if (ptr)
2729 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
2730
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002731 }
2732 return 1;
2733}
2734
Willy Tarreau87b09662015-04-03 00:22:06 +02002735/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002736 * frontend counters and return its previous value into temp integer.
2737 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002738 */
2739static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002740smp_fetch_sc_clr_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002741 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002742{
Willy Tarreau192252e2015-04-04 01:47:55 +02002743 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002744
2745 if (!stkctr)
2746 return 0;
2747
Willy Tarreau37406352012-04-23 16:16:37 +02002748 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002749 smp->type = SMP_T_UINT;
2750 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002751 if (stkctr_entry(stkctr) != NULL) {
2752 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002753 if (!ptr)
2754 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002755 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002756 stktable_data_cast(ptr, gpc0) = 0;
2757 }
2758 return 1;
2759}
2760
Willy Tarreau87b09662015-04-03 00:22:06 +02002761/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002762 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2763 * "src_conn_cnt" only.
2764 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002765static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002766smp_fetch_sc_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002767 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002768{
Willy Tarreau192252e2015-04-04 01:47:55 +02002769 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002770
2771 if (!stkctr)
2772 return 0;
2773
Willy Tarreau37406352012-04-23 16:16:37 +02002774 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002775 smp->type = SMP_T_UINT;
2776 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002777 if (stkctr_entry(stkctr) != NULL) {
2778 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002779 if (!ptr)
2780 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002781 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002782 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002783 return 1;
2784}
2785
Willy Tarreau87b09662015-04-03 00:22:06 +02002786/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002787 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2788 * only.
2789 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002790static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002791smp_fetch_sc_conn_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002792 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002793{
Willy Tarreau192252e2015-04-04 01:47:55 +02002794 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002795
2796 if (!stkctr)
2797 return 0;
2798
Willy Tarreau37406352012-04-23 16:16:37 +02002799 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002800 smp->type = SMP_T_UINT;
2801 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002802 if (stkctr_entry(stkctr) != NULL) {
2803 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002804 if (!ptr)
2805 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002806 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002807 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002808 }
2809 return 1;
2810}
2811
Willy Tarreau87b09662015-04-03 00:22:06 +02002812/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002813 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002814 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002815 */
2816static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002817smp_fetch_src_updt_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002818 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002819{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002820 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002821 struct stksess *ts;
2822 struct stktable_key *key;
2823 void *ptr;
2824
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002825 if (!conn)
2826 return 0;
2827
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002828 key = addr_to_stktable_key(&conn->addr.from, px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002829 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002830 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002831
Willy Tarreau24e32d82012-04-23 23:55:44 +02002832 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002833
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002834 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2835 /* entry does not exist and could not be created */
2836 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002837
2838 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2839 if (!ptr)
2840 return 0; /* parameter not stored in this table */
2841
Willy Tarreauf853c462012-04-23 18:53:56 +02002842 smp->type = SMP_T_UINT;
2843 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002844 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002845 return 1;
2846}
2847
Willy Tarreau87b09662015-04-03 00:22:06 +02002848/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002849 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2850 * "src_conn_cur" only.
2851 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002852static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002853smp_fetch_sc_conn_cur(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002854 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002855{
Willy Tarreau192252e2015-04-04 01:47:55 +02002856 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002857
2858 if (!stkctr)
2859 return 0;
2860
Willy Tarreau37406352012-04-23 16:16:37 +02002861 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002862 smp->type = SMP_T_UINT;
2863 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002864 if (stkctr_entry(stkctr) != NULL) {
2865 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002866 if (!ptr)
2867 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002868 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002869 }
2870 return 1;
2871}
2872
Willy Tarreau87b09662015-04-03 00:22:06 +02002873/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02002874 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2875 * "src_sess_cnt" only.
2876 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002877static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002878smp_fetch_sc_sess_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002879 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002880{
Willy Tarreau192252e2015-04-04 01:47:55 +02002881 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02002882
2883 if (!stkctr)
2884 return 0;
2885
Willy Tarreau37406352012-04-23 16:16:37 +02002886 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002887 smp->type = SMP_T_UINT;
2888 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002889 if (stkctr_entry(stkctr) != NULL) {
2890 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002891 if (!ptr)
2892 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002893 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002894 }
2895 return 1;
2896}
2897
Willy Tarreau87b09662015-04-03 00:22:06 +02002898/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002899 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2900 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002901static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002902smp_fetch_sc_sess_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002903 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002904{
Willy Tarreau192252e2015-04-04 01:47:55 +02002905 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002906
2907 if (!stkctr)
2908 return 0;
2909
Willy Tarreau37406352012-04-23 16:16:37 +02002910 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002911 smp->type = SMP_T_UINT;
2912 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002913 if (stkctr_entry(stkctr) != NULL) {
2914 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002915 if (!ptr)
2916 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002917 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002918 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002919 }
2920 return 1;
2921}
2922
Willy Tarreau87b09662015-04-03 00:22:06 +02002923/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02002924 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2925 * "src_http_req_cnt" only.
2926 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002927static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002928smp_fetch_sc_http_req_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002929 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002930{
Willy Tarreau192252e2015-04-04 01:47:55 +02002931 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02002932
2933 if (!stkctr)
2934 return 0;
2935
Willy Tarreau37406352012-04-23 16:16:37 +02002936 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002937 smp->type = SMP_T_UINT;
2938 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002939 if (stkctr_entry(stkctr) != NULL) {
2940 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002941 if (!ptr)
2942 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002943 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002944 }
2945 return 1;
2946}
2947
Willy Tarreau87b09662015-04-03 00:22:06 +02002948/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02002949 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2950 * "src_http_req_rate" only.
2951 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002952static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002953smp_fetch_sc_http_req_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002954 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002955{
Willy Tarreau192252e2015-04-04 01:47:55 +02002956 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02002957
2958 if (!stkctr)
2959 return 0;
2960
Willy Tarreau37406352012-04-23 16:16:37 +02002961 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002962 smp->type = SMP_T_UINT;
2963 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002964 if (stkctr_entry(stkctr) != NULL) {
2965 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002966 if (!ptr)
2967 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002968 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02002969 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002970 }
2971 return 1;
2972}
2973
Willy Tarreau87b09662015-04-03 00:22:06 +02002974/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02002975 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2976 * "src_http_err_cnt" only.
2977 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002978static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002979smp_fetch_sc_http_err_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002980 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002981{
Willy Tarreau192252e2015-04-04 01:47:55 +02002982 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02002983
2984 if (!stkctr)
2985 return 0;
2986
Willy Tarreau37406352012-04-23 16:16:37 +02002987 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002988 smp->type = SMP_T_UINT;
2989 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002990 if (stkctr_entry(stkctr) != NULL) {
2991 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002992 if (!ptr)
2993 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002994 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002995 }
2996 return 1;
2997}
2998
Willy Tarreau87b09662015-04-03 00:22:06 +02002999/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003000 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3001 * "src_http_err_rate" only.
3002 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003003static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003004smp_fetch_sc_http_err_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003005 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003006{
Willy Tarreau192252e2015-04-04 01:47:55 +02003007 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003008
3009 if (!stkctr)
3010 return 0;
3011
Willy Tarreau37406352012-04-23 16:16:37 +02003012 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003013 smp->type = SMP_T_UINT;
3014 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003015 if (stkctr_entry(stkctr) != NULL) {
3016 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003017 if (!ptr)
3018 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003019 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003020 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003021 }
3022 return 1;
3023}
3024
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003025/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003026 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003027 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3028 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003029static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003030smp_fetch_sc_kbytes_in(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003031 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003032{
Willy Tarreau192252e2015-04-04 01:47:55 +02003033 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003034
3035 if (!stkctr)
3036 return 0;
3037
Willy Tarreau37406352012-04-23 16:16:37 +02003038 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003039 smp->type = SMP_T_UINT;
3040 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003041 if (stkctr_entry(stkctr) != NULL) {
3042 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003043 if (!ptr)
3044 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003045 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003046 }
3047 return 1;
3048}
3049
Willy Tarreau613fe992013-07-23 17:39:19 +02003050/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003051 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003052 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003053 */
3054static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003055smp_fetch_sc_bytes_in_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003056 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003057{
Willy Tarreau192252e2015-04-04 01:47:55 +02003058 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003059
3060 if (!stkctr)
3061 return 0;
3062
Willy Tarreau37406352012-04-23 16:16:37 +02003063 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003064 smp->type = SMP_T_UINT;
3065 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003066 if (stkctr_entry(stkctr) != NULL) {
3067 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003068 if (!ptr)
3069 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003070 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003071 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003072 }
3073 return 1;
3074}
3075
Willy Tarreau53aea102013-07-23 17:39:02 +02003076/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003077 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003078 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3079 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003080static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003081smp_fetch_sc_kbytes_out(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02003082 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003083{
Willy Tarreau192252e2015-04-04 01:47:55 +02003084 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003085
3086 if (!stkctr)
3087 return 0;
3088
Willy Tarreau37406352012-04-23 16:16:37 +02003089 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003090 smp->type = SMP_T_UINT;
3091 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003092 if (stkctr_entry(stkctr) != NULL) {
3093 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003094 if (!ptr)
3095 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003096 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003097 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003098 return 1;
3099}
3100
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003101/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003102 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003103 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003104 */
3105static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003106smp_fetch_sc_bytes_out_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003107 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003108{
Willy Tarreau192252e2015-04-04 01:47:55 +02003109 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003110
3111 if (!stkctr)
3112 return 0;
3113
Willy Tarreau37406352012-04-23 16:16:37 +02003114 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003115 smp->type = SMP_T_UINT;
3116 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003117 if (stkctr_entry(stkctr) != NULL) {
3118 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003119 if (!ptr)
3120 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003121 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003122 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003123 }
3124 return 1;
3125}
3126
Willy Tarreau87b09662015-04-03 00:22:06 +02003127/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003128 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3129 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003130static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003131smp_fetch_sc_trackers(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003132 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003133{
Willy Tarreau192252e2015-04-04 01:47:55 +02003134 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003135
Willy Tarreau563eef42013-07-23 18:32:02 +02003136 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003137 return 0;
3138
Willy Tarreau563eef42013-07-23 18:32:02 +02003139 smp->flags = SMP_F_VOL_TEST;
3140 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003141 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003142 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003143}
3144
Willy Tarreau34db1082012-04-19 17:16:54 +02003145/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003146 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003147 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003148static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003149smp_fetch_table_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003150 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003151{
Willy Tarreau37406352012-04-23 16:16:37 +02003152 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003153 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003154 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003155 return 1;
3156}
3157
Willy Tarreau34db1082012-04-19 17:16:54 +02003158/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003159 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003160 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003161static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003162smp_fetch_table_avl(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003163 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003164{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003165 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003166 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003167 smp->type = SMP_T_UINT;
3168 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003169 return 1;
3170}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003171
Willy Tarreau61612d42012-04-19 18:42:05 +02003172/* Note: must not be declared <const> as its list will be overwritten.
3173 * Please take care of keeping this list alphabetically sorted.
3174 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003175static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003176 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003177}};
3178
Willy Tarreau281c7992013-01-08 01:23:27 +01003179/* Note: must not be declared <const> as its list will be overwritten.
3180 * Please take care of keeping this list alphabetically sorted.
3181 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003182static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003183 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3184 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3185 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3186 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3187 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3188 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3189 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3190 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3191 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3192 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3193 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3194 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3195 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3196 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3197 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3198 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3199 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3200 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3201 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3202 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3203 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3204 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3205 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3206 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3207 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3208 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3209 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3210 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3211 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3212 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3213 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3214 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3215 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3216 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3217 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3218 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3219 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3220 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3221 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3222 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3223 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3224 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3225 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3226 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3227 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3228 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3229 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3230 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3231 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3232 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3233 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3234 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3235 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3236 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3237 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3238 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3239 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3240 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3241 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3242 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3243 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3244 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3245 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3246 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3247 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3248 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3249 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3250 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3251 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3252 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3253 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3254 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3255 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3256 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3257 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3258 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3259 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3260 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3261 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3262 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3263 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3264 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3265 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3266 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3267 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3268 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3269 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3270 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3271 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3272 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3273 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3274 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3275 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3276 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3277 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3278 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003279 { /* END */ },
3280}};
3281
Willy Tarreau8b22a712010-06-18 17:46:06 +02003282__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003283static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003284{
Willy Tarreau281c7992013-01-08 01:23:27 +01003285 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003286 acl_register_keywords(&acl_kws);
3287}
3288
Willy Tarreaubaaee002006-06-26 02:48:02 +02003289/*
3290 * Local variables:
3291 * c-indent-level: 8
3292 * c-basic-offset: 8
3293 * End:
3294 */