blob: fe48be428d86873c06035c10131fe9584d87cde1 [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
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020017#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020018#include <common/config.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020019#include <common/buffer.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020020#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020021#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020023#include <types/applet.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010025#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020027#include <proto/acl.h>
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020028#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020029#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010030#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020031#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010032#include <proto/checks.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020033#include <proto/connection.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020034#include <proto/dumpstats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020035#include <proto/fd.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020036#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020037#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010038#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010039#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020040#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020041#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020042#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020043#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020044#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010045#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010046#include <proto/proto_http.h>
47#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020048#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010050#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020051#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010052#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010053#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010054#include <proto/task.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020055#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056
Willy Tarreau87b09662015-04-03 00:22:06 +020057struct pool_head *pool2_stream;
58struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
Willy Tarreau87b09662015-04-03 00:22:06 +020060/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010061struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
62
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020063/* List of all use-service keywords. */
64static struct list service_keywords = LIST_HEAD_INIT(service_keywords);
65
Willy Tarreau9903f0e2015-04-04 18:50:31 +020066/* This function is called from the session handler which detects the end of
Willy Tarreau73b65ac2015-04-08 18:26:29 +020067 * handshake, in order to complete initialization of a valid stream. It must be
68 * called with a session (which may be embryonic). It returns the pointer to
69 * the newly created stream, or NULL in case of fatal error. The client-facing
70 * end point is assigned to <origin>, which must be valid. The task's context
71 * is set to the new stream, and its function is set to process_stream().
72 * Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +020073 */
Willy Tarreau73b65ac2015-04-08 18:26:29 +020074struct stream *stream_new(struct session *sess, struct task *t, enum obj_type *origin)
Willy Tarreau2542b532012-08-31 16:01:23 +020075{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020076 struct stream *s;
Willy Tarreau73b65ac2015-04-08 18:26:29 +020077 struct connection *conn = objt_conn(origin);
78 struct appctx *appctx = objt_appctx(origin);
Willy Tarreau2542b532012-08-31 16:01:23 +020079
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020080 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020081 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020082
83 /* minimum stream initialization required for an embryonic stream is
84 * fairly low. We need very little to execute L4 ACLs, then we need a
85 * task to make the client-side connection live on its own.
86 * - flags
87 * - stick-entry tracking
88 */
89 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020090 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020091 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020092 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
93 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
94 tv_zero(&s->logs.tv_request);
95 s->logs.t_queue = -1;
96 s->logs.t_connect = -1;
97 s->logs.t_data = -1;
98 s->logs.t_close = 0;
99 s->logs.bytes_in = s->logs.bytes_out = 0;
100 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
101 s->logs.srv_queue_size = 0; /* we will get this number soon */
102
103 /* default logging function */
104 s->do_log = strm_log;
105
106 /* default error reporting function, may be changed by analysers */
107 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200108
109 /* Initialise the current rule list pointer to NULL. We are sure that
110 * any rulelist match the NULL pointer.
111 */
112 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200113 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200114
Willy Tarreaua68f7622015-09-21 17:48:24 +0200115 /* Copy SC counters for the stream. We don't touch refcounts because
116 * any reference we have is inherited from the session. Since the stream
117 * doesn't exist without the session, the session's existence guarantees
118 * we don't lose the entry. During the store operation, the stream won't
119 * touch these ones.
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200120 */
Thierry FOURNIERc8fdb982015-08-16 12:03:39 +0200121 memcpy(s->stkctr, sess->stkctr, sizeof(s->stkctr));
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200122
123 s->sess = sess;
124 s->si[0].flags = SI_FL_NONE;
125 s->si[1].flags = SI_FL_ISBACK;
126
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200127 s->uniq_id = global.req_count++;
128
Willy Tarreau87b09662015-04-03 00:22:06 +0200129 /* OK, we're keeping the stream, so let's properly initialize the stream */
130 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200131 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100132 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200133
Willy Tarreaue7dff022015-04-03 01:14:29 +0200134 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200135 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200136
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200137 s->task = t;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200138 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200139 t->context = s;
140 t->expire = TICK_ETERNITY;
141
Willy Tarreau87b09662015-04-03 00:22:06 +0200142 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200143 * This changes later when switching rules are executed or
144 * when the default backend is assigned.
145 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200146 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200147 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100148 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200149 s->req_cap = NULL;
150 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200151
Willy Tarreauebcd4842015-06-19 11:59:02 +0200152 /* Initialise all the variables contexts even if not used.
153 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200154 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200155 vars_init(&s->vars_txn, SCOPE_TXN);
156 vars_init(&s->vars_reqres, SCOPE_REQ);
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 Tarreaub363a1f2013-10-01 10:45:07 +0200175
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200176 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200177 s->si[1].flags |= SI_FL_INDEP_STR;
178
Willy Tarreau87b09662015-04-03 00:22:06 +0200179 stream_init_srv_conn(s);
Willy Tarreaud1769b82015-04-06 00:25:48 +0200180 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200181 s->pend_pos = NULL;
182
183 /* init store persistence */
184 s->store_count = 0;
185
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100186 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100187 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200188 s->req.analysers = 0;
189 channel_auto_connect(&s->req); /* don't wait to establish connection */
190 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200191
192 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100193 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100194 s->req.rex = TICK_ETERNITY;
195 s->req.wex = TICK_ETERNITY;
196 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200197
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100198 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100199 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100200 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200201
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200202 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100203 s->req.flags |= CF_NEVER_WAIT;
204 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200205 }
206
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200207 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100208 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100209 s->res.rex = TICK_ETERNITY;
210 s->res.wex = TICK_ETERNITY;
211 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200212
Willy Tarreaueee5b512015-04-03 23:46:31 +0200213 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100214
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100215 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100216
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200217 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200218 if (conn)
219 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200220 else if (appctx)
Willy Tarreaufe127932015-04-21 19:23:39 +0200221 si_applet_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200222
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200223 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200224 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200225
226 /* it is important not to call the wakeup function directly but to
227 * pass through task_wakeup(), because this one knows how to apply
228 * priorities to tasks.
229 */
230 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200231 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200232
233 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200234 out_fail_accept:
Willy Tarreau3b246412014-11-25 17:10:33 +0100235 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200236 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200237 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200238}
239
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200241 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200242 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200243static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200245 struct session *sess = strm_sess(s);
246 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100247 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200248 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200249 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100250
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251 if (s->pend_pos)
252 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100253
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100254 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200255 if (s->flags & SF_CURR_SESS) {
256 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100257 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100258 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100259 if (may_dequeue_tasks(objt_server(s->target), s->be))
260 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100261 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100262
Willy Tarreau7c669d72008-06-20 15:04:11 +0200263 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200264 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200265 * it should normally be only the same as the one above,
266 * so this should not happen in fact.
267 */
268 sess_change_server(s, NULL);
269 }
270
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100271 if (s->req.pipe)
272 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100273
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100274 if (s->res.pipe)
275 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100276
Willy Tarreaubf883e02014-11-25 21:10:35 +0100277 /* We may still be present in the buffer wait queue */
278 if (!LIST_ISEMPTY(&s->buffer_wait)) {
279 LIST_DEL(&s->buffer_wait);
280 LIST_INIT(&s->buffer_wait);
281 }
282
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100283 b_drop(&s->req.buf);
284 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100285 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200286 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200287
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100288 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200289 if (s->txn)
290 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100291
Willy Tarreau1e954912012-10-12 17:50:05 +0200292 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200293 if (cli_conn)
294 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200295
Willy Tarreaua4cda672010-06-06 18:28:49 +0200296 for (i = 0; i < s->store_count; i++) {
297 if (!s->store[i].ts)
298 continue;
299 stksess_free(s->store[i].table, s->store[i].ts);
300 s->store[i].ts = NULL;
301 }
302
Willy Tarreaueee5b512015-04-03 23:46:31 +0200303 if (s->txn) {
304 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
305 pool_free2(pool2_http_txn, s->txn);
306 s->txn = NULL;
307 }
308
Willy Tarreau92fb9832007-10-16 17:34:28 +0200309 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200310 pool_free2(fe->rsp_cap_pool, s->res_cap);
311 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200312 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100313
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200314 /* Cleanup all variable contexts. */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200315 vars_prune(&s->vars_txn, s);
316 vars_prune(&s->vars_reqres, s);
317
Willy Tarreau87b09662015-04-03 00:22:06 +0200318 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200319
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100320 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100321 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200322 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100323 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100324 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100325 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200326 if (s->list.n != &streams)
327 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100328 bref->ref = s->list.n;
329 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100330 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200331 si_release_endpoint(&s->si[1]);
332 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200333
334 /* FIXME: for now we have a 1:1 relation between stream and session so
335 * the stream must free the session.
336 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200337 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200338 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200339
340 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200341 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200342 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200343 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200344 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200345 pool_flush2(pool2_requri);
346 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200347 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200348 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100349 pool_flush2(pool2_connection);
350 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200351 pool_flush2(fe->req_cap_pool);
352 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200353 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200354}
355
Willy Tarreau78955f42014-12-28 13:09:02 +0100356/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
357 * that it's not the last available buffer or it's the response buffer. Unless
358 * the buffer is the response buffer, an extra control is made so that we always
359 * keep <tune.buffers.reserved> buffers available after this allocation. To be
360 * called at the beginning of recv() callbacks to ensure that the required
361 * buffers are properly allocated. Returns 0 in case of failure, non-zero
362 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100363 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200364int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100365{
Willy Tarreau87b09662015-04-03 00:22:06 +0200366 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100367 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100368 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100369
Willy Tarreau78955f42014-12-28 13:09:02 +0100370 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100371 margin = global.tune.reserved_bufs;
372
Thierry FOURNIER27929fb2015-09-25 08:36:11 +0200373 s = chn_strm(chn);
Willy Tarreau78955f42014-12-28 13:09:02 +0100374
375 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100376 if (b)
377 return 1;
378
Willy Tarreaubf883e02014-11-25 21:10:35 +0100379 if (LIST_ISEMPTY(&s->buffer_wait))
380 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100381 return 0;
382}
383
Willy Tarreau87b09662015-04-03 00:22:06 +0200384/* Allocates a work buffer for stream <s>. It is meant to be called inside
385 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200386 * to work fine, which is the response buffer so that an error message may be
387 * built and returned. Response buffers may be allocated from the reserve, this
388 * is critical to ensure that a response may always flow and will never block a
389 * server from releasing a connection. Returns 0 in case of failure, non-zero
390 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100391 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200392int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100393{
Willy Tarreaubf883e02014-11-25 21:10:35 +0100394 if (!LIST_ISEMPTY(&s->buffer_wait)) {
395 LIST_DEL(&s->buffer_wait);
396 LIST_INIT(&s->buffer_wait);
397 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100398
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200399 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100400 return 1;
401
Willy Tarreaubf883e02014-11-25 21:10:35 +0100402 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100403 return 0;
404}
405
406/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100407 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200408 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100409 * a single buffer, so it makes sense to try to wake two up when two buffers
410 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100411 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200412void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100413{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100414 if (s->req.buf->size && buffer_empty(s->req.buf))
415 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100416
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100417 if (s->res.buf->size && buffer_empty(s->res.buf))
418 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100419
Willy Tarreaubf883e02014-11-25 21:10:35 +0100420 /* if we're certain to have at least 1 buffer available, and there is
421 * someone waiting, we can wake up a waiter and offer them.
422 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100423 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200424 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100425}
426
Willy Tarreau87b09662015-04-03 00:22:06 +0200427/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100428 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200429 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100430 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200431void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100432{
Willy Tarreau87b09662015-04-03 00:22:06 +0200433 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100434
435 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100436 if (rqlimit <= run_queue)
437 break;
438
Willy Tarreaubf883e02014-11-25 21:10:35 +0100439 if (sess->task->state & TASK_RUNNING)
440 continue;
441
442 LIST_DEL(&sess->buffer_wait);
443 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100444 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100445 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100446}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200447
448/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200449int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200450{
Willy Tarreau87b09662015-04-03 00:22:06 +0200451 LIST_INIT(&streams);
452 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
453 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200454}
455
Willy Tarreau87b09662015-04-03 00:22:06 +0200456void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100457{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200458 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100459 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200460 void *ptr1,*ptr2;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100461 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100462
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100463 bytes = s->req.total - s->logs.bytes_in;
464 s->logs.bytes_in = s->req.total;
465 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200466 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100467
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100468 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100469
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100470 if (objt_server(s->target))
471 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200472
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200473 if (sess->listener && sess->listener->counters)
474 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200475
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100476 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200477 struct stkctr *stkctr = &s->stkctr[i];
478
479 if (!stkctr_entry(stkctr)) {
480 stkctr = &sess->stkctr[i];
481 if (!stkctr_entry(stkctr))
482 continue;
483 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200484
Emeric Brun57056f02015-06-15 18:29:57 +0200485 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
486 if (ptr1)
487 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200488
Emeric Brun57056f02015-06-15 18:29:57 +0200489 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
490 if (ptr2)
491 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200492 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200493
494 /* If data was modified, we need to touch to re-schedule sync */
495 if (ptr1 || ptr2)
496 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100497 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100498 }
499
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100500 bytes = s->res.total - s->logs.bytes_out;
501 s->logs.bytes_out = s->res.total;
502 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200503 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100504
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100505 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100506
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100507 if (objt_server(s->target))
508 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200509
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200510 if (sess->listener && sess->listener->counters)
511 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200512
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100513 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200514 struct stkctr *stkctr = &s->stkctr[i];
515
516 if (!stkctr_entry(stkctr)) {
517 stkctr = &sess->stkctr[i];
518 if (!stkctr_entry(stkctr))
519 continue;
520 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200521
Emeric Brun57056f02015-06-15 18:29:57 +0200522 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
523 if (ptr1)
524 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200525
Emeric Brun57056f02015-06-15 18:29:57 +0200526 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
527 if (ptr2)
528 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200529 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200530
531 /* If data was modified, we need to touch to re-schedule sync */
532 if (ptr1 || ptr2)
533 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100534 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100535 }
536}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100538/* This function is called with (si->state == SI_ST_CON) meaning that a
539 * connection was attempted and that the file descriptor is already allocated.
540 * We must check for establishment, error and abort. Possible output states
541 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
542 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200543 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100544 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200545static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100546{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100547 struct stream_interface *si = &s->si[1];
548 struct channel *req = &s->req;
549 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200550 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100551
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100552 /* If we got an error, or if nothing happened and the connection timed
553 * out, we must give up. The CER state handler will take care of retry
554 * attempts and error reports.
555 */
556 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100557 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100558 /* Some data were sent past the connection establishment,
559 * so we need to pretend we're established to log correctly
560 * and let later states handle the failure.
561 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100562 si->state = SI_ST_EST;
563 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100564 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100565 return 1;
566 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100567 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100568 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100569
Willy Tarreauf79c8172013-10-21 16:30:56 +0200570 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100571
572 if (si->err_type)
573 return 0;
574
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100575 if (si->flags & SI_FL_ERR)
576 si->err_type = SI_ET_CONN_ERR;
577 else
578 si->err_type = SI_ET_CONN_TO;
579 return 0;
580 }
581
582 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100583 if (!(req->flags & CF_WRITE_PARTIAL) &&
584 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200585 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
586 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100587 s->be->options & PR_O_ABRT_CLOSE)))) {
588 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200589 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100590 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100591 if (s->srv_error)
592 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100593 return 1;
594 }
595
596 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200597 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100598 return 1;
599
600 /* OK, this means that a connection succeeded. The caller will be
601 * responsible for handling the transition from CON to EST.
602 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100603 si->state = SI_ST_EST;
604 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100605 return 1;
606}
607
608/* This function is called with (si->state == SI_ST_CER) meaning that a
609 * previous connection attempt has failed and that the file descriptor
610 * has already been released. Possible causes include asynchronous error
611 * notification and time out. Possible output states are SI_ST_CLO when
612 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
613 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
614 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
615 * marked as in error state. It returns 0.
616 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200617static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100618{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100619 struct stream_interface *si = &s->si[1];
620
Willy Tarreau87b09662015-04-03 00:22:06 +0200621 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100622 if (objt_server(s->target)) {
623 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100624
Willy Tarreaue7dff022015-04-03 01:14:29 +0200625 if (s->flags & SF_CURR_SESS) {
626 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100627 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100628 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100629 }
630
631 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200632 si->conn_retries--;
633 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634 if (!si->err_type) {
635 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100636 }
637
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100638 if (objt_server(s->target))
639 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100640 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100641 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100642 if (may_dequeue_tasks(objt_server(s->target), s->be))
643 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644
645 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200646 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100647 s->req.flags |= CF_WRITE_ERROR;
648 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100649
650 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100651 if (s->srv_error)
652 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100653 return 0;
654 }
655
656 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700657 * retry on another server. By default this redispatch occurs on the
658 * last retry, but if configured we allow redispatches to occur on
659 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200660 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100661 * bit to ignore any persistence cookie. We won't count a retry nor a
662 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200663 * If the connection is not persistent, the balancing algorithm is not
664 * determinist (round robin) and there is more than one active server,
665 * we accept to perform an immediate redispatch without waiting since
666 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100667 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200668 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700669 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
Willy Tarreau49008c12016-01-13 07:58:44 +0100670 ((__objt_server(s->target)->state < SRV_ST_RUNNING) ||
671 (((s->be->redispatch_after > 0) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700672 ((s->be->conn_retries - si->conn_retries) %
673 s->be->redispatch_after == 0)) ||
674 ((s->be->redispatch_after < 0) &&
675 ((s->be->conn_retries - si->conn_retries) %
676 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200677 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700678 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100679 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100680 if (may_dequeue_tasks(objt_server(s->target), s->be))
681 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100682
Willy Tarreaue7dff022015-04-03 01:14:29 +0200683 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100684 si->state = SI_ST_REQ;
685 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100686 if (objt_server(s->target))
687 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100688 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100689 si->state = SI_ST_ASS;
690 }
691
692 if (si->flags & SI_FL_ERR) {
693 /* The error was an asynchronous connection error, and we will
694 * likely have to retry connecting to the same server, most
695 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200696 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100697 */
698
Willy Tarreaub0290662014-06-13 17:04:44 +0200699 int delay = 1000;
700
701 if (s->be->timeout.connect && s->be->timeout.connect < delay)
702 delay = s->be->timeout.connect;
703
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100704 if (!si->err_type)
705 si->err_type = SI_ET_CONN_ERR;
706
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200707 /* only wait when we're retrying on the same server */
708 if (si->state == SI_ST_ASS ||
709 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
710 (s->be->srv_act <= 1)) {
711 si->state = SI_ST_TAR;
712 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
713 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100714 return 0;
715 }
716 return 0;
717}
718
719/*
720 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200721 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200722 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100723 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200724static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100725{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100726 struct stream_interface *si = &s->si[1];
727 struct channel *req = &s->req;
728 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100729
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100730 /* First, centralize the timers information */
731 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
732 si->exp = TICK_ETERNITY;
733
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100734 if (objt_server(s->target))
735 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100736
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100738 /* if the user wants to log as soon as possible, without counting
739 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200740 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100741 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100742 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100743 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744 }
745 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100746 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100747 }
748
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200749 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200750 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200751 if (req->flags & CF_WAKE_CONNECT) {
752 req->flags |= CF_WAKE_ONCE;
753 req->flags &= ~CF_WAKE_CONNECT;
754 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200755 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200756 /* real connections have timeouts */
757 req->wto = s->be->timeout.server;
758 rep->rto = s->be->timeout.server;
759 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100760 req->wex = TICK_ETERNITY;
761}
762
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100763/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
764 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100765 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
766 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
767 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100768 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200769static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100770{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100771 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100772 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100773 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100774
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100775 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 +0100776 now_ms, __FUNCTION__,
777 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400778 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100779 req->rex, s->res.wex,
780 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400781 req->buf->i, req->buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100782
783 if (si->state == SI_ST_ASS) {
784 /* Server assigned to connection request, we have to try to connect now */
785 int conn_err;
786
787 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100788 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100789
Willy Tarreaue7dff022015-04-03 01:14:29 +0200790 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100791 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100792 if (srv)
793 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500794 if (srv)
795 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100796 return;
797 }
798
799 /* We have received a synchronous error. We might have to
800 * abort, retry immediately or redispatch.
801 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200802 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100803 if (!si->err_type) {
804 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100805 }
806
Willy Tarreau827aee92011-03-10 16:55:02 +0100807 if (srv)
808 srv_inc_sess_ctr(srv);
809 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500810 srv_set_sess_last(srv);
811 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100812 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100813 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100814
Willy Tarreau87b09662015-04-03 00:22:06 +0200815 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100816 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100817 if (may_dequeue_tasks(srv, s->be))
818 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100819
820 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200821 si_shutr(si);
822 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100823 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100824
825 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
826
Willy Tarreau87b09662015-04-03 00:22:06 +0200827 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100828 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100829 if (s->srv_error)
830 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100831 return;
832 }
833
834 /* We are facing a retryable error, but we don't want to run a
835 * turn-around now, as the problem is likely a source port
836 * allocation problem, so we want to retry now.
837 */
838 si->state = SI_ST_CER;
839 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100840 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100841 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
842 return;
843 }
844 else if (si->state == SI_ST_QUE) {
845 /* connection request was queued, check for any update */
846 if (!s->pend_pos) {
847 /* The connection is not in the queue anymore. Either
848 * we have a server connection slot available and we
849 * go directly to the assigned state, or we need to
850 * load-balance first and go to the INI state.
851 */
852 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200853 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100854 si->state = SI_ST_REQ;
855 else {
856 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
857 si->state = SI_ST_ASS;
858 }
859 return;
860 }
861
862 /* Connection request still in queue... */
863 if (si->flags & SI_FL_EXP) {
864 /* ... and timeout expired */
865 si->exp = TICK_ETERNITY;
866 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100867 if (srv)
868 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100869 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200870 si_shutr(si);
871 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100872 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100873 if (!si->err_type)
874 si->err_type = SI_ET_QUEUE_TO;
875 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100876 if (s->srv_error)
877 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100878 return;
879 }
880
881 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100882 if ((req->flags & (CF_READ_ERROR)) ||
883 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
884 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100885 /* give up */
886 si->exp = TICK_ETERNITY;
887 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200888 si_shutr(si);
889 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100890 si->err_type |= SI_ET_QUEUE_ABRT;
891 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100892 if (s->srv_error)
893 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100894 return;
895 }
896
897 /* Nothing changed */
898 return;
899 }
900 else if (si->state == SI_ST_TAR) {
901 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100902 if ((req->flags & (CF_READ_ERROR)) ||
903 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
904 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100905 /* give up */
906 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200907 si_shutr(si);
908 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100909 si->err_type |= SI_ET_CONN_ABRT;
910 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100911 if (s->srv_error)
912 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100913 return;
914 }
915
916 if (!(si->flags & SI_FL_EXP))
917 return; /* still in turn-around */
918
919 si->exp = TICK_ETERNITY;
920
Willy Tarreau87b09662015-04-03 00:22:06 +0200921 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100922 * marked "assigned".
923 * FIXME: Should we force a redispatch attempt when the server is down ?
924 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200925 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100926 si->state = SI_ST_ASS;
927 else
928 si->state = SI_ST_REQ;
929 return;
930 }
931}
932
Willy Tarreau87b09662015-04-03 00:22:06 +0200933/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900934 * also counts a failed request if the server state has not reached the request
935 * stage.
936 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200937static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900938{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200939 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900940 if (s->si[1].state < SI_ST_REQ) {
941
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200942 strm_fe(s)->fe_counters.failed_req++;
Willy Tarreau2c1068c2015-09-23 12:21:21 +0200943 if (strm_li(s) && strm_li(s)->counters)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200944 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900945
Willy Tarreaue7dff022015-04-03 01:14:29 +0200946 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900947 }
948 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200949 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900950 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200951 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900952 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200953 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900954 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200955 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900956 }
957}
958
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100959/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100960 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
961 * a real connection to a server, indicating that a server has been assigned,
962 * or SI_ST_EST for a successful connection to an applet. It may also return
963 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100964 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200965static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100966{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100967 struct stream_interface *si = &s->si[1];
968
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100969 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 +0100970 now_ms, __FUNCTION__,
971 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400972 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100973 s->req.rex, s->res.wex,
974 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400975 s->req.buf->i, s->req.buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100976
977 if (si->state != SI_ST_REQ)
978 return;
979
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100980 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
981 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100982 struct appctx *appctx = objt_appctx(si->end);
983
984 if (!appctx || appctx->applet != __objt_applet(s->target))
985 appctx = stream_int_register_handler(si, objt_applet(s->target));
986
987 if (!appctx) {
988 /* No more memory, let's immediately abort. Force the
989 * error code to ignore the ERR_LOCAL which is not a
990 * real error.
991 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200992 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100993
994 si_shutr(si);
995 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100996 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +0100997 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100998 si->state = SI_ST_CLO;
999 if (s->srv_error)
1000 s->srv_error(s, si);
1001 return;
1002 }
1003
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001004 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001005 si->state = SI_ST_EST;
1006 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001007 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001008 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001009 return;
1010 }
1011
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001012 /* Try to assign a server */
1013 if (srv_redispatch_connect(s) != 0) {
1014 /* We did not get a server. Either we queued the
1015 * connection request, or we encountered an error.
1016 */
1017 if (si->state == SI_ST_QUE)
1018 return;
1019
1020 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001021 si_shutr(si);
1022 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001023 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001024 if (!si->err_type)
1025 si->err_type = SI_ET_CONN_OTHER;
1026 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001027 if (s->srv_error)
1028 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001029 return;
1030 }
1031
1032 /* The server is assigned */
1033 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1034 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001035 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001036}
1037
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001038/* This function parses the use-service action ruleset. It executes
1039 * the associated ACL and set an applet as a stream or txn final node.
1040 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1041 * consistent state. It returns ACT_RET_STOP in succes case because
1042 * use-service must be a terminal action. Returns ACT_RET_YIELD
1043 * if the initialisation function require more data.
1044 */
1045enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1046 struct session *sess, struct stream *s, int flags)
1047
1048{
1049 struct appctx *appctx;
1050
1051 /* Initialises the applet if it is required. */
1052 if (flags & ACT_FLAG_FIRST) {
1053 /* Register applet. this function schedules the applet. */
1054 s->target = &rule->applet.obj_type;
1055 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target))))
1056 return ACT_RET_ERR;
1057
1058 /* Initialise the context. */
1059 appctx = si_appctx(&s->si[1]);
1060 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1061 appctx->rule = rule;
1062 }
1063 else
1064 appctx = si_appctx(&s->si[1]);
1065
1066 /* Stops the applet sheduling, in case of the init function miss
1067 * some data.
1068 */
1069 appctx_pause(appctx);
1070 si_applet_stop_get(&s->si[1]);
1071
1072 /* Call initialisation. */
1073 if (rule->applet.init)
1074 switch (rule->applet.init(appctx, px, s)) {
1075 case 0: return ACT_RET_ERR;
1076 case 1: break;
1077 default: return ACT_RET_YIELD;
1078 }
1079
1080 /* Now we can schedule the applet. */
1081 si_applet_cant_get(&s->si[1]);
1082 appctx_wakeup(appctx);
1083
1084 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1085 sess->fe->fe_counters.intercepted_req++;
1086
1087 /* The flag SF_ASSIGNED prevent from server assignment. */
1088 s->flags |= SF_ASSIGNED;
1089
1090 return ACT_RET_STOP;
1091}
1092
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001093/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001094 * if appropriate. The default_backend rule is also considered, then the
1095 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001096 * It returns 1 if the processing can continue on next analysers, or zero if it
1097 * either needs more data or wants to immediately abort the request.
1098 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001099static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001100{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001101 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001102 struct session *sess = s->sess;
1103 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001104
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001105 req->analysers &= ~an_bit;
1106 req->analyse_exp = TICK_ETERNITY;
1107
Willy Tarreau87b09662015-04-03 00:22:06 +02001108 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 +02001109 now_ms, __FUNCTION__,
1110 s,
1111 req,
1112 req->rex, req->wex,
1113 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001114 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001115 req->analysers);
1116
1117 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001118 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001119 struct switching_rule *rule;
1120
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001121 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001122 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001123
Willy Tarreauf51658d2014-04-23 01:21:56 +02001124 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001125 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001126 ret = acl_pass(ret);
1127 if (rule->cond->pol == ACL_COND_UNLESS)
1128 ret = !ret;
1129 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001130
1131 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001132 /* If the backend name is dynamic, try to resolve the name.
1133 * If we can't resolve the name, or if any error occurs, break
1134 * the loop and fallback to the default backend.
1135 */
1136 struct proxy *backend;
1137
1138 if (rule->dynamic) {
1139 struct chunk *tmp = get_trash_chunk();
1140 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1141 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001142 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001143 if (!backend)
1144 break;
1145 }
1146 else
1147 backend = rule->be.backend;
1148
Willy Tarreau87b09662015-04-03 00:22:06 +02001149 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001150 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001151 break;
1152 }
1153 }
1154
1155 /* To ensure correct connection accounting on the backend, we
1156 * have to assign one if it was not set (eg: a listen). This
1157 * measure also takes care of correctly setting the default
1158 * backend if any.
1159 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001160 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001161 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001162 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001163 }
1164
Willy Tarreaufb356202010-08-03 14:02:05 +02001165 /* 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 +02001166 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001167 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1168 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001169 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001170
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001171 /* 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 +02001172 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001173 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001174 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001175 int ret = 1;
1176
1177 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001178 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 +01001179 ret = acl_pass(ret);
1180 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1181 ret = !ret;
1182 }
1183
1184 if (ret) {
1185 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001186 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001187 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001188 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001189 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001190 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001191 break;
1192 }
1193 }
1194
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001195 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001196
1197 sw_failed:
1198 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001199 channel_abort(&s->req);
1200 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001201
Willy Tarreaue7dff022015-04-03 01:14:29 +02001202 if (!(s->flags & SF_ERR_MASK))
1203 s->flags |= SF_ERR_RESOURCE;
1204 if (!(s->flags & SF_FINST_MASK))
1205 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001206
Willy Tarreaueee5b512015-04-03 23:46:31 +02001207 if (s->txn)
1208 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001209 s->req.analysers = 0;
1210 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001211 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001212}
1213
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001214/* This stream analyser works on a request. It applies all use-server rules on
1215 * it then returns 1. The data must already be present in the buffer otherwise
1216 * they won't match. It always returns 1.
1217 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001218static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001219{
1220 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001221 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001222 struct server_rule *rule;
1223
Willy Tarreau87b09662015-04-03 00:22:06 +02001224 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 +02001225 now_ms, __FUNCTION__,
1226 s,
1227 req,
1228 req->rex, req->wex,
1229 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001230 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001231 req->analysers);
1232
Willy Tarreaue7dff022015-04-03 01:14:29 +02001233 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001234 list_for_each_entry(rule, &px->server_rules, list) {
1235 int ret;
1236
Willy Tarreau192252e2015-04-04 01:47:55 +02001237 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001238 ret = acl_pass(ret);
1239 if (rule->cond->pol == ACL_COND_UNLESS)
1240 ret = !ret;
1241
1242 if (ret) {
1243 struct server *srv = rule->srv.ptr;
1244
Willy Tarreau892337c2014-05-13 23:41:20 +02001245 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001246 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001247 (s->flags & SF_FORCE_PRST)) {
1248 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001249 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001250 break;
1251 }
1252 /* if the server is not UP, let's go on with next rules
1253 * just in case another one is suited.
1254 */
1255 }
1256 }
1257 }
1258
1259 req->analysers &= ~an_bit;
1260 req->analyse_exp = TICK_ETERNITY;
1261 return 1;
1262}
1263
Emeric Brun1d33b292010-01-04 15:47:17 +01001264/* This stream analyser works on a request. It applies all sticking rules on
1265 * it then returns 1. The data must already be present in the buffer otherwise
1266 * they won't match. It always returns 1.
1267 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001268static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001269{
1270 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001271 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001272 struct sticking_rule *rule;
1273
Willy Tarreau87b09662015-04-03 00:22:06 +02001274 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 +01001275 now_ms, __FUNCTION__,
1276 s,
1277 req,
1278 req->rex, req->wex,
1279 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001280 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001281 req->analysers);
1282
1283 list_for_each_entry(rule, &px->sticking_rules, list) {
1284 int ret = 1 ;
1285 int i;
1286
Willy Tarreau9667a802013-12-09 12:52:13 +01001287 /* Only the first stick store-request of each table is applied
1288 * and other ones are ignored. The purpose is to allow complex
1289 * configurations which look for multiple entries by decreasing
1290 * order of precision and to stop at the first which matches.
1291 * An example could be a store of the IP address from an HTTP
1292 * header first, then from the source if not found.
1293 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001294 for (i = 0; i < s->store_count; i++) {
1295 if (rule->table.t == s->store[i].table)
1296 break;
1297 }
1298
1299 if (i != s->store_count)
1300 continue;
1301
1302 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001303 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001304 ret = acl_pass(ret);
1305 if (rule->cond->pol == ACL_COND_UNLESS)
1306 ret = !ret;
1307 }
1308
1309 if (ret) {
1310 struct stktable_key *key;
1311
Willy Tarreau192252e2015-04-04 01:47:55 +02001312 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 +01001313 if (!key)
1314 continue;
1315
1316 if (rule->flags & STK_IS_MATCH) {
1317 struct stksess *ts;
1318
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001319 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001320 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001321 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001322 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001323
1324 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001325 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1326 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001327 if (node) {
1328 struct server *srv;
1329
1330 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001331 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001332 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001333 (s->flags & SF_FORCE_PRST)) {
1334 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001335 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001336 }
1337 }
1338 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001339 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001340 }
1341 }
1342 if (rule->flags & STK_IS_STORE) {
1343 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1344 struct stksess *ts;
1345
1346 ts = stksess_new(rule->table.t, key);
1347 if (ts) {
1348 s->store[s->store_count].table = rule->table.t;
1349 s->store[s->store_count++].ts = ts;
1350 }
1351 }
1352 }
1353 }
1354 }
1355
1356 req->analysers &= ~an_bit;
1357 req->analyse_exp = TICK_ETERNITY;
1358 return 1;
1359}
1360
1361/* This stream analyser works on a response. It applies all store rules on it
1362 * then returns 1. The data must already be present in the buffer otherwise
1363 * they won't match. It always returns 1.
1364 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001365static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001366{
1367 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001368 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001369 struct sticking_rule *rule;
1370 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001371 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001372
Willy Tarreau87b09662015-04-03 00:22:06 +02001373 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 +01001374 now_ms, __FUNCTION__,
1375 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001376 rep,
1377 rep->rex, rep->wex,
1378 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001379 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001380 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001381
1382 list_for_each_entry(rule, &px->storersp_rules, list) {
1383 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001384
Willy Tarreau9667a802013-12-09 12:52:13 +01001385 /* Only the first stick store-response of each table is applied
1386 * and other ones are ignored. The purpose is to allow complex
1387 * configurations which look for multiple entries by decreasing
1388 * order of precision and to stop at the first which matches.
1389 * An example could be a store of a set-cookie value, with a
1390 * fallback to a parameter found in a 302 redirect.
1391 *
1392 * The store-response rules are not allowed to override the
1393 * store-request rules for the same table, but they may coexist.
1394 * Thus we can have up to one store-request entry and one store-
1395 * response entry for the same table at any time.
1396 */
1397 for (i = nbreq; i < s->store_count; i++) {
1398 if (rule->table.t == s->store[i].table)
1399 break;
1400 }
1401
1402 /* skip existing entries for this table */
1403 if (i < s->store_count)
1404 continue;
1405
Emeric Brun1d33b292010-01-04 15:47:17 +01001406 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001407 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001408 ret = acl_pass(ret);
1409 if (rule->cond->pol == ACL_COND_UNLESS)
1410 ret = !ret;
1411 }
1412
1413 if (ret) {
1414 struct stktable_key *key;
1415
Willy Tarreau192252e2015-04-04 01:47:55 +02001416 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 +01001417 if (!key)
1418 continue;
1419
Willy Tarreau37e340c2013-12-06 23:05:21 +01001420 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001421 struct stksess *ts;
1422
1423 ts = stksess_new(rule->table.t, key);
1424 if (ts) {
1425 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001426 s->store[s->store_count++].ts = ts;
1427 }
1428 }
1429 }
1430 }
1431
1432 /* process store request and store response */
1433 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001434 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001435 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001436
Willy Tarreauc93cd162014-05-13 15:54:22 +02001437 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001438 stksess_free(s->store[i].table, s->store[i].ts);
1439 s->store[i].ts = NULL;
1440 continue;
1441 }
1442
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001443 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1444 if (ts) {
1445 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001446 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001447 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001448 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001449 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001450 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001451
1452 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001453 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001454 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001455 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001456 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001457
1458 rep->analysers &= ~an_bit;
1459 rep->analyse_exp = TICK_ETERNITY;
1460 return 1;
1461}
1462
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001463/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001464 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001465 */
1466#define UPDATE_ANALYSERS(real, list, back, flag) { \
1467 list = (((list) & ~(flag)) | ~(back)) & (real); \
1468 back = real; \
1469 if (!(list)) \
1470 break; \
1471 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1472 continue; \
1473}
1474
Willy Tarreau87b09662015-04-03 00:22:06 +02001475/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001476 * then puts it back to the wait queue in a clean state, or cleans up its
1477 * resources if it must be deleted. Returns in <next> the date the task wants
1478 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1479 * nothing too many times, the request and response buffers flags are monitored
1480 * and each function is called only if at least another function has changed at
1481 * least one flag it is interested in.
1482 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001483struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001484{
Willy Tarreau827aee92011-03-10 16:55:02 +01001485 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001486 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001487 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001488 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001489 unsigned int rq_prod_last, rq_cons_last;
1490 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001491 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001492 struct channel *req, *res;
1493 struct stream_interface *si_f, *si_b;
1494
1495 req = &s->req;
1496 res = &s->res;
1497
1498 si_f = &s->si[0];
1499 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001500
1501 //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 +01001502 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001503
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001504 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001505 if (s->txn)
1506 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001507
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001508 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001509 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1510 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001511
1512 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001513 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1514 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001515
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001516 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001517 si_f->flags |= SI_FL_DONT_WAKE;
1518 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001519
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001520 /* 1a: Check for low level timeouts if needed. We just set a flag on
1521 * stream interfaces when their timeouts have expired.
1522 */
1523 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001524 stream_int_check_timeouts(si_f);
1525 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001526
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001527 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001528 * for future reads or writes. Note: this will also concern upper layers
1529 * but we do not touch any other flag. We must be careful and correctly
1530 * detect state changes when calling them.
1531 */
1532
Willy Tarreau8f128b42014-11-28 15:07:47 +01001533 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001534
Willy Tarreau8f128b42014-11-28 15:07:47 +01001535 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1536 si_b->flags |= SI_FL_NOLINGER;
1537 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001538 }
1539
Willy Tarreau8f128b42014-11-28 15:07:47 +01001540 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1541 if (si_f->flags & SI_FL_NOHALF)
1542 si_f->flags |= SI_FL_NOLINGER;
1543 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001544 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001545
Willy Tarreau8f128b42014-11-28 15:07:47 +01001546 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001547
Willy Tarreau8f128b42014-11-28 15:07:47 +01001548 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1549 si_f->flags |= SI_FL_NOLINGER;
1550 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001551 }
1552
Willy Tarreau8f128b42014-11-28 15:07:47 +01001553 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1554 if (si_b->flags & SI_FL_NOHALF)
1555 si_b->flags |= SI_FL_NOLINGER;
1556 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001557 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001558
1559 /* Once in a while we're woken up because the task expires. But
1560 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001561 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001562 * timeout needs to be refreshed.
1563 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001564 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001565 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1566 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001567 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001568 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1569 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001570 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001571
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001572 /* below we may emit error messages so we have to ensure that we have
1573 * our buffers properly allocated.
1574 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001575 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001576 /* No buffer available, we've been subscribed to the list of
1577 * buffer waiters, let's wait for our turn.
1578 */
1579 goto update_exp_and_leave;
1580 }
1581
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001582 /* 1b: check for low-level errors reported at the stream interface.
1583 * First we check if it's a retryable error (in which case we don't
1584 * want to tell the buffer). Otherwise we report the error one level
1585 * upper by setting flags into the buffers. Note that the side towards
1586 * the client cannot have connect (hence retryable) errors. Also, the
1587 * connection setup code must be able to deal with any type of abort.
1588 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001589 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001590 if (unlikely(si_f->flags & SI_FL_ERR)) {
1591 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1592 si_shutr(si_f);
1593 si_shutw(si_f);
1594 stream_int_report_error(si_f);
1595 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001596 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001597 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001598 if (srv)
1599 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001600 if (!(s->flags & SF_ERR_MASK))
1601 s->flags |= SF_ERR_CLICL;
1602 if (!(s->flags & SF_FINST_MASK))
1603 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001604 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001605 }
1606 }
1607
Willy Tarreau8f128b42014-11-28 15:07:47 +01001608 if (unlikely(si_b->flags & SI_FL_ERR)) {
1609 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1610 si_shutr(si_b);
1611 si_shutw(si_b);
1612 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001613 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001614 if (srv)
1615 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001616 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001617 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001618 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001619 if (srv)
1620 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001621 if (!(s->flags & SF_ERR_MASK))
1622 s->flags |= SF_ERR_SRVCL;
1623 if (!(s->flags & SF_FINST_MASK))
1624 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001625 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001626 }
1627 /* note: maybe we should process connection errors here ? */
1628 }
1629
Willy Tarreau8f128b42014-11-28 15:07:47 +01001630 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001631 /* we were trying to establish a connection on the server side,
1632 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1633 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001634 if (unlikely(!sess_update_st_con_tcp(s)))
1635 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001636 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001637 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001638
1639 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1640 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1641 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1642 */
1643 }
1644
Willy Tarreau8f128b42014-11-28 15:07:47 +01001645 rq_prod_last = si_f->state;
1646 rq_cons_last = si_b->state;
1647 rp_cons_last = si_f->state;
1648 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001649
1650 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001651 /* Check for connection closure */
1652
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001653 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001654 "[%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 +01001655 now_ms, __FUNCTION__, __LINE__,
1656 t,
1657 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001658 req, res,
1659 req->rex, res->wex,
1660 req->flags, res->flags,
1661 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1662 si_f->err_type, si_b->err_type,
1663 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001664
1665 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001666 if (unlikely(si_f->state == SI_ST_DIS))
1667 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001668
1669 /* When a server-side connection is released, we have to count it and
1670 * check for pending connections on this server.
1671 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001672 if (unlikely(si_b->state == SI_ST_DIS)) {
1673 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001674 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001675 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001676 if (s->flags & SF_CURR_SESS) {
1677 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001678 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001679 }
1680 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001681 if (may_dequeue_tasks(srv, s->be))
1682 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001683 }
1684 }
1685
1686 /*
1687 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1688 * at this point.
1689 */
1690
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001691 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001692 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001693 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1694 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1695 si_f->state != rq_prod_last ||
1696 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001697 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001698 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001699
Willy Tarreau8f128b42014-11-28 15:07:47 +01001700 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001701 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001702 unsigned int ana_list;
1703 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001704
Willy Tarreau90deb182010-01-07 00:20:41 +01001705 /* it's up to the analysers to stop new connections,
1706 * disable reading or closing. Note: if an analyser
1707 * disables any of these bits, it is responsible for
1708 * enabling them again when it disables itself, so
1709 * that other analysers are called in similar conditions.
1710 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001711 channel_auto_read(req);
1712 channel_auto_connect(req);
1713 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001714
1715 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001716 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001717 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001718 * the list when not needed. Any analyser may return 0
1719 * to break out of the loop, either because of missing
1720 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001721 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001722 * analyser, and we may loop again if other analysers
1723 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001724 *
1725 * We build a list of analysers to run. We evaluate all
1726 * of these analysers in the order of the lower bit to
1727 * the higher bit. This ordering is very important.
1728 * An analyser will often add/remove other analysers,
1729 * including itself. Any changes to itself have no effect
1730 * on the loop. If it removes any other analysers, we
1731 * want those analysers not to be called anymore during
1732 * this loop. If it adds an analyser that is located
1733 * after itself, we want it to be scheduled for being
1734 * processed during the loop. If it adds an analyser
1735 * which is located before it, we want it to switch to
1736 * it immediately, even if it has already been called
1737 * once but removed since.
1738 *
1739 * In order to achieve this, we compare the analyser
1740 * list after the call with a copy of it before the
1741 * call. The work list is fed with analyser bits that
1742 * appeared during the call. Then we compare previous
1743 * work list with the new one, and check the bits that
1744 * appeared. If the lowest of these bits is lower than
1745 * the current bit, it means we have enabled a previous
1746 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001747 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001748
Willy Tarreau8f128b42014-11-28 15:07:47 +01001749 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001750 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001751 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001752
Willy Tarreaufb356202010-08-03 14:02:05 +02001753 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001754 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001755 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001756 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001757 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001758
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001759 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001760 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001761 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001762 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001763 }
1764
Willy Tarreau748179e2015-05-01 21:52:31 +02001765 if (ana_list & AN_REQ_HTTP_BODY) {
1766 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
1767 break;
1768 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
1769 }
1770
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001771 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001772 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001773 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001774 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001775 }
1776
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001777 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001778 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001779 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001780 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001781 }
1782
Willy Tarreaufb356202010-08-03 14:02:05 +02001783 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001784 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001785 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001786 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001787 }
1788
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001789 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001790 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001791 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001792 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001793 }
1794
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001795 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001796 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001797 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001798 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001799 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001800
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001801 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001802 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001803 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001804 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001805 }
1806
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001807 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001808 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001809 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001810 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001811 }
1812
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001813 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001814 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001815 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001816 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001817 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001818
Emeric Brun1d33b292010-01-04 15:47:17 +01001819 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001820 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001821 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001822 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001823 }
1824
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001825 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001826 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001827 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001828 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001829 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001830 break;
1831 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001832 }
Willy Tarreau84455332009-03-15 22:34:05 +01001833
Willy Tarreau8f128b42014-11-28 15:07:47 +01001834 rq_prod_last = si_f->state;
1835 rq_cons_last = si_b->state;
1836 req->flags &= ~CF_WAKE_ONCE;
1837 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001838
Willy Tarreau8f128b42014-11-28 15:07:47 +01001839 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001840 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001841 }
1842
Willy Tarreau576507f2010-01-07 00:09:04 +01001843 /* we'll monitor the request analysers while parsing the response,
1844 * because some response analysers may indirectly enable new request
1845 * analysers (eg: HTTP keep-alive).
1846 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001847 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001848
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001849 resync_response:
1850 /* Analyse response */
1851
Willy Tarreau8f128b42014-11-28 15:07:47 +01001852 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1853 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1854 si_f->state != rp_cons_last ||
1855 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001856 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001857 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001858
Willy Tarreau8f128b42014-11-28 15:07:47 +01001859 if ((res->flags & CF_MASK_ANALYSER) &&
1860 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001861 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1862 * for some free space in the response buffer, so we might need to call
1863 * it when something changes in the response buffer, but still we pass
1864 * it the request buffer. Note that the SI state might very well still
1865 * be zero due to us returning a flow of redirects!
1866 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001867 res->analysers &= ~AN_REQ_ALL;
1868 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001869 }
1870
Willy Tarreau8f128b42014-11-28 15:07:47 +01001871 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001872 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001873 unsigned int ana_list;
1874 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001875
Willy Tarreau90deb182010-01-07 00:20:41 +01001876 /* it's up to the analysers to stop disable reading or
1877 * closing. Note: if an analyser disables any of these
1878 * bits, it is responsible for enabling them again when
1879 * it disables itself, so that other analysers are called
1880 * in similar conditions.
1881 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001882 channel_auto_read(res);
1883 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001884
1885 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001886 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001887 * to MSB. The analysers must remove themselves from
1888 * the list when not needed. Any analyser may return 0
1889 * to break out of the loop, either because of missing
1890 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001891 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001892 * analyser, and we may loop again if other analysers
1893 * are added in the middle.
1894 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001895
Willy Tarreau8f128b42014-11-28 15:07:47 +01001896 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001897 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001898 /* Warning! ensure that analysers are always placed in ascending order! */
1899
Emeric Brun97679e72010-09-23 17:56:44 +02001900 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001901 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001902 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001903 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001904 }
1905
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001906 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001907 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001908 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001909 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001910 }
1911
Emeric Brun1d33b292010-01-04 15:47:17 +01001912 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001913 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001914 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001915 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001916 }
1917
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001918 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001919 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001920 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001921 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001922 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001923
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001924 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001925 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001926 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001927 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001928 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001929 break;
1930 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001931 }
1932
Willy Tarreau8f128b42014-11-28 15:07:47 +01001933 rp_cons_last = si_f->state;
1934 rp_prod_last = si_b->state;
1935 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001936
Willy Tarreau8f128b42014-11-28 15:07:47 +01001937 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001938 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001939 }
1940
Willy Tarreau576507f2010-01-07 00:09:04 +01001941 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001942 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001943 goto resync_request;
1944
Willy Tarreau8f128b42014-11-28 15:07:47 +01001945 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001946 goto resync_request;
1947
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001948 /* FIXME: here we should call protocol handlers which rely on
1949 * both buffers.
1950 */
1951
1952
1953 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001954 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001955 * we're just in a data phase here since it means we have not
1956 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001957 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001958 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001959 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001960 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001961 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001962 req->analysers = 0;
1963 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001964 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001965 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001966 if (srv)
1967 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001968 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001969 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001970 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001971 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001972 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001973 if (srv)
1974 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001975 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001976 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001977 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001978 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001979 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001980 if (srv)
1981 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001982 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001983 }
1984 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001985 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001986 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001987 if (srv)
1988 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001989 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001990 }
Willy Tarreau84455332009-03-15 22:34:05 +01001991 sess_set_term_flags(s);
1992 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001993 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001994 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001995 res->analysers = 0;
1996 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001997 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001998 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001999 if (srv)
2000 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002001 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002002 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002003 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002004 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002005 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002006 if (srv)
2007 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002008 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002009 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002010 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002011 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002012 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002013 if (srv)
2014 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002015 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002016 }
2017 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002018 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002019 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002020 if (srv)
2021 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002022 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002023 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002024 sess_set_term_flags(s);
2025 }
Willy Tarreau84455332009-03-15 22:34:05 +01002026 }
2027
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002028 /*
2029 * Here we take care of forwarding unhandled data. This also includes
2030 * connection establishments and shutdown requests.
2031 */
2032
2033
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002034 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002035 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002036 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002037 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002038 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002039 if (unlikely(!req->analysers &&
2040 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2041 (si_f->state >= SI_ST_EST) &&
2042 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002043 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002044 * attached to it. If any data are left in, we'll permit them to
2045 * move.
2046 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002047 channel_auto_read(req);
2048 channel_auto_connect(req);
2049 channel_auto_close(req);
2050 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002051
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002052 /* We'll let data flow between the producer (if still connected)
2053 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002054 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002055 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2056 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau42529c32015-07-09 18:38:57 +02002057
2058 /* Just in order to support fetching HTTP contents after start
2059 * of forwarding when the HTTP forwarding analyser is not used,
2060 * we simply reset msg->sov so that HTTP rewinding points to the
2061 * headers.
2062 */
2063 if (s->txn)
2064 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - req->buf->o;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002065 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002066
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002067 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002068 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2069 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002070 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002071 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2072 (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 +01002073 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002074 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2075 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002076 (req->flags & CF_STREAMER_FAST)))) {
2077 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002078 }
2079
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002080 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002081 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002082
2083 /*
2084 * Now forward all shutdown requests between both sides of the buffer
2085 */
2086
Willy Tarreau520d95e2009-09-19 21:04:57 +02002087 /* first, let's check if the request buffer needs to shutdown(write), which may
2088 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002089 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2090 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002091 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002092 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002093 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002094 channel_shutw_now(req);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002095 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002096
2097 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002098 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2099 channel_is_empty(req))) {
2100 if (req->flags & CF_READ_ERROR)
2101 si_b->flags |= SI_FL_NOLINGER;
2102 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002103 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002104 res->rto = s->be->timeout.serverfin;
2105 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002106 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002107 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002108
2109 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002110 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2111 !req->analysers))
2112 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002113
2114 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002115 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2116 if (si_f->flags & SI_FL_NOHALF)
2117 si_f->flags |= SI_FL_NOLINGER;
2118 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002119 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002120
Willy Tarreau520d95e2009-09-19 21:04:57 +02002121 /* it's possible that an upper layer has requested a connection setup or abort.
2122 * There are 2 situations where we decide to establish a new connection :
2123 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002124 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002125 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002126 if (si_b->state == SI_ST_INI) {
2127 if (!(req->flags & CF_SHUTW)) {
2128 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002129 /* If we have an appctx, there is no connect method, so we
2130 * immediately switch to the connected state, otherwise we
2131 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002132 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002133 si_b->state = SI_ST_REQ; /* new connection requested */
2134 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002135 }
Willy Tarreau73201222009-08-16 18:27:24 +02002136 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002137 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002138 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2139 channel_shutw_now(req); /* fix buffer flags upon abort */
2140 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002141 }
Willy Tarreau92795622009-03-06 12:51:23 +01002142 }
2143
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002144
2145 /* we may have a pending connection request, or a connection waiting
2146 * for completion.
2147 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002148 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002149
2150 /* prune the request variables and swap to the response variables. */
2151 if (s->vars_reqres.scope != SCOPE_RES) {
2152 vars_prune(&s->vars_reqres, s);
2153 vars_init(&s->vars_reqres, SCOPE_RES);
2154 }
2155
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002156 do {
2157 /* nb: step 1 might switch from QUE to ASS, but we first want
2158 * to give a chance to step 2 to perform a redirect if needed.
2159 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002160 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002161 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002162 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002163 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002164
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002165 /* applets directly go to the ESTABLISHED state. Similarly,
2166 * servers experience the same fate when their connection
2167 * is reused.
2168 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002169 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002170 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002171
Willy Tarreaub44c8732013-12-31 23:16:50 +01002172 /* Now we can add the server name to a header (if requested) */
2173 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002174 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002175 (s->be->server_id_hdr_name != NULL) &&
2176 (s->be->mode == PR_MODE_HTTP) &&
2177 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002178 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002179 }
2180
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002181 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002182 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002183 http_perform_server_redirect(s, si_b);
2184 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002185 }
2186
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002187 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002188 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002189 goto resync_stream_interface;
2190
Willy Tarreau815a9b22010-07-27 17:15:12 +02002191 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002192 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002193 goto resync_request;
2194
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002195 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002196
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002197 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002198 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002199 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002200 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002201 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002202 if (unlikely(!res->analysers &&
2203 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2204 (si_b->state >= SI_ST_EST) &&
2205 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002206 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002207 * attached to it. If any data are left in, we'll permit them to
2208 * move.
2209 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002210 channel_auto_read(res);
2211 channel_auto_close(res);
2212 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002213
2214 /* We'll let data flow between the producer (if still connected)
2215 * to the consumer.
2216 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002217 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2218 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002219
Willy Tarreau42529c32015-07-09 18:38:57 +02002220 /* Just in order to support fetching HTTP contents after start
2221 * of forwarding when the HTTP forwarding analyser is not used,
2222 * we simply reset msg->sov so that HTTP rewinding points to the
2223 * headers.
2224 */
2225 if (s->txn)
2226 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2227
Willy Tarreauce887fd2012-05-12 12:50:00 +02002228 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002229 * tunnel timeout set, use it now. Note that we must respect
2230 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002231 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002232 if (!req->analysers && s->be->timeout.tunnel) {
2233 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002234 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002235
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002236 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2237 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002238 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2239 res->rto = s->be->timeout.serverfin;
2240 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2241 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002242 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2243 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002244
Willy Tarreau8f128b42014-11-28 15:07:47 +01002245 req->rex = tick_add(now_ms, req->rto);
2246 req->wex = tick_add(now_ms, req->wto);
2247 res->rex = tick_add(now_ms, res->rto);
2248 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002249 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002250 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002251
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002252 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002253 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2254 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002255 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002256 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2257 (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 +01002258 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002259 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2260 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002261 (res->flags & CF_STREAMER_FAST)))) {
2262 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002263 }
2264
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002265 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002266 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002267
2268 /*
2269 * Now forward all shutdown requests between both sides of the buffer
2270 */
2271
2272 /*
2273 * FIXME: this is probably where we should produce error responses.
2274 */
2275
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002276 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002277 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002278 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002279 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002280 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002281
2282 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002283 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2284 channel_is_empty(res))) {
2285 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002286 if (tick_isset(sess->fe->timeout.clientfin)) {
2287 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002288 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002289 }
2290 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002291
2292 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002293 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2294 !res->analysers)
2295 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002296
2297 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002298 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2299 if (si_b->flags & SI_FL_NOHALF)
2300 si_b->flags |= SI_FL_NOLINGER;
2301 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002302 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002303
Willy Tarreau8f128b42014-11-28 15:07:47 +01002304 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002305 goto resync_stream_interface;
2306
Willy Tarreau8f128b42014-11-28 15:07:47 +01002307 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002308 goto resync_request;
2309
Willy Tarreau8f128b42014-11-28 15:07:47 +01002310 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002311 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002312
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002313 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002314 si_f->flags &= ~SI_FL_DONT_WAKE;
2315 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002316
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002317 /* This is needed only when debugging is enabled, to indicate
2318 * client-side or server-side close. Please note that in the unlikely
2319 * event where both sides would close at once, the sequence is reported
2320 * on the server side first.
2321 */
2322 if (unlikely((global.mode & MODE_DEBUG) &&
2323 (!(global.mode & MODE_QUIET) ||
2324 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002325 if (si_b->state == SI_ST_CLO &&
2326 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002327 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002328 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002329 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2330 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002331 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002332 }
2333
Willy Tarreau8f128b42014-11-28 15:07:47 +01002334 if (si_f->state == SI_ST_CLO &&
2335 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002336 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002337 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002338 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2339 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002340 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002341 }
2342 }
2343
Willy Tarreau8f128b42014-11-28 15:07:47 +01002344 if (likely((si_f->state != SI_ST_CLO) ||
2345 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002346
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002347 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002348 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002349
Willy Tarreau563cc372015-04-19 18:13:56 +02002350 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002351 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002352
Willy Tarreau563cc372015-04-19 18:13:56 +02002353 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002354 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002355
Willy Tarreau8f128b42014-11-28 15:07:47 +01002356 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2357 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2358 si_f->prev_state = si_f->state;
2359 si_b->prev_state = si_b->state;
2360 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2361 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002362
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002363 /* Trick: if a request is being waiting for the server to respond,
2364 * and if we know the server can timeout, we don't want the timeout
2365 * to expire on the client side first, but we're still interested
2366 * in passing data from the client to the server (eg: POST). Thus,
2367 * we can cancel the client's request timeout if the server's
2368 * request timeout is set and the server has not yet sent a response.
2369 */
2370
Willy Tarreau8f128b42014-11-28 15:07:47 +01002371 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2372 (tick_isset(req->wex) || tick_isset(res->rex))) {
2373 req->flags |= CF_READ_NOEXP;
2374 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002375 }
2376
Willy Tarreau798f4322012-11-08 14:49:17 +01002377 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002378 t->expire = tick_first(tick_first(req->rex, req->wex),
2379 tick_first(res->rex, res->wex));
2380 if (req->analysers)
2381 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002382
Willy Tarreau8f128b42014-11-28 15:07:47 +01002383 if (si_f->exp)
2384 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002385
Willy Tarreau8f128b42014-11-28 15:07:47 +01002386 if (si_b->exp)
2387 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002388
2389#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002390 fprintf(stderr,
2391 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2392 " 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 +01002393 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2394 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002395#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002396
2397#ifdef DEBUG_DEV
2398 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002399 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002400 ABORT_NOW();
2401#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002402 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002403 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002404 }
2405
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002406 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002407 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002408 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002409 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002410 if (sess->listener) {
2411 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002412 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002413 sess->listener->nbconn--;
2414 if (sess->listener->state == LI_FULL)
2415 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002416
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002417 /* Dequeues all of the listeners waiting for a resource */
2418 if (!LIST_ISEMPTY(&global_listener_queue))
2419 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002420
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002421 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2422 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2423 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002424 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002425
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002426 if (unlikely((global.mode & MODE_DEBUG) &&
2427 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002428 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002429 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002430 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2431 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002432 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002433 }
2434
2435 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002436 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002437
Willy Tarreaueee5b512015-04-03 23:46:31 +02002438 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002439 int n;
2440
Willy Tarreaueee5b512015-04-03 23:46:31 +02002441 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002442 if (n < 1 || n > 5)
2443 n = 0;
2444
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002445 if (sess->fe->mode == PR_MODE_HTTP) {
2446 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002447 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002448 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002449 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002450 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002451 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002452 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002453 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002454 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002455 s->be->be_counters.p.http.comp_rsp++;
2456 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002457 }
2458
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002459 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002460 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002461 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002462 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002463 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002464 }
2465
Willy Tarreau87b09662015-04-03 00:22:06 +02002466 /* update time stats for this stream */
2467 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002468
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002469 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002470 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002471 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002472 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002473 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002474}
2475
Willy Tarreau87b09662015-04-03 00:22:06 +02002476/* Update the stream's backend and server time stats */
2477void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002478{
2479 int t_request;
2480 int t_queue;
2481 int t_connect;
2482 int t_data;
2483 int t_close;
2484 struct server *srv;
2485
2486 t_request = 0;
2487 t_queue = s->logs.t_queue;
2488 t_connect = s->logs.t_connect;
2489 t_close = s->logs.t_close;
2490 t_data = s->logs.t_data;
2491
2492 if (s->be->mode != PR_MODE_HTTP)
2493 t_data = t_connect;
2494
2495 if (t_connect < 0 || t_data < 0)
2496 return;
2497
2498 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2499 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2500
2501 t_data -= t_connect;
2502 t_connect -= t_queue;
2503 t_queue -= t_request;
2504
2505 srv = objt_server(s->target);
2506 if (srv) {
2507 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2508 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2509 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2510 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2511 }
2512 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2513 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2514 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2515 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2516}
2517
Willy Tarreau7c669d72008-06-20 15:04:11 +02002518/*
2519 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002520 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002521 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002522 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002523 * server.
2524 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002525void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002526{
2527 if (sess->srv_conn == newsrv)
2528 return;
2529
2530 if (sess->srv_conn) {
2531 sess->srv_conn->served--;
2532 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2533 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002534 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002535 }
2536
2537 if (newsrv) {
2538 newsrv->served++;
2539 if (newsrv->proxy->lbprm.server_take_conn)
2540 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002541 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002542 }
2543}
2544
Willy Tarreau84455332009-03-15 22:34:05 +01002545/* Handle server-side errors for default protocols. It is called whenever a a
2546 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002547 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002548 * them. It's installed as ->srv_error for the server-side stream_interface.
2549 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002550void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002551{
2552 int err_type = si->err_type;
2553 int err = 0, fin = 0;
2554
2555 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002556 err = SF_ERR_CLICL;
2557 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002558 }
2559 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002560 err = SF_ERR_CLICL;
2561 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002562 }
2563 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002564 err = SF_ERR_SRVTO;
2565 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002566 }
2567 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002568 err = SF_ERR_SRVCL;
2569 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002570 }
2571 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002572 err = SF_ERR_SRVTO;
2573 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002574 }
2575 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002576 err = SF_ERR_SRVCL;
2577 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002578 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002579 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002580 err = SF_ERR_RESOURCE;
2581 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002582 }
Willy Tarreau84455332009-03-15 22:34:05 +01002583 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002584 err = SF_ERR_INTERNAL;
2585 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002586 }
2587
Willy Tarreaue7dff022015-04-03 01:14:29 +02002588 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002589 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002590 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002591 s->flags |= fin;
2592}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002593
Willy Tarreaue7dff022015-04-03 01:14:29 +02002594/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002595void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002596{
Willy Tarreau87b09662015-04-03 00:22:06 +02002597 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002598 return;
2599
Willy Tarreau87b09662015-04-03 00:22:06 +02002600 channel_shutw_now(&stream->req);
2601 channel_shutr_now(&stream->res);
2602 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002603 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002604 stream->flags |= why;
2605 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002606}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002607
Willy Tarreau8b22a712010-06-18 17:46:06 +02002608/************************************************************************/
2609/* All supported ACL keywords must be declared here. */
2610/************************************************************************/
2611
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002612/* Returns a pointer to a stkctr depending on the fetch keyword name.
2613 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002614 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002615 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002616 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002617 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002618 * NULL may be returned if the designated stkctr is not tracked. For
2619 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2620 * passed. When present, the currently tracked key is then looked up
2621 * in the specified table instead of the current table. The purpose is
2622 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002623 * multiple tables). <strm> is allowed to be NULL, in which case only
2624 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002625 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002626struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002627smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002628{
2629 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002630 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002631 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002632 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002633 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002634
Willy Tarreau0f791d42013-07-23 19:56:43 +02002635 if (num == '_' - '0') {
2636 /* sc_* variant, args[0] = ctr# (mandatory) */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002637 num = args[arg++].data.sint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002638 if (num >= MAX_SESS_STKCTR)
2639 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002640 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002641 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002642 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002643 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002644 struct sample smp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002645
2646 if (!conn)
2647 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002648
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002649 /* Fetch source adress in a sample. */
2650 smp.px = NULL;
2651 smp.sess = sess;
2652 smp.strm = strm;
2653 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2654 return NULL;
2655
2656 /* Converts into key. */
2657 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002658 if (!key)
2659 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002660
Willy Tarreaua65536c2013-07-22 22:40:11 +02002661 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002662 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002663 return &stkctr;
2664 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002665
2666 /* Here, <num> contains the counter number from 0 to 9 for
2667 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002668 * args[arg] is the first optional argument. We first lookup the
2669 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002670 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002671
2672 stkptr = &strm->stkctr[num];
2673 if (!strm || !stkctr_entry(stkptr)) {
2674 stkptr = &sess->stkctr[num];
2675 if (!stkctr_entry(stkptr))
2676 return NULL;
2677 }
2678
2679 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002680 if (!stksess)
2681 return NULL;
2682
Willy Tarreau0f791d42013-07-23 19:56:43 +02002683 if (unlikely(args[arg].type == ARGT_TAB)) {
2684 /* an alternate table was specified, let's look up the same key there */
2685 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002686 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002687 return &stkctr;
2688 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002689 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002690}
2691
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002692/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2693 * the entry if it doesn't exist yet. This is needed for a few fetch
2694 * functions which need to create an entry, such as src_inc_gpc* and
2695 * src_clr_gpc*.
2696 */
2697struct stkctr *
2698smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
2699{
2700 static struct stkctr stkctr;
2701 struct stktable_key *key;
2702 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002703 struct sample smp;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002704
2705 if (strncmp(kw, "src_", 4) != 0)
2706 return NULL;
2707
2708 if (!conn)
2709 return NULL;
2710
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002711 /* Fetch source adress in a sample. */
2712 smp.px = NULL;
2713 smp.sess = sess;
2714 smp.strm = strm;
2715 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2716 return NULL;
2717
2718 /* Converts into key. */
2719 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002720 if (!key)
2721 return NULL;
2722
2723 stkctr.table = &args->data.prx->table;
2724 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
2725 return &stkctr;
2726}
2727
Willy Tarreau87b09662015-04-03 00:22:06 +02002728/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002729 * currently being tracked or not.
2730 * Supports being called as "sc[0-9]_tracked" only.
2731 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002732static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002733smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002734{
2735 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002736 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002737 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002738 return 1;
2739}
2740
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002741/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2742 * frontend counters or from the src.
2743 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2744 * zero is returned if the key is new.
2745 */
2746static int
2747smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2748{
2749 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2750
2751 if (!stkctr)
2752 return 0;
2753
2754 smp->flags = SMP_F_VOL_TEST;
2755 smp->data.type = SMP_T_SINT;
2756 smp->data.u.sint = 0;
2757
2758 if (stkctr_entry(stkctr) != NULL) {
2759 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2760 if (!ptr)
2761 return 0; /* parameter not stored */
2762 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
2763 }
2764 return 1;
2765}
2766
Willy Tarreau87b09662015-04-03 00:22:06 +02002767/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002768 * frontend counters or from the src.
2769 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2770 * zero is returned if the key is new.
2771 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002772static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002773smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002774{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002775 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002776
2777 if (!stkctr)
2778 return 0;
2779
Willy Tarreau37406352012-04-23 16:16:37 +02002780 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002781 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002782 smp->data.u.sint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002783
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002784 if (stkctr_entry(stkctr) != NULL) {
2785 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002786 if (!ptr)
2787 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002788 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002789 }
2790 return 1;
2791}
2792
Willy Tarreau87b09662015-04-03 00:22:06 +02002793/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002794 * tracked frontend counters or from the src.
2795 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2796 * Value zero is returned if the key is new.
2797 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002798static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002799smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002800{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002801 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002802
2803 if (!stkctr)
2804 return 0;
2805
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002806 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002807 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002808 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002809 if (stkctr_entry(stkctr) != NULL) {
2810 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002811 if (!ptr)
2812 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002813 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002814 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002815 }
2816 return 1;
2817}
2818
Willy Tarreau87b09662015-04-03 00:22:06 +02002819/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002820 * frontend counters and return it into temp integer.
2821 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002822 */
2823static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002824smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002825{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002826 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002827
2828 if (!stkctr)
2829 return 0;
2830
Willy Tarreau37406352012-04-23 16:16:37 +02002831 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002832 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002833 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002834
2835 if (stkctr_entry(stkctr) == NULL)
2836 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2837
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002838 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun57056f02015-06-15 18:29:57 +02002839 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002840
2841 /* First, update gpc0_rate if it's tracked. Second, update its
2842 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2843 */
Emeric Brun57056f02015-06-15 18:29:57 +02002844 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2845 if (ptr1) {
2846 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002847 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002848 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002849 }
2850
Emeric Brun57056f02015-06-15 18:29:57 +02002851 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2852 if (ptr2)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002853 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002854
Emeric Brun57056f02015-06-15 18:29:57 +02002855 /* If data was modified, we need to touch to re-schedule sync */
2856 if (ptr1 || ptr2)
2857 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002858 }
2859 return 1;
2860}
2861
Willy Tarreau87b09662015-04-03 00:22:06 +02002862/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002863 * frontend counters and return its previous value into temp integer.
2864 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002865 */
2866static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002867smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002868{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002869 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002870
2871 if (!stkctr)
2872 return 0;
2873
Willy Tarreau37406352012-04-23 16:16:37 +02002874 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002875 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002876 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002877
2878 if (stkctr_entry(stkctr) == NULL)
2879 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2880
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002881 if (stkctr_entry(stkctr) != NULL) {
2882 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002883 if (!ptr)
2884 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002885 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002886 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002887 /* If data was modified, we need to touch to re-schedule sync */
2888 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002889 }
2890 return 1;
2891}
2892
Willy Tarreau87b09662015-04-03 00:22:06 +02002893/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002894 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2895 * "src_conn_cnt" only.
2896 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002897static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002898smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002899{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002900 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002901
2902 if (!stkctr)
2903 return 0;
2904
Willy Tarreau37406352012-04-23 16:16:37 +02002905 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002906 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002907 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002908 if (stkctr_entry(stkctr) != NULL) {
2909 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002910 if (!ptr)
2911 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002912 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002913 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002914 return 1;
2915}
2916
Willy Tarreau87b09662015-04-03 00:22:06 +02002917/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002918 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2919 * only.
2920 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002921static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002922smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002923{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002924 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002925
2926 if (!stkctr)
2927 return 0;
2928
Willy Tarreau37406352012-04-23 16:16:37 +02002929 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002930 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002931 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002932 if (stkctr_entry(stkctr) != NULL) {
2933 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002934 if (!ptr)
2935 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002936 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002937 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002938 }
2939 return 1;
2940}
2941
Willy Tarreau87b09662015-04-03 00:22:06 +02002942/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002943 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002944 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002945 */
2946static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002947smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002948{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002949 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002950 struct stksess *ts;
2951 struct stktable_key *key;
2952 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002953 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002954
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002955 if (!conn)
2956 return 0;
2957
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002958 /* Fetch source adress in a sample. */
2959 if (!smp_fetch_src(NULL, smp, NULL, NULL))
2960 return 0;
2961
2962 /* Converts into key. */
2963 key = smp_to_stkey(smp, &args->data.prx->table);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002964 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002965 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002966
Willy Tarreau24e32d82012-04-23 23:55:44 +02002967 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002968
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002969 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2970 /* entry does not exist and could not be created */
2971 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002972
2973 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2974 if (!ptr)
2975 return 0; /* parameter not stored in this table */
2976
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002977 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002978 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02002979 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02002980 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002981 return 1;
2982}
2983
Willy Tarreau87b09662015-04-03 00:22:06 +02002984/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002985 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2986 * "src_conn_cur" only.
2987 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002988static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002989smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002990{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002991 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002992
2993 if (!stkctr)
2994 return 0;
2995
Willy Tarreau37406352012-04-23 16:16:37 +02002996 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002997 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002998 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002999 if (stkctr_entry(stkctr) != NULL) {
3000 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003001 if (!ptr)
3002 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003003 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003004 }
3005 return 1;
3006}
3007
Willy Tarreau87b09662015-04-03 00:22:06 +02003008/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02003009 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3010 * "src_sess_cnt" only.
3011 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003012static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003013smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003014{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003015 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02003016
3017 if (!stkctr)
3018 return 0;
3019
Willy Tarreau37406352012-04-23 16:16:37 +02003020 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003021 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003022 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003023 if (stkctr_entry(stkctr) != NULL) {
3024 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003025 if (!ptr)
3026 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003027 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003028 }
3029 return 1;
3030}
3031
Willy Tarreau87b09662015-04-03 00:22:06 +02003032/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003033 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3034 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003035static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003036smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003037{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003038 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003039
3040 if (!stkctr)
3041 return 0;
3042
Willy Tarreau37406352012-04-23 16:16:37 +02003043 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003044 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003045 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003046 if (stkctr_entry(stkctr) != NULL) {
3047 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003048 if (!ptr)
3049 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003050 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003051 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003052 }
3053 return 1;
3054}
3055
Willy Tarreau87b09662015-04-03 00:22:06 +02003056/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02003057 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3058 * "src_http_req_cnt" only.
3059 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003060static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003061smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003062{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003063 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02003064
3065 if (!stkctr)
3066 return 0;
3067
Willy Tarreau37406352012-04-23 16:16:37 +02003068 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003069 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003070 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003071 if (stkctr_entry(stkctr) != NULL) {
3072 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003073 if (!ptr)
3074 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003075 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003076 }
3077 return 1;
3078}
3079
Willy Tarreau87b09662015-04-03 00:22:06 +02003080/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02003081 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3082 * "src_http_req_rate" only.
3083 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003084static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003085smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003086{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003087 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02003088
3089 if (!stkctr)
3090 return 0;
3091
Willy Tarreau37406352012-04-23 16:16:37 +02003092 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003093 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003094 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003095 if (stkctr_entry(stkctr) != NULL) {
3096 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003097 if (!ptr)
3098 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003099 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003100 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003101 }
3102 return 1;
3103}
3104
Willy Tarreau87b09662015-04-03 00:22:06 +02003105/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003106 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3107 * "src_http_err_cnt" only.
3108 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003109static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003110smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003111{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003112 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003113
3114 if (!stkctr)
3115 return 0;
3116
Willy Tarreau37406352012-04-23 16:16:37 +02003117 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003118 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003119 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003120 if (stkctr_entry(stkctr) != NULL) {
3121 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003122 if (!ptr)
3123 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003124 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003125 }
3126 return 1;
3127}
3128
Willy Tarreau87b09662015-04-03 00:22:06 +02003129/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003130 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3131 * "src_http_err_rate" only.
3132 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003133static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003134smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003135{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003136 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003137
3138 if (!stkctr)
3139 return 0;
3140
Willy Tarreau37406352012-04-23 16:16:37 +02003141 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003142 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003143 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003144 if (stkctr_entry(stkctr) != NULL) {
3145 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003146 if (!ptr)
3147 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003148 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003149 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003150 }
3151 return 1;
3152}
3153
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003154/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003155 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003156 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3157 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003158static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003159smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003160{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003161 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003162
3163 if (!stkctr)
3164 return 0;
3165
Willy Tarreau37406352012-04-23 16:16:37 +02003166 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003167 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003168 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003169 if (stkctr_entry(stkctr) != NULL) {
3170 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003171 if (!ptr)
3172 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003173 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003174 }
3175 return 1;
3176}
3177
Willy Tarreau613fe992013-07-23 17:39:19 +02003178/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003179 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003180 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003181 */
3182static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003183smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003184{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003185 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003186
3187 if (!stkctr)
3188 return 0;
3189
Willy Tarreau37406352012-04-23 16:16:37 +02003190 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003191 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003192 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003193 if (stkctr_entry(stkctr) != NULL) {
3194 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003195 if (!ptr)
3196 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003197 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003198 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003199 }
3200 return 1;
3201}
3202
Willy Tarreau53aea102013-07-23 17:39:02 +02003203/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003204 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003205 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3206 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003207static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003208smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003209{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003210 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003211
3212 if (!stkctr)
3213 return 0;
3214
Willy Tarreau37406352012-04-23 16:16:37 +02003215 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003216 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003217 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003218 if (stkctr_entry(stkctr) != NULL) {
3219 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003220 if (!ptr)
3221 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003222 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003223 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003224 return 1;
3225}
3226
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003227/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003228 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003229 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003230 */
3231static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003232smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003233{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003234 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003235
3236 if (!stkctr)
3237 return 0;
3238
Willy Tarreau37406352012-04-23 16:16:37 +02003239 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003240 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003241 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003242 if (stkctr_entry(stkctr) != NULL) {
3243 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003244 if (!ptr)
3245 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003246 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003247 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003248 }
3249 return 1;
3250}
3251
Willy Tarreau87b09662015-04-03 00:22:06 +02003252/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003253 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3254 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003255static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003256smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003257{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003258 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003259
Willy Tarreau563eef42013-07-23 18:32:02 +02003260 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003261 return 0;
3262
Willy Tarreau563eef42013-07-23 18:32:02 +02003263 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003264 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003265 smp->data.u.sint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003266 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003267}
3268
Willy Tarreau34db1082012-04-19 17:16:54 +02003269/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003270 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003271 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003272static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003273smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003274{
Willy Tarreau37406352012-04-23 16:16:37 +02003275 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003276 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003277 smp->data.u.sint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003278 return 1;
3279}
3280
Willy Tarreau34db1082012-04-19 17:16:54 +02003281/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003282 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003283 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003284static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003285smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003286{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003287 struct proxy *px;
3288
Willy Tarreau24e32d82012-04-23 23:55:44 +02003289 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003290 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003291 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003292 smp->data.u.sint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003293 return 1;
3294}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003295
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003296/* 0=OK, <0=Alert, >0=Warning */
3297static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
3298 struct proxy *px, struct act_rule *rule,
3299 char **err)
3300{
3301 struct action_kw *kw;
3302
3303 /* Check if the service name exists. */
3304 if (*(args[*cur_arg]) == 0) {
3305 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01003306 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003307 }
3308
3309 /* lookup for keyword corresponding to a service. */
3310 kw = action_lookup(&service_keywords, args[*cur_arg]);
3311 if (!kw) {
3312 memprintf(err, "'%s' unknown service name.", args[1]);
3313 return ACT_RET_PRS_ERR;
3314 }
3315 (*cur_arg)++;
3316
3317 /* executes specific rule parser. */
3318 rule->kw = kw;
3319 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
3320 return ACT_RET_PRS_ERR;
3321
3322 /* Register processing function. */
3323 rule->action_ptr = process_use_service;
3324 rule->action = ACT_CUSTOM;
3325
3326 return ACT_RET_PRS_OK;
3327}
3328
3329void service_keywords_register(struct action_kw_list *kw_list)
3330{
3331 LIST_ADDQ(&service_keywords, &kw_list->list);
3332}
3333
3334/* main configuration keyword registration. */
3335static struct action_kw_list stream_tcp_keywords = { ILH, {
3336 { "use-service", stream_parse_use_service },
3337 { /* END */ }
3338}};
3339
3340static struct action_kw_list stream_http_keywords = { ILH, {
3341 { "use-service", stream_parse_use_service },
3342 { /* END */ }
3343}};
3344
Willy Tarreau61612d42012-04-19 18:42:05 +02003345/* Note: must not be declared <const> as its list will be overwritten.
3346 * Please take care of keeping this list alphabetically sorted.
3347 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003348static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003349 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003350}};
3351
Willy Tarreau281c7992013-01-08 01:23:27 +01003352/* Note: must not be declared <const> as its list will be overwritten.
3353 * Please take care of keeping this list alphabetically sorted.
3354 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003355static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003356 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3357 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3358 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3359 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3360 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3361 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003362 { "sc_get_gpt0", smp_fetch_sc_get_gpt0, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003363 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3364 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3365 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3366 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3367 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3368 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3369 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3370 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3371 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3372 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3373 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3374 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3375 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003376 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3377 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3378 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3379 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3380 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3381 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003382 { "sc0_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003383 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3384 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3385 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3386 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3387 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3388 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3389 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3390 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3391 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3392 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3393 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau0f791d42013-07-23 19:56:43 +02003394 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003395 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3396 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3397 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3398 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3399 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3400 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3401 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003402 { "sc1_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003403 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3404 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3405 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3406 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3407 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3408 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3409 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3410 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3411 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3412 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3413 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau0f791d42013-07-23 19:56:43 +02003414 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003415 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3416 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3417 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3418 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3419 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3420 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3421 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003422 { "sc2_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003423 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3424 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3425 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3426 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3427 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3428 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3429 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3430 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3431 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3432 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3433 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau0f791d42013-07-23 19:56:43 +02003434 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003435 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3436 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3437 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3438 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3439 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3440 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3441 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003442 { "src_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(1,TAB), NULL, SMP_T_BOOL, SMP_USE_L4CLI, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003443 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3444 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3445 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3446 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3447 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3448 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3449 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3450 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3451 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3452 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3453 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3454 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3455 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3456 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003457 { /* END */ },
3458}};
3459
Willy Tarreau8b22a712010-06-18 17:46:06 +02003460__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003461static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003462{
Willy Tarreau281c7992013-01-08 01:23:27 +01003463 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003464 acl_register_keywords(&acl_kws);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003465 tcp_req_cont_keywords_register(&stream_tcp_keywords);
3466 http_req_keywords_register(&stream_http_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003467}
3468
Willy Tarreaubaaee002006-06-26 02:48:02 +02003469/*
3470 * Local variables:
3471 * c-indent-level: 8
3472 * c-basic-offset: 8
3473 * End:
3474 */