blob: dca1c88842b722c5933afc62d06f03c8efe26f8e [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 Tarreauf2b98742015-04-05 01:30:42 +020069 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau470280e2015-04-05 01:33:13 +020070 struct appctx *appctx = objt_appctx(sess->origin);
Willy Tarreau20d46a52012-12-09 15:55:40 +010071 int i;
Willy Tarreau2542b532012-08-31 16:01:23 +020072
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020073 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020074 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020075
76 /* minimum stream initialization required for an embryonic stream is
77 * fairly low. We need very little to execute L4 ACLs, then we need a
78 * task to make the client-side connection live on its own.
79 * - flags
80 * - stick-entry tracking
81 */
82 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020083 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020084 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020085 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
86 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
87 tv_zero(&s->logs.tv_request);
88 s->logs.t_queue = -1;
89 s->logs.t_connect = -1;
90 s->logs.t_data = -1;
91 s->logs.t_close = 0;
92 s->logs.bytes_in = s->logs.bytes_out = 0;
93 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
94 s->logs.srv_queue_size = 0; /* we will get this number soon */
95
96 /* default logging function */
97 s->do_log = strm_log;
98
99 /* default error reporting function, may be changed by analysers */
100 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200101
102 /* Initialise the current rule list pointer to NULL. We are sure that
103 * any rulelist match the NULL pointer.
104 */
105 s->current_rule_list = NULL;
106
107 memset(s->stkctr, 0, sizeof(s->stkctr));
108
109 s->sess = sess;
110 s->si[0].flags = SI_FL_NONE;
111 s->si[1].flags = SI_FL_ISBACK;
112
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200113 s->uniq_id = global.req_count++;
114
Willy Tarreau87b09662015-04-03 00:22:06 +0200115 /* OK, we're keeping the stream, so let's properly initialize the stream */
116 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200117 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100118 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200119
Willy Tarreaue7dff022015-04-03 01:14:29 +0200120 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200121 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200122
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200123 s->task = t;
Willy Tarreau2542b532012-08-31 16:01:23 +0200124 t->process = l->handler;
125 t->context = s;
126 t->expire = TICK_ETERNITY;
127
Willy Tarreau87b09662015-04-03 00:22:06 +0200128 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200129 * This changes later when switching rules are executed or
130 * when the default backend is assigned.
131 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200132 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200133 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100134 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200135 s->req_cap = NULL;
136 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200137
Willy Tarreau87b09662015-04-03 00:22:06 +0200138 /* Let's count a stream now */
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200139 proxy_inc_fe_sess_ctr(l, sess->fe);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200140
Willy Tarreaub4c84932013-07-23 19:15:30 +0200141 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200142 void *ptr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200143 struct stkctr *stkctr = &sess->stkctr[i];
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200144
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200145 if (!stkctr_entry(stkctr))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100146 continue;
147
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200148 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200149 if (ptr)
150 stktable_data_cast(ptr, sess_cnt)++;
151
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200152 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200153 if (ptr)
154 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200155 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200156 }
157
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200158 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100159 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200160 si_set_state(&s->si[0], SI_ST_EST);
161
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200162 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200163 if (conn)
164 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200165 else if (appctx)
166 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200167
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200168 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200169 s->si[0].flags |= SI_FL_INDEP_STR;
170
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200171 /* pre-initialize the other side's stream interface to an INIT state. The
172 * callbacks will be initialized before attempting to connect.
173 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100174 si_reset(&s->si[1]);
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200175 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200176
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200177 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200178 s->si[1].flags |= SI_FL_INDEP_STR;
179
Willy Tarreau87b09662015-04-03 00:22:06 +0200180 stream_init_srv_conn(s);
Willy Tarreau10b688f2015-03-13 16:43:12 +0100181 s->target = l->default_target; /* used by peers and CLI */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200182 s->pend_pos = NULL;
183
184 /* init store persistence */
185 s->store_count = 0;
186
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100187 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100188 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200189
190 /* activate default analysers enabled for this listener */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100191 s->req.analysers = l->analysers;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200192
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200193 if (!s->req.analysers) {
194 channel_auto_connect(&s->req); /* don't wait to establish connection */
195 channel_auto_close(&s->req); /* let the producer forward close requests */
196 }
197
198 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100199 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100200 s->req.rex = TICK_ETERNITY;
201 s->req.wex = TICK_ETERNITY;
202 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200203
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100204 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100205 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100206 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200207
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200208 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100209 s->req.flags |= CF_NEVER_WAIT;
210 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200211 }
212
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200213 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100214 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100215 s->res.rex = TICK_ETERNITY;
216 s->res.wex = TICK_ETERNITY;
217 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200218
Willy Tarreaueee5b512015-04-03 23:46:31 +0200219 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100220
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100221 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100222
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200223 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200224 if (conn)
225 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200226 else if (appctx)
227 s->si[0].flags |= SI_FL_WAIT_DATA;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200228
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200229 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200230 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200231
Willy Tarreauf2b98742015-04-05 01:30:42 +0200232 if (conn) {
233 /* if logs require transport layer information, note it on the connection */
234 if (s->logs.logwait & LW_XPRT)
235 conn->flags |= CO_FL_XPRT_TRACKED;
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200236
Willy Tarreauf2b98742015-04-05 01:30:42 +0200237 /* we want the connection handler to notify the stream interface about updates. */
238 conn->flags |= CO_FL_WAKE_DATA;
239 }
Willy Tarreau2542b532012-08-31 16:01:23 +0200240
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200241 /* it is important not to call the wakeup function directly but to
242 * pass through task_wakeup(), because this one knows how to apply
243 * priorities to tasks.
244 */
245 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200246 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200247
248 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200249 out_fail_accept:
Willy Tarreau3b246412014-11-25 17:10:33 +0100250 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200251 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200252 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200253}
254
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200256 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200257 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200258static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200259{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200260 struct session *sess = strm_sess(s);
261 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100262 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200263 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200264 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100265
Willy Tarreaubaaee002006-06-26 02:48:02 +0200266 if (s->pend_pos)
267 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100268
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100269 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200270 if (s->flags & SF_CURR_SESS) {
271 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100272 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100273 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100274 if (may_dequeue_tasks(objt_server(s->target), s->be))
275 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100276 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100277
Willy Tarreau7c669d72008-06-20 15:04:11 +0200278 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200279 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200280 * it should normally be only the same as the one above,
281 * so this should not happen in fact.
282 */
283 sess_change_server(s, NULL);
284 }
285
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100286 if (s->req.pipe)
287 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100288
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100289 if (s->res.pipe)
290 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100291
Willy Tarreaubf883e02014-11-25 21:10:35 +0100292 /* We may still be present in the buffer wait queue */
293 if (!LIST_ISEMPTY(&s->buffer_wait)) {
294 LIST_DEL(&s->buffer_wait);
295 LIST_INIT(&s->buffer_wait);
296 }
297
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100298 b_drop(&s->req.buf);
299 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100300 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200301 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200302
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100303 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200304 if (s->txn)
305 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100306
Willy Tarreau1e954912012-10-12 17:50:05 +0200307 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200308 if (cli_conn)
309 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200310
Willy Tarreaua4cda672010-06-06 18:28:49 +0200311 for (i = 0; i < s->store_count; i++) {
312 if (!s->store[i].ts)
313 continue;
314 stksess_free(s->store[i].table, s->store[i].ts);
315 s->store[i].ts = NULL;
316 }
317
Willy Tarreaueee5b512015-04-03 23:46:31 +0200318 if (s->txn) {
319 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
320 pool_free2(pool2_http_txn, s->txn);
321 s->txn = NULL;
322 }
323
Willy Tarreau92fb9832007-10-16 17:34:28 +0200324 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200325 pool_free2(fe->rsp_cap_pool, s->res_cap);
326 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100328
Willy Tarreau87b09662015-04-03 00:22:06 +0200329 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200330
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100331 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100332 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200333 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100334 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100335 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100336 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200337 if (s->list.n != &streams)
338 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100339 bref->ref = s->list.n;
340 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100341 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200342 si_release_endpoint(&s->si[1]);
343 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200344
345 /* FIXME: for now we have a 1:1 relation between stream and session so
346 * the stream must free the session.
347 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200348 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200349 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200350
351 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200352 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200353 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200354 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200355 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200356 pool_flush2(pool2_requri);
357 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200358 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200359 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100360 pool_flush2(pool2_connection);
361 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200362 pool_flush2(fe->req_cap_pool);
363 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200364 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200365}
366
Willy Tarreau78955f42014-12-28 13:09:02 +0100367/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
368 * that it's not the last available buffer or it's the response buffer. Unless
369 * the buffer is the response buffer, an extra control is made so that we always
370 * keep <tune.buffers.reserved> buffers available after this allocation. To be
371 * called at the beginning of recv() callbacks to ensure that the required
372 * buffers are properly allocated. Returns 0 in case of failure, non-zero
373 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100374 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200375int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100376{
Willy Tarreau87b09662015-04-03 00:22:06 +0200377 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100378 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100379 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100380
Willy Tarreau78955f42014-12-28 13:09:02 +0100381 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100382 margin = global.tune.reserved_bufs;
383
Willy Tarreau78955f42014-12-28 13:09:02 +0100384 s = chn_sess(chn);
385
386 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100387 if (b)
388 return 1;
389
Willy Tarreaubf883e02014-11-25 21:10:35 +0100390 if (LIST_ISEMPTY(&s->buffer_wait))
391 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100392 return 0;
393}
394
Willy Tarreau87b09662015-04-03 00:22:06 +0200395/* Allocates a work buffer for stream <s>. It is meant to be called inside
396 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaua24adf02014-11-27 01:11:56 +0100397 * to work fine. For a regular connection, only the response is needed so that
398 * an error message may be built and returned. In case where the initiator is
399 * an applet (eg: peers), then we need to allocate the request buffer for the
400 * applet to be able to send its data (in this case a response is not needed).
401 * Request buffers are never picked from the reserved pool, but response
402 * buffers may be allocated from the reserve. This is critical to ensure that
403 * a response may always flow and will never block a server from releasing a
404 * connection. Returns 0 in case of failure, non-zero otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100405 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200406int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100407{
Willy Tarreaua24adf02014-11-27 01:11:56 +0100408 int margin;
409 struct buffer **buf;
410
411 if (objt_appctx(s->si[0].end)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100412 buf = &s->req.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100413 margin = global.tune.reserved_bufs;
414 }
415 else {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100416 buf = &s->res.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100417 margin = 0;
418 }
419
Willy Tarreaubf883e02014-11-25 21:10:35 +0100420 if (!LIST_ISEMPTY(&s->buffer_wait)) {
421 LIST_DEL(&s->buffer_wait);
422 LIST_INIT(&s->buffer_wait);
423 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100424
Willy Tarreaua24adf02014-11-27 01:11:56 +0100425 if (b_alloc_margin(buf, margin))
Willy Tarreau656859d2014-11-25 19:46:36 +0100426 return 1;
427
Willy Tarreaubf883e02014-11-25 21:10:35 +0100428 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100429 return 0;
430}
431
432/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100433 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200434 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100435 * a single buffer, so it makes sense to try to wake two up when two buffers
436 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100437 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200438void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100439{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100440 if (s->req.buf->size && buffer_empty(s->req.buf))
441 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100442
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100443 if (s->res.buf->size && buffer_empty(s->res.buf))
444 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100445
Willy Tarreaubf883e02014-11-25 21:10:35 +0100446 /* if we're certain to have at least 1 buffer available, and there is
447 * someone waiting, we can wake up a waiter and offer them.
448 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100449 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200450 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100451}
452
Willy Tarreau87b09662015-04-03 00:22:06 +0200453/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100454 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200455 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100456 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200457void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100458{
Willy Tarreau87b09662015-04-03 00:22:06 +0200459 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100460
461 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100462 if (rqlimit <= run_queue)
463 break;
464
Willy Tarreaubf883e02014-11-25 21:10:35 +0100465 if (sess->task->state & TASK_RUNNING)
466 continue;
467
468 LIST_DEL(&sess->buffer_wait);
469 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100470 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100471 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100472}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200473
474/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200475int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200476{
Willy Tarreau87b09662015-04-03 00:22:06 +0200477 LIST_INIT(&streams);
478 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
479 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480}
481
Willy Tarreau87b09662015-04-03 00:22:06 +0200482void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100483{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200484 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100485 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100486 void *ptr;
487 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100488
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100489 bytes = s->req.total - s->logs.bytes_in;
490 s->logs.bytes_in = s->req.total;
491 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200492 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100493
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100494 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100495
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100496 if (objt_server(s->target))
497 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200498
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200499 if (sess->listener && sess->listener->counters)
500 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200501
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100502 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200503 struct stkctr *stkctr = &s->stkctr[i];
504
505 if (!stkctr_entry(stkctr)) {
506 stkctr = &sess->stkctr[i];
507 if (!stkctr_entry(stkctr))
508 continue;
509 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200510
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200511 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100512 if (ptr)
513 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200514
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200515 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100516 if (ptr)
517 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200518 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100519 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100520 }
521
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100522 bytes = s->res.total - s->logs.bytes_out;
523 s->logs.bytes_out = s->res.total;
524 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200525 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100526
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100527 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100528
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100529 if (objt_server(s->target))
530 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200531
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200532 if (sess->listener && sess->listener->counters)
533 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200534
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100535 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200536 struct stkctr *stkctr = &s->stkctr[i];
537
538 if (!stkctr_entry(stkctr)) {
539 stkctr = &sess->stkctr[i];
540 if (!stkctr_entry(stkctr))
541 continue;
542 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200543
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200544 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100545 if (ptr)
546 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200547
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200548 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100549 if (ptr)
550 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200551 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100552 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100553 }
554}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100556/* This function is called with (si->state == SI_ST_CON) meaning that a
557 * connection was attempted and that the file descriptor is already allocated.
558 * We must check for establishment, error and abort. Possible output states
559 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
560 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200561 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100562 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200563static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100564{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100565 struct stream_interface *si = &s->si[1];
566 struct channel *req = &s->req;
567 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200568 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100569
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100570 /* If we got an error, or if nothing happened and the connection timed
571 * out, we must give up. The CER state handler will take care of retry
572 * attempts and error reports.
573 */
574 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100575 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100576 /* Some data were sent past the connection establishment,
577 * so we need to pretend we're established to log correctly
578 * and let later states handle the failure.
579 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100580 si->state = SI_ST_EST;
581 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100582 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100583 return 1;
584 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100585 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100586 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100587
Willy Tarreauf79c8172013-10-21 16:30:56 +0200588 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100589
590 if (si->err_type)
591 return 0;
592
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100593 if (si->flags & SI_FL_ERR)
594 si->err_type = SI_ET_CONN_ERR;
595 else
596 si->err_type = SI_ET_CONN_TO;
597 return 0;
598 }
599
600 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100601 if (!(req->flags & CF_WRITE_PARTIAL) &&
602 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200603 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
604 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100605 s->be->options & PR_O_ABRT_CLOSE)))) {
606 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200607 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100608 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100609 if (s->srv_error)
610 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100611 return 1;
612 }
613
614 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200615 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100616 return 1;
617
618 /* OK, this means that a connection succeeded. The caller will be
619 * responsible for handling the transition from CON to EST.
620 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100621 si->state = SI_ST_EST;
622 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100623 return 1;
624}
625
626/* This function is called with (si->state == SI_ST_CER) meaning that a
627 * previous connection attempt has failed and that the file descriptor
628 * has already been released. Possible causes include asynchronous error
629 * notification and time out. Possible output states are SI_ST_CLO when
630 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
631 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
632 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
633 * marked as in error state. It returns 0.
634 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200635static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100636{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100637 struct stream_interface *si = &s->si[1];
638
Willy Tarreau87b09662015-04-03 00:22:06 +0200639 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100640 if (objt_server(s->target)) {
641 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100642
Willy Tarreaue7dff022015-04-03 01:14:29 +0200643 if (s->flags & SF_CURR_SESS) {
644 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100645 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100646 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100647 }
648
649 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200650 si->conn_retries--;
651 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100652 if (!si->err_type) {
653 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100654 }
655
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100656 if (objt_server(s->target))
657 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100658 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100659 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100660 if (may_dequeue_tasks(objt_server(s->target), s->be))
661 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100662
663 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200664 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100665 s->req.flags |= CF_WRITE_ERROR;
666 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100667
668 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100669 if (s->srv_error)
670 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100671 return 0;
672 }
673
674 /* If the "redispatch" option is set on the backend, we are allowed to
675 * retry on another server for the last retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200676 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100677 * bit to ignore any persistence cookie. We won't count a retry nor a
678 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200679 * If the connection is not persistent, the balancing algorithm is not
680 * determinist (round robin) and there is more than one active server,
681 * we accept to perform an immediate redispatch without waiting since
682 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100683 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200684 if (objt_server(s->target) &&
685 (si->conn_retries == 0 ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200686 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Willy Tarreau33a14e52014-06-13 17:49:40 +0200687 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200688 s->be->options & PR_O_REDISP && !(s->flags & SF_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100689 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100690 if (may_dequeue_tasks(objt_server(s->target), s->be))
691 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100692
Willy Tarreaue7dff022015-04-03 01:14:29 +0200693 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100694 si->state = SI_ST_REQ;
695 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100696 if (objt_server(s->target))
697 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100698 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100699 si->state = SI_ST_ASS;
700 }
701
702 if (si->flags & SI_FL_ERR) {
703 /* The error was an asynchronous connection error, and we will
704 * likely have to retry connecting to the same server, most
705 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200706 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100707 */
708
Willy Tarreaub0290662014-06-13 17:04:44 +0200709 int delay = 1000;
710
711 if (s->be->timeout.connect && s->be->timeout.connect < delay)
712 delay = s->be->timeout.connect;
713
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100714 if (!si->err_type)
715 si->err_type = SI_ET_CONN_ERR;
716
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200717 /* only wait when we're retrying on the same server */
718 if (si->state == SI_ST_ASS ||
719 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
720 (s->be->srv_act <= 1)) {
721 si->state = SI_ST_TAR;
722 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
723 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100724 return 0;
725 }
726 return 0;
727}
728
729/*
730 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200731 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200732 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100733 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200734static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100735{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100736 struct stream_interface *si = &s->si[1];
737 struct channel *req = &s->req;
738 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100740 /* First, centralize the timers information */
741 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
742 si->exp = TICK_ETERNITY;
743
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100744 if (objt_server(s->target))
745 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100746
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100747 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100748 /* if the user wants to log as soon as possible, without counting
749 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200750 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100752 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100753 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100754 }
755 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100756 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100757 }
758
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200759 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200760 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200761 if (req->flags & CF_WAKE_CONNECT) {
762 req->flags |= CF_WAKE_ONCE;
763 req->flags &= ~CF_WAKE_CONNECT;
764 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200765 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200766 /* real connections have timeouts */
767 req->wto = s->be->timeout.server;
768 rep->rto = s->be->timeout.server;
769 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100770 req->wex = TICK_ETERNITY;
771}
772
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100773/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
774 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100775 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
776 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
777 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100778 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200779static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100780{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100781 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100782 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100783 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100784
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100785 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 +0100786 now_ms, __FUNCTION__,
787 s,
Willy Tarreau103197d2014-11-28 15:26:12 +0100788 req, s->rep,
789 req->rex, s->res.wex,
790 req->flags, s->res.flags,
791 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 +0100792
793 if (si->state == SI_ST_ASS) {
794 /* Server assigned to connection request, we have to try to connect now */
795 int conn_err;
796
797 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100798 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100799
Willy Tarreaue7dff022015-04-03 01:14:29 +0200800 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100801 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100802 if (srv)
803 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500804 if (srv)
805 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100806 return;
807 }
808
809 /* We have received a synchronous error. We might have to
810 * abort, retry immediately or redispatch.
811 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200812 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100813 if (!si->err_type) {
814 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100815 }
816
Willy Tarreau827aee92011-03-10 16:55:02 +0100817 if (srv)
818 srv_inc_sess_ctr(srv);
819 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500820 srv_set_sess_last(srv);
821 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100822 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100823 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100824
Willy Tarreau87b09662015-04-03 00:22:06 +0200825 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100826 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100827 if (may_dequeue_tasks(srv, s->be))
828 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100829
830 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200831 si_shutr(si);
832 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100833 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100834
835 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
836
Willy Tarreau87b09662015-04-03 00:22:06 +0200837 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100838 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100839 if (s->srv_error)
840 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100841 return;
842 }
843
844 /* We are facing a retryable error, but we don't want to run a
845 * turn-around now, as the problem is likely a source port
846 * allocation problem, so we want to retry now.
847 */
848 si->state = SI_ST_CER;
849 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100850 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100851 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
852 return;
853 }
854 else if (si->state == SI_ST_QUE) {
855 /* connection request was queued, check for any update */
856 if (!s->pend_pos) {
857 /* The connection is not in the queue anymore. Either
858 * we have a server connection slot available and we
859 * go directly to the assigned state, or we need to
860 * load-balance first and go to the INI state.
861 */
862 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200863 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100864 si->state = SI_ST_REQ;
865 else {
866 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
867 si->state = SI_ST_ASS;
868 }
869 return;
870 }
871
872 /* Connection request still in queue... */
873 if (si->flags & SI_FL_EXP) {
874 /* ... and timeout expired */
875 si->exp = TICK_ETERNITY;
876 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100877 if (srv)
878 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100879 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200880 si_shutr(si);
881 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100882 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100883 if (!si->err_type)
884 si->err_type = SI_ET_QUEUE_TO;
885 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100886 if (s->srv_error)
887 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100888 return;
889 }
890
891 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100892 if ((req->flags & (CF_READ_ERROR)) ||
893 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
894 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100895 /* give up */
896 si->exp = TICK_ETERNITY;
897 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200898 si_shutr(si);
899 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100900 si->err_type |= SI_ET_QUEUE_ABRT;
901 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100902 if (s->srv_error)
903 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100904 return;
905 }
906
907 /* Nothing changed */
908 return;
909 }
910 else if (si->state == SI_ST_TAR) {
911 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100912 if ((req->flags & (CF_READ_ERROR)) ||
913 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
914 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100915 /* give up */
916 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200917 si_shutr(si);
918 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100919 si->err_type |= SI_ET_CONN_ABRT;
920 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100921 if (s->srv_error)
922 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100923 return;
924 }
925
926 if (!(si->flags & SI_FL_EXP))
927 return; /* still in turn-around */
928
929 si->exp = TICK_ETERNITY;
930
Willy Tarreau87b09662015-04-03 00:22:06 +0200931 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100932 * marked "assigned".
933 * FIXME: Should we force a redispatch attempt when the server is down ?
934 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200935 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100936 si->state = SI_ST_ASS;
937 else
938 si->state = SI_ST_REQ;
939 return;
940 }
941}
942
Willy Tarreau87b09662015-04-03 00:22:06 +0200943/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900944 * also counts a failed request if the server state has not reached the request
945 * stage.
946 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200947static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900948{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200949 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900950 if (s->si[1].state < SI_ST_REQ) {
951
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200952 strm_fe(s)->fe_counters.failed_req++;
953 if (strm_li(s)->counters)
954 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900955
Willy Tarreaue7dff022015-04-03 01:14:29 +0200956 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900957 }
958 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200959 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900960 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200961 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900962 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200963 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900964 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200965 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900966 }
967}
968
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100969/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100970 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
971 * a real connection to a server, indicating that a server has been assigned,
972 * or SI_ST_EST for a successful connection to an applet. It may also return
973 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100974 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200975static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100976{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100977 struct stream_interface *si = &s->si[1];
978
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100979 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 +0100980 now_ms, __FUNCTION__,
981 s,
982 s->req, s->rep,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100983 s->req.rex, s->res.wex,
984 s->req.flags, s->res.flags,
Willy Tarreau350f4872014-11-28 14:42:25 +0100985 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 +0100986
987 if (si->state != SI_ST_REQ)
988 return;
989
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100990 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
991 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100992 struct appctx *appctx = objt_appctx(si->end);
993
994 if (!appctx || appctx->applet != __objt_applet(s->target))
995 appctx = stream_int_register_handler(si, objt_applet(s->target));
996
997 if (!appctx) {
998 /* No more memory, let's immediately abort. Force the
999 * error code to ignore the ERR_LOCAL which is not a
1000 * real error.
1001 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001002 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001003
1004 si_shutr(si);
1005 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001006 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001007 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001008 si->state = SI_ST_CLO;
1009 if (s->srv_error)
1010 s->srv_error(s, si);
1011 return;
1012 }
1013
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001014 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001015 si->state = SI_ST_EST;
1016 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001017 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001018 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001019 return;
1020 }
1021
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001022 /* Try to assign a server */
1023 if (srv_redispatch_connect(s) != 0) {
1024 /* We did not get a server. Either we queued the
1025 * connection request, or we encountered an error.
1026 */
1027 if (si->state == SI_ST_QUE)
1028 return;
1029
1030 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001031 si_shutr(si);
1032 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001033 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001034 if (!si->err_type)
1035 si->err_type = SI_ET_CONN_OTHER;
1036 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001037 if (s->srv_error)
1038 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001039 return;
1040 }
1041
1042 /* The server is assigned */
1043 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1044 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001045 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001046}
1047
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001048/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001049 * if appropriate. The default_backend rule is also considered, then the
1050 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001051 * It returns 1 if the processing can continue on next analysers, or zero if it
1052 * either needs more data or wants to immediately abort the request.
1053 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001054static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001055{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001056 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001057 struct session *sess = s->sess;
1058 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001059
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001060 req->analysers &= ~an_bit;
1061 req->analyse_exp = TICK_ETERNITY;
1062
Willy Tarreau87b09662015-04-03 00:22:06 +02001063 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 +02001064 now_ms, __FUNCTION__,
1065 s,
1066 req,
1067 req->rex, req->wex,
1068 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001069 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001070 req->analysers);
1071
1072 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001073 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001074 struct switching_rule *rule;
1075
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001076 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001077 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001078
Willy Tarreauf51658d2014-04-23 01:21:56 +02001079 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001080 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001081 ret = acl_pass(ret);
1082 if (rule->cond->pol == ACL_COND_UNLESS)
1083 ret = !ret;
1084 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001085
1086 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001087 /* If the backend name is dynamic, try to resolve the name.
1088 * If we can't resolve the name, or if any error occurs, break
1089 * the loop and fallback to the default backend.
1090 */
1091 struct proxy *backend;
1092
1093 if (rule->dynamic) {
1094 struct chunk *tmp = get_trash_chunk();
1095 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1096 break;
1097 backend = findproxy(tmp->str, PR_CAP_BE);
1098 if (!backend)
1099 break;
1100 }
1101 else
1102 backend = rule->be.backend;
1103
Willy Tarreau87b09662015-04-03 00:22:06 +02001104 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001105 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001106 break;
1107 }
1108 }
1109
1110 /* To ensure correct connection accounting on the backend, we
1111 * have to assign one if it was not set (eg: a listen). This
1112 * measure also takes care of correctly setting the default
1113 * backend if any.
1114 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001115 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001116 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001117 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001118 }
1119
Willy Tarreaufb356202010-08-03 14:02:05 +02001120 /* 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 +02001121 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001122 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1123 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001124 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001125
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001126 /* 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 +02001127 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001128 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001129 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001130 int ret = 1;
1131
1132 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001133 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 +01001134 ret = acl_pass(ret);
1135 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1136 ret = !ret;
1137 }
1138
1139 if (ret) {
1140 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001141 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001142 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001143 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001144 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001145 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001146 break;
1147 }
1148 }
1149
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001150 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001151
1152 sw_failed:
1153 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001154 channel_abort(&s->req);
1155 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001156
Willy Tarreaue7dff022015-04-03 01:14:29 +02001157 if (!(s->flags & SF_ERR_MASK))
1158 s->flags |= SF_ERR_RESOURCE;
1159 if (!(s->flags & SF_FINST_MASK))
1160 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001161
Willy Tarreaueee5b512015-04-03 23:46:31 +02001162 if (s->txn)
1163 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001164 s->req.analysers = 0;
1165 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001166 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001167}
1168
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001169/* This stream analyser works on a request. It applies all use-server rules on
1170 * it then returns 1. The data must already be present in the buffer otherwise
1171 * they won't match. It always returns 1.
1172 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001173static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001174{
1175 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001176 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001177 struct server_rule *rule;
1178
Willy Tarreau87b09662015-04-03 00:22:06 +02001179 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 +02001180 now_ms, __FUNCTION__,
1181 s,
1182 req,
1183 req->rex, req->wex,
1184 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001185 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001186 req->analysers);
1187
Willy Tarreaue7dff022015-04-03 01:14:29 +02001188 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001189 list_for_each_entry(rule, &px->server_rules, list) {
1190 int ret;
1191
Willy Tarreau192252e2015-04-04 01:47:55 +02001192 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001193 ret = acl_pass(ret);
1194 if (rule->cond->pol == ACL_COND_UNLESS)
1195 ret = !ret;
1196
1197 if (ret) {
1198 struct server *srv = rule->srv.ptr;
1199
Willy Tarreau892337c2014-05-13 23:41:20 +02001200 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001201 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001202 (s->flags & SF_FORCE_PRST)) {
1203 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001204 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001205 break;
1206 }
1207 /* if the server is not UP, let's go on with next rules
1208 * just in case another one is suited.
1209 */
1210 }
1211 }
1212 }
1213
1214 req->analysers &= ~an_bit;
1215 req->analyse_exp = TICK_ETERNITY;
1216 return 1;
1217}
1218
Emeric Brun1d33b292010-01-04 15:47:17 +01001219/* This stream analyser works on a request. It applies all sticking rules on
1220 * it then returns 1. The data must already be present in the buffer otherwise
1221 * they won't match. It always returns 1.
1222 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001223static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001224{
1225 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001226 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001227 struct sticking_rule *rule;
1228
Willy Tarreau87b09662015-04-03 00:22:06 +02001229 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 +01001230 now_ms, __FUNCTION__,
1231 s,
1232 req,
1233 req->rex, req->wex,
1234 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001235 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001236 req->analysers);
1237
1238 list_for_each_entry(rule, &px->sticking_rules, list) {
1239 int ret = 1 ;
1240 int i;
1241
Willy Tarreau9667a802013-12-09 12:52:13 +01001242 /* Only the first stick store-request of each table is applied
1243 * and other ones are ignored. The purpose is to allow complex
1244 * configurations which look for multiple entries by decreasing
1245 * order of precision and to stop at the first which matches.
1246 * An example could be a store of the IP address from an HTTP
1247 * header first, then from the source if not found.
1248 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001249 for (i = 0; i < s->store_count; i++) {
1250 if (rule->table.t == s->store[i].table)
1251 break;
1252 }
1253
1254 if (i != s->store_count)
1255 continue;
1256
1257 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001258 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001259 ret = acl_pass(ret);
1260 if (rule->cond->pol == ACL_COND_UNLESS)
1261 ret = !ret;
1262 }
1263
1264 if (ret) {
1265 struct stktable_key *key;
1266
Willy Tarreau192252e2015-04-04 01:47:55 +02001267 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 +01001268 if (!key)
1269 continue;
1270
1271 if (rule->flags & STK_IS_MATCH) {
1272 struct stksess *ts;
1273
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001274 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001275 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001276 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001277 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001278
1279 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001280 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1281 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001282 if (node) {
1283 struct server *srv;
1284
1285 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001286 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001287 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001288 (s->flags & SF_FORCE_PRST)) {
1289 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001290 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001291 }
1292 }
1293 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001294 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001295 }
1296 }
1297 if (rule->flags & STK_IS_STORE) {
1298 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1299 struct stksess *ts;
1300
1301 ts = stksess_new(rule->table.t, key);
1302 if (ts) {
1303 s->store[s->store_count].table = rule->table.t;
1304 s->store[s->store_count++].ts = ts;
1305 }
1306 }
1307 }
1308 }
1309 }
1310
1311 req->analysers &= ~an_bit;
1312 req->analyse_exp = TICK_ETERNITY;
1313 return 1;
1314}
1315
1316/* This stream analyser works on a response. It applies all store rules on it
1317 * then returns 1. The data must already be present in the buffer otherwise
1318 * they won't match. It always returns 1.
1319 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001320static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001321{
1322 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001323 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001324 struct sticking_rule *rule;
1325 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001326 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001327
Willy Tarreau87b09662015-04-03 00:22:06 +02001328 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 +01001329 now_ms, __FUNCTION__,
1330 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001331 rep,
1332 rep->rex, rep->wex,
1333 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001334 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001335 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001336
1337 list_for_each_entry(rule, &px->storersp_rules, list) {
1338 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001339
Willy Tarreau9667a802013-12-09 12:52:13 +01001340 /* Only the first stick store-response of each table is applied
1341 * and other ones are ignored. The purpose is to allow complex
1342 * configurations which look for multiple entries by decreasing
1343 * order of precision and to stop at the first which matches.
1344 * An example could be a store of a set-cookie value, with a
1345 * fallback to a parameter found in a 302 redirect.
1346 *
1347 * The store-response rules are not allowed to override the
1348 * store-request rules for the same table, but they may coexist.
1349 * Thus we can have up to one store-request entry and one store-
1350 * response entry for the same table at any time.
1351 */
1352 for (i = nbreq; i < s->store_count; i++) {
1353 if (rule->table.t == s->store[i].table)
1354 break;
1355 }
1356
1357 /* skip existing entries for this table */
1358 if (i < s->store_count)
1359 continue;
1360
Emeric Brun1d33b292010-01-04 15:47:17 +01001361 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001362 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001363 ret = acl_pass(ret);
1364 if (rule->cond->pol == ACL_COND_UNLESS)
1365 ret = !ret;
1366 }
1367
1368 if (ret) {
1369 struct stktable_key *key;
1370
Willy Tarreau192252e2015-04-04 01:47:55 +02001371 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 +01001372 if (!key)
1373 continue;
1374
Willy Tarreau37e340c2013-12-06 23:05:21 +01001375 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001376 struct stksess *ts;
1377
1378 ts = stksess_new(rule->table.t, key);
1379 if (ts) {
1380 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001381 s->store[s->store_count++].ts = ts;
1382 }
1383 }
1384 }
1385 }
1386
1387 /* process store request and store response */
1388 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001389 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001390 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001391
Willy Tarreauc93cd162014-05-13 15:54:22 +02001392 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001393 stksess_free(s->store[i].table, s->store[i].ts);
1394 s->store[i].ts = NULL;
1395 continue;
1396 }
1397
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001398 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1399 if (ts) {
1400 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001401 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001402 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001403 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001404 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001405 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001406
1407 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001408 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001409 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001410 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001411 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001412
1413 rep->analysers &= ~an_bit;
1414 rep->analyse_exp = TICK_ETERNITY;
1415 return 1;
1416}
1417
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001418/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001419 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001420 */
1421#define UPDATE_ANALYSERS(real, list, back, flag) { \
1422 list = (((list) & ~(flag)) | ~(back)) & (real); \
1423 back = real; \
1424 if (!(list)) \
1425 break; \
1426 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1427 continue; \
1428}
1429
Willy Tarreau87b09662015-04-03 00:22:06 +02001430/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001431 * then puts it back to the wait queue in a clean state, or cleans up its
1432 * resources if it must be deleted. Returns in <next> the date the task wants
1433 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1434 * nothing too many times, the request and response buffers flags are monitored
1435 * and each function is called only if at least another function has changed at
1436 * least one flag it is interested in.
1437 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001438struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001439{
Willy Tarreau827aee92011-03-10 16:55:02 +01001440 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001441 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001442 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001443 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001444 unsigned int rq_prod_last, rq_cons_last;
1445 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001446 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001447 struct channel *req, *res;
1448 struct stream_interface *si_f, *si_b;
1449
1450 req = &s->req;
1451 res = &s->res;
1452
1453 si_f = &s->si[0];
1454 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001455
1456 //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 +01001457 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001458
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001459 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001460 if (s->txn)
1461 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001462
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001463 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001464 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1465 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001466
1467 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001468 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1469 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001470
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001471 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001472 si_f->flags |= SI_FL_DONT_WAKE;
1473 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001474
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001475 /* 1a: Check for low level timeouts if needed. We just set a flag on
1476 * stream interfaces when their timeouts have expired.
1477 */
1478 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001479 stream_int_check_timeouts(si_f);
1480 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001481
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001482 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001483 * for future reads or writes. Note: this will also concern upper layers
1484 * but we do not touch any other flag. We must be careful and correctly
1485 * detect state changes when calling them.
1486 */
1487
Willy Tarreau8f128b42014-11-28 15:07:47 +01001488 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001489
Willy Tarreau8f128b42014-11-28 15:07:47 +01001490 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1491 si_b->flags |= SI_FL_NOLINGER;
1492 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001493 }
1494
Willy Tarreau8f128b42014-11-28 15:07:47 +01001495 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1496 if (si_f->flags & SI_FL_NOHALF)
1497 si_f->flags |= SI_FL_NOLINGER;
1498 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001499 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001500
Willy Tarreau8f128b42014-11-28 15:07:47 +01001501 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001502
Willy Tarreau8f128b42014-11-28 15:07:47 +01001503 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1504 si_f->flags |= SI_FL_NOLINGER;
1505 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001506 }
1507
Willy Tarreau8f128b42014-11-28 15:07:47 +01001508 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1509 if (si_b->flags & SI_FL_NOHALF)
1510 si_b->flags |= SI_FL_NOLINGER;
1511 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001512 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001513
1514 /* Once in a while we're woken up because the task expires. But
1515 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001516 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001517 * timeout needs to be refreshed.
1518 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001519 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001520 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1521 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001522 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001523 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1524 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001525 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001526
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001527 /* below we may emit error messages so we have to ensure that we have
1528 * our buffers properly allocated.
1529 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001530 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001531 /* No buffer available, we've been subscribed to the list of
1532 * buffer waiters, let's wait for our turn.
1533 */
1534 goto update_exp_and_leave;
1535 }
1536
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001537 /* 1b: check for low-level errors reported at the stream interface.
1538 * First we check if it's a retryable error (in which case we don't
1539 * want to tell the buffer). Otherwise we report the error one level
1540 * upper by setting flags into the buffers. Note that the side towards
1541 * the client cannot have connect (hence retryable) errors. Also, the
1542 * connection setup code must be able to deal with any type of abort.
1543 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001544 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001545 if (unlikely(si_f->flags & SI_FL_ERR)) {
1546 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1547 si_shutr(si_f);
1548 si_shutw(si_f);
1549 stream_int_report_error(si_f);
1550 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001551 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001552 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001553 if (srv)
1554 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001555 if (!(s->flags & SF_ERR_MASK))
1556 s->flags |= SF_ERR_CLICL;
1557 if (!(s->flags & SF_FINST_MASK))
1558 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001559 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001560 }
1561 }
1562
Willy Tarreau8f128b42014-11-28 15:07:47 +01001563 if (unlikely(si_b->flags & SI_FL_ERR)) {
1564 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1565 si_shutr(si_b);
1566 si_shutw(si_b);
1567 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001568 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001569 if (srv)
1570 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001571 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001572 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001573 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001574 if (srv)
1575 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001576 if (!(s->flags & SF_ERR_MASK))
1577 s->flags |= SF_ERR_SRVCL;
1578 if (!(s->flags & SF_FINST_MASK))
1579 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001580 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001581 }
1582 /* note: maybe we should process connection errors here ? */
1583 }
1584
Willy Tarreau8f128b42014-11-28 15:07:47 +01001585 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001586 /* we were trying to establish a connection on the server side,
1587 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1588 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001589 if (unlikely(!sess_update_st_con_tcp(s)))
1590 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001591 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001592 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001593
1594 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1595 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1596 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1597 */
1598 }
1599
Willy Tarreau8f128b42014-11-28 15:07:47 +01001600 rq_prod_last = si_f->state;
1601 rq_cons_last = si_b->state;
1602 rp_cons_last = si_f->state;
1603 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001604
1605 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001606 /* Check for connection closure */
1607
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001608 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001609 "[%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 +01001610 now_ms, __FUNCTION__, __LINE__,
1611 t,
1612 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001613 req, res,
1614 req->rex, res->wex,
1615 req->flags, res->flags,
1616 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1617 si_f->err_type, si_b->err_type,
1618 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001619
1620 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001621 if (unlikely(si_f->state == SI_ST_DIS))
1622 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001623
1624 /* When a server-side connection is released, we have to count it and
1625 * check for pending connections on this server.
1626 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001627 if (unlikely(si_b->state == SI_ST_DIS)) {
1628 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001629 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001630 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001631 if (s->flags & SF_CURR_SESS) {
1632 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001633 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001634 }
1635 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001636 if (may_dequeue_tasks(srv, s->be))
1637 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001638 }
1639 }
1640
1641 /*
1642 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1643 * at this point.
1644 */
1645
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001646 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001647 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001648 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1649 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1650 si_f->state != rq_prod_last ||
1651 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001652 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001653 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001654
Willy Tarreau8f128b42014-11-28 15:07:47 +01001655 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001656 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001657 unsigned int ana_list;
1658 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001659
Willy Tarreau90deb182010-01-07 00:20:41 +01001660 /* it's up to the analysers to stop new connections,
1661 * disable reading or closing. Note: if an analyser
1662 * disables any of these bits, it is responsible for
1663 * enabling them again when it disables itself, so
1664 * that other analysers are called in similar conditions.
1665 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001666 channel_auto_read(req);
1667 channel_auto_connect(req);
1668 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001669
1670 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001671 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001672 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001673 * the list when not needed. Any analyser may return 0
1674 * to break out of the loop, either because of missing
1675 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001676 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001677 * analyser, and we may loop again if other analysers
1678 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001679 *
1680 * We build a list of analysers to run. We evaluate all
1681 * of these analysers in the order of the lower bit to
1682 * the higher bit. This ordering is very important.
1683 * An analyser will often add/remove other analysers,
1684 * including itself. Any changes to itself have no effect
1685 * on the loop. If it removes any other analysers, we
1686 * want those analysers not to be called anymore during
1687 * this loop. If it adds an analyser that is located
1688 * after itself, we want it to be scheduled for being
1689 * processed during the loop. If it adds an analyser
1690 * which is located before it, we want it to switch to
1691 * it immediately, even if it has already been called
1692 * once but removed since.
1693 *
1694 * In order to achieve this, we compare the analyser
1695 * list after the call with a copy of it before the
1696 * call. The work list is fed with analyser bits that
1697 * appeared during the call. Then we compare previous
1698 * work list with the new one, and check the bits that
1699 * appeared. If the lowest of these bits is lower than
1700 * the current bit, it means we have enabled a previous
1701 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001702 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001703
Willy Tarreau8f128b42014-11-28 15:07:47 +01001704 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001705 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001706 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001707
Willy Tarreaufb356202010-08-03 14:02:05 +02001708 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001709 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001710 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001711 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001712 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001713
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001714 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001715 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001716 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001717 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001718 }
1719
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001720 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001721 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
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_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001724 }
1725
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001726 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001727 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001728 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001729 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001730 }
1731
Willy Tarreaufb356202010-08-03 14:02:05 +02001732 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001733 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001734 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001735 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001736 }
1737
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001738 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001739 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001740 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001741 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001742 }
1743
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001744 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001745 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001746 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001747 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001748 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001749
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001750 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001751 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001752 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001753 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001754 }
1755
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001756 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001757 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001758 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001759 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001760 }
1761
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001762 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001763 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001764 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001765 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001766 }
Emeric Brun647caf12009-06-30 17:57:00 +02001767
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001768 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001769 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001770 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001771 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001772 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001773
Emeric Brun1d33b292010-01-04 15:47:17 +01001774 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001775 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001776 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001777 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001778 }
1779
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001780 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001781 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001782 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001783 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001784 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001785 break;
1786 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001787 }
Willy Tarreau84455332009-03-15 22:34:05 +01001788
Willy Tarreau8f128b42014-11-28 15:07:47 +01001789 rq_prod_last = si_f->state;
1790 rq_cons_last = si_b->state;
1791 req->flags &= ~CF_WAKE_ONCE;
1792 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001793
Willy Tarreau8f128b42014-11-28 15:07:47 +01001794 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001795 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001796 }
1797
Willy Tarreau576507f2010-01-07 00:09:04 +01001798 /* we'll monitor the request analysers while parsing the response,
1799 * because some response analysers may indirectly enable new request
1800 * analysers (eg: HTTP keep-alive).
1801 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001802 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001803
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001804 resync_response:
1805 /* Analyse response */
1806
Willy Tarreau8f128b42014-11-28 15:07:47 +01001807 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1808 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1809 si_f->state != rp_cons_last ||
1810 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001811 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001812 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001813
Willy Tarreau8f128b42014-11-28 15:07:47 +01001814 if ((res->flags & CF_MASK_ANALYSER) &&
1815 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001816 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1817 * for some free space in the response buffer, so we might need to call
1818 * it when something changes in the response buffer, but still we pass
1819 * it the request buffer. Note that the SI state might very well still
1820 * be zero due to us returning a flow of redirects!
1821 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001822 res->analysers &= ~AN_REQ_ALL;
1823 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001824 }
1825
Willy Tarreau8f128b42014-11-28 15:07:47 +01001826 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001827 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001828 unsigned int ana_list;
1829 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001830
Willy Tarreau90deb182010-01-07 00:20:41 +01001831 /* it's up to the analysers to stop disable reading or
1832 * closing. Note: if an analyser disables any of these
1833 * bits, it is responsible for enabling them again when
1834 * it disables itself, so that other analysers are called
1835 * in similar conditions.
1836 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001837 channel_auto_read(res);
1838 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001839
1840 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001841 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001842 * to MSB. The analysers must remove themselves from
1843 * the list when not needed. Any analyser may return 0
1844 * to break out of the loop, either because of missing
1845 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001846 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001847 * analyser, and we may loop again if other analysers
1848 * are added in the middle.
1849 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001850
Willy Tarreau8f128b42014-11-28 15:07:47 +01001851 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001852 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001853 /* Warning! ensure that analysers are always placed in ascending order! */
1854
Emeric Brun97679e72010-09-23 17:56:44 +02001855 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001856 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001857 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001858 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001859 }
1860
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001861 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001862 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001863 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001864 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001865 }
1866
Emeric Brun1d33b292010-01-04 15:47:17 +01001867 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001868 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001869 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001870 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001871 }
1872
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001873 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001874 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001875 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001876 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001877 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001878
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001879 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001880 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001881 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001882 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001883 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001884 break;
1885 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001886 }
1887
Willy Tarreau8f128b42014-11-28 15:07:47 +01001888 rp_cons_last = si_f->state;
1889 rp_prod_last = si_b->state;
1890 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001891
Willy Tarreau8f128b42014-11-28 15:07:47 +01001892 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001893 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001894 }
1895
Willy Tarreau576507f2010-01-07 00:09:04 +01001896 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001897 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001898 goto resync_request;
1899
Willy Tarreau8f128b42014-11-28 15:07:47 +01001900 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001901 goto resync_request;
1902
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001903 /* FIXME: here we should call protocol handlers which rely on
1904 * both buffers.
1905 */
1906
1907
1908 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001909 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001910 * we're just in a data phase here since it means we have not
1911 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001912 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001913 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001914 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001915 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001916 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001917 req->analysers = 0;
1918 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001919 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001920 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001921 if (srv)
1922 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001923 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001924 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001925 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001926 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001927 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001928 if (srv)
1929 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001930 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001931 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001932 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001933 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001934 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001935 if (srv)
1936 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001937 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001938 }
1939 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001940 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001941 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001942 if (srv)
1943 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001944 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001945 }
Willy Tarreau84455332009-03-15 22:34:05 +01001946 sess_set_term_flags(s);
1947 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001948 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001949 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001950 res->analysers = 0;
1951 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001952 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001953 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001954 if (srv)
1955 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001956 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001957 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001958 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001959 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001960 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001961 if (srv)
1962 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001963 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001964 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001965 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001966 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001967 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001968 if (srv)
1969 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001970 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001971 }
1972 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001973 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001974 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001975 if (srv)
1976 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001977 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001978 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001979 sess_set_term_flags(s);
1980 }
Willy Tarreau84455332009-03-15 22:34:05 +01001981 }
1982
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001983 /*
1984 * Here we take care of forwarding unhandled data. This also includes
1985 * connection establishments and shutdown requests.
1986 */
1987
1988
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001989 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001990 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001991 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001992 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001993 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001994 if (unlikely(!req->analysers &&
1995 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
1996 (si_f->state >= SI_ST_EST) &&
1997 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01001998 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001999 * attached to it. If any data are left in, we'll permit them to
2000 * move.
2001 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002002 channel_auto_read(req);
2003 channel_auto_connect(req);
2004 channel_auto_close(req);
2005 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002006
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002007 /* We'll let data flow between the producer (if still connected)
2008 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002009 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002010 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2011 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002012 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002013
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002014 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002015 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2016 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002017 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002018 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2019 (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 +01002020 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002021 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2022 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002023 (req->flags & CF_STREAMER_FAST)))) {
2024 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002025 }
2026
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002027 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002028 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002029
2030 /*
2031 * Now forward all shutdown requests between both sides of the buffer
2032 */
2033
Willy Tarreau520d95e2009-09-19 21:04:57 +02002034 /* first, let's check if the request buffer needs to shutdown(write), which may
2035 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002036 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2037 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002038 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002039 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002040 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002041 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002042 if (tick_isset(sess->fe->timeout.clientfin)) {
2043 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002044 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002045 }
2046 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002047
2048 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002049 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2050 channel_is_empty(req))) {
2051 if (req->flags & CF_READ_ERROR)
2052 si_b->flags |= SI_FL_NOLINGER;
2053 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002054 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002055 res->rto = s->be->timeout.serverfin;
2056 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002057 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002058 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002059
2060 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002061 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2062 !req->analysers))
2063 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002064
2065 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002066 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2067 if (si_f->flags & SI_FL_NOHALF)
2068 si_f->flags |= SI_FL_NOLINGER;
2069 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002070 if (tick_isset(sess->fe->timeout.clientfin)) {
2071 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002072 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002073 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002074 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002075
Willy Tarreau520d95e2009-09-19 21:04:57 +02002076 /* it's possible that an upper layer has requested a connection setup or abort.
2077 * There are 2 situations where we decide to establish a new connection :
2078 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002079 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002080 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002081 if (si_b->state == SI_ST_INI) {
2082 if (!(req->flags & CF_SHUTW)) {
2083 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002084 /* If we have an appctx, there is no connect method, so we
2085 * immediately switch to the connected state, otherwise we
2086 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002087 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002088 si_b->state = SI_ST_REQ; /* new connection requested */
2089 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002090 }
Willy Tarreau73201222009-08-16 18:27:24 +02002091 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002092 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002093 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2094 channel_shutw_now(req); /* fix buffer flags upon abort */
2095 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002096 }
Willy Tarreau92795622009-03-06 12:51:23 +01002097 }
2098
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002099
2100 /* we may have a pending connection request, or a connection waiting
2101 * for completion.
2102 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002103 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002104 do {
2105 /* nb: step 1 might switch from QUE to ASS, but we first want
2106 * to give a chance to step 2 to perform a redirect if needed.
2107 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002108 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002109 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002110 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002111 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002112
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002113 /* applets directly go to the ESTABLISHED state. Similarly,
2114 * servers experience the same fate when their connection
2115 * is reused.
2116 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002117 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002118 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002119
Willy Tarreaub44c8732013-12-31 23:16:50 +01002120 /* Now we can add the server name to a header (if requested) */
2121 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002122 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002123 (s->be->server_id_hdr_name != NULL) &&
2124 (s->be->mode == PR_MODE_HTTP) &&
2125 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002126 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002127 }
2128
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002129 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002130 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002131 http_perform_server_redirect(s, si_b);
2132 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002133 }
2134
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002135 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002136 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002137 goto resync_stream_interface;
2138
Willy Tarreau815a9b22010-07-27 17:15:12 +02002139 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002140 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002141 goto resync_request;
2142
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002143 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002144
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002145 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002146 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002147 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002148 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002149 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002150 if (unlikely(!res->analysers &&
2151 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2152 (si_b->state >= SI_ST_EST) &&
2153 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002154 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002155 * attached to it. If any data are left in, we'll permit them to
2156 * move.
2157 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002158 channel_auto_read(res);
2159 channel_auto_close(res);
2160 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002161
2162 /* We'll let data flow between the producer (if still connected)
2163 * to the consumer.
2164 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002165 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2166 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002167
2168 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002169 * tunnel timeout set, use it now. Note that we must respect
2170 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002171 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002172 if (!req->analysers && s->be->timeout.tunnel) {
2173 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002174 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002175
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002176 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2177 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002178 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2179 res->rto = s->be->timeout.serverfin;
2180 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2181 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002182 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2183 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002184
Willy Tarreau8f128b42014-11-28 15:07:47 +01002185 req->rex = tick_add(now_ms, req->rto);
2186 req->wex = tick_add(now_ms, req->wto);
2187 res->rex = tick_add(now_ms, res->rto);
2188 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002189 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002190 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002191
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002192 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002193 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2194 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002195 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002196 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2197 (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 +01002198 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002199 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2200 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002201 (res->flags & CF_STREAMER_FAST)))) {
2202 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002203 }
2204
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002205 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002206 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002207
2208 /*
2209 * Now forward all shutdown requests between both sides of the buffer
2210 */
2211
2212 /*
2213 * FIXME: this is probably where we should produce error responses.
2214 */
2215
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002216 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002217 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002218 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002219 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002220 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002221 req->wto = s->be->timeout.serverfin;
2222 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002223 }
2224 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002225
2226 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002227 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2228 channel_is_empty(res))) {
2229 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002230 if (tick_isset(sess->fe->timeout.clientfin)) {
2231 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002232 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002233 }
2234 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002235
2236 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002237 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2238 !res->analysers)
2239 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002240
2241 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002242 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2243 if (si_b->flags & SI_FL_NOHALF)
2244 si_b->flags |= SI_FL_NOLINGER;
2245 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002246 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002247 req->wto = s->be->timeout.serverfin;
2248 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002249 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002250 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002251
Willy Tarreau8f128b42014-11-28 15:07:47 +01002252 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002253 goto resync_stream_interface;
2254
Willy Tarreau8f128b42014-11-28 15:07:47 +01002255 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002256 goto resync_request;
2257
Willy Tarreau8f128b42014-11-28 15:07:47 +01002258 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002259 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002260
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002261 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002262 si_f->flags &= ~SI_FL_DONT_WAKE;
2263 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002264
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002265 /* This is needed only when debugging is enabled, to indicate
2266 * client-side or server-side close. Please note that in the unlikely
2267 * event where both sides would close at once, the sequence is reported
2268 * on the server side first.
2269 */
2270 if (unlikely((global.mode & MODE_DEBUG) &&
2271 (!(global.mode & MODE_QUIET) ||
2272 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002273 if (si_b->state == SI_ST_CLO &&
2274 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002275 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002276 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002277 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2278 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002279 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002280 }
2281
Willy Tarreau8f128b42014-11-28 15:07:47 +01002282 if (si_f->state == SI_ST_CLO &&
2283 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002284 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002285 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002286 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2287 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002288 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002289 }
2290 }
2291
Willy Tarreau8f128b42014-11-28 15:07:47 +01002292 if (likely((si_f->state != SI_ST_CLO) ||
2293 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002294
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002295 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002296 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002297
Willy Tarreau8f128b42014-11-28 15:07:47 +01002298 if (si_f->state == SI_ST_EST && obj_type(si_f->end) != OBJ_TYPE_APPCTX)
2299 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002300
Willy Tarreau8f128b42014-11-28 15:07:47 +01002301 if (si_b->state == SI_ST_EST && obj_type(si_b->end) != OBJ_TYPE_APPCTX)
2302 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002303
Willy Tarreau8f128b42014-11-28 15:07:47 +01002304 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2305 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2306 si_f->prev_state = si_f->state;
2307 si_b->prev_state = si_b->state;
2308 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2309 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002310
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002311 /* Trick: if a request is being waiting for the server to respond,
2312 * and if we know the server can timeout, we don't want the timeout
2313 * to expire on the client side first, but we're still interested
2314 * in passing data from the client to the server (eg: POST). Thus,
2315 * we can cancel the client's request timeout if the server's
2316 * request timeout is set and the server has not yet sent a response.
2317 */
2318
Willy Tarreau8f128b42014-11-28 15:07:47 +01002319 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2320 (tick_isset(req->wex) || tick_isset(res->rex))) {
2321 req->flags |= CF_READ_NOEXP;
2322 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002323 }
2324
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002325 /* When any of the stream interfaces is attached to an applet,
2326 * we have to call it here. Note that this one may wake the
2327 * task up again. If at least one applet was called, the current
2328 * task might have been woken up, in which case we don't want it
2329 * to be requeued to the wait queue but rather to the run queue
2330 * to run ASAP. The bitwise "or" in the condition ensures that
2331 * both functions are always called and that we wake up if at
2332 * least one did something.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002333 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002334 if ((si_applet_call(si_b) | si_applet_call(si_f)) != 0) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002335 if (task_in_rq(t)) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002336 t->expire = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02002337 stream_release_buffers(s);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002338 return t;
2339 }
2340 }
2341
Willy Tarreau798f4322012-11-08 14:49:17 +01002342 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002343 t->expire = tick_first(tick_first(req->rex, req->wex),
2344 tick_first(res->rex, res->wex));
2345 if (req->analysers)
2346 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002347
Willy Tarreau8f128b42014-11-28 15:07:47 +01002348 if (si_f->exp)
2349 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002350
Willy Tarreau8f128b42014-11-28 15:07:47 +01002351 if (si_b->exp)
2352 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002353
2354#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002355 fprintf(stderr,
2356 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2357 " 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 +01002358 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2359 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002360#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002361
2362#ifdef DEBUG_DEV
2363 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002364 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002365 ABORT_NOW();
2366#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002367 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002368 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002369 }
2370
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002371 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002372 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002373 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002374 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002375 if (sess->listener) {
2376 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002377 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002378 sess->listener->nbconn--;
2379 if (sess->listener->state == LI_FULL)
2380 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002381
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002382 /* Dequeues all of the listeners waiting for a resource */
2383 if (!LIST_ISEMPTY(&global_listener_queue))
2384 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002385
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002386 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2387 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2388 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002389 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002390
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002391 if (unlikely((global.mode & MODE_DEBUG) &&
2392 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002393 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002394 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002395 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2396 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002397 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002398 }
2399
2400 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002401 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002402
Willy Tarreaueee5b512015-04-03 23:46:31 +02002403 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002404 int n;
2405
Willy Tarreaueee5b512015-04-03 23:46:31 +02002406 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002407 if (n < 1 || n > 5)
2408 n = 0;
2409
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002410 if (sess->fe->mode == PR_MODE_HTTP) {
2411 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002412 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002413 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002414 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002415 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002416 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002417 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002418 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002419 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002420 s->be->be_counters.p.http.comp_rsp++;
2421 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002422 }
2423
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002424 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002425 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002426 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002427 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002428 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002429 }
2430
Willy Tarreau87b09662015-04-03 00:22:06 +02002431 /* update time stats for this stream */
2432 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002433
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002434 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002435 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002436 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002437 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002438 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002439}
2440
Willy Tarreau87b09662015-04-03 00:22:06 +02002441/* Update the stream's backend and server time stats */
2442void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002443{
2444 int t_request;
2445 int t_queue;
2446 int t_connect;
2447 int t_data;
2448 int t_close;
2449 struct server *srv;
2450
2451 t_request = 0;
2452 t_queue = s->logs.t_queue;
2453 t_connect = s->logs.t_connect;
2454 t_close = s->logs.t_close;
2455 t_data = s->logs.t_data;
2456
2457 if (s->be->mode != PR_MODE_HTTP)
2458 t_data = t_connect;
2459
2460 if (t_connect < 0 || t_data < 0)
2461 return;
2462
2463 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2464 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2465
2466 t_data -= t_connect;
2467 t_connect -= t_queue;
2468 t_queue -= t_request;
2469
2470 srv = objt_server(s->target);
2471 if (srv) {
2472 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2473 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2474 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2475 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2476 }
2477 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2478 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2479 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2480 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2481}
2482
Willy Tarreau7c669d72008-06-20 15:04:11 +02002483/*
2484 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002485 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002486 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002487 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002488 * server.
2489 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002490void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002491{
2492 if (sess->srv_conn == newsrv)
2493 return;
2494
2495 if (sess->srv_conn) {
2496 sess->srv_conn->served--;
2497 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2498 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002499 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002500 }
2501
2502 if (newsrv) {
2503 newsrv->served++;
2504 if (newsrv->proxy->lbprm.server_take_conn)
2505 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002506 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002507 }
2508}
2509
Willy Tarreau84455332009-03-15 22:34:05 +01002510/* Handle server-side errors for default protocols. It is called whenever a a
2511 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002512 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002513 * them. It's installed as ->srv_error for the server-side stream_interface.
2514 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002515void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002516{
2517 int err_type = si->err_type;
2518 int err = 0, fin = 0;
2519
2520 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002521 err = SF_ERR_CLICL;
2522 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002523 }
2524 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002525 err = SF_ERR_CLICL;
2526 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002527 }
2528 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002529 err = SF_ERR_SRVTO;
2530 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002531 }
2532 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002533 err = SF_ERR_SRVCL;
2534 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002535 }
2536 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002537 err = SF_ERR_SRVTO;
2538 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002539 }
2540 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002541 err = SF_ERR_SRVCL;
2542 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002543 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002544 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002545 err = SF_ERR_RESOURCE;
2546 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002547 }
Willy Tarreau84455332009-03-15 22:34:05 +01002548 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002549 err = SF_ERR_INTERNAL;
2550 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002551 }
2552
Willy Tarreaue7dff022015-04-03 01:14:29 +02002553 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002554 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002555 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002556 s->flags |= fin;
2557}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002558
Willy Tarreaue7dff022015-04-03 01:14:29 +02002559/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002560void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002561{
Willy Tarreau87b09662015-04-03 00:22:06 +02002562 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002563 return;
2564
Willy Tarreau87b09662015-04-03 00:22:06 +02002565 channel_shutw_now(&stream->req);
2566 channel_shutr_now(&stream->res);
2567 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002568 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002569 stream->flags |= why;
2570 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002571}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002572
Willy Tarreau8b22a712010-06-18 17:46:06 +02002573/************************************************************************/
2574/* All supported ACL keywords must be declared here. */
2575/************************************************************************/
2576
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002577/* Returns a pointer to a stkctr depending on the fetch keyword name.
2578 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002579 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002580 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002581 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002582 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002583 * NULL may be returned if the designated stkctr is not tracked. For
2584 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2585 * passed. When present, the currently tracked key is then looked up
2586 * in the specified table instead of the current table. The purpose is
2587 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002588 * multiple tables). <strm> is allowed to be NULL, in which case only
2589 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002590 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002591struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002592smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002593{
2594 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002595 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002596 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002597 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002598 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002599
Willy Tarreau0f791d42013-07-23 19:56:43 +02002600 if (num == '_' - '0') {
2601 /* sc_* variant, args[0] = ctr# (mandatory) */
2602 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002603 if (num >= MAX_SESS_STKCTR)
2604 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002605 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002606 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002607 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002608 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002609
2610 if (!conn)
2611 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002612
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002613 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002614 if (!key)
2615 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002616
Willy Tarreaua65536c2013-07-22 22:40:11 +02002617 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002618 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002619 return &stkctr;
2620 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002621
2622 /* Here, <num> contains the counter number from 0 to 9 for
2623 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002624 * args[arg] is the first optional argument. We first lookup the
2625 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002626 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002627
2628 stkptr = &strm->stkctr[num];
2629 if (!strm || !stkctr_entry(stkptr)) {
2630 stkptr = &sess->stkctr[num];
2631 if (!stkctr_entry(stkptr))
2632 return NULL;
2633 }
2634
2635 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002636 if (!stksess)
2637 return NULL;
2638
Willy Tarreau0f791d42013-07-23 19:56:43 +02002639 if (unlikely(args[arg].type == ARGT_TAB)) {
2640 /* an alternate table was specified, let's look up the same key there */
2641 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002642 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002643 return &stkctr;
2644 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002645 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002646}
2647
Willy Tarreau87b09662015-04-03 00:22:06 +02002648/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002649 * currently being tracked or not.
2650 * Supports being called as "sc[0-9]_tracked" only.
2651 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002652static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002653smp_fetch_sc_tracked(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02002654 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002655{
2656 smp->flags = SMP_F_VOL_TEST;
2657 smp->type = SMP_T_BOOL;
Willy Tarreau192252e2015-04-04 01:47:55 +02002658 smp->data.uint = !!smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002659 return 1;
2660}
2661
Willy Tarreau87b09662015-04-03 00:22:06 +02002662/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002663 * frontend counters or from the src.
2664 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2665 * zero is returned if the key is new.
2666 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002667static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002668smp_fetch_sc_get_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002669 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002670{
Willy Tarreau192252e2015-04-04 01:47:55 +02002671 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002672
2673 if (!stkctr)
2674 return 0;
2675
Willy Tarreau37406352012-04-23 16:16:37 +02002676 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002677 smp->type = SMP_T_UINT;
2678 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002679
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002680 if (stkctr_entry(stkctr) != NULL) {
2681 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002682 if (!ptr)
2683 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002684 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002685 }
2686 return 1;
2687}
2688
Willy Tarreau87b09662015-04-03 00:22:06 +02002689/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002690 * tracked frontend counters or from the src.
2691 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2692 * Value zero is returned if the key is new.
2693 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002694static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002695smp_fetch_sc_gpc0_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002696 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002697{
Willy Tarreau192252e2015-04-04 01:47:55 +02002698 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002699
2700 if (!stkctr)
2701 return 0;
2702
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002703 smp->flags = SMP_F_VOL_TEST;
2704 smp->type = SMP_T_UINT;
2705 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002706 if (stkctr_entry(stkctr) != NULL) {
2707 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002708 if (!ptr)
2709 return 0; /* parameter not stored */
2710 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002711 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002712 }
2713 return 1;
2714}
2715
Willy Tarreau87b09662015-04-03 00:22:06 +02002716/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002717 * frontend counters and return it into temp integer.
2718 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002719 */
2720static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002721smp_fetch_sc_inc_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002722 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002723{
Willy Tarreau192252e2015-04-04 01:47:55 +02002724 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002725
2726 if (!stkctr)
2727 return 0;
2728
Willy Tarreau37406352012-04-23 16:16:37 +02002729 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002730 smp->type = SMP_T_UINT;
2731 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002732 if (stkctr_entry(stkctr) != NULL) {
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002733 void *ptr;
2734
2735 /* First, update gpc0_rate if it's tracked. Second, update its
2736 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2737 */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002738 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002739 if (ptr) {
2740 update_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002741 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002742 smp->data.uint = (&stktable_data_cast(ptr, gpc0_rate))->curr_ctr;
2743 }
2744
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002745 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002746 if (ptr)
2747 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
2748
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002749 }
2750 return 1;
2751}
2752
Willy Tarreau87b09662015-04-03 00:22:06 +02002753/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002754 * frontend counters and return its previous value into temp integer.
2755 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002756 */
2757static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002758smp_fetch_sc_clr_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002759 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002760{
Willy Tarreau192252e2015-04-04 01:47:55 +02002761 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002762
2763 if (!stkctr)
2764 return 0;
2765
Willy Tarreau37406352012-04-23 16:16:37 +02002766 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002767 smp->type = SMP_T_UINT;
2768 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002769 if (stkctr_entry(stkctr) != NULL) {
2770 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002771 if (!ptr)
2772 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002773 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002774 stktable_data_cast(ptr, gpc0) = 0;
2775 }
2776 return 1;
2777}
2778
Willy Tarreau87b09662015-04-03 00:22:06 +02002779/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002780 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2781 * "src_conn_cnt" only.
2782 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002783static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002784smp_fetch_sc_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002785 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002786{
Willy Tarreau192252e2015-04-04 01:47:55 +02002787 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002788
2789 if (!stkctr)
2790 return 0;
2791
Willy Tarreau37406352012-04-23 16:16:37 +02002792 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002793 smp->type = SMP_T_UINT;
2794 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002795 if (stkctr_entry(stkctr) != NULL) {
2796 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002797 if (!ptr)
2798 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002799 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002800 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002801 return 1;
2802}
2803
Willy Tarreau87b09662015-04-03 00:22:06 +02002804/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002805 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2806 * only.
2807 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002808static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002809smp_fetch_sc_conn_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002810 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002811{
Willy Tarreau192252e2015-04-04 01:47:55 +02002812 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002813
2814 if (!stkctr)
2815 return 0;
2816
Willy Tarreau37406352012-04-23 16:16:37 +02002817 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002818 smp->type = SMP_T_UINT;
2819 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002820 if (stkctr_entry(stkctr) != NULL) {
2821 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002822 if (!ptr)
2823 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002824 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002825 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002826 }
2827 return 1;
2828}
2829
Willy Tarreau87b09662015-04-03 00:22:06 +02002830/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002831 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002832 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002833 */
2834static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002835smp_fetch_src_updt_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002836 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002837{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002838 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002839 struct stksess *ts;
2840 struct stktable_key *key;
2841 void *ptr;
2842
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002843 if (!conn)
2844 return 0;
2845
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002846 key = addr_to_stktable_key(&conn->addr.from, px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002847 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002848 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002849
Willy Tarreau24e32d82012-04-23 23:55:44 +02002850 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002851
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002852 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2853 /* entry does not exist and could not be created */
2854 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002855
2856 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2857 if (!ptr)
2858 return 0; /* parameter not stored in this table */
2859
Willy Tarreauf853c462012-04-23 18:53:56 +02002860 smp->type = SMP_T_UINT;
2861 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002862 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002863 return 1;
2864}
2865
Willy Tarreau87b09662015-04-03 00:22:06 +02002866/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002867 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2868 * "src_conn_cur" only.
2869 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002870static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002871smp_fetch_sc_conn_cur(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002872 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002873{
Willy Tarreau192252e2015-04-04 01:47:55 +02002874 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002875
2876 if (!stkctr)
2877 return 0;
2878
Willy Tarreau37406352012-04-23 16:16:37 +02002879 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002880 smp->type = SMP_T_UINT;
2881 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002882 if (stkctr_entry(stkctr) != NULL) {
2883 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002884 if (!ptr)
2885 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002886 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002887 }
2888 return 1;
2889}
2890
Willy Tarreau87b09662015-04-03 00:22:06 +02002891/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02002892 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2893 * "src_sess_cnt" only.
2894 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002895static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002896smp_fetch_sc_sess_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002897 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002898{
Willy Tarreau192252e2015-04-04 01:47:55 +02002899 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02002900
2901 if (!stkctr)
2902 return 0;
2903
Willy Tarreau37406352012-04-23 16:16:37 +02002904 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002905 smp->type = SMP_T_UINT;
2906 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002907 if (stkctr_entry(stkctr) != NULL) {
2908 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002909 if (!ptr)
2910 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002911 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002912 }
2913 return 1;
2914}
2915
Willy Tarreau87b09662015-04-03 00:22:06 +02002916/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002917 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2918 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002919static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002920smp_fetch_sc_sess_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002921 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002922{
Willy Tarreau192252e2015-04-04 01:47:55 +02002923 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002924
2925 if (!stkctr)
2926 return 0;
2927
Willy Tarreau37406352012-04-23 16:16:37 +02002928 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002929 smp->type = SMP_T_UINT;
2930 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002931 if (stkctr_entry(stkctr) != NULL) {
2932 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002933 if (!ptr)
2934 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002935 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002936 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002937 }
2938 return 1;
2939}
2940
Willy Tarreau87b09662015-04-03 00:22:06 +02002941/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02002942 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2943 * "src_http_req_cnt" only.
2944 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002945static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002946smp_fetch_sc_http_req_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002947 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002948{
Willy Tarreau192252e2015-04-04 01:47:55 +02002949 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02002950
2951 if (!stkctr)
2952 return 0;
2953
Willy Tarreau37406352012-04-23 16:16:37 +02002954 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002955 smp->type = SMP_T_UINT;
2956 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002957 if (stkctr_entry(stkctr) != NULL) {
2958 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002959 if (!ptr)
2960 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002961 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002962 }
2963 return 1;
2964}
2965
Willy Tarreau87b09662015-04-03 00:22:06 +02002966/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02002967 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2968 * "src_http_req_rate" only.
2969 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002970static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002971smp_fetch_sc_http_req_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002972 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002973{
Willy Tarreau192252e2015-04-04 01:47:55 +02002974 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02002975
2976 if (!stkctr)
2977 return 0;
2978
Willy Tarreau37406352012-04-23 16:16:37 +02002979 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002980 smp->type = SMP_T_UINT;
2981 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002982 if (stkctr_entry(stkctr) != NULL) {
2983 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002984 if (!ptr)
2985 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002986 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02002987 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002988 }
2989 return 1;
2990}
2991
Willy Tarreau87b09662015-04-03 00:22:06 +02002992/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02002993 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2994 * "src_http_err_cnt" only.
2995 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002996static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002997smp_fetch_sc_http_err_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002998 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002999{
Willy Tarreau192252e2015-04-04 01:47:55 +02003000 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003001
3002 if (!stkctr)
3003 return 0;
3004
Willy Tarreau37406352012-04-23 16:16:37 +02003005 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003006 smp->type = SMP_T_UINT;
3007 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003008 if (stkctr_entry(stkctr) != NULL) {
3009 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003010 if (!ptr)
3011 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003012 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003013 }
3014 return 1;
3015}
3016
Willy Tarreau87b09662015-04-03 00:22:06 +02003017/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003018 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3019 * "src_http_err_rate" only.
3020 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003021static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003022smp_fetch_sc_http_err_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003023 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003024{
Willy Tarreau192252e2015-04-04 01:47:55 +02003025 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003026
3027 if (!stkctr)
3028 return 0;
3029
Willy Tarreau37406352012-04-23 16:16:37 +02003030 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003031 smp->type = SMP_T_UINT;
3032 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003033 if (stkctr_entry(stkctr) != NULL) {
3034 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003035 if (!ptr)
3036 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003037 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003038 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003039 }
3040 return 1;
3041}
3042
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003043/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003044 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003045 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3046 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003047static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003048smp_fetch_sc_kbytes_in(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003049 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003050{
Willy Tarreau192252e2015-04-04 01:47:55 +02003051 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003052
3053 if (!stkctr)
3054 return 0;
3055
Willy Tarreau37406352012-04-23 16:16:37 +02003056 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003057 smp->type = SMP_T_UINT;
3058 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003059 if (stkctr_entry(stkctr) != NULL) {
3060 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003061 if (!ptr)
3062 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003063 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003064 }
3065 return 1;
3066}
3067
Willy Tarreau613fe992013-07-23 17:39:19 +02003068/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003069 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003070 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003071 */
3072static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003073smp_fetch_sc_bytes_in_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003074 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003075{
Willy Tarreau192252e2015-04-04 01:47:55 +02003076 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003077
3078 if (!stkctr)
3079 return 0;
3080
Willy Tarreau37406352012-04-23 16:16:37 +02003081 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003082 smp->type = SMP_T_UINT;
3083 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003084 if (stkctr_entry(stkctr) != NULL) {
3085 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003086 if (!ptr)
3087 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003088 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003089 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003090 }
3091 return 1;
3092}
3093
Willy Tarreau53aea102013-07-23 17:39:02 +02003094/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003095 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003096 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3097 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003098static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003099smp_fetch_sc_kbytes_out(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02003100 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003101{
Willy Tarreau192252e2015-04-04 01:47:55 +02003102 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003103
3104 if (!stkctr)
3105 return 0;
3106
Willy Tarreau37406352012-04-23 16:16:37 +02003107 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003108 smp->type = SMP_T_UINT;
3109 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003110 if (stkctr_entry(stkctr) != NULL) {
3111 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003112 if (!ptr)
3113 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003114 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003115 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003116 return 1;
3117}
3118
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003119/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003120 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003121 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003122 */
3123static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003124smp_fetch_sc_bytes_out_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003125 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003126{
Willy Tarreau192252e2015-04-04 01:47:55 +02003127 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003128
3129 if (!stkctr)
3130 return 0;
3131
Willy Tarreau37406352012-04-23 16:16:37 +02003132 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003133 smp->type = SMP_T_UINT;
3134 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003135 if (stkctr_entry(stkctr) != NULL) {
3136 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003137 if (!ptr)
3138 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003139 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003140 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003141 }
3142 return 1;
3143}
3144
Willy Tarreau87b09662015-04-03 00:22:06 +02003145/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003146 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3147 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003148static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003149smp_fetch_sc_trackers(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 Tarreau2406db42012-12-09 12:16:43 +01003151{
Willy Tarreau192252e2015-04-04 01:47:55 +02003152 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003153
Willy Tarreau563eef42013-07-23 18:32:02 +02003154 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003155 return 0;
3156
Willy Tarreau563eef42013-07-23 18:32:02 +02003157 smp->flags = SMP_F_VOL_TEST;
3158 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003159 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003160 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003161}
3162
Willy Tarreau34db1082012-04-19 17:16:54 +02003163/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003164 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003165 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003166static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003167smp_fetch_table_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003168 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003169{
Willy Tarreau37406352012-04-23 16:16:37 +02003170 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003171 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003172 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003173 return 1;
3174}
3175
Willy Tarreau34db1082012-04-19 17:16:54 +02003176/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003177 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003178 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003179static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003180smp_fetch_table_avl(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003181 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003182{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003183 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003184 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003185 smp->type = SMP_T_UINT;
3186 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003187 return 1;
3188}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003189
Willy Tarreau61612d42012-04-19 18:42:05 +02003190/* Note: must not be declared <const> as its list will be overwritten.
3191 * Please take care of keeping this list alphabetically sorted.
3192 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003193static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003194 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003195}};
3196
Willy Tarreau281c7992013-01-08 01:23:27 +01003197/* Note: must not be declared <const> as its list will be overwritten.
3198 * Please take care of keeping this list alphabetically sorted.
3199 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003200static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003201 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3202 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3203 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3204 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3205 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3206 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3207 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3208 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3209 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3210 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3211 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3212 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3213 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3214 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3215 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3216 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3217 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3218 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3219 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3220 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3221 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3222 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3223 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3224 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3225 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3226 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3227 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3228 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3229 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3230 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3231 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3232 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3233 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3234 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3235 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3236 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3237 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3238 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3239 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3240 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3241 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3242 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3243 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3244 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3245 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3246 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3247 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3248 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3249 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3250 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3251 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3252 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3253 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3254 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3255 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3256 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3257 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3258 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3259 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3260 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3261 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3262 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3263 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3264 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3265 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3266 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3267 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3268 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3269 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3270 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3271 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3272 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3273 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3274 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3275 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3276 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3277 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3278 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3279 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3280 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3281 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3282 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3283 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3284 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3285 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3286 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3287 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3288 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3289 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3290 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3291 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3292 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3293 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3294 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3295 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3296 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003297 { /* END */ },
3298}};
3299
Willy Tarreau8b22a712010-06-18 17:46:06 +02003300__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003301static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003302{
Willy Tarreau281c7992013-01-08 01:23:27 +01003303 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003304 acl_register_keywords(&acl_kws);
3305}
3306
Willy Tarreaubaaee002006-06-26 02:48:02 +02003307/*
3308 * Local variables:
3309 * c-indent-level: 8
3310 * c-basic-offset: 8
3311 * End:
3312 */