blob: 0d18f9c2fd7334bbfed6c22bc8d35b62b33ff7c5 [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) &&
670 ((((s->be->redispatch_after > 0) &&
671 ((s->be->conn_retries - si->conn_retries) %
672 s->be->redispatch_after == 0)) ||
673 ((s->be->redispatch_after < 0) &&
674 ((s->be->conn_retries - si->conn_retries) %
675 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200676 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700677 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100678 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100679 if (may_dequeue_tasks(objt_server(s->target), s->be))
680 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681
Willy Tarreaue7dff022015-04-03 01:14:29 +0200682 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100683 si->state = SI_ST_REQ;
684 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100685 if (objt_server(s->target))
686 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100687 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100688 si->state = SI_ST_ASS;
689 }
690
691 if (si->flags & SI_FL_ERR) {
692 /* The error was an asynchronous connection error, and we will
693 * likely have to retry connecting to the same server, most
694 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200695 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100696 */
697
Willy Tarreaub0290662014-06-13 17:04:44 +0200698 int delay = 1000;
699
700 if (s->be->timeout.connect && s->be->timeout.connect < delay)
701 delay = s->be->timeout.connect;
702
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 if (!si->err_type)
704 si->err_type = SI_ET_CONN_ERR;
705
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200706 /* only wait when we're retrying on the same server */
707 if (si->state == SI_ST_ASS ||
708 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
709 (s->be->srv_act <= 1)) {
710 si->state = SI_ST_TAR;
711 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
712 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100713 return 0;
714 }
715 return 0;
716}
717
718/*
719 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200720 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200721 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100722 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200723static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100724{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100725 struct stream_interface *si = &s->si[1];
726 struct channel *req = &s->req;
727 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100728
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100729 /* First, centralize the timers information */
730 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
731 si->exp = TICK_ETERNITY;
732
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100733 if (objt_server(s->target))
734 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100735
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 /* if the user wants to log as soon as possible, without counting
738 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200739 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100740 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100741 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100742 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100743 }
744 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100745 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100746 }
747
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200748 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200749 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200750 if (req->flags & CF_WAKE_CONNECT) {
751 req->flags |= CF_WAKE_ONCE;
752 req->flags &= ~CF_WAKE_CONNECT;
753 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200754 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200755 /* real connections have timeouts */
756 req->wto = s->be->timeout.server;
757 rep->rto = s->be->timeout.server;
758 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100759 req->wex = TICK_ETERNITY;
760}
761
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100762/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
763 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100764 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
765 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
766 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100767 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200768static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100769{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100770 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100771 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100772 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100773
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100774 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 +0100775 now_ms, __FUNCTION__,
776 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400777 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100778 req->rex, s->res.wex,
779 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400780 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 +0100781
782 if (si->state == SI_ST_ASS) {
783 /* Server assigned to connection request, we have to try to connect now */
784 int conn_err;
785
786 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100787 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100788
Willy Tarreaue7dff022015-04-03 01:14:29 +0200789 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100790 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100791 if (srv)
792 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500793 if (srv)
794 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100795 return;
796 }
797
798 /* We have received a synchronous error. We might have to
799 * abort, retry immediately or redispatch.
800 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200801 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100802 if (!si->err_type) {
803 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100804 }
805
Willy Tarreau827aee92011-03-10 16:55:02 +0100806 if (srv)
807 srv_inc_sess_ctr(srv);
808 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500809 srv_set_sess_last(srv);
810 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100811 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100812 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100813
Willy Tarreau87b09662015-04-03 00:22:06 +0200814 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100815 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100816 if (may_dequeue_tasks(srv, s->be))
817 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100818
819 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200820 si_shutr(si);
821 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100822 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100823
824 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
825
Willy Tarreau87b09662015-04-03 00:22:06 +0200826 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100827 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100828 if (s->srv_error)
829 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100830 return;
831 }
832
833 /* We are facing a retryable error, but we don't want to run a
834 * turn-around now, as the problem is likely a source port
835 * allocation problem, so we want to retry now.
836 */
837 si->state = SI_ST_CER;
838 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100839 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100840 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
841 return;
842 }
843 else if (si->state == SI_ST_QUE) {
844 /* connection request was queued, check for any update */
845 if (!s->pend_pos) {
846 /* The connection is not in the queue anymore. Either
847 * we have a server connection slot available and we
848 * go directly to the assigned state, or we need to
849 * load-balance first and go to the INI state.
850 */
851 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200852 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100853 si->state = SI_ST_REQ;
854 else {
855 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
856 si->state = SI_ST_ASS;
857 }
858 return;
859 }
860
861 /* Connection request still in queue... */
862 if (si->flags & SI_FL_EXP) {
863 /* ... and timeout expired */
864 si->exp = TICK_ETERNITY;
865 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100866 if (srv)
867 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100868 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200869 si_shutr(si);
870 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100871 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100872 if (!si->err_type)
873 si->err_type = SI_ET_QUEUE_TO;
874 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100875 if (s->srv_error)
876 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100877 return;
878 }
879
880 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100881 if ((req->flags & (CF_READ_ERROR)) ||
882 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
883 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100884 /* give up */
885 si->exp = TICK_ETERNITY;
886 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200887 si_shutr(si);
888 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100889 si->err_type |= SI_ET_QUEUE_ABRT;
890 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100891 if (s->srv_error)
892 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100893 return;
894 }
895
896 /* Nothing changed */
897 return;
898 }
899 else if (si->state == SI_ST_TAR) {
900 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100901 if ((req->flags & (CF_READ_ERROR)) ||
902 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
903 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100904 /* give up */
905 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200906 si_shutr(si);
907 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100908 si->err_type |= SI_ET_CONN_ABRT;
909 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100910 if (s->srv_error)
911 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100912 return;
913 }
914
915 if (!(si->flags & SI_FL_EXP))
916 return; /* still in turn-around */
917
918 si->exp = TICK_ETERNITY;
919
Willy Tarreau87b09662015-04-03 00:22:06 +0200920 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100921 * marked "assigned".
922 * FIXME: Should we force a redispatch attempt when the server is down ?
923 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200924 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100925 si->state = SI_ST_ASS;
926 else
927 si->state = SI_ST_REQ;
928 return;
929 }
930}
931
Willy Tarreau87b09662015-04-03 00:22:06 +0200932/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900933 * also counts a failed request if the server state has not reached the request
934 * stage.
935 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200936static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900937{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200938 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900939 if (s->si[1].state < SI_ST_REQ) {
940
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200941 strm_fe(s)->fe_counters.failed_req++;
Willy Tarreau2c1068c2015-09-23 12:21:21 +0200942 if (strm_li(s) && strm_li(s)->counters)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200943 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900944
Willy Tarreaue7dff022015-04-03 01:14:29 +0200945 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900946 }
947 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200948 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900949 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200950 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900951 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200952 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900953 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200954 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900955 }
956}
957
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100958/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100959 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
960 * a real connection to a server, indicating that a server has been assigned,
961 * or SI_ST_EST for a successful connection to an applet. It may also return
962 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100963 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200964static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100965{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100966 struct stream_interface *si = &s->si[1];
967
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100968 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 +0100969 now_ms, __FUNCTION__,
970 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400971 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100972 s->req.rex, s->res.wex,
973 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400974 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 +0100975
976 if (si->state != SI_ST_REQ)
977 return;
978
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100979 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
980 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100981 struct appctx *appctx = objt_appctx(si->end);
982
983 if (!appctx || appctx->applet != __objt_applet(s->target))
984 appctx = stream_int_register_handler(si, objt_applet(s->target));
985
986 if (!appctx) {
987 /* No more memory, let's immediately abort. Force the
988 * error code to ignore the ERR_LOCAL which is not a
989 * real error.
990 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200991 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100992
993 si_shutr(si);
994 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100995 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +0100996 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100997 si->state = SI_ST_CLO;
998 if (s->srv_error)
999 s->srv_error(s, si);
1000 return;
1001 }
1002
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001003 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001004 si->state = SI_ST_EST;
1005 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001006 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001007 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001008 return;
1009 }
1010
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001011 /* Try to assign a server */
1012 if (srv_redispatch_connect(s) != 0) {
1013 /* We did not get a server. Either we queued the
1014 * connection request, or we encountered an error.
1015 */
1016 if (si->state == SI_ST_QUE)
1017 return;
1018
1019 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001020 si_shutr(si);
1021 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001022 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001023 if (!si->err_type)
1024 si->err_type = SI_ET_CONN_OTHER;
1025 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001026 if (s->srv_error)
1027 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001028 return;
1029 }
1030
1031 /* The server is assigned */
1032 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1033 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001034 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001035}
1036
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001037/* This function parses the use-service action ruleset. It executes
1038 * the associated ACL and set an applet as a stream or txn final node.
1039 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1040 * consistent state. It returns ACT_RET_STOP in succes case because
1041 * use-service must be a terminal action. Returns ACT_RET_YIELD
1042 * if the initialisation function require more data.
1043 */
1044enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1045 struct session *sess, struct stream *s, int flags)
1046
1047{
1048 struct appctx *appctx;
1049
1050 /* Initialises the applet if it is required. */
1051 if (flags & ACT_FLAG_FIRST) {
1052 /* Register applet. this function schedules the applet. */
1053 s->target = &rule->applet.obj_type;
1054 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target))))
1055 return ACT_RET_ERR;
1056
1057 /* Initialise the context. */
1058 appctx = si_appctx(&s->si[1]);
1059 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1060 appctx->rule = rule;
1061 }
1062 else
1063 appctx = si_appctx(&s->si[1]);
1064
1065 /* Stops the applet sheduling, in case of the init function miss
1066 * some data.
1067 */
1068 appctx_pause(appctx);
1069 si_applet_stop_get(&s->si[1]);
1070
1071 /* Call initialisation. */
1072 if (rule->applet.init)
1073 switch (rule->applet.init(appctx, px, s)) {
1074 case 0: return ACT_RET_ERR;
1075 case 1: break;
1076 default: return ACT_RET_YIELD;
1077 }
1078
1079 /* Now we can schedule the applet. */
1080 si_applet_cant_get(&s->si[1]);
1081 appctx_wakeup(appctx);
1082
1083 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1084 sess->fe->fe_counters.intercepted_req++;
1085
1086 /* The flag SF_ASSIGNED prevent from server assignment. */
1087 s->flags |= SF_ASSIGNED;
1088
1089 return ACT_RET_STOP;
1090}
1091
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001092/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001093 * if appropriate. The default_backend rule is also considered, then the
1094 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001095 * It returns 1 if the processing can continue on next analysers, or zero if it
1096 * either needs more data or wants to immediately abort the request.
1097 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001098static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001099{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001100 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001101 struct session *sess = s->sess;
1102 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001103
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001104 req->analysers &= ~an_bit;
1105 req->analyse_exp = TICK_ETERNITY;
1106
Willy Tarreau87b09662015-04-03 00:22:06 +02001107 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 +02001108 now_ms, __FUNCTION__,
1109 s,
1110 req,
1111 req->rex, req->wex,
1112 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001113 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001114 req->analysers);
1115
1116 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001117 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001118 struct switching_rule *rule;
1119
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001120 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001121 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001122
Willy Tarreauf51658d2014-04-23 01:21:56 +02001123 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001124 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001125 ret = acl_pass(ret);
1126 if (rule->cond->pol == ACL_COND_UNLESS)
1127 ret = !ret;
1128 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001129
1130 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001131 /* If the backend name is dynamic, try to resolve the name.
1132 * If we can't resolve the name, or if any error occurs, break
1133 * the loop and fallback to the default backend.
1134 */
1135 struct proxy *backend;
1136
1137 if (rule->dynamic) {
1138 struct chunk *tmp = get_trash_chunk();
1139 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1140 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001141 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001142 if (!backend)
1143 break;
1144 }
1145 else
1146 backend = rule->be.backend;
1147
Willy Tarreau87b09662015-04-03 00:22:06 +02001148 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001149 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001150 break;
1151 }
1152 }
1153
1154 /* To ensure correct connection accounting on the backend, we
1155 * have to assign one if it was not set (eg: a listen). This
1156 * measure also takes care of correctly setting the default
1157 * backend if any.
1158 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001159 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001160 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001161 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001162 }
1163
Willy Tarreaufb356202010-08-03 14:02:05 +02001164 /* 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 +02001165 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001166 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1167 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001168 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001169
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001170 /* 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 +02001171 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001172 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001173 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001174 int ret = 1;
1175
1176 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001177 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 +01001178 ret = acl_pass(ret);
1179 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1180 ret = !ret;
1181 }
1182
1183 if (ret) {
1184 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001185 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001186 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001187 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001188 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001189 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001190 break;
1191 }
1192 }
1193
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001194 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001195
1196 sw_failed:
1197 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001198 channel_abort(&s->req);
1199 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001200
Willy Tarreaue7dff022015-04-03 01:14:29 +02001201 if (!(s->flags & SF_ERR_MASK))
1202 s->flags |= SF_ERR_RESOURCE;
1203 if (!(s->flags & SF_FINST_MASK))
1204 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001205
Willy Tarreaueee5b512015-04-03 23:46:31 +02001206 if (s->txn)
1207 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001208 s->req.analysers = 0;
1209 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001210 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001211}
1212
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001213/* This stream analyser works on a request. It applies all use-server rules on
1214 * it then returns 1. The data must already be present in the buffer otherwise
1215 * they won't match. It always returns 1.
1216 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001217static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001218{
1219 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001220 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001221 struct server_rule *rule;
1222
Willy Tarreau87b09662015-04-03 00:22:06 +02001223 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 +02001224 now_ms, __FUNCTION__,
1225 s,
1226 req,
1227 req->rex, req->wex,
1228 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001229 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001230 req->analysers);
1231
Willy Tarreaue7dff022015-04-03 01:14:29 +02001232 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001233 list_for_each_entry(rule, &px->server_rules, list) {
1234 int ret;
1235
Willy Tarreau192252e2015-04-04 01:47:55 +02001236 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001237 ret = acl_pass(ret);
1238 if (rule->cond->pol == ACL_COND_UNLESS)
1239 ret = !ret;
1240
1241 if (ret) {
1242 struct server *srv = rule->srv.ptr;
1243
Willy Tarreau892337c2014-05-13 23:41:20 +02001244 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001245 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001246 (s->flags & SF_FORCE_PRST)) {
1247 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001248 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001249 break;
1250 }
1251 /* if the server is not UP, let's go on with next rules
1252 * just in case another one is suited.
1253 */
1254 }
1255 }
1256 }
1257
1258 req->analysers &= ~an_bit;
1259 req->analyse_exp = TICK_ETERNITY;
1260 return 1;
1261}
1262
Emeric Brun1d33b292010-01-04 15:47:17 +01001263/* This stream analyser works on a request. It applies all sticking rules on
1264 * it then returns 1. The data must already be present in the buffer otherwise
1265 * they won't match. It always returns 1.
1266 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001267static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001268{
1269 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001270 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001271 struct sticking_rule *rule;
1272
Willy Tarreau87b09662015-04-03 00:22:06 +02001273 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 +01001274 now_ms, __FUNCTION__,
1275 s,
1276 req,
1277 req->rex, req->wex,
1278 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001279 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001280 req->analysers);
1281
1282 list_for_each_entry(rule, &px->sticking_rules, list) {
1283 int ret = 1 ;
1284 int i;
1285
Willy Tarreau9667a802013-12-09 12:52:13 +01001286 /* Only the first stick store-request of each table is applied
1287 * and other ones are ignored. The purpose is to allow complex
1288 * configurations which look for multiple entries by decreasing
1289 * order of precision and to stop at the first which matches.
1290 * An example could be a store of the IP address from an HTTP
1291 * header first, then from the source if not found.
1292 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001293 for (i = 0; i < s->store_count; i++) {
1294 if (rule->table.t == s->store[i].table)
1295 break;
1296 }
1297
1298 if (i != s->store_count)
1299 continue;
1300
1301 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001302 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001303 ret = acl_pass(ret);
1304 if (rule->cond->pol == ACL_COND_UNLESS)
1305 ret = !ret;
1306 }
1307
1308 if (ret) {
1309 struct stktable_key *key;
1310
Willy Tarreau192252e2015-04-04 01:47:55 +02001311 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 +01001312 if (!key)
1313 continue;
1314
1315 if (rule->flags & STK_IS_MATCH) {
1316 struct stksess *ts;
1317
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001318 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001319 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001320 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001321 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001322
1323 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001324 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1325 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001326 if (node) {
1327 struct server *srv;
1328
1329 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001330 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001331 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001332 (s->flags & SF_FORCE_PRST)) {
1333 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001334 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001335 }
1336 }
1337 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001338 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001339 }
1340 }
1341 if (rule->flags & STK_IS_STORE) {
1342 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1343 struct stksess *ts;
1344
1345 ts = stksess_new(rule->table.t, key);
1346 if (ts) {
1347 s->store[s->store_count].table = rule->table.t;
1348 s->store[s->store_count++].ts = ts;
1349 }
1350 }
1351 }
1352 }
1353 }
1354
1355 req->analysers &= ~an_bit;
1356 req->analyse_exp = TICK_ETERNITY;
1357 return 1;
1358}
1359
1360/* This stream analyser works on a response. It applies all store rules on it
1361 * then returns 1. The data must already be present in the buffer otherwise
1362 * they won't match. It always returns 1.
1363 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001364static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001365{
1366 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001367 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001368 struct sticking_rule *rule;
1369 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001370 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001371
Willy Tarreau87b09662015-04-03 00:22:06 +02001372 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 +01001373 now_ms, __FUNCTION__,
1374 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001375 rep,
1376 rep->rex, rep->wex,
1377 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001378 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001379 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001380
1381 list_for_each_entry(rule, &px->storersp_rules, list) {
1382 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001383
Willy Tarreau9667a802013-12-09 12:52:13 +01001384 /* Only the first stick store-response of each table is applied
1385 * and other ones are ignored. The purpose is to allow complex
1386 * configurations which look for multiple entries by decreasing
1387 * order of precision and to stop at the first which matches.
1388 * An example could be a store of a set-cookie value, with a
1389 * fallback to a parameter found in a 302 redirect.
1390 *
1391 * The store-response rules are not allowed to override the
1392 * store-request rules for the same table, but they may coexist.
1393 * Thus we can have up to one store-request entry and one store-
1394 * response entry for the same table at any time.
1395 */
1396 for (i = nbreq; i < s->store_count; i++) {
1397 if (rule->table.t == s->store[i].table)
1398 break;
1399 }
1400
1401 /* skip existing entries for this table */
1402 if (i < s->store_count)
1403 continue;
1404
Emeric Brun1d33b292010-01-04 15:47:17 +01001405 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001406 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001407 ret = acl_pass(ret);
1408 if (rule->cond->pol == ACL_COND_UNLESS)
1409 ret = !ret;
1410 }
1411
1412 if (ret) {
1413 struct stktable_key *key;
1414
Willy Tarreau192252e2015-04-04 01:47:55 +02001415 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 +01001416 if (!key)
1417 continue;
1418
Willy Tarreau37e340c2013-12-06 23:05:21 +01001419 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001420 struct stksess *ts;
1421
1422 ts = stksess_new(rule->table.t, key);
1423 if (ts) {
1424 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001425 s->store[s->store_count++].ts = ts;
1426 }
1427 }
1428 }
1429 }
1430
1431 /* process store request and store response */
1432 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001433 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001434 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001435
Willy Tarreauc93cd162014-05-13 15:54:22 +02001436 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001437 stksess_free(s->store[i].table, s->store[i].ts);
1438 s->store[i].ts = NULL;
1439 continue;
1440 }
1441
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001442 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1443 if (ts) {
1444 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001445 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001446 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001447 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001448 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001449 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001450
1451 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001452 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001453 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001454 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001455 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001456
1457 rep->analysers &= ~an_bit;
1458 rep->analyse_exp = TICK_ETERNITY;
1459 return 1;
1460}
1461
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001462/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001463 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001464 */
1465#define UPDATE_ANALYSERS(real, list, back, flag) { \
1466 list = (((list) & ~(flag)) | ~(back)) & (real); \
1467 back = real; \
1468 if (!(list)) \
1469 break; \
1470 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1471 continue; \
1472}
1473
Willy Tarreau87b09662015-04-03 00:22:06 +02001474/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001475 * then puts it back to the wait queue in a clean state, or cleans up its
1476 * resources if it must be deleted. Returns in <next> the date the task wants
1477 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1478 * nothing too many times, the request and response buffers flags are monitored
1479 * and each function is called only if at least another function has changed at
1480 * least one flag it is interested in.
1481 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001482struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001483{
Willy Tarreau827aee92011-03-10 16:55:02 +01001484 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001485 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001486 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001487 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001488 unsigned int rq_prod_last, rq_cons_last;
1489 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001490 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001491 struct channel *req, *res;
1492 struct stream_interface *si_f, *si_b;
1493
1494 req = &s->req;
1495 res = &s->res;
1496
1497 si_f = &s->si[0];
1498 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001499
1500 //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 +01001501 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001502
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001503 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001504 if (s->txn)
1505 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001506
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001507 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001508 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1509 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001510
1511 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001512 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1513 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001514
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001515 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001516 si_f->flags |= SI_FL_DONT_WAKE;
1517 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001518
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001519 /* 1a: Check for low level timeouts if needed. We just set a flag on
1520 * stream interfaces when their timeouts have expired.
1521 */
1522 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001523 stream_int_check_timeouts(si_f);
1524 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001525
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001526 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001527 * for future reads or writes. Note: this will also concern upper layers
1528 * but we do not touch any other flag. We must be careful and correctly
1529 * detect state changes when calling them.
1530 */
1531
Willy Tarreau8f128b42014-11-28 15:07:47 +01001532 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001533
Willy Tarreau8f128b42014-11-28 15:07:47 +01001534 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1535 si_b->flags |= SI_FL_NOLINGER;
1536 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001537 }
1538
Willy Tarreau8f128b42014-11-28 15:07:47 +01001539 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1540 if (si_f->flags & SI_FL_NOHALF)
1541 si_f->flags |= SI_FL_NOLINGER;
1542 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001543 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001544
Willy Tarreau8f128b42014-11-28 15:07:47 +01001545 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001546
Willy Tarreau8f128b42014-11-28 15:07:47 +01001547 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1548 si_f->flags |= SI_FL_NOLINGER;
1549 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001550 }
1551
Willy Tarreau8f128b42014-11-28 15:07:47 +01001552 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1553 if (si_b->flags & SI_FL_NOHALF)
1554 si_b->flags |= SI_FL_NOLINGER;
1555 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001556 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001557
1558 /* Once in a while we're woken up because the task expires. But
1559 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001560 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001561 * timeout needs to be refreshed.
1562 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001563 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001564 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1565 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001566 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001567 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1568 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001569 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001570
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001571 /* below we may emit error messages so we have to ensure that we have
1572 * our buffers properly allocated.
1573 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001574 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001575 /* No buffer available, we've been subscribed to the list of
1576 * buffer waiters, let's wait for our turn.
1577 */
1578 goto update_exp_and_leave;
1579 }
1580
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001581 /* 1b: check for low-level errors reported at the stream interface.
1582 * First we check if it's a retryable error (in which case we don't
1583 * want to tell the buffer). Otherwise we report the error one level
1584 * upper by setting flags into the buffers. Note that the side towards
1585 * the client cannot have connect (hence retryable) errors. Also, the
1586 * connection setup code must be able to deal with any type of abort.
1587 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001588 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001589 if (unlikely(si_f->flags & SI_FL_ERR)) {
1590 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1591 si_shutr(si_f);
1592 si_shutw(si_f);
1593 stream_int_report_error(si_f);
1594 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001595 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001596 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001597 if (srv)
1598 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001599 if (!(s->flags & SF_ERR_MASK))
1600 s->flags |= SF_ERR_CLICL;
1601 if (!(s->flags & SF_FINST_MASK))
1602 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001603 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001604 }
1605 }
1606
Willy Tarreau8f128b42014-11-28 15:07:47 +01001607 if (unlikely(si_b->flags & SI_FL_ERR)) {
1608 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1609 si_shutr(si_b);
1610 si_shutw(si_b);
1611 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001612 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001613 if (srv)
1614 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001615 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001616 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001617 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001618 if (srv)
1619 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001620 if (!(s->flags & SF_ERR_MASK))
1621 s->flags |= SF_ERR_SRVCL;
1622 if (!(s->flags & SF_FINST_MASK))
1623 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001624 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001625 }
1626 /* note: maybe we should process connection errors here ? */
1627 }
1628
Willy Tarreau8f128b42014-11-28 15:07:47 +01001629 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001630 /* we were trying to establish a connection on the server side,
1631 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1632 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001633 if (unlikely(!sess_update_st_con_tcp(s)))
1634 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001635 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001636 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001637
1638 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1639 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1640 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1641 */
1642 }
1643
Willy Tarreau8f128b42014-11-28 15:07:47 +01001644 rq_prod_last = si_f->state;
1645 rq_cons_last = si_b->state;
1646 rp_cons_last = si_f->state;
1647 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001648
1649 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001650 /* Check for connection closure */
1651
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001652 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001653 "[%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 +01001654 now_ms, __FUNCTION__, __LINE__,
1655 t,
1656 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001657 req, res,
1658 req->rex, res->wex,
1659 req->flags, res->flags,
1660 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1661 si_f->err_type, si_b->err_type,
1662 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001663
1664 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001665 if (unlikely(si_f->state == SI_ST_DIS))
1666 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001667
1668 /* When a server-side connection is released, we have to count it and
1669 * check for pending connections on this server.
1670 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001671 if (unlikely(si_b->state == SI_ST_DIS)) {
1672 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001673 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001674 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001675 if (s->flags & SF_CURR_SESS) {
1676 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001677 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001678 }
1679 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001680 if (may_dequeue_tasks(srv, s->be))
1681 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001682 }
1683 }
1684
1685 /*
1686 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1687 * at this point.
1688 */
1689
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001690 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001691 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001692 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1693 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1694 si_f->state != rq_prod_last ||
1695 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001696 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001697 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001698
Willy Tarreau8f128b42014-11-28 15:07:47 +01001699 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001700 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001701 unsigned int ana_list;
1702 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001703
Willy Tarreau90deb182010-01-07 00:20:41 +01001704 /* it's up to the analysers to stop new connections,
1705 * disable reading or closing. Note: if an analyser
1706 * disables any of these bits, it is responsible for
1707 * enabling them again when it disables itself, so
1708 * that other analysers are called in similar conditions.
1709 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001710 channel_auto_read(req);
1711 channel_auto_connect(req);
1712 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001713
1714 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001715 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001716 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001717 * the list when not needed. Any analyser may return 0
1718 * to break out of the loop, either because of missing
1719 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001720 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001721 * analyser, and we may loop again if other analysers
1722 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001723 *
1724 * We build a list of analysers to run. We evaluate all
1725 * of these analysers in the order of the lower bit to
1726 * the higher bit. This ordering is very important.
1727 * An analyser will often add/remove other analysers,
1728 * including itself. Any changes to itself have no effect
1729 * on the loop. If it removes any other analysers, we
1730 * want those analysers not to be called anymore during
1731 * this loop. If it adds an analyser that is located
1732 * after itself, we want it to be scheduled for being
1733 * processed during the loop. If it adds an analyser
1734 * which is located before it, we want it to switch to
1735 * it immediately, even if it has already been called
1736 * once but removed since.
1737 *
1738 * In order to achieve this, we compare the analyser
1739 * list after the call with a copy of it before the
1740 * call. The work list is fed with analyser bits that
1741 * appeared during the call. Then we compare previous
1742 * work list with the new one, and check the bits that
1743 * appeared. If the lowest of these bits is lower than
1744 * the current bit, it means we have enabled a previous
1745 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001746 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001747
Willy Tarreau8f128b42014-11-28 15:07:47 +01001748 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001749 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001750 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001751
Willy Tarreaufb356202010-08-03 14:02:05 +02001752 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001753 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001754 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001755 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001756 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001757
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001758 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001759 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001760 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001761 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001762 }
1763
Willy Tarreau748179e2015-05-01 21:52:31 +02001764 if (ana_list & AN_REQ_HTTP_BODY) {
1765 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
1766 break;
1767 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
1768 }
1769
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001770 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001771 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001772 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001773 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001774 }
1775
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001776 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001777 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001778 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001779 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001780 }
1781
Willy Tarreaufb356202010-08-03 14:02:05 +02001782 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001783 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001784 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001785 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001786 }
1787
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001788 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001789 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001790 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001791 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001792 }
1793
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001794 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001795 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001796 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001797 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001798 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001799
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001800 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001801 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001802 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001803 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001804 }
1805
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001806 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001807 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001808 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001809 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001810 }
1811
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001812 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001813 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001814 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001815 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001816 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001817
Emeric Brun1d33b292010-01-04 15:47:17 +01001818 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001819 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001820 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001821 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001822 }
1823
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001824 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001825 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001827 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001828 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001829 break;
1830 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001831 }
Willy Tarreau84455332009-03-15 22:34:05 +01001832
Willy Tarreau8f128b42014-11-28 15:07:47 +01001833 rq_prod_last = si_f->state;
1834 rq_cons_last = si_b->state;
1835 req->flags &= ~CF_WAKE_ONCE;
1836 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001837
Willy Tarreau8f128b42014-11-28 15:07:47 +01001838 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001839 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001840 }
1841
Willy Tarreau576507f2010-01-07 00:09:04 +01001842 /* we'll monitor the request analysers while parsing the response,
1843 * because some response analysers may indirectly enable new request
1844 * analysers (eg: HTTP keep-alive).
1845 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001846 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001847
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001848 resync_response:
1849 /* Analyse response */
1850
Willy Tarreau8f128b42014-11-28 15:07:47 +01001851 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1852 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1853 si_f->state != rp_cons_last ||
1854 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001855 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001856 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001857
Willy Tarreau8f128b42014-11-28 15:07:47 +01001858 if ((res->flags & CF_MASK_ANALYSER) &&
1859 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001860 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1861 * for some free space in the response buffer, so we might need to call
1862 * it when something changes in the response buffer, but still we pass
1863 * it the request buffer. Note that the SI state might very well still
1864 * be zero due to us returning a flow of redirects!
1865 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001866 res->analysers &= ~AN_REQ_ALL;
1867 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001868 }
1869
Willy Tarreau8f128b42014-11-28 15:07:47 +01001870 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001871 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001872 unsigned int ana_list;
1873 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001874
Willy Tarreau90deb182010-01-07 00:20:41 +01001875 /* it's up to the analysers to stop disable reading or
1876 * closing. Note: if an analyser disables any of these
1877 * bits, it is responsible for enabling them again when
1878 * it disables itself, so that other analysers are called
1879 * in similar conditions.
1880 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001881 channel_auto_read(res);
1882 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001883
1884 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001885 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001886 * to MSB. The analysers must remove themselves from
1887 * the list when not needed. Any analyser may return 0
1888 * to break out of the loop, either because of missing
1889 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001890 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001891 * analyser, and we may loop again if other analysers
1892 * are added in the middle.
1893 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001894
Willy Tarreau8f128b42014-11-28 15:07:47 +01001895 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001896 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001897 /* Warning! ensure that analysers are always placed in ascending order! */
1898
Emeric Brun97679e72010-09-23 17:56:44 +02001899 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001900 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001901 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001902 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001903 }
1904
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001905 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001906 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001907 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001908 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001909 }
1910
Emeric Brun1d33b292010-01-04 15:47:17 +01001911 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001912 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001913 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001914 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001915 }
1916
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001917 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001918 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001919 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001920 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001921 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001923 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001924 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001926 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001927 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001928 break;
1929 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001930 }
1931
Willy Tarreau8f128b42014-11-28 15:07:47 +01001932 rp_cons_last = si_f->state;
1933 rp_prod_last = si_b->state;
1934 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001935
Willy Tarreau8f128b42014-11-28 15:07:47 +01001936 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001937 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001938 }
1939
Willy Tarreau576507f2010-01-07 00:09:04 +01001940 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001941 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001942 goto resync_request;
1943
Willy Tarreau8f128b42014-11-28 15:07:47 +01001944 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001945 goto resync_request;
1946
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001947 /* FIXME: here we should call protocol handlers which rely on
1948 * both buffers.
1949 */
1950
1951
1952 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001953 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001954 * we're just in a data phase here since it means we have not
1955 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001956 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001957 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001958 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001959 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001960 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001961 req->analysers = 0;
1962 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001963 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001964 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001965 if (srv)
1966 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001967 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001968 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001969 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001970 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001971 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001972 if (srv)
1973 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001974 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001975 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001976 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001977 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001978 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001979 if (srv)
1980 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001981 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001982 }
1983 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001984 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001985 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001986 if (srv)
1987 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001988 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001989 }
Willy Tarreau84455332009-03-15 22:34:05 +01001990 sess_set_term_flags(s);
1991 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001992 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001993 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001994 res->analysers = 0;
1995 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001996 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001997 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001998 if (srv)
1999 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002000 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002001 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002002 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002003 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002004 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002005 if (srv)
2006 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002007 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002008 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002009 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002010 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002011 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002012 if (srv)
2013 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002014 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002015 }
2016 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002017 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002018 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002019 if (srv)
2020 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002021 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002022 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002023 sess_set_term_flags(s);
2024 }
Willy Tarreau84455332009-03-15 22:34:05 +01002025 }
2026
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002027 /*
2028 * Here we take care of forwarding unhandled data. This also includes
2029 * connection establishments and shutdown requests.
2030 */
2031
2032
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002033 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002034 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002035 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002036 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002037 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002038 if (unlikely(!req->analysers &&
2039 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2040 (si_f->state >= SI_ST_EST) &&
2041 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002042 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002043 * attached to it. If any data are left in, we'll permit them to
2044 * move.
2045 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002046 channel_auto_read(req);
2047 channel_auto_connect(req);
2048 channel_auto_close(req);
2049 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002050
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002051 /* We'll let data flow between the producer (if still connected)
2052 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002053 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002054 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2055 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau42529c32015-07-09 18:38:57 +02002056
2057 /* Just in order to support fetching HTTP contents after start
2058 * of forwarding when the HTTP forwarding analyser is not used,
2059 * we simply reset msg->sov so that HTTP rewinding points to the
2060 * headers.
2061 */
2062 if (s->txn)
2063 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - req->buf->o;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002064 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002065
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002066 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002067 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2068 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002069 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002070 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2071 (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 +01002072 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002073 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2074 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002075 (req->flags & CF_STREAMER_FAST)))) {
2076 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002077 }
2078
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002079 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002080 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002081
2082 /*
2083 * Now forward all shutdown requests between both sides of the buffer
2084 */
2085
Willy Tarreau520d95e2009-09-19 21:04:57 +02002086 /* first, let's check if the request buffer needs to shutdown(write), which may
2087 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002088 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2089 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002090 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002091 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002092 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002093 channel_shutw_now(req);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002094 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002095
2096 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002097 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2098 channel_is_empty(req))) {
2099 if (req->flags & CF_READ_ERROR)
2100 si_b->flags |= SI_FL_NOLINGER;
2101 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002102 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002103 res->rto = s->be->timeout.serverfin;
2104 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002105 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002106 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002107
2108 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002109 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2110 !req->analysers))
2111 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002112
2113 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002114 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2115 if (si_f->flags & SI_FL_NOHALF)
2116 si_f->flags |= SI_FL_NOLINGER;
2117 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002118 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002119
Willy Tarreau520d95e2009-09-19 21:04:57 +02002120 /* it's possible that an upper layer has requested a connection setup or abort.
2121 * There are 2 situations where we decide to establish a new connection :
2122 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002123 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002124 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002125 if (si_b->state == SI_ST_INI) {
2126 if (!(req->flags & CF_SHUTW)) {
2127 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002128 /* If we have an appctx, there is no connect method, so we
2129 * immediately switch to the connected state, otherwise we
2130 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002131 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002132 si_b->state = SI_ST_REQ; /* new connection requested */
2133 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002134 }
Willy Tarreau73201222009-08-16 18:27:24 +02002135 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002136 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002137 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2138 channel_shutw_now(req); /* fix buffer flags upon abort */
2139 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002140 }
Willy Tarreau92795622009-03-06 12:51:23 +01002141 }
2142
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002143
2144 /* we may have a pending connection request, or a connection waiting
2145 * for completion.
2146 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002147 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002148
2149 /* prune the request variables and swap to the response variables. */
2150 if (s->vars_reqres.scope != SCOPE_RES) {
2151 vars_prune(&s->vars_reqres, s);
2152 vars_init(&s->vars_reqres, SCOPE_RES);
2153 }
2154
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002155 do {
2156 /* nb: step 1 might switch from QUE to ASS, but we first want
2157 * to give a chance to step 2 to perform a redirect if needed.
2158 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002159 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002160 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002161 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002162 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002163
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002164 /* applets directly go to the ESTABLISHED state. Similarly,
2165 * servers experience the same fate when their connection
2166 * is reused.
2167 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002168 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002169 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002170
Willy Tarreaub44c8732013-12-31 23:16:50 +01002171 /* Now we can add the server name to a header (if requested) */
2172 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002173 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002174 (s->be->server_id_hdr_name != NULL) &&
2175 (s->be->mode == PR_MODE_HTTP) &&
2176 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002177 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002178 }
2179
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002180 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002181 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002182 http_perform_server_redirect(s, si_b);
2183 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002184 }
2185
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002186 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002187 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002188 goto resync_stream_interface;
2189
Willy Tarreau815a9b22010-07-27 17:15:12 +02002190 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002191 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002192 goto resync_request;
2193
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002194 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002195
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002196 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002197 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002198 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002199 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002200 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002201 if (unlikely(!res->analysers &&
2202 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2203 (si_b->state >= SI_ST_EST) &&
2204 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002205 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002206 * attached to it. If any data are left in, we'll permit them to
2207 * move.
2208 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002209 channel_auto_read(res);
2210 channel_auto_close(res);
2211 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002212
2213 /* We'll let data flow between the producer (if still connected)
2214 * to the consumer.
2215 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002216 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2217 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002218
Willy Tarreau42529c32015-07-09 18:38:57 +02002219 /* Just in order to support fetching HTTP contents after start
2220 * of forwarding when the HTTP forwarding analyser is not used,
2221 * we simply reset msg->sov so that HTTP rewinding points to the
2222 * headers.
2223 */
2224 if (s->txn)
2225 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2226
Willy Tarreauce887fd2012-05-12 12:50:00 +02002227 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002228 * tunnel timeout set, use it now. Note that we must respect
2229 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002230 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002231 if (!req->analysers && s->be->timeout.tunnel) {
2232 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002233 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002234
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002235 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2236 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002237 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2238 res->rto = s->be->timeout.serverfin;
2239 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2240 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002241 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2242 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002243
Willy Tarreau8f128b42014-11-28 15:07:47 +01002244 req->rex = tick_add(now_ms, req->rto);
2245 req->wex = tick_add(now_ms, req->wto);
2246 res->rex = tick_add(now_ms, res->rto);
2247 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002248 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002249 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002250
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002251 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002252 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2253 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002254 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002255 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2256 (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 +01002257 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002258 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2259 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002260 (res->flags & CF_STREAMER_FAST)))) {
2261 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002262 }
2263
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002264 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002265 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002266
2267 /*
2268 * Now forward all shutdown requests between both sides of the buffer
2269 */
2270
2271 /*
2272 * FIXME: this is probably where we should produce error responses.
2273 */
2274
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002275 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002276 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002277 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002278 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002279 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002280
2281 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002282 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2283 channel_is_empty(res))) {
2284 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002285 if (tick_isset(sess->fe->timeout.clientfin)) {
2286 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002287 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002288 }
2289 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002290
2291 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002292 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2293 !res->analysers)
2294 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002295
2296 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002297 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2298 if (si_b->flags & SI_FL_NOHALF)
2299 si_b->flags |= SI_FL_NOLINGER;
2300 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002301 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002302
Willy Tarreau8f128b42014-11-28 15:07:47 +01002303 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002304 goto resync_stream_interface;
2305
Willy Tarreau8f128b42014-11-28 15:07:47 +01002306 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002307 goto resync_request;
2308
Willy Tarreau8f128b42014-11-28 15:07:47 +01002309 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002310 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002311
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002312 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002313 si_f->flags &= ~SI_FL_DONT_WAKE;
2314 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002315
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002316 /* This is needed only when debugging is enabled, to indicate
2317 * client-side or server-side close. Please note that in the unlikely
2318 * event where both sides would close at once, the sequence is reported
2319 * on the server side first.
2320 */
2321 if (unlikely((global.mode & MODE_DEBUG) &&
2322 (!(global.mode & MODE_QUIET) ||
2323 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002324 if (si_b->state == SI_ST_CLO &&
2325 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002326 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002327 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002328 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2329 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002330 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002331 }
2332
Willy Tarreau8f128b42014-11-28 15:07:47 +01002333 if (si_f->state == SI_ST_CLO &&
2334 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002335 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002336 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002337 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2338 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002339 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002340 }
2341 }
2342
Willy Tarreau8f128b42014-11-28 15:07:47 +01002343 if (likely((si_f->state != SI_ST_CLO) ||
2344 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002345
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002346 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002347 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002348
Willy Tarreau563cc372015-04-19 18:13:56 +02002349 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002350 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002351
Willy Tarreau563cc372015-04-19 18:13:56 +02002352 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002353 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002354
Willy Tarreau8f128b42014-11-28 15:07:47 +01002355 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2356 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2357 si_f->prev_state = si_f->state;
2358 si_b->prev_state = si_b->state;
2359 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2360 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002361
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002362 /* Trick: if a request is being waiting for the server to respond,
2363 * and if we know the server can timeout, we don't want the timeout
2364 * to expire on the client side first, but we're still interested
2365 * in passing data from the client to the server (eg: POST). Thus,
2366 * we can cancel the client's request timeout if the server's
2367 * request timeout is set and the server has not yet sent a response.
2368 */
2369
Willy Tarreau8f128b42014-11-28 15:07:47 +01002370 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2371 (tick_isset(req->wex) || tick_isset(res->rex))) {
2372 req->flags |= CF_READ_NOEXP;
2373 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002374 }
2375
Willy Tarreau798f4322012-11-08 14:49:17 +01002376 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002377 t->expire = tick_first(tick_first(req->rex, req->wex),
2378 tick_first(res->rex, res->wex));
2379 if (req->analysers)
2380 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002381
Willy Tarreau8f128b42014-11-28 15:07:47 +01002382 if (si_f->exp)
2383 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002384
Willy Tarreau8f128b42014-11-28 15:07:47 +01002385 if (si_b->exp)
2386 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002387
2388#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002389 fprintf(stderr,
2390 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2391 " 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 +01002392 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2393 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002394#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002395
2396#ifdef DEBUG_DEV
2397 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002398 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002399 ABORT_NOW();
2400#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002401 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002402 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002403 }
2404
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002405 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002406 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002407 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002408 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002409 if (sess->listener) {
2410 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002411 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002412 sess->listener->nbconn--;
2413 if (sess->listener->state == LI_FULL)
2414 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002415
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002416 /* Dequeues all of the listeners waiting for a resource */
2417 if (!LIST_ISEMPTY(&global_listener_queue))
2418 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002419
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002420 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2421 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2422 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002423 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002424
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002425 if (unlikely((global.mode & MODE_DEBUG) &&
2426 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002427 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002428 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002429 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2430 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002431 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002432 }
2433
2434 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002435 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002436
Willy Tarreaueee5b512015-04-03 23:46:31 +02002437 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002438 int n;
2439
Willy Tarreaueee5b512015-04-03 23:46:31 +02002440 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002441 if (n < 1 || n > 5)
2442 n = 0;
2443
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002444 if (sess->fe->mode == PR_MODE_HTTP) {
2445 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002446 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002447 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002448 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002449 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002450 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002451 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002452 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002453 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002454 s->be->be_counters.p.http.comp_rsp++;
2455 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002456 }
2457
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002458 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002459 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002460 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002461 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002462 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002463 }
2464
Willy Tarreau87b09662015-04-03 00:22:06 +02002465 /* update time stats for this stream */
2466 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002467
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002468 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002469 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002470 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002471 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002472 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002473}
2474
Willy Tarreau87b09662015-04-03 00:22:06 +02002475/* Update the stream's backend and server time stats */
2476void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002477{
2478 int t_request;
2479 int t_queue;
2480 int t_connect;
2481 int t_data;
2482 int t_close;
2483 struct server *srv;
2484
2485 t_request = 0;
2486 t_queue = s->logs.t_queue;
2487 t_connect = s->logs.t_connect;
2488 t_close = s->logs.t_close;
2489 t_data = s->logs.t_data;
2490
2491 if (s->be->mode != PR_MODE_HTTP)
2492 t_data = t_connect;
2493
2494 if (t_connect < 0 || t_data < 0)
2495 return;
2496
2497 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2498 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2499
2500 t_data -= t_connect;
2501 t_connect -= t_queue;
2502 t_queue -= t_request;
2503
2504 srv = objt_server(s->target);
2505 if (srv) {
2506 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2507 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2508 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2509 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2510 }
2511 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2512 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2513 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2514 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2515}
2516
Willy Tarreau7c669d72008-06-20 15:04:11 +02002517/*
2518 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002519 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002520 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002521 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002522 * server.
2523 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002524void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002525{
2526 if (sess->srv_conn == newsrv)
2527 return;
2528
2529 if (sess->srv_conn) {
2530 sess->srv_conn->served--;
2531 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2532 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002533 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002534 }
2535
2536 if (newsrv) {
2537 newsrv->served++;
2538 if (newsrv->proxy->lbprm.server_take_conn)
2539 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002540 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002541 }
2542}
2543
Willy Tarreau84455332009-03-15 22:34:05 +01002544/* Handle server-side errors for default protocols. It is called whenever a a
2545 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002546 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002547 * them. It's installed as ->srv_error for the server-side stream_interface.
2548 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002549void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002550{
2551 int err_type = si->err_type;
2552 int err = 0, fin = 0;
2553
2554 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002555 err = SF_ERR_CLICL;
2556 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002557 }
2558 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002559 err = SF_ERR_CLICL;
2560 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002561 }
2562 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002563 err = SF_ERR_SRVTO;
2564 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002565 }
2566 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002567 err = SF_ERR_SRVCL;
2568 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002569 }
2570 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002571 err = SF_ERR_SRVTO;
2572 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002573 }
2574 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002575 err = SF_ERR_SRVCL;
2576 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002577 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002578 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002579 err = SF_ERR_RESOURCE;
2580 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002581 }
Willy Tarreau84455332009-03-15 22:34:05 +01002582 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002583 err = SF_ERR_INTERNAL;
2584 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002585 }
2586
Willy Tarreaue7dff022015-04-03 01:14:29 +02002587 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002588 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002589 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002590 s->flags |= fin;
2591}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002592
Willy Tarreaue7dff022015-04-03 01:14:29 +02002593/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002594void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002595{
Willy Tarreau87b09662015-04-03 00:22:06 +02002596 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002597 return;
2598
Willy Tarreau87b09662015-04-03 00:22:06 +02002599 channel_shutw_now(&stream->req);
2600 channel_shutr_now(&stream->res);
2601 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002602 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002603 stream->flags |= why;
2604 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002605}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002606
Willy Tarreau8b22a712010-06-18 17:46:06 +02002607/************************************************************************/
2608/* All supported ACL keywords must be declared here. */
2609/************************************************************************/
2610
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002611/* Returns a pointer to a stkctr depending on the fetch keyword name.
2612 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002613 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002614 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002615 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002616 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002617 * NULL may be returned if the designated stkctr is not tracked. For
2618 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2619 * passed. When present, the currently tracked key is then looked up
2620 * in the specified table instead of the current table. The purpose is
2621 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002622 * multiple tables). <strm> is allowed to be NULL, in which case only
2623 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002624 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002625struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002626smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002627{
2628 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002629 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002630 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002631 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002632 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002633
Willy Tarreau0f791d42013-07-23 19:56:43 +02002634 if (num == '_' - '0') {
2635 /* sc_* variant, args[0] = ctr# (mandatory) */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002636 num = args[arg++].data.sint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002637 if (num >= MAX_SESS_STKCTR)
2638 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002639 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002640 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002641 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002642 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002643 struct sample smp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002644
2645 if (!conn)
2646 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002647
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002648 /* Fetch source adress in a sample. */
2649 smp.px = NULL;
2650 smp.sess = sess;
2651 smp.strm = strm;
2652 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2653 return NULL;
2654
2655 /* Converts into key. */
2656 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002657 if (!key)
2658 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002659
Willy Tarreaua65536c2013-07-22 22:40:11 +02002660 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002661 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002662 return &stkctr;
2663 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002664
2665 /* Here, <num> contains the counter number from 0 to 9 for
2666 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002667 * args[arg] is the first optional argument. We first lookup the
2668 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002669 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002670
2671 stkptr = &strm->stkctr[num];
2672 if (!strm || !stkctr_entry(stkptr)) {
2673 stkptr = &sess->stkctr[num];
2674 if (!stkctr_entry(stkptr))
2675 return NULL;
2676 }
2677
2678 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002679 if (!stksess)
2680 return NULL;
2681
Willy Tarreau0f791d42013-07-23 19:56:43 +02002682 if (unlikely(args[arg].type == ARGT_TAB)) {
2683 /* an alternate table was specified, let's look up the same key there */
2684 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002685 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002686 return &stkctr;
2687 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002688 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002689}
2690
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002691/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2692 * the entry if it doesn't exist yet. This is needed for a few fetch
2693 * functions which need to create an entry, such as src_inc_gpc* and
2694 * src_clr_gpc*.
2695 */
2696struct stkctr *
2697smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
2698{
2699 static struct stkctr stkctr;
2700 struct stktable_key *key;
2701 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002702 struct sample smp;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002703
2704 if (strncmp(kw, "src_", 4) != 0)
2705 return NULL;
2706
2707 if (!conn)
2708 return NULL;
2709
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002710 /* Fetch source adress in a sample. */
2711 smp.px = NULL;
2712 smp.sess = sess;
2713 smp.strm = strm;
2714 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2715 return NULL;
2716
2717 /* Converts into key. */
2718 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002719 if (!key)
2720 return NULL;
2721
2722 stkctr.table = &args->data.prx->table;
2723 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
2724 return &stkctr;
2725}
2726
Willy Tarreau87b09662015-04-03 00:22:06 +02002727/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002728 * currently being tracked or not.
2729 * Supports being called as "sc[0-9]_tracked" only.
2730 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002731static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002732smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002733{
2734 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002735 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002736 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002737 return 1;
2738}
2739
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002740/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2741 * frontend counters or from the src.
2742 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2743 * zero is returned if the key is new.
2744 */
2745static int
2746smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2747{
2748 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2749
2750 if (!stkctr)
2751 return 0;
2752
2753 smp->flags = SMP_F_VOL_TEST;
2754 smp->data.type = SMP_T_SINT;
2755 smp->data.u.sint = 0;
2756
2757 if (stkctr_entry(stkctr) != NULL) {
2758 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2759 if (!ptr)
2760 return 0; /* parameter not stored */
2761 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
2762 }
2763 return 1;
2764}
2765
Willy Tarreau87b09662015-04-03 00:22:06 +02002766/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002767 * frontend counters or from the src.
2768 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2769 * zero is returned if the key is new.
2770 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002771static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002772smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002773{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002774 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002775
2776 if (!stkctr)
2777 return 0;
2778
Willy Tarreau37406352012-04-23 16:16:37 +02002779 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002780 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002781 smp->data.u.sint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002782
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002783 if (stkctr_entry(stkctr) != NULL) {
2784 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002785 if (!ptr)
2786 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002787 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002788 }
2789 return 1;
2790}
2791
Willy Tarreau87b09662015-04-03 00:22:06 +02002792/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002793 * tracked frontend counters or from the src.
2794 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2795 * Value zero is returned if the key is new.
2796 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002797static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002798smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002799{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002800 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002801
2802 if (!stkctr)
2803 return 0;
2804
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002805 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002806 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002807 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002808 if (stkctr_entry(stkctr) != NULL) {
2809 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002810 if (!ptr)
2811 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002812 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002813 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002814 }
2815 return 1;
2816}
2817
Willy Tarreau87b09662015-04-03 00:22:06 +02002818/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002819 * frontend counters and return it into temp integer.
2820 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002821 */
2822static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002823smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002824{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002825 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002826
2827 if (!stkctr)
2828 return 0;
2829
Willy Tarreau37406352012-04-23 16:16:37 +02002830 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002831 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002832 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002833
2834 if (stkctr_entry(stkctr) == NULL)
2835 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2836
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002837 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun57056f02015-06-15 18:29:57 +02002838 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002839
2840 /* First, update gpc0_rate if it's tracked. Second, update its
2841 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2842 */
Emeric Brun57056f02015-06-15 18:29:57 +02002843 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2844 if (ptr1) {
2845 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002846 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002847 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002848 }
2849
Emeric Brun57056f02015-06-15 18:29:57 +02002850 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2851 if (ptr2)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002852 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002853
Emeric Brun57056f02015-06-15 18:29:57 +02002854 /* If data was modified, we need to touch to re-schedule sync */
2855 if (ptr1 || ptr2)
2856 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002857 }
2858 return 1;
2859}
2860
Willy Tarreau87b09662015-04-03 00:22:06 +02002861/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002862 * frontend counters and return its previous value into temp integer.
2863 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002864 */
2865static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002866smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002867{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002868 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002869
2870 if (!stkctr)
2871 return 0;
2872
Willy Tarreau37406352012-04-23 16:16:37 +02002873 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002874 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002875 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002876
2877 if (stkctr_entry(stkctr) == NULL)
2878 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2879
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002880 if (stkctr_entry(stkctr) != NULL) {
2881 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002882 if (!ptr)
2883 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002884 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002885 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002886 /* If data was modified, we need to touch to re-schedule sync */
2887 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002888 }
2889 return 1;
2890}
2891
Willy Tarreau87b09662015-04-03 00:22:06 +02002892/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002893 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2894 * "src_conn_cnt" only.
2895 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002896static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002897smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002898{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002899 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002900
2901 if (!stkctr)
2902 return 0;
2903
Willy Tarreau37406352012-04-23 16:16:37 +02002904 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002905 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002906 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002907 if (stkctr_entry(stkctr) != NULL) {
2908 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002909 if (!ptr)
2910 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002911 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002912 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002913 return 1;
2914}
2915
Willy Tarreau87b09662015-04-03 00:22:06 +02002916/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002917 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2918 * only.
2919 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002920static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002921smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002922{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002923 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002924
2925 if (!stkctr)
2926 return 0;
2927
Willy Tarreau37406352012-04-23 16:16:37 +02002928 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002929 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002930 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002931 if (stkctr_entry(stkctr) != NULL) {
2932 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002933 if (!ptr)
2934 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002935 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002936 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002937 }
2938 return 1;
2939}
2940
Willy Tarreau87b09662015-04-03 00:22:06 +02002941/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002942 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002943 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002944 */
2945static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002946smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002947{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002948 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002949 struct stksess *ts;
2950 struct stktable_key *key;
2951 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002952 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002953
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002954 if (!conn)
2955 return 0;
2956
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002957 /* Fetch source adress in a sample. */
2958 if (!smp_fetch_src(NULL, smp, NULL, NULL))
2959 return 0;
2960
2961 /* Converts into key. */
2962 key = smp_to_stkey(smp, &args->data.prx->table);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002963 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002964 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002965
Willy Tarreau24e32d82012-04-23 23:55:44 +02002966 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002967
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002968 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2969 /* entry does not exist and could not be created */
2970 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002971
2972 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2973 if (!ptr)
2974 return 0; /* parameter not stored in this table */
2975
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002976 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002977 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02002978 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02002979 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002980 return 1;
2981}
2982
Willy Tarreau87b09662015-04-03 00:22:06 +02002983/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002984 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2985 * "src_conn_cur" only.
2986 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002987static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002988smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002989{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002990 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002991
2992 if (!stkctr)
2993 return 0;
2994
Willy Tarreau37406352012-04-23 16:16:37 +02002995 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002996 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002997 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002998 if (stkctr_entry(stkctr) != NULL) {
2999 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003000 if (!ptr)
3001 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003002 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003003 }
3004 return 1;
3005}
3006
Willy Tarreau87b09662015-04-03 00:22:06 +02003007/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02003008 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3009 * "src_sess_cnt" only.
3010 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003011static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003012smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003013{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003014 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02003015
3016 if (!stkctr)
3017 return 0;
3018
Willy Tarreau37406352012-04-23 16:16:37 +02003019 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003020 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003021 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003022 if (stkctr_entry(stkctr) != NULL) {
3023 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003024 if (!ptr)
3025 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003026 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003027 }
3028 return 1;
3029}
3030
Willy Tarreau87b09662015-04-03 00:22:06 +02003031/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003032 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3033 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003034static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003035smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003036{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003037 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003038
3039 if (!stkctr)
3040 return 0;
3041
Willy Tarreau37406352012-04-23 16:16:37 +02003042 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003043 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003044 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003045 if (stkctr_entry(stkctr) != NULL) {
3046 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003047 if (!ptr)
3048 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003049 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003050 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003051 }
3052 return 1;
3053}
3054
Willy Tarreau87b09662015-04-03 00:22:06 +02003055/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02003056 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3057 * "src_http_req_cnt" only.
3058 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003059static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003060smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003061{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003062 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02003063
3064 if (!stkctr)
3065 return 0;
3066
Willy Tarreau37406352012-04-23 16:16:37 +02003067 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003068 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003069 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003070 if (stkctr_entry(stkctr) != NULL) {
3071 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003072 if (!ptr)
3073 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003074 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003075 }
3076 return 1;
3077}
3078
Willy Tarreau87b09662015-04-03 00:22:06 +02003079/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02003080 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3081 * "src_http_req_rate" only.
3082 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003083static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003084smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003085{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003086 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02003087
3088 if (!stkctr)
3089 return 0;
3090
Willy Tarreau37406352012-04-23 16:16:37 +02003091 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003092 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003093 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003094 if (stkctr_entry(stkctr) != NULL) {
3095 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003096 if (!ptr)
3097 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003098 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003099 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003100 }
3101 return 1;
3102}
3103
Willy Tarreau87b09662015-04-03 00:22:06 +02003104/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003105 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3106 * "src_http_err_cnt" only.
3107 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003108static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003109smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003110{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003111 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003112
3113 if (!stkctr)
3114 return 0;
3115
Willy Tarreau37406352012-04-23 16:16:37 +02003116 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003117 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003118 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003119 if (stkctr_entry(stkctr) != NULL) {
3120 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003121 if (!ptr)
3122 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003123 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003124 }
3125 return 1;
3126}
3127
Willy Tarreau87b09662015-04-03 00:22:06 +02003128/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003129 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3130 * "src_http_err_rate" only.
3131 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003132static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003133smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003134{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003135 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003136
3137 if (!stkctr)
3138 return 0;
3139
Willy Tarreau37406352012-04-23 16:16:37 +02003140 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003141 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003142 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003143 if (stkctr_entry(stkctr) != NULL) {
3144 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003145 if (!ptr)
3146 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003147 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003148 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003149 }
3150 return 1;
3151}
3152
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003153/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003154 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003155 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3156 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003157static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003158smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003159{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003160 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003161
3162 if (!stkctr)
3163 return 0;
3164
Willy Tarreau37406352012-04-23 16:16:37 +02003165 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003166 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003167 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003168 if (stkctr_entry(stkctr) != NULL) {
3169 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003170 if (!ptr)
3171 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003172 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003173 }
3174 return 1;
3175}
3176
Willy Tarreau613fe992013-07-23 17:39:19 +02003177/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003178 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003179 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003180 */
3181static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003182smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003183{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003184 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003185
3186 if (!stkctr)
3187 return 0;
3188
Willy Tarreau37406352012-04-23 16:16:37 +02003189 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003190 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003191 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003192 if (stkctr_entry(stkctr) != NULL) {
3193 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003194 if (!ptr)
3195 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003196 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003197 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003198 }
3199 return 1;
3200}
3201
Willy Tarreau53aea102013-07-23 17:39:02 +02003202/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003203 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003204 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3205 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003206static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003207smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003208{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003209 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003210
3211 if (!stkctr)
3212 return 0;
3213
Willy Tarreau37406352012-04-23 16:16:37 +02003214 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003215 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003216 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003217 if (stkctr_entry(stkctr) != NULL) {
3218 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003219 if (!ptr)
3220 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003221 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003222 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003223 return 1;
3224}
3225
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003226/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003227 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003228 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003229 */
3230static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003231smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003232{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003233 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003234
3235 if (!stkctr)
3236 return 0;
3237
Willy Tarreau37406352012-04-23 16:16:37 +02003238 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003239 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003240 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003241 if (stkctr_entry(stkctr) != NULL) {
3242 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003243 if (!ptr)
3244 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003245 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003246 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003247 }
3248 return 1;
3249}
3250
Willy Tarreau87b09662015-04-03 00:22:06 +02003251/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003252 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3253 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003254static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003255smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003256{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003257 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003258
Willy Tarreau563eef42013-07-23 18:32:02 +02003259 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003260 return 0;
3261
Willy Tarreau563eef42013-07-23 18:32:02 +02003262 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003263 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003264 smp->data.u.sint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003265 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003266}
3267
Willy Tarreau34db1082012-04-19 17:16:54 +02003268/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003269 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003270 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003271static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003272smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003273{
Willy Tarreau37406352012-04-23 16:16:37 +02003274 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003275 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003276 smp->data.u.sint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003277 return 1;
3278}
3279
Willy Tarreau34db1082012-04-19 17:16:54 +02003280/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003281 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003282 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003283static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003284smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003285{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003286 struct proxy *px;
3287
Willy Tarreau24e32d82012-04-23 23:55:44 +02003288 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003289 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003290 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003291 smp->data.u.sint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003292 return 1;
3293}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003294
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003295/* 0=OK, <0=Alert, >0=Warning */
3296static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
3297 struct proxy *px, struct act_rule *rule,
3298 char **err)
3299{
3300 struct action_kw *kw;
3301
3302 /* Check if the service name exists. */
3303 if (*(args[*cur_arg]) == 0) {
3304 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01003305 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003306 }
3307
3308 /* lookup for keyword corresponding to a service. */
3309 kw = action_lookup(&service_keywords, args[*cur_arg]);
3310 if (!kw) {
3311 memprintf(err, "'%s' unknown service name.", args[1]);
3312 return ACT_RET_PRS_ERR;
3313 }
3314 (*cur_arg)++;
3315
3316 /* executes specific rule parser. */
3317 rule->kw = kw;
3318 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
3319 return ACT_RET_PRS_ERR;
3320
3321 /* Register processing function. */
3322 rule->action_ptr = process_use_service;
3323 rule->action = ACT_CUSTOM;
3324
3325 return ACT_RET_PRS_OK;
3326}
3327
3328void service_keywords_register(struct action_kw_list *kw_list)
3329{
3330 LIST_ADDQ(&service_keywords, &kw_list->list);
3331}
3332
3333/* main configuration keyword registration. */
3334static struct action_kw_list stream_tcp_keywords = { ILH, {
3335 { "use-service", stream_parse_use_service },
3336 { /* END */ }
3337}};
3338
3339static struct action_kw_list stream_http_keywords = { ILH, {
3340 { "use-service", stream_parse_use_service },
3341 { /* END */ }
3342}};
3343
Willy Tarreau61612d42012-04-19 18:42:05 +02003344/* Note: must not be declared <const> as its list will be overwritten.
3345 * Please take care of keeping this list alphabetically sorted.
3346 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003347static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003348 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003349}};
3350
Willy Tarreau281c7992013-01-08 01:23:27 +01003351/* Note: must not be declared <const> as its list will be overwritten.
3352 * Please take care of keeping this list alphabetically sorted.
3353 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003354static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003355 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3356 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3357 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3358 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3359 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3360 { "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 +02003361 { "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 +02003362 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3363 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3364 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3365 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3366 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3367 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3368 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3369 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3370 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3371 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3372 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3373 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3374 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003375 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3376 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3377 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3378 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3379 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3380 { "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 +02003381 { "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 +02003382 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3383 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3384 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3385 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3386 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3387 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3388 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3389 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3390 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3391 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3392 { "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 +02003393 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003394 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3395 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3396 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3397 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3398 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3399 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3400 { "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 +02003401 { "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 +02003402 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3403 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3404 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3405 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3406 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3407 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3408 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3409 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3410 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3411 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3412 { "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 +02003413 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003414 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3415 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3416 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3417 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3418 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3419 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3420 { "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 +02003421 { "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 +02003422 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3423 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3424 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3425 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3426 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3427 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3428 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3429 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3430 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3431 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3432 { "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 +02003433 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003434 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3435 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3436 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3437 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3438 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3439 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3440 { "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 +02003441 { "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 +02003442 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3443 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3444 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3445 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3446 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3447 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3448 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3449 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3450 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3451 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3452 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3453 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3454 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3455 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003456 { /* END */ },
3457}};
3458
Willy Tarreau8b22a712010-06-18 17:46:06 +02003459__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003460static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003461{
Willy Tarreau281c7992013-01-08 01:23:27 +01003462 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003463 acl_register_keywords(&acl_kws);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003464 tcp_req_cont_keywords_register(&stream_tcp_keywords);
3465 http_req_keywords_register(&stream_http_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003466}
3467
Willy Tarreaubaaee002006-06-26 02:48:02 +02003468/*
3469 * Local variables:
3470 * c-indent-level: 8
3471 * c-basic-offset: 8
3472 * End:
3473 */