blob: 3982daab79874b108bf68f0738c99cf6036d399a [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>
Christopher Fauletd7c91962015-04-30 11:48:27 +020025#include <types/filters.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010026#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020028#include <proto/acl.h>
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020029#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020030#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010031#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020032#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010033#include <proto/checks.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020034#include <proto/connection.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020035#include <proto/dumpstats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020036#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020037#include <proto/filters.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020038#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020039#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010040#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010041#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020042#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020043#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020044#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020045#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020046#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010047#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010048#include <proto/proto_http.h>
49#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020050#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020053#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010054#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010055#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010056#include <proto/task.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020057#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau87b09662015-04-03 00:22:06 +020059struct pool_head *pool2_stream;
60struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
Willy Tarreau87b09662015-04-03 00:22:06 +020062/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010063struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
64
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020065/* List of all use-service keywords. */
66static struct list service_keywords = LIST_HEAD_INIT(service_keywords);
67
Willy Tarreau9903f0e2015-04-04 18:50:31 +020068/* This function is called from the session handler which detects the end of
Willy Tarreau73b65ac2015-04-08 18:26:29 +020069 * handshake, in order to complete initialization of a valid stream. It must be
70 * called with a session (which may be embryonic). It returns the pointer to
71 * the newly created stream, or NULL in case of fatal error. The client-facing
72 * end point is assigned to <origin>, which must be valid. The task's context
73 * is set to the new stream, and its function is set to process_stream().
74 * Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +020075 */
Willy Tarreau73b65ac2015-04-08 18:26:29 +020076struct stream *stream_new(struct session *sess, struct task *t, enum obj_type *origin)
Willy Tarreau2542b532012-08-31 16:01:23 +020077{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020078 struct stream *s;
Willy Tarreau73b65ac2015-04-08 18:26:29 +020079 struct connection *conn = objt_conn(origin);
80 struct appctx *appctx = objt_appctx(origin);
Willy Tarreau2542b532012-08-31 16:01:23 +020081
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020082 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020083 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020084
85 /* minimum stream initialization required for an embryonic stream is
86 * fairly low. We need very little to execute L4 ACLs, then we need a
87 * task to make the client-side connection live on its own.
88 * - flags
89 * - stick-entry tracking
90 */
91 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020092 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020093 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020094 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
95 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +020096 /* This function is called just after the handshake, so the handshake duration is
97 * between the accept time and now.
98 */
99 s->logs.t_handshake = tv_ms_elapsed(&sess->tv_accept, &now);
100 s->logs.t_idle = -1;
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200101 tv_zero(&s->logs.tv_request);
102 s->logs.t_queue = -1;
103 s->logs.t_connect = -1;
104 s->logs.t_data = -1;
105 s->logs.t_close = 0;
106 s->logs.bytes_in = s->logs.bytes_out = 0;
107 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
108 s->logs.srv_queue_size = 0; /* we will get this number soon */
109
110 /* default logging function */
111 s->do_log = strm_log;
112
113 /* default error reporting function, may be changed by analysers */
114 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200115
116 /* Initialise the current rule list pointer to NULL. We are sure that
117 * any rulelist match the NULL pointer.
118 */
119 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200120 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200121
Willy Tarreaua68f7622015-09-21 17:48:24 +0200122 /* Copy SC counters for the stream. We don't touch refcounts because
123 * any reference we have is inherited from the session. Since the stream
124 * doesn't exist without the session, the session's existence guarantees
125 * we don't lose the entry. During the store operation, the stream won't
126 * touch these ones.
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200127 */
Thierry FOURNIERc8fdb982015-08-16 12:03:39 +0200128 memcpy(s->stkctr, sess->stkctr, sizeof(s->stkctr));
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200129
130 s->sess = sess;
131 s->si[0].flags = SI_FL_NONE;
132 s->si[1].flags = SI_FL_ISBACK;
133
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200134 s->uniq_id = global.req_count++;
135
Willy Tarreau87b09662015-04-03 00:22:06 +0200136 /* OK, we're keeping the stream, so let's properly initialize the stream */
137 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200138 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100139 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200140
Willy Tarreaue7dff022015-04-03 01:14:29 +0200141 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200142 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200143
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200144 s->task = t;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200145 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200146 t->context = s;
147 t->expire = TICK_ETERNITY;
148
Willy Tarreau87b09662015-04-03 00:22:06 +0200149 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200150 * This changes later when switching rules are executed or
151 * when the default backend is assigned.
152 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200153 s->be = sess->fe;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100154 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200155 s->req_cap = NULL;
156 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200157
Willy Tarreauebcd4842015-06-19 11:59:02 +0200158 /* Initialise all the variables contexts even if not used.
159 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200160 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200161 vars_init(&s->vars_txn, SCOPE_TXN);
162 vars_init(&s->vars_reqres, SCOPE_REQ);
163
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200164 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100165 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200166 si_set_state(&s->si[0], SI_ST_EST);
167
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200168 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200169 if (conn)
170 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200171 else if (appctx)
172 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200173
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200174 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200175 s->si[0].flags |= SI_FL_INDEP_STR;
176
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200177 /* pre-initialize the other side's stream interface to an INIT state. The
178 * callbacks will be initialized before attempting to connect.
179 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100180 si_reset(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200181
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200182 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200183 s->si[1].flags |= SI_FL_INDEP_STR;
184
Willy Tarreau87b09662015-04-03 00:22:06 +0200185 stream_init_srv_conn(s);
Willy Tarreaud1769b82015-04-06 00:25:48 +0200186 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200187 s->pend_pos = NULL;
188
189 /* init store persistence */
190 s->store_count = 0;
191
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100192 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100193 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200194 s->req.analysers = 0;
195 channel_auto_connect(&s->req); /* don't wait to establish connection */
196 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200197
198 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100199 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100200 s->req.rex = TICK_ETERNITY;
201 s->req.wex = TICK_ETERNITY;
202 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200203
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100204 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100205 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100206 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200207
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200208 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100209 s->req.flags |= CF_NEVER_WAIT;
210 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200211 }
212
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200213 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100214 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100215 s->res.rex = TICK_ETERNITY;
216 s->res.wex = TICK_ETERNITY;
217 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200218
Willy Tarreaueee5b512015-04-03 23:46:31 +0200219 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100220
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100221 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100222
Christopher Faulet92d36382015-11-05 13:35:03 +0100223 if (flt_stream_init(s) < 0 || flt_stream_start(s) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200224 goto out_fail_accept;
225
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200226 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200227 if (conn)
228 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200229 else if (appctx)
Willy Tarreaufe127932015-04-21 19:23:39 +0200230 si_applet_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200231
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200232 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200233 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200234
235 /* it is important not to call the wakeup function directly but to
236 * pass through task_wakeup(), because this one knows how to apply
237 * priorities to tasks.
238 */
239 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200240 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200241
242 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200243 out_fail_accept:
Christopher Faulet92d36382015-11-05 13:35:03 +0100244 flt_stream_release(s, 0);
Willy Tarreau3b246412014-11-25 17:10:33 +0100245 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200246 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200247 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200248}
249
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200251 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200252 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200253static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200254{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200255 struct session *sess = strm_sess(s);
256 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100257 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200258 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200259 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100260
Willy Tarreaubaaee002006-06-26 02:48:02 +0200261 if (s->pend_pos)
262 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100263
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100264 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200265 if (s->flags & SF_CURR_SESS) {
266 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100267 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100268 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100269 if (may_dequeue_tasks(objt_server(s->target), s->be))
270 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100271 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100272
Willy Tarreau7c669d72008-06-20 15:04:11 +0200273 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200274 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200275 * it should normally be only the same as the one above,
276 * so this should not happen in fact.
277 */
278 sess_change_server(s, NULL);
279 }
280
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100281 if (s->req.pipe)
282 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100283
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100284 if (s->res.pipe)
285 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100286
Willy Tarreaubf883e02014-11-25 21:10:35 +0100287 /* We may still be present in the buffer wait queue */
288 if (!LIST_ISEMPTY(&s->buffer_wait)) {
289 LIST_DEL(&s->buffer_wait);
290 LIST_INIT(&s->buffer_wait);
291 }
292
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100293 b_drop(&s->req.buf);
294 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100295 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200296 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200297
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100298 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200299 if (s->txn)
300 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100301
Willy Tarreau1e954912012-10-12 17:50:05 +0200302 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200303 if (cli_conn)
304 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200305
Willy Tarreaua4cda672010-06-06 18:28:49 +0200306 for (i = 0; i < s->store_count; i++) {
307 if (!s->store[i].ts)
308 continue;
309 stksess_free(s->store[i].table, s->store[i].ts);
310 s->store[i].ts = NULL;
311 }
312
Willy Tarreaueee5b512015-04-03 23:46:31 +0200313 if (s->txn) {
314 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
315 pool_free2(pool2_http_txn, s->txn);
316 s->txn = NULL;
317 }
318
Christopher Fauletd7c91962015-04-30 11:48:27 +0200319 flt_stream_stop(s);
Christopher Faulet92d36382015-11-05 13:35:03 +0100320 flt_stream_release(s, 0);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200321
Willy Tarreau92fb9832007-10-16 17:34:28 +0200322 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200323 pool_free2(fe->rsp_cap_pool, s->res_cap);
324 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100326
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200327 /* Cleanup all variable contexts. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100328 vars_prune(&s->vars_txn, s->sess, s);
329 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200330
Willy Tarreau87b09662015-04-03 00:22:06 +0200331 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200332
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100333 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100334 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200335 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100336 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100337 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100338 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200339 if (s->list.n != &streams)
340 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100341 bref->ref = s->list.n;
342 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100343 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200344 si_release_endpoint(&s->si[1]);
345 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200346
347 /* FIXME: for now we have a 1:1 relation between stream and session so
348 * the stream must free the session.
349 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200350 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200351 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200352
353 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200354 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200355 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200356 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200357 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200358 pool_flush2(pool2_requri);
359 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200360 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200361 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100362 pool_flush2(pool2_connection);
363 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200364 pool_flush2(fe->req_cap_pool);
365 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200366 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200367}
368
Willy Tarreau78955f42014-12-28 13:09:02 +0100369/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
370 * that it's not the last available buffer or it's the response buffer. Unless
371 * the buffer is the response buffer, an extra control is made so that we always
372 * keep <tune.buffers.reserved> buffers available after this allocation. To be
373 * called at the beginning of recv() callbacks to ensure that the required
374 * buffers are properly allocated. Returns 0 in case of failure, non-zero
375 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100376 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200377int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100378{
Willy Tarreau87b09662015-04-03 00:22:06 +0200379 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100380 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100381 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100382
Willy Tarreau78955f42014-12-28 13:09:02 +0100383 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100384 margin = global.tune.reserved_bufs;
385
Thierry FOURNIER27929fb2015-09-25 08:36:11 +0200386 s = chn_strm(chn);
Willy Tarreau78955f42014-12-28 13:09:02 +0100387
388 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100389 if (b)
390 return 1;
391
Willy Tarreaubf883e02014-11-25 21:10:35 +0100392 if (LIST_ISEMPTY(&s->buffer_wait))
393 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100394 return 0;
395}
396
Willy Tarreau87b09662015-04-03 00:22:06 +0200397/* Allocates a work buffer for stream <s>. It is meant to be called inside
398 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200399 * to work fine, which is the response buffer so that an error message may be
400 * built and returned. Response buffers may be allocated from the reserve, this
401 * is critical to ensure that a response may always flow and will never block a
402 * server from releasing a connection. Returns 0 in case of failure, non-zero
403 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100404 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200405int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100406{
Willy Tarreaubf883e02014-11-25 21:10:35 +0100407 if (!LIST_ISEMPTY(&s->buffer_wait)) {
408 LIST_DEL(&s->buffer_wait);
409 LIST_INIT(&s->buffer_wait);
410 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100411
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200412 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100413 return 1;
414
Willy Tarreaubf883e02014-11-25 21:10:35 +0100415 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100416 return 0;
417}
418
419/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100420 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200421 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100422 * a single buffer, so it makes sense to try to wake two up when two buffers
423 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100424 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200425void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100426{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100427 if (s->req.buf->size && buffer_empty(s->req.buf))
428 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100429
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100430 if (s->res.buf->size && buffer_empty(s->res.buf))
431 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100432
Willy Tarreaubf883e02014-11-25 21:10:35 +0100433 /* if we're certain to have at least 1 buffer available, and there is
434 * someone waiting, we can wake up a waiter and offer them.
435 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100436 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200437 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100438}
439
Willy Tarreau87b09662015-04-03 00:22:06 +0200440/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100441 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200442 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100443 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200444void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100445{
Willy Tarreau87b09662015-04-03 00:22:06 +0200446 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100447
448 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100449 if (rqlimit <= run_queue)
450 break;
451
Willy Tarreaubf883e02014-11-25 21:10:35 +0100452 if (sess->task->state & TASK_RUNNING)
453 continue;
454
455 LIST_DEL(&sess->buffer_wait);
456 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100457 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100458 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100459}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200460
461/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200462int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200463{
Willy Tarreau87b09662015-04-03 00:22:06 +0200464 LIST_INIT(&streams);
465 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
466 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200467}
468
Willy Tarreau87b09662015-04-03 00:22:06 +0200469void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100470{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200471 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100472 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200473 void *ptr1,*ptr2;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100474 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100475
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100476 bytes = s->req.total - s->logs.bytes_in;
477 s->logs.bytes_in = s->req.total;
478 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200479 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100480
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100481 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100482
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100483 if (objt_server(s->target))
484 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200485
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200486 if (sess->listener && sess->listener->counters)
487 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200488
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100489 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200490 struct stkctr *stkctr = &s->stkctr[i];
491
492 if (!stkctr_entry(stkctr)) {
493 stkctr = &sess->stkctr[i];
494 if (!stkctr_entry(stkctr))
495 continue;
496 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200497
Emeric Brun57056f02015-06-15 18:29:57 +0200498 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
499 if (ptr1)
500 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200501
Emeric Brun57056f02015-06-15 18:29:57 +0200502 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
503 if (ptr2)
504 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200505 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200506
507 /* If data was modified, we need to touch to re-schedule sync */
508 if (ptr1 || ptr2)
509 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100510 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100511 }
512
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100513 bytes = s->res.total - s->logs.bytes_out;
514 s->logs.bytes_out = s->res.total;
515 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200516 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100517
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100518 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100519
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100520 if (objt_server(s->target))
521 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200522
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200523 if (sess->listener && sess->listener->counters)
524 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200525
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100526 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200527 struct stkctr *stkctr = &s->stkctr[i];
528
529 if (!stkctr_entry(stkctr)) {
530 stkctr = &sess->stkctr[i];
531 if (!stkctr_entry(stkctr))
532 continue;
533 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200534
Emeric Brun57056f02015-06-15 18:29:57 +0200535 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
536 if (ptr1)
537 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200538
Emeric Brun57056f02015-06-15 18:29:57 +0200539 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
540 if (ptr2)
541 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200542 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200543
544 /* If data was modified, we need to touch to re-schedule sync */
545 if (ptr1 || ptr2)
546 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100547 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100548 }
549}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100551/* This function is called with (si->state == SI_ST_CON) meaning that a
552 * connection was attempted and that the file descriptor is already allocated.
553 * We must check for establishment, error and abort. Possible output states
554 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
555 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200556 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100557 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200558static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100559{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100560 struct stream_interface *si = &s->si[1];
561 struct channel *req = &s->req;
562 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100563
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100564 /* If we got an error, or if nothing happened and the connection timed
565 * out, we must give up. The CER state handler will take care of retry
566 * attempts and error reports.
567 */
568 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100569 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100570 /* Some data were sent past the connection establishment,
571 * so we need to pretend we're established to log correctly
572 * and let later states handle the failure.
573 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100574 si->state = SI_ST_EST;
575 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100576 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100577 return 1;
578 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100579 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100580 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100581
Willy Tarreau568743a2016-08-07 08:12:35 +0200582 si_release_endpoint(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100583
584 if (si->err_type)
585 return 0;
586
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100587 if (si->flags & SI_FL_ERR)
588 si->err_type = SI_ET_CONN_ERR;
589 else
590 si->err_type = SI_ET_CONN_TO;
591 return 0;
592 }
593
594 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100595 if (!(req->flags & CF_WRITE_PARTIAL) &&
596 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200597 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
598 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100599 s->be->options & PR_O_ABRT_CLOSE)))) {
600 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200601 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100602 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100603 if (s->srv_error)
604 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100605 return 1;
606 }
607
608 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200609 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100610 return 1;
611
612 /* OK, this means that a connection succeeded. The caller will be
613 * responsible for handling the transition from CON to EST.
614 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100615 si->state = SI_ST_EST;
616 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100617 return 1;
618}
619
620/* This function is called with (si->state == SI_ST_CER) meaning that a
621 * previous connection attempt has failed and that the file descriptor
622 * has already been released. Possible causes include asynchronous error
623 * notification and time out. Possible output states are SI_ST_CLO when
624 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
625 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
626 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
627 * marked as in error state. It returns 0.
628 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200629static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100630{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100631 struct stream_interface *si = &s->si[1];
632
Willy Tarreau87b09662015-04-03 00:22:06 +0200633 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100634 if (objt_server(s->target)) {
635 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100636
Willy Tarreaue7dff022015-04-03 01:14:29 +0200637 if (s->flags & SF_CURR_SESS) {
638 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100639 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100640 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100641 }
642
643 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200644 si->conn_retries--;
645 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100646 if (!si->err_type) {
647 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100648 }
649
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100650 if (objt_server(s->target))
651 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100652 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100653 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100654 if (may_dequeue_tasks(objt_server(s->target), s->be))
655 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100656
657 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200658 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100659 s->req.flags |= CF_WRITE_ERROR;
660 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100661
662 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100663 if (s->srv_error)
664 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100665 return 0;
666 }
667
668 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700669 * retry on another server. By default this redispatch occurs on the
670 * last retry, but if configured we allow redispatches to occur on
671 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200672 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100673 * bit to ignore any persistence cookie. We won't count a retry nor a
674 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200675 * If the connection is not persistent, the balancing algorithm is not
676 * determinist (round robin) and there is more than one active server,
677 * we accept to perform an immediate redispatch without waiting since
678 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100679 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200680 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700681 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
Willy Tarreau49008c12016-01-13 07:58:44 +0100682 ((__objt_server(s->target)->state < SRV_ST_RUNNING) ||
683 (((s->be->redispatch_after > 0) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700684 ((s->be->conn_retries - si->conn_retries) %
685 s->be->redispatch_after == 0)) ||
686 ((s->be->redispatch_after < 0) &&
687 ((s->be->conn_retries - si->conn_retries) %
688 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200689 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700690 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100691 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100692 if (may_dequeue_tasks(objt_server(s->target), s->be))
693 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100694
Willy Tarreaue7dff022015-04-03 01:14:29 +0200695 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100696 si->state = SI_ST_REQ;
697 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100698 if (objt_server(s->target))
699 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100700 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100701 si->state = SI_ST_ASS;
702 }
703
704 if (si->flags & SI_FL_ERR) {
705 /* The error was an asynchronous connection error, and we will
706 * likely have to retry connecting to the same server, most
707 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200708 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100709 */
710
Willy Tarreaub0290662014-06-13 17:04:44 +0200711 int delay = 1000;
712
713 if (s->be->timeout.connect && s->be->timeout.connect < delay)
714 delay = s->be->timeout.connect;
715
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100716 if (!si->err_type)
717 si->err_type = SI_ET_CONN_ERR;
718
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200719 /* only wait when we're retrying on the same server */
720 if (si->state == SI_ST_ASS ||
721 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
722 (s->be->srv_act <= 1)) {
723 si->state = SI_ST_TAR;
724 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
725 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100726 return 0;
727 }
728 return 0;
729}
730
731/*
732 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200733 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200734 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100735 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200736static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100738 struct stream_interface *si = &s->si[1];
739 struct channel *req = &s->req;
740 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100741
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100742 /* First, centralize the timers information */
743 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
744 si->exp = TICK_ETERNITY;
745
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100746 if (objt_server(s->target))
747 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100748
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100749 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100750 /* if the user wants to log as soon as possible, without counting
751 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200752 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100753 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100754 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100755 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100756 }
757 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100758 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100759 }
760
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200761 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Christopher Faulet309c6412015-12-02 09:57:32 +0100762
763 /* Be sure to filter response headers if the backend is an HTTP proxy
764 * and if there are filters attached to the stream. */
765 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
766 rep->analysers |= AN_FLT_HTTP_HDRS;
767
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200768 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200769 if (req->flags & CF_WAKE_CONNECT) {
770 req->flags |= CF_WAKE_ONCE;
771 req->flags &= ~CF_WAKE_CONNECT;
772 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200773 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200774 /* real connections have timeouts */
775 req->wto = s->be->timeout.server;
776 rep->rto = s->be->timeout.server;
777 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100778 req->wex = TICK_ETERNITY;
779}
780
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700781/* Check if the connection request is in such a state that it can be aborted. */
782static int check_req_may_abort(struct channel *req, struct stream *s)
783{
784 return ((req->flags & (CF_READ_ERROR)) ||
785 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
786 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE)));
787}
788
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100789/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
790 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100791 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
792 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
793 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100794 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200795static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100796{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100797 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100798 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100799 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100800
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100801 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 +0100802 now_ms, __FUNCTION__,
803 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400804 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100805 req->rex, s->res.wex,
806 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400807 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 +0100808
809 if (si->state == SI_ST_ASS) {
810 /* Server assigned to connection request, we have to try to connect now */
811 int conn_err;
812
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700813 /* Before we try to initiate the connection, see if the
814 * request may be aborted instead.
815 */
816 if (check_req_may_abort(req, s)) {
817 si->err_type |= SI_ET_CONN_ABRT;
818 goto abort_connection;
819 }
820
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100821 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100822 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100823
Willy Tarreaue7dff022015-04-03 01:14:29 +0200824 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100825 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100826 if (srv)
827 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500828 if (srv)
829 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100830 return;
831 }
832
833 /* We have received a synchronous error. We might have to
834 * abort, retry immediately or redispatch.
835 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200836 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100837 if (!si->err_type) {
838 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100839 }
840
Willy Tarreau827aee92011-03-10 16:55:02 +0100841 if (srv)
842 srv_inc_sess_ctr(srv);
843 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500844 srv_set_sess_last(srv);
845 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100846 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100847 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100848
Willy Tarreau87b09662015-04-03 00:22:06 +0200849 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100850 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100851 if (may_dequeue_tasks(srv, s->be))
852 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100853
854 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200855 si_shutr(si);
856 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100857 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100858
859 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
860
Willy Tarreau87b09662015-04-03 00:22:06 +0200861 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100862 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100863 if (s->srv_error)
864 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100865 return;
866 }
867
868 /* We are facing a retryable error, but we don't want to run a
869 * turn-around now, as the problem is likely a source port
870 * allocation problem, so we want to retry now.
871 */
872 si->state = SI_ST_CER;
873 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100874 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100875 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
876 return;
877 }
878 else if (si->state == SI_ST_QUE) {
879 /* connection request was queued, check for any update */
880 if (!s->pend_pos) {
881 /* The connection is not in the queue anymore. Either
882 * we have a server connection slot available and we
883 * go directly to the assigned state, or we need to
884 * load-balance first and go to the INI state.
885 */
886 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200887 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100888 si->state = SI_ST_REQ;
889 else {
890 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
891 si->state = SI_ST_ASS;
892 }
893 return;
894 }
895
896 /* Connection request still in queue... */
897 if (si->flags & SI_FL_EXP) {
898 /* ... and timeout expired */
899 si->exp = TICK_ETERNITY;
900 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100901 if (srv)
902 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100903 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200904 si_shutr(si);
905 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100906 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100907 if (!si->err_type)
908 si->err_type = SI_ET_QUEUE_TO;
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 /* Connection remains in queue, check if we have to abort it */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700916 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100917 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100918 si->err_type |= SI_ET_QUEUE_ABRT;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700919 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100920 }
921
922 /* Nothing changed */
923 return;
924 }
925 else if (si->state == SI_ST_TAR) {
926 /* Connection request might be aborted */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700927 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100928 si->err_type |= SI_ET_CONN_ABRT;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700929 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100930 }
931
932 if (!(si->flags & SI_FL_EXP))
933 return; /* still in turn-around */
934
935 si->exp = TICK_ETERNITY;
936
Willy Tarreau87b09662015-04-03 00:22:06 +0200937 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100938 * marked "assigned".
939 * FIXME: Should we force a redispatch attempt when the server is down ?
940 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200941 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100942 si->state = SI_ST_ASS;
943 else
944 si->state = SI_ST_REQ;
945 return;
946 }
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700947 return;
948
949abort_connection:
950 /* give up */
951 si->exp = TICK_ETERNITY;
952 si_shutr(si);
953 si_shutw(si);
954 si->state = SI_ST_CLO;
955 if (s->srv_error)
956 s->srv_error(s, si);
957 return;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100958}
959
Willy Tarreau87b09662015-04-03 00:22:06 +0200960/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900961 * also counts a failed request if the server state has not reached the request
962 * stage.
963 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200964static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900965{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200966 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900967 if (s->si[1].state < SI_ST_REQ) {
968
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200969 strm_fe(s)->fe_counters.failed_req++;
Willy Tarreau2c1068c2015-09-23 12:21:21 +0200970 if (strm_li(s) && strm_li(s)->counters)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200971 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900972
Willy Tarreaue7dff022015-04-03 01:14:29 +0200973 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900974 }
975 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200976 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900977 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200978 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900979 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200980 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900981 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200982 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900983 }
984}
985
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100986/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100987 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
988 * a real connection to a server, indicating that a server has been assigned,
989 * or SI_ST_EST for a successful connection to an applet. It may also return
990 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100991 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200992static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100993{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100994 struct stream_interface *si = &s->si[1];
995
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100996 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 +0100997 now_ms, __FUNCTION__,
998 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400999 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001000 s->req.rex, s->res.wex,
1001 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -04001002 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 +01001003
1004 if (si->state != SI_ST_REQ)
1005 return;
1006
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001007 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
1008 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001009 struct appctx *appctx = objt_appctx(si->end);
1010
1011 if (!appctx || appctx->applet != __objt_applet(s->target))
1012 appctx = stream_int_register_handler(si, objt_applet(s->target));
1013
1014 if (!appctx) {
1015 /* No more memory, let's immediately abort. Force the
1016 * error code to ignore the ERR_LOCAL which is not a
1017 * real error.
1018 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001019 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001020
1021 si_shutr(si);
1022 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001023 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001024 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001025 si->state = SI_ST_CLO;
1026 if (s->srv_error)
1027 s->srv_error(s, si);
1028 return;
1029 }
1030
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001031 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001032 si->state = SI_ST_EST;
1033 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001034 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001035 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001036 return;
1037 }
1038
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001039 /* Try to assign a server */
1040 if (srv_redispatch_connect(s) != 0) {
1041 /* We did not get a server. Either we queued the
1042 * connection request, or we encountered an error.
1043 */
1044 if (si->state == SI_ST_QUE)
1045 return;
1046
1047 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001048 si_shutr(si);
1049 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001050 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001051 if (!si->err_type)
1052 si->err_type = SI_ET_CONN_OTHER;
1053 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001054 if (s->srv_error)
1055 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001056 return;
1057 }
1058
1059 /* The server is assigned */
1060 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1061 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001062 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001063}
1064
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001065/* This function parses the use-service action ruleset. It executes
1066 * the associated ACL and set an applet as a stream or txn final node.
1067 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1068 * consistent state. It returns ACT_RET_STOP in succes case because
1069 * use-service must be a terminal action. Returns ACT_RET_YIELD
1070 * if the initialisation function require more data.
1071 */
1072enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1073 struct session *sess, struct stream *s, int flags)
1074
1075{
1076 struct appctx *appctx;
1077
1078 /* Initialises the applet if it is required. */
1079 if (flags & ACT_FLAG_FIRST) {
1080 /* Register applet. this function schedules the applet. */
1081 s->target = &rule->applet.obj_type;
1082 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target))))
1083 return ACT_RET_ERR;
1084
1085 /* Initialise the context. */
1086 appctx = si_appctx(&s->si[1]);
1087 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1088 appctx->rule = rule;
1089 }
1090 else
1091 appctx = si_appctx(&s->si[1]);
1092
1093 /* Stops the applet sheduling, in case of the init function miss
1094 * some data.
1095 */
1096 appctx_pause(appctx);
1097 si_applet_stop_get(&s->si[1]);
1098
1099 /* Call initialisation. */
1100 if (rule->applet.init)
1101 switch (rule->applet.init(appctx, px, s)) {
1102 case 0: return ACT_RET_ERR;
1103 case 1: break;
1104 default: return ACT_RET_YIELD;
1105 }
1106
1107 /* Now we can schedule the applet. */
1108 si_applet_cant_get(&s->si[1]);
1109 appctx_wakeup(appctx);
1110
1111 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1112 sess->fe->fe_counters.intercepted_req++;
1113
1114 /* The flag SF_ASSIGNED prevent from server assignment. */
1115 s->flags |= SF_ASSIGNED;
1116
1117 return ACT_RET_STOP;
1118}
1119
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001120/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001121 * if appropriate. The default_backend rule is also considered, then the
1122 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001123 * It returns 1 if the processing can continue on next analysers, or zero if it
1124 * either needs more data or wants to immediately abort the request.
1125 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001126static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001127{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001128 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001129 struct session *sess = s->sess;
1130 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001131
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001132 req->analysers &= ~an_bit;
1133 req->analyse_exp = TICK_ETERNITY;
1134
Willy Tarreau87b09662015-04-03 00:22:06 +02001135 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 +02001136 now_ms, __FUNCTION__,
1137 s,
1138 req,
1139 req->rex, req->wex,
1140 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001141 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001142 req->analysers);
1143
1144 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001145 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001146 struct switching_rule *rule;
1147
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001148 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001149 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001150
Willy Tarreauf51658d2014-04-23 01:21:56 +02001151 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001152 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001153 ret = acl_pass(ret);
1154 if (rule->cond->pol == ACL_COND_UNLESS)
1155 ret = !ret;
1156 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001157
1158 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001159 /* If the backend name is dynamic, try to resolve the name.
1160 * If we can't resolve the name, or if any error occurs, break
1161 * the loop and fallback to the default backend.
1162 */
1163 struct proxy *backend;
1164
1165 if (rule->dynamic) {
1166 struct chunk *tmp = get_trash_chunk();
1167 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1168 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001169 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001170 if (!backend)
1171 break;
1172 }
1173 else
1174 backend = rule->be.backend;
1175
Willy Tarreau87b09662015-04-03 00:22:06 +02001176 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001177 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001178 break;
1179 }
1180 }
1181
1182 /* To ensure correct connection accounting on the backend, we
1183 * have to assign one if it was not set (eg: a listen). This
1184 * measure also takes care of correctly setting the default
1185 * backend if any.
1186 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001187 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001188 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001189 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001190 }
1191
Willy Tarreaufb356202010-08-03 14:02:05 +02001192 /* 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 +02001193 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001194 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1195 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001196 s->req.analysers &= ~AN_FLT_START_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001197 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001198
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001199 /* 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 +02001200 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001201 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001202 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001203 int ret = 1;
1204
1205 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001206 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 +01001207 ret = acl_pass(ret);
1208 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1209 ret = !ret;
1210 }
1211
1212 if (ret) {
1213 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001214 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001215 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001216 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001217 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001218 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001219 break;
1220 }
1221 }
1222
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001223 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001224
1225 sw_failed:
1226 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001227 channel_abort(&s->req);
1228 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001229
Willy Tarreaue7dff022015-04-03 01:14:29 +02001230 if (!(s->flags & SF_ERR_MASK))
1231 s->flags |= SF_ERR_RESOURCE;
1232 if (!(s->flags & SF_FINST_MASK))
1233 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001234
Willy Tarreaueee5b512015-04-03 23:46:31 +02001235 if (s->txn)
1236 s->txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001237 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001238 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001239 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001240}
1241
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001242/* This stream analyser works on a request. It applies all use-server rules on
1243 * it then returns 1. The data must already be present in the buffer otherwise
1244 * they won't match. It always returns 1.
1245 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001246static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001247{
1248 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001249 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001250 struct server_rule *rule;
1251
Willy Tarreau87b09662015-04-03 00:22:06 +02001252 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 +02001253 now_ms, __FUNCTION__,
1254 s,
1255 req,
1256 req->rex, req->wex,
1257 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001258 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001259 req->analysers);
1260
Willy Tarreaue7dff022015-04-03 01:14:29 +02001261 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001262 list_for_each_entry(rule, &px->server_rules, list) {
1263 int ret;
1264
Willy Tarreau192252e2015-04-04 01:47:55 +02001265 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001266 ret = acl_pass(ret);
1267 if (rule->cond->pol == ACL_COND_UNLESS)
1268 ret = !ret;
1269
1270 if (ret) {
1271 struct server *srv = rule->srv.ptr;
1272
Willy Tarreau892337c2014-05-13 23:41:20 +02001273 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001274 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001275 (s->flags & SF_FORCE_PRST)) {
1276 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001277 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001278 break;
1279 }
1280 /* if the server is not UP, let's go on with next rules
1281 * just in case another one is suited.
1282 */
1283 }
1284 }
1285 }
1286
1287 req->analysers &= ~an_bit;
1288 req->analyse_exp = TICK_ETERNITY;
1289 return 1;
1290}
1291
Emeric Brun1d33b292010-01-04 15:47:17 +01001292/* This stream analyser works on a request. It applies all sticking rules on
1293 * it then returns 1. The data must already be present in the buffer otherwise
1294 * they won't match. It always returns 1.
1295 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001296static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001297{
1298 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001299 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001300 struct sticking_rule *rule;
1301
Willy Tarreau87b09662015-04-03 00:22:06 +02001302 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 +01001303 now_ms, __FUNCTION__,
1304 s,
1305 req,
1306 req->rex, req->wex,
1307 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001308 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001309 req->analysers);
1310
1311 list_for_each_entry(rule, &px->sticking_rules, list) {
1312 int ret = 1 ;
1313 int i;
1314
Willy Tarreau9667a802013-12-09 12:52:13 +01001315 /* Only the first stick store-request of each table is applied
1316 * and other ones are ignored. The purpose is to allow complex
1317 * configurations which look for multiple entries by decreasing
1318 * order of precision and to stop at the first which matches.
1319 * An example could be a store of the IP address from an HTTP
1320 * header first, then from the source if not found.
1321 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001322 for (i = 0; i < s->store_count; i++) {
1323 if (rule->table.t == s->store[i].table)
1324 break;
1325 }
1326
1327 if (i != s->store_count)
1328 continue;
1329
1330 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001331 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001332 ret = acl_pass(ret);
1333 if (rule->cond->pol == ACL_COND_UNLESS)
1334 ret = !ret;
1335 }
1336
1337 if (ret) {
1338 struct stktable_key *key;
1339
Willy Tarreau192252e2015-04-04 01:47:55 +02001340 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 +01001341 if (!key)
1342 continue;
1343
1344 if (rule->flags & STK_IS_MATCH) {
1345 struct stksess *ts;
1346
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001347 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001348 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001349 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001350 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001351
1352 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001353 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1354 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001355 if (node) {
1356 struct server *srv;
1357
1358 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001359 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001360 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001361 (s->flags & SF_FORCE_PRST)) {
1362 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001363 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001364 }
1365 }
1366 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001367 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001368 }
1369 }
1370 if (rule->flags & STK_IS_STORE) {
1371 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1372 struct stksess *ts;
1373
1374 ts = stksess_new(rule->table.t, key);
1375 if (ts) {
1376 s->store[s->store_count].table = rule->table.t;
1377 s->store[s->store_count++].ts = ts;
1378 }
1379 }
1380 }
1381 }
1382 }
1383
1384 req->analysers &= ~an_bit;
1385 req->analyse_exp = TICK_ETERNITY;
1386 return 1;
1387}
1388
1389/* This stream analyser works on a response. It applies all store rules on it
1390 * then returns 1. The data must already be present in the buffer otherwise
1391 * they won't match. It always returns 1.
1392 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001393static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001394{
1395 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001396 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001397 struct sticking_rule *rule;
1398 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001399 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001400
Willy Tarreau87b09662015-04-03 00:22:06 +02001401 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 +01001402 now_ms, __FUNCTION__,
1403 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001404 rep,
1405 rep->rex, rep->wex,
1406 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001407 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001408 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001409
1410 list_for_each_entry(rule, &px->storersp_rules, list) {
1411 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001412
Willy Tarreau9667a802013-12-09 12:52:13 +01001413 /* Only the first stick store-response of each table is applied
1414 * and other ones are ignored. The purpose is to allow complex
1415 * configurations which look for multiple entries by decreasing
1416 * order of precision and to stop at the first which matches.
1417 * An example could be a store of a set-cookie value, with a
1418 * fallback to a parameter found in a 302 redirect.
1419 *
1420 * The store-response rules are not allowed to override the
1421 * store-request rules for the same table, but they may coexist.
1422 * Thus we can have up to one store-request entry and one store-
1423 * response entry for the same table at any time.
1424 */
1425 for (i = nbreq; i < s->store_count; i++) {
1426 if (rule->table.t == s->store[i].table)
1427 break;
1428 }
1429
1430 /* skip existing entries for this table */
1431 if (i < s->store_count)
1432 continue;
1433
Emeric Brun1d33b292010-01-04 15:47:17 +01001434 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001435 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001436 ret = acl_pass(ret);
1437 if (rule->cond->pol == ACL_COND_UNLESS)
1438 ret = !ret;
1439 }
1440
1441 if (ret) {
1442 struct stktable_key *key;
1443
Willy Tarreau192252e2015-04-04 01:47:55 +02001444 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 +01001445 if (!key)
1446 continue;
1447
Willy Tarreau37e340c2013-12-06 23:05:21 +01001448 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001449 struct stksess *ts;
1450
1451 ts = stksess_new(rule->table.t, key);
1452 if (ts) {
1453 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001454 s->store[s->store_count++].ts = ts;
1455 }
1456 }
1457 }
1458 }
1459
1460 /* process store request and store response */
1461 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001462 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001463 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001464
Willy Tarreauc93cd162014-05-13 15:54:22 +02001465 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001466 stksess_free(s->store[i].table, s->store[i].ts);
1467 s->store[i].ts = NULL;
1468 continue;
1469 }
1470
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001471 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1472 if (ts) {
1473 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001474 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001475 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001476 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001477 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001478 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001479
1480 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001481 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001482 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001483 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001484 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001485
1486 rep->analysers &= ~an_bit;
1487 rep->analyse_exp = TICK_ETERNITY;
1488 return 1;
1489}
1490
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001491/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001492 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001493 */
1494#define UPDATE_ANALYSERS(real, list, back, flag) { \
1495 list = (((list) & ~(flag)) | ~(back)) & (real); \
1496 back = real; \
1497 if (!(list)) \
1498 break; \
1499 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1500 continue; \
1501}
1502
Christopher Fauleta9215b72016-05-11 17:06:28 +02001503/* These 2 following macros call an analayzer for the specified channel if the
1504 * right flag is set. The first one is used for "filterable" analyzers. If a
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001505 * stream has some registered filters, pre and post analyaze callbacks are
1506 * called. The second are used for other analyzers (AN_FLT_* and
Christopher Fauleta9215b72016-05-11 17:06:28 +02001507 * AN_REQ/RES_HTTP_XFER_BODY) */
1508#define FLT_ANALYZE(strm, chn, fun, list, back, flag, ...) \
1509 { \
1510 if ((list) & (flag)) { \
1511 if (HAS_FILTERS(strm)) { \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001512 if (!flt_pre_analyze((strm), (chn), (flag))) \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001513 break; \
1514 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1515 break; \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001516 if (!flt_post_analyze((strm), (chn), (flag))) \
1517 break; \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001518 } \
1519 else { \
1520 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1521 break; \
1522 } \
1523 UPDATE_ANALYSERS((chn)->analysers, (list), \
1524 (back), (flag)); \
1525 } \
1526 }
1527
1528#define ANALYZE(strm, chn, fun, list, back, flag, ...) \
1529 { \
1530 if ((list) & (flag)) { \
1531 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1532 break; \
1533 UPDATE_ANALYSERS((chn)->analysers, (list), \
1534 (back), (flag)); \
1535 } \
1536 }
1537
Willy Tarreau87b09662015-04-03 00:22:06 +02001538/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001539 * then puts it back to the wait queue in a clean state, or cleans up its
1540 * resources if it must be deleted. Returns in <next> the date the task wants
1541 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1542 * nothing too many times, the request and response buffers flags are monitored
1543 * and each function is called only if at least another function has changed at
1544 * least one flag it is interested in.
1545 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001546struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001547{
Willy Tarreau827aee92011-03-10 16:55:02 +01001548 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001549 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001550 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001551 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001552 unsigned int rq_prod_last, rq_cons_last;
1553 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001554 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001555 struct channel *req, *res;
1556 struct stream_interface *si_f, *si_b;
1557
1558 req = &s->req;
1559 res = &s->res;
1560
1561 si_f = &s->si[0];
1562 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001563
1564 //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 +01001565 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001566
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001567 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001568 if (s->txn)
1569 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001570
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001571 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001572 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1573 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001574
1575 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001576 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1577 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001578
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001579 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001580 si_f->flags |= SI_FL_DONT_WAKE;
1581 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001582
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001583 /* 1a: Check for low level timeouts if needed. We just set a flag on
1584 * stream interfaces when their timeouts have expired.
1585 */
1586 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001587 stream_int_check_timeouts(si_f);
1588 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001589
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001590 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001591 * for future reads or writes. Note: this will also concern upper layers
1592 * but we do not touch any other flag. We must be careful and correctly
1593 * detect state changes when calling them.
1594 */
1595
Willy Tarreau8f128b42014-11-28 15:07:47 +01001596 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001597
Willy Tarreau8f128b42014-11-28 15:07:47 +01001598 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1599 si_b->flags |= SI_FL_NOLINGER;
1600 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001601 }
1602
Willy Tarreau8f128b42014-11-28 15:07:47 +01001603 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1604 if (si_f->flags & SI_FL_NOHALF)
1605 si_f->flags |= SI_FL_NOLINGER;
1606 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001607 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001608
Willy Tarreau8f128b42014-11-28 15:07:47 +01001609 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001610
Willy Tarreau8f128b42014-11-28 15:07:47 +01001611 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1612 si_f->flags |= SI_FL_NOLINGER;
1613 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001614 }
1615
Willy Tarreau8f128b42014-11-28 15:07:47 +01001616 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1617 if (si_b->flags & SI_FL_NOHALF)
1618 si_b->flags |= SI_FL_NOLINGER;
1619 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001620 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001621
1622 /* Once in a while we're woken up because the task expires. But
1623 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001624 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001625 * timeout needs to be refreshed.
1626 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001627 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001628 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1629 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001630 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau5fb04712016-05-04 10:18:37 +02001631 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER)) {
1632 si_f->flags &= ~SI_FL_DONT_WAKE;
1633 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau798f4322012-11-08 14:49:17 +01001634 goto update_exp_and_leave;
Willy Tarreau5fb04712016-05-04 10:18:37 +02001635 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001636 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001637
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001638 /* below we may emit error messages so we have to ensure that we have
1639 * our buffers properly allocated.
1640 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001641 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001642 /* No buffer available, we've been subscribed to the list of
1643 * buffer waiters, let's wait for our turn.
1644 */
Willy Tarreau5fb04712016-05-04 10:18:37 +02001645 si_f->flags &= ~SI_FL_DONT_WAKE;
1646 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001647 goto update_exp_and_leave;
1648 }
1649
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001650 /* 1b: check for low-level errors reported at the stream interface.
1651 * First we check if it's a retryable error (in which case we don't
1652 * want to tell the buffer). Otherwise we report the error one level
1653 * upper by setting flags into the buffers. Note that the side towards
1654 * the client cannot have connect (hence retryable) errors. Also, the
1655 * connection setup code must be able to deal with any type of abort.
1656 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001657 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001658 if (unlikely(si_f->flags & SI_FL_ERR)) {
1659 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1660 si_shutr(si_f);
1661 si_shutw(si_f);
1662 stream_int_report_error(si_f);
1663 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001664 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001665 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001666 if (srv)
1667 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001668 if (!(s->flags & SF_ERR_MASK))
1669 s->flags |= SF_ERR_CLICL;
1670 if (!(s->flags & SF_FINST_MASK))
1671 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001672 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001673 }
1674 }
1675
Willy Tarreau8f128b42014-11-28 15:07:47 +01001676 if (unlikely(si_b->flags & SI_FL_ERR)) {
1677 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1678 si_shutr(si_b);
1679 si_shutw(si_b);
1680 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001681 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001682 if (srv)
1683 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001684 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001685 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001686 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001687 if (srv)
1688 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001689 if (!(s->flags & SF_ERR_MASK))
1690 s->flags |= SF_ERR_SRVCL;
1691 if (!(s->flags & SF_FINST_MASK))
1692 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001693 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001694 }
1695 /* note: maybe we should process connection errors here ? */
1696 }
1697
Willy Tarreau8f128b42014-11-28 15:07:47 +01001698 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001699 /* we were trying to establish a connection on the server side,
1700 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1701 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001702 if (unlikely(!sess_update_st_con_tcp(s)))
1703 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001704 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001705 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001706
1707 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1708 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1709 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1710 */
1711 }
1712
Willy Tarreau8f128b42014-11-28 15:07:47 +01001713 rq_prod_last = si_f->state;
1714 rq_cons_last = si_b->state;
1715 rp_cons_last = si_f->state;
1716 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001717
1718 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001719 /* Check for connection closure */
1720
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001721 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001722 "[%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 +01001723 now_ms, __FUNCTION__, __LINE__,
1724 t,
1725 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001726 req, res,
1727 req->rex, res->wex,
1728 req->flags, res->flags,
1729 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1730 si_f->err_type, si_b->err_type,
1731 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001732
1733 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001734 if (unlikely(si_f->state == SI_ST_DIS))
1735 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001736
1737 /* When a server-side connection is released, we have to count it and
1738 * check for pending connections on this server.
1739 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001740 if (unlikely(si_b->state == SI_ST_DIS)) {
1741 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001742 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001743 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001744 if (s->flags & SF_CURR_SESS) {
1745 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001746 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001747 }
1748 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001749 if (may_dequeue_tasks(srv, s->be))
1750 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001751 }
1752 }
1753
1754 /*
1755 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1756 * at this point.
1757 */
1758
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001759 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001760 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001761 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1762 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1763 si_f->state != rq_prod_last ||
1764 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001765 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001766 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001767
Willy Tarreau8f128b42014-11-28 15:07:47 +01001768 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001769 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001770 unsigned int ana_list;
1771 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001772
Willy Tarreau90deb182010-01-07 00:20:41 +01001773 /* it's up to the analysers to stop new connections,
1774 * disable reading or closing. Note: if an analyser
1775 * disables any of these bits, it is responsible for
1776 * enabling them again when it disables itself, so
1777 * that other analysers are called in similar conditions.
1778 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001779 channel_auto_read(req);
1780 channel_auto_connect(req);
1781 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001782
1783 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001784 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001785 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001786 * the list when not needed. Any analyser may return 0
1787 * to break out of the loop, either because of missing
1788 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001789 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001790 * analyser, and we may loop again if other analysers
1791 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001792 *
1793 * We build a list of analysers to run. We evaluate all
1794 * of these analysers in the order of the lower bit to
1795 * the higher bit. This ordering is very important.
1796 * An analyser will often add/remove other analysers,
1797 * including itself. Any changes to itself have no effect
1798 * on the loop. If it removes any other analysers, we
1799 * want those analysers not to be called anymore during
1800 * this loop. If it adds an analyser that is located
1801 * after itself, we want it to be scheduled for being
1802 * processed during the loop. If it adds an analyser
1803 * which is located before it, we want it to switch to
1804 * it immediately, even if it has already been called
1805 * once but removed since.
1806 *
1807 * In order to achieve this, we compare the analyser
1808 * list after the call with a copy of it before the
1809 * call. The work list is fed with analyser bits that
1810 * appeared during the call. Then we compare previous
1811 * work list with the new one, and check the bits that
1812 * appeared. If the lowest of these bits is lower than
1813 * the current bit, it means we have enabled a previous
1814 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001815 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001816
Willy Tarreau8f128b42014-11-28 15:07:47 +01001817 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001818 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001819 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauleta9215b72016-05-11 17:06:28 +02001820 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_FLT_START_FE);
1821 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_FE);
1822 FLT_ANALYZE(s, req, http_wait_for_request, ana_list, ana_back, AN_REQ_WAIT_HTTP);
1823 FLT_ANALYZE(s, req, http_wait_for_request_body, ana_list, ana_back, AN_REQ_HTTP_BODY);
1824 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE, sess->fe);
1825 FLT_ANALYZE(s, req, process_switching_rules, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
1826 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_FLT_START_BE);
1827 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_BE);
1828 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE, s->be);
1829 FLT_ANALYZE(s, req, http_process_tarpit, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
1830 FLT_ANALYZE(s, req, process_server_rules, ana_list, ana_back, AN_REQ_SRV_RULES);
1831 FLT_ANALYZE(s, req, http_process_request, ana_list, ana_back, AN_REQ_HTTP_INNER);
1832 FLT_ANALYZE(s, req, tcp_persist_rdp_cookie, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
1833 FLT_ANALYZE(s, req, process_sticking_rules, ana_list, ana_back, AN_REQ_STICKING_RULES);
1834 ANALYZE (s, req, flt_analyze_http_headers, ana_list, ana_back, AN_FLT_HTTP_HDRS);
1835 ANALYZE (s, req, flt_xfer_data, ana_list, ana_back, AN_FLT_XFER_DATA);
1836 ANALYZE (s, req, http_request_forward_body, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
1837 ANALYZE (s, req, flt_end_analyze, ana_list, ana_back, AN_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01001838 break;
1839 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001840 }
Willy Tarreau84455332009-03-15 22:34:05 +01001841
Willy Tarreau8f128b42014-11-28 15:07:47 +01001842 rq_prod_last = si_f->state;
1843 rq_cons_last = si_b->state;
1844 req->flags &= ~CF_WAKE_ONCE;
1845 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001846
Willy Tarreau8f128b42014-11-28 15:07:47 +01001847 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001848 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001849 }
1850
Willy Tarreau576507f2010-01-07 00:09:04 +01001851 /* we'll monitor the request analysers while parsing the response,
1852 * because some response analysers may indirectly enable new request
1853 * analysers (eg: HTTP keep-alive).
1854 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001855 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001856
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001857 resync_response:
1858 /* Analyse response */
1859
Willy Tarreau8f128b42014-11-28 15:07:47 +01001860 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1861 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1862 si_f->state != rp_cons_last ||
1863 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001864 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001865 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001866
Willy Tarreau8f128b42014-11-28 15:07:47 +01001867 if ((res->flags & CF_MASK_ANALYSER) &&
1868 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001869 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1870 * for some free space in the response buffer, so we might need to call
1871 * it when something changes in the response buffer, but still we pass
1872 * it the request buffer. Note that the SI state might very well still
1873 * be zero due to us returning a flow of redirects!
1874 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001875 res->analysers &= ~AN_REQ_ALL;
1876 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001877 }
1878
Willy Tarreau8f128b42014-11-28 15:07:47 +01001879 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001880 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001881 unsigned int ana_list;
1882 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001883
Willy Tarreau90deb182010-01-07 00:20:41 +01001884 /* it's up to the analysers to stop disable reading or
1885 * closing. Note: if an analyser disables any of these
1886 * bits, it is responsible for enabling them again when
1887 * it disables itself, so that other analysers are called
1888 * in similar conditions.
1889 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001890 channel_auto_read(res);
1891 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001892
1893 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001894 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001895 * to MSB. The analysers must remove themselves from
1896 * the list when not needed. Any analyser may return 0
1897 * to break out of the loop, either because of missing
1898 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001899 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001900 * analyser, and we may loop again if other analysers
1901 * are added in the middle.
1902 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001903
Willy Tarreau8f128b42014-11-28 15:07:47 +01001904 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001905 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001906 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauleta9215b72016-05-11 17:06:28 +02001907 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_FLT_START_FE);
1908 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_FLT_START_BE);
1909 FLT_ANALYZE(s, res, tcp_inspect_response, ana_list, ana_back, AN_RES_INSPECT);
1910 FLT_ANALYZE(s, res, http_wait_for_response, ana_list, ana_back, AN_RES_WAIT_HTTP);
1911 FLT_ANALYZE(s, res, process_store_rules, ana_list, ana_back, AN_RES_STORE_RULES);
1912 FLT_ANALYZE(s, res, http_process_res_common, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE, s->be);
1913 ANALYZE (s, res, flt_analyze_http_headers, ana_list, ana_back, AN_FLT_HTTP_HDRS);
1914 ANALYZE (s, res, flt_xfer_data, ana_list, ana_back, AN_FLT_XFER_DATA);
1915 ANALYZE (s, res, http_response_forward_body, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
1916 ANALYZE (s, res, flt_end_analyze, ana_list, ana_back, AN_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01001917 break;
1918 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001919 }
1920
Willy Tarreau8f128b42014-11-28 15:07:47 +01001921 rp_cons_last = si_f->state;
1922 rp_prod_last = si_b->state;
1923 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001924
Willy Tarreau8f128b42014-11-28 15:07:47 +01001925 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001926 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001927 }
1928
Willy Tarreau576507f2010-01-07 00:09:04 +01001929 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001930 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001931 goto resync_request;
1932
Willy Tarreau8f128b42014-11-28 15:07:47 +01001933 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001934 goto resync_request;
1935
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001936 /* FIXME: here we should call protocol handlers which rely on
1937 * both buffers.
1938 */
1939
1940
1941 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001942 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001943 * we're just in a data phase here since it means we have not
1944 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001945 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001946 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001947 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001948 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001949 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001950 req->analysers = 0;
1951 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001952 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001953 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001954 if (srv)
1955 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001956 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001957 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001958 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001959 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001960 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001961 if (srv)
1962 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001963 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001964 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001965 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001966 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001967 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001968 if (srv)
1969 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001970 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001971 }
1972 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001973 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001974 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001975 if (srv)
1976 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001977 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001978 }
Willy Tarreau84455332009-03-15 22:34:05 +01001979 sess_set_term_flags(s);
1980 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001981 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001982 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001983 res->analysers = 0;
1984 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001985 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001986 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001987 if (srv)
1988 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001989 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001990 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001991 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001992 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001993 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001994 if (srv)
1995 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001996 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001997 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001998 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001999 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002000 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002001 if (srv)
2002 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002003 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002004 }
2005 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002006 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002007 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002008 if (srv)
2009 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002010 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002011 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002012 sess_set_term_flags(s);
2013 }
Willy Tarreau84455332009-03-15 22:34:05 +01002014 }
2015
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002016 /*
2017 * Here we take care of forwarding unhandled data. This also includes
2018 * connection establishments and shutdown requests.
2019 */
2020
2021
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002022 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002023 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002024 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002025 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002026 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002027 if (unlikely(!req->analysers &&
2028 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2029 (si_f->state >= SI_ST_EST) &&
2030 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002031 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002032 * attached to it. If any data are left in, we'll permit them to
2033 * move.
2034 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002035 channel_auto_read(req);
2036 channel_auto_connect(req);
2037 channel_auto_close(req);
2038 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002039
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002040 /* We'll let data flow between the producer (if still connected)
2041 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002042 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002043 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8bf242b2016-05-04 14:05:58 +02002044 channel_forward_forever(req);
Willy Tarreau42529c32015-07-09 18:38:57 +02002045
2046 /* Just in order to support fetching HTTP contents after start
2047 * of forwarding when the HTTP forwarding analyser is not used,
2048 * we simply reset msg->sov so that HTTP rewinding points to the
2049 * headers.
2050 */
2051 if (s->txn)
2052 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - req->buf->o;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002053 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002054
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002055 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002056 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2057 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002058 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002059 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2060 (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 +01002061 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002062 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2063 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002064 (req->flags & CF_STREAMER_FAST)))) {
2065 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002066 }
2067
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002068 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002069 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002070
2071 /*
2072 * Now forward all shutdown requests between both sides of the buffer
2073 */
2074
Willy Tarreau520d95e2009-09-19 21:04:57 +02002075 /* first, let's check if the request buffer needs to shutdown(write), which may
2076 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002077 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2078 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002079 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002080 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002081 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002082 channel_shutw_now(req);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002083 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002084
2085 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002086 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2087 channel_is_empty(req))) {
2088 if (req->flags & CF_READ_ERROR)
2089 si_b->flags |= SI_FL_NOLINGER;
2090 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002091 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002092 res->rto = s->be->timeout.serverfin;
2093 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002094 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002095 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002096
2097 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002098 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2099 !req->analysers))
2100 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002101
2102 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002103 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2104 if (si_f->flags & SI_FL_NOHALF)
2105 si_f->flags |= SI_FL_NOLINGER;
2106 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002107 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002108
Willy Tarreau520d95e2009-09-19 21:04:57 +02002109 /* it's possible that an upper layer has requested a connection setup or abort.
2110 * There are 2 situations where we decide to establish a new connection :
2111 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002112 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002113 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002114 if (si_b->state == SI_ST_INI) {
2115 if (!(req->flags & CF_SHUTW)) {
2116 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002117 /* If we have an appctx, there is no connect method, so we
2118 * immediately switch to the connected state, otherwise we
2119 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002120 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002121 si_b->state = SI_ST_REQ; /* new connection requested */
2122 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002123 }
Willy Tarreau73201222009-08-16 18:27:24 +02002124 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002125 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002126 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2127 channel_shutw_now(req); /* fix buffer flags upon abort */
2128 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002129 }
Willy Tarreau92795622009-03-06 12:51:23 +01002130 }
2131
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002132
2133 /* we may have a pending connection request, or a connection waiting
2134 * for completion.
2135 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002136 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002137
2138 /* prune the request variables and swap to the response variables. */
2139 if (s->vars_reqres.scope != SCOPE_RES) {
Willy Tarreau6204cd92016-03-10 16:33:04 +01002140 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002141 vars_init(&s->vars_reqres, SCOPE_RES);
2142 }
2143
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002144 do {
2145 /* nb: step 1 might switch from QUE to ASS, but we first want
2146 * to give a chance to step 2 to perform a redirect if needed.
2147 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002148 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002149 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002150 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002151 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002152
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002153 /* applets directly go to the ESTABLISHED state. Similarly,
2154 * servers experience the same fate when their connection
2155 * is reused.
2156 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002157 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002158 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002159
Willy Tarreaub44c8732013-12-31 23:16:50 +01002160 /* Now we can add the server name to a header (if requested) */
2161 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002162 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002163 (s->be->server_id_hdr_name != NULL) &&
2164 (s->be->mode == PR_MODE_HTTP) &&
2165 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002166 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002167 }
2168
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002169 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002170 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002171 http_perform_server_redirect(s, si_b);
2172 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002173 }
2174
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002175 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002176 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002177 goto resync_stream_interface;
2178
Willy Tarreau815a9b22010-07-27 17:15:12 +02002179 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002180 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002181 goto resync_request;
2182
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002183 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002184
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002185 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002186 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002187 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002188 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002189 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002190 if (unlikely(!res->analysers &&
2191 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2192 (si_b->state >= SI_ST_EST) &&
2193 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002194 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002195 * attached to it. If any data are left in, we'll permit them to
2196 * move.
2197 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002198 channel_auto_read(res);
2199 channel_auto_close(res);
2200 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002201
2202 /* We'll let data flow between the producer (if still connected)
2203 * to the consumer.
2204 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002205 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8bf242b2016-05-04 14:05:58 +02002206 channel_forward_forever(res);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002207
Willy Tarreau42529c32015-07-09 18:38:57 +02002208 /* Just in order to support fetching HTTP contents after start
2209 * of forwarding when the HTTP forwarding analyser is not used,
2210 * we simply reset msg->sov so that HTTP rewinding points to the
2211 * headers.
2212 */
2213 if (s->txn)
2214 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2215
Willy Tarreauce887fd2012-05-12 12:50:00 +02002216 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002217 * tunnel timeout set, use it now. Note that we must respect
2218 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002219 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002220 if (!req->analysers && s->be->timeout.tunnel) {
2221 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002222 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002223
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002224 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2225 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002226 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2227 res->rto = s->be->timeout.serverfin;
2228 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2229 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002230 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2231 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002232
Willy Tarreau8f128b42014-11-28 15:07:47 +01002233 req->rex = tick_add(now_ms, req->rto);
2234 req->wex = tick_add(now_ms, req->wto);
2235 res->rex = tick_add(now_ms, res->rto);
2236 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002237 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002238 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002239
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002240 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002241 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2242 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002243 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002244 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2245 (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 +01002246 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002247 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2248 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002249 (res->flags & CF_STREAMER_FAST)))) {
2250 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002251 }
2252
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002253 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002254 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002255
2256 /*
2257 * Now forward all shutdown requests between both sides of the buffer
2258 */
2259
2260 /*
2261 * FIXME: this is probably where we should produce error responses.
2262 */
2263
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002264 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002265 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002266 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002267 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002268 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002269
2270 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002271 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2272 channel_is_empty(res))) {
2273 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002274 if (tick_isset(sess->fe->timeout.clientfin)) {
2275 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002276 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002277 }
2278 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002279
2280 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002281 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2282 !res->analysers)
2283 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002284
2285 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002286 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2287 if (si_b->flags & SI_FL_NOHALF)
2288 si_b->flags |= SI_FL_NOLINGER;
2289 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002290 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002291
Willy Tarreau8f128b42014-11-28 15:07:47 +01002292 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002293 goto resync_stream_interface;
2294
Willy Tarreau8f128b42014-11-28 15:07:47 +01002295 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002296 goto resync_request;
2297
Willy Tarreau8f128b42014-11-28 15:07:47 +01002298 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002299 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002300
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002301 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002302 si_f->flags &= ~SI_FL_DONT_WAKE;
2303 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002304
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002305 /* This is needed only when debugging is enabled, to indicate
2306 * client-side or server-side close. Please note that in the unlikely
2307 * event where both sides would close at once, the sequence is reported
2308 * on the server side first.
2309 */
2310 if (unlikely((global.mode & MODE_DEBUG) &&
2311 (!(global.mode & MODE_QUIET) ||
2312 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002313 if (si_b->state == SI_ST_CLO &&
2314 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002315 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002316 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002317 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2318 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002319 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002320 }
2321
Willy Tarreau8f128b42014-11-28 15:07:47 +01002322 if (si_f->state == SI_ST_CLO &&
2323 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002324 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002325 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002326 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2327 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002328 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002329 }
2330 }
2331
Willy Tarreau8f128b42014-11-28 15:07:47 +01002332 if (likely((si_f->state != SI_ST_CLO) ||
2333 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002334
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002335 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002336 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002337
Willy Tarreau563cc372015-04-19 18:13:56 +02002338 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002339 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002340
Willy Tarreau563cc372015-04-19 18:13:56 +02002341 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002342 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002343
Willy Tarreau8f128b42014-11-28 15:07:47 +01002344 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2345 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2346 si_f->prev_state = si_f->state;
2347 si_b->prev_state = si_b->state;
2348 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2349 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002350
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002351 /* Trick: if a request is being waiting for the server to respond,
2352 * and if we know the server can timeout, we don't want the timeout
2353 * to expire on the client side first, but we're still interested
2354 * in passing data from the client to the server (eg: POST). Thus,
2355 * we can cancel the client's request timeout if the server's
2356 * request timeout is set and the server has not yet sent a response.
2357 */
2358
Willy Tarreau8f128b42014-11-28 15:07:47 +01002359 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2360 (tick_isset(req->wex) || tick_isset(res->rex))) {
2361 req->flags |= CF_READ_NOEXP;
2362 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002363 }
2364
Willy Tarreau798f4322012-11-08 14:49:17 +01002365 update_exp_and_leave:
Willy Tarreau5fb04712016-05-04 10:18:37 +02002366 /* Note: please ensure that if you branch here you disable SI_FL_DONT_WAKE */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002367 t->expire = tick_first(tick_first(req->rex, req->wex),
2368 tick_first(res->rex, res->wex));
2369 if (req->analysers)
2370 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002371
Willy Tarreau8f128b42014-11-28 15:07:47 +01002372 if (si_f->exp)
2373 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002374
Willy Tarreau8f128b42014-11-28 15:07:47 +01002375 if (si_b->exp)
2376 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002377
2378#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002379 fprintf(stderr,
2380 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2381 " 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 +01002382 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2383 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002384#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002385
2386#ifdef DEBUG_DEV
2387 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002388 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002389 ABORT_NOW();
2390#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002391 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002392 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002393 }
2394
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002395 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002396 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002397 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002398 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002399 if (sess->listener) {
2400 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002401 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002402 sess->listener->nbconn--;
2403 if (sess->listener->state == LI_FULL)
2404 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002405
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002406 /* Dequeues all of the listeners waiting for a resource */
2407 if (!LIST_ISEMPTY(&global_listener_queue))
2408 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002409
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002410 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2411 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2412 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002413 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002414
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002415 if (unlikely((global.mode & MODE_DEBUG) &&
2416 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002417 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002418 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002419 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2420 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002421 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002422 }
2423
2424 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002425 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002426
Willy Tarreaueee5b512015-04-03 23:46:31 +02002427 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002428 int n;
2429
Willy Tarreaueee5b512015-04-03 23:46:31 +02002430 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002431 if (n < 1 || n > 5)
2432 n = 0;
2433
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002434 if (sess->fe->mode == PR_MODE_HTTP) {
2435 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002436 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002437 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002438 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002439 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002440 s->be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002441 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002442 }
2443
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002444 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002445 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002446 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002447 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002448 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002449 }
2450
Willy Tarreau87b09662015-04-03 00:22:06 +02002451 /* update time stats for this stream */
2452 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002453
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002454 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002455 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002456 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002457 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002458 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002459}
2460
Willy Tarreau87b09662015-04-03 00:22:06 +02002461/* Update the stream's backend and server time stats */
2462void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002463{
2464 int t_request;
2465 int t_queue;
2466 int t_connect;
2467 int t_data;
2468 int t_close;
2469 struct server *srv;
2470
2471 t_request = 0;
2472 t_queue = s->logs.t_queue;
2473 t_connect = s->logs.t_connect;
2474 t_close = s->logs.t_close;
2475 t_data = s->logs.t_data;
2476
2477 if (s->be->mode != PR_MODE_HTTP)
2478 t_data = t_connect;
2479
2480 if (t_connect < 0 || t_data < 0)
2481 return;
2482
2483 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2484 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2485
2486 t_data -= t_connect;
2487 t_connect -= t_queue;
2488 t_queue -= t_request;
2489
2490 srv = objt_server(s->target);
2491 if (srv) {
2492 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2493 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2494 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2495 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2496 }
2497 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2498 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2499 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2500 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2501}
2502
Willy Tarreau7c669d72008-06-20 15:04:11 +02002503/*
2504 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002505 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002506 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002507 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002508 * server.
2509 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002510void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002511{
2512 if (sess->srv_conn == newsrv)
2513 return;
2514
2515 if (sess->srv_conn) {
2516 sess->srv_conn->served--;
2517 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2518 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002519 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002520 }
2521
2522 if (newsrv) {
2523 newsrv->served++;
2524 if (newsrv->proxy->lbprm.server_take_conn)
2525 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002526 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002527 }
2528}
2529
Willy Tarreau84455332009-03-15 22:34:05 +01002530/* Handle server-side errors for default protocols. It is called whenever a a
2531 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002532 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002533 * them. It's installed as ->srv_error for the server-side stream_interface.
2534 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002535void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002536{
2537 int err_type = si->err_type;
2538 int err = 0, fin = 0;
2539
2540 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002541 err = SF_ERR_CLICL;
2542 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002543 }
2544 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002545 err = SF_ERR_CLICL;
2546 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002547 }
2548 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002549 err = SF_ERR_SRVTO;
2550 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002551 }
2552 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002553 err = SF_ERR_SRVCL;
2554 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002555 }
2556 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002557 err = SF_ERR_SRVTO;
2558 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002559 }
2560 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002561 err = SF_ERR_SRVCL;
2562 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002563 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002564 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002565 err = SF_ERR_RESOURCE;
2566 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002567 }
Willy Tarreau84455332009-03-15 22:34:05 +01002568 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002569 err = SF_ERR_INTERNAL;
2570 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002571 }
2572
Willy Tarreaue7dff022015-04-03 01:14:29 +02002573 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002574 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002575 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002576 s->flags |= fin;
2577}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002578
Willy Tarreaue7dff022015-04-03 01:14:29 +02002579/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002580void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002581{
Willy Tarreau87b09662015-04-03 00:22:06 +02002582 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002583 return;
2584
Willy Tarreau87b09662015-04-03 00:22:06 +02002585 channel_shutw_now(&stream->req);
2586 channel_shutr_now(&stream->res);
2587 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002588 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002589 stream->flags |= why;
2590 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002591}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002592
Willy Tarreau8b22a712010-06-18 17:46:06 +02002593/************************************************************************/
2594/* All supported ACL keywords must be declared here. */
2595/************************************************************************/
2596
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002597/* Returns a pointer to a stkctr depending on the fetch keyword name.
2598 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002599 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002600 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002601 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002602 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002603 * NULL may be returned if the designated stkctr is not tracked. For
2604 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2605 * passed. When present, the currently tracked key is then looked up
2606 * in the specified table instead of the current table. The purpose is
2607 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002608 * multiple tables). <strm> is allowed to be NULL, in which case only
2609 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002610 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002611struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002612smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002613{
2614 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002615 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002616 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002617 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002618 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002619
Willy Tarreau0f791d42013-07-23 19:56:43 +02002620 if (num == '_' - '0') {
2621 /* sc_* variant, args[0] = ctr# (mandatory) */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002622 num = args[arg++].data.sint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002623 if (num >= MAX_SESS_STKCTR)
2624 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002625 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002626 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002627 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002628 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002629 struct sample smp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002630
2631 if (!conn)
2632 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002633
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002634 /* Fetch source adress in a sample. */
2635 smp.px = NULL;
2636 smp.sess = sess;
2637 smp.strm = strm;
2638 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2639 return NULL;
2640
2641 /* Converts into key. */
2642 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002643 if (!key)
2644 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002645
Willy Tarreaua65536c2013-07-22 22:40:11 +02002646 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002647 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002648 return &stkctr;
2649 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002650
2651 /* Here, <num> contains the counter number from 0 to 9 for
2652 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002653 * args[arg] is the first optional argument. We first lookup the
2654 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002655 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002656
Thierry Fournier40e1d512016-03-29 21:27:36 +02002657 if (strm)
2658 stkptr = &strm->stkctr[num];
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002659 if (!strm || !stkctr_entry(stkptr)) {
2660 stkptr = &sess->stkctr[num];
2661 if (!stkctr_entry(stkptr))
2662 return NULL;
2663 }
2664
2665 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002666 if (!stksess)
2667 return NULL;
2668
Willy Tarreau0f791d42013-07-23 19:56:43 +02002669 if (unlikely(args[arg].type == ARGT_TAB)) {
2670 /* an alternate table was specified, let's look up the same key there */
2671 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002672 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002673 return &stkctr;
2674 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002675 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002676}
2677
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002678/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2679 * the entry if it doesn't exist yet. This is needed for a few fetch
2680 * functions which need to create an entry, such as src_inc_gpc* and
2681 * src_clr_gpc*.
2682 */
2683struct stkctr *
2684smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
2685{
2686 static struct stkctr stkctr;
2687 struct stktable_key *key;
2688 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002689 struct sample smp;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002690
2691 if (strncmp(kw, "src_", 4) != 0)
2692 return NULL;
2693
2694 if (!conn)
2695 return NULL;
2696
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002697 /* Fetch source adress in a sample. */
2698 smp.px = NULL;
2699 smp.sess = sess;
2700 smp.strm = strm;
2701 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2702 return NULL;
2703
2704 /* Converts into key. */
2705 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002706 if (!key)
2707 return NULL;
2708
2709 stkctr.table = &args->data.prx->table;
2710 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
2711 return &stkctr;
2712}
2713
Willy Tarreau87b09662015-04-03 00:22:06 +02002714/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002715 * currently being tracked or not.
2716 * Supports being called as "sc[0-9]_tracked" only.
2717 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002718static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002719smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002720{
2721 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002722 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002723 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002724 return 1;
2725}
2726
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002727/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2728 * frontend counters or from the src.
2729 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2730 * zero is returned if the key is new.
2731 */
2732static int
2733smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2734{
Willy Tarreaube508f12016-03-10 11:47:01 +01002735 struct stkctr *stkctr;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002736
Willy Tarreaube508f12016-03-10 11:47:01 +01002737 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002738 if (!stkctr)
2739 return 0;
2740
2741 smp->flags = SMP_F_VOL_TEST;
2742 smp->data.type = SMP_T_SINT;
2743 smp->data.u.sint = 0;
2744
2745 if (stkctr_entry(stkctr) != NULL) {
2746 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2747 if (!ptr)
2748 return 0; /* parameter not stored */
2749 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
2750 }
2751 return 1;
2752}
2753
Willy Tarreau87b09662015-04-03 00:22:06 +02002754/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002755 * frontend counters or from the src.
2756 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2757 * zero is returned if the key is new.
2758 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002759static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002760smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002761{
Willy Tarreaube508f12016-03-10 11:47:01 +01002762 struct stkctr *stkctr;
2763
Willy Tarreaube508f12016-03-10 11:47:01 +01002764 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002765 if (!stkctr)
2766 return 0;
2767
Willy Tarreau37406352012-04-23 16:16:37 +02002768 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002769 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002770 smp->data.u.sint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002771
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002772 if (stkctr_entry(stkctr) != NULL) {
2773 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002774 if (!ptr)
2775 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002776 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002777 }
2778 return 1;
2779}
2780
Willy Tarreau87b09662015-04-03 00:22:06 +02002781/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002782 * tracked frontend counters or from the src.
2783 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2784 * Value zero is returned if the key is new.
2785 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002786static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002787smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002788{
Willy Tarreaube508f12016-03-10 11:47:01 +01002789 struct stkctr *stkctr;
2790
Willy Tarreaube508f12016-03-10 11:47:01 +01002791 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002792 if (!stkctr)
2793 return 0;
2794
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002795 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002796 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002797 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002798 if (stkctr_entry(stkctr) != NULL) {
2799 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002800 if (!ptr)
2801 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002802 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002803 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002804 }
2805 return 1;
2806}
2807
Willy Tarreau87b09662015-04-03 00:22:06 +02002808/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002809 * frontend counters and return it into temp integer.
2810 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002811 */
2812static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002813smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002814{
Willy Tarreaube508f12016-03-10 11:47:01 +01002815 struct stkctr *stkctr;
2816
Willy Tarreaube508f12016-03-10 11:47:01 +01002817 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002818 if (!stkctr)
2819 return 0;
2820
Willy Tarreau37406352012-04-23 16:16:37 +02002821 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002822 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002823 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002824
2825 if (stkctr_entry(stkctr) == NULL)
2826 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2827
Thierry Fournier6fc340f2016-06-06 18:28:05 +02002828 if (stkctr && stkctr_entry(stkctr)) {
Emeric Brun57056f02015-06-15 18:29:57 +02002829 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002830
2831 /* First, update gpc0_rate if it's tracked. Second, update its
2832 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2833 */
Emeric Brun57056f02015-06-15 18:29:57 +02002834 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2835 if (ptr1) {
2836 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002837 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002838 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002839 }
2840
Emeric Brun57056f02015-06-15 18:29:57 +02002841 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2842 if (ptr2)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002843 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002844
Emeric Brun57056f02015-06-15 18:29:57 +02002845 /* If data was modified, we need to touch to re-schedule sync */
2846 if (ptr1 || ptr2)
2847 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002848 }
2849 return 1;
2850}
2851
Willy Tarreau87b09662015-04-03 00:22:06 +02002852/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002853 * frontend counters and return its previous value into temp integer.
2854 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002855 */
2856static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002857smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002858{
Willy Tarreaube508f12016-03-10 11:47:01 +01002859 struct stkctr *stkctr;
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002860
Willy Tarreaube508f12016-03-10 11:47:01 +01002861 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002862 if (!stkctr)
2863 return 0;
2864
Willy Tarreau37406352012-04-23 16:16:37 +02002865 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002866 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002867 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002868
2869 if (stkctr_entry(stkctr) == NULL)
2870 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2871
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002872 if (stkctr_entry(stkctr) != NULL) {
2873 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002874 if (!ptr)
2875 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002876 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002877 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002878 /* If data was modified, we need to touch to re-schedule sync */
2879 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002880 }
2881 return 1;
2882}
2883
Willy Tarreau87b09662015-04-03 00:22:06 +02002884/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002885 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2886 * "src_conn_cnt" only.
2887 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002888static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002889smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002890{
Willy Tarreaube508f12016-03-10 11:47:01 +01002891 struct stkctr *stkctr;
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002892
Willy Tarreaube508f12016-03-10 11:47:01 +01002893 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002894 if (!stkctr)
2895 return 0;
2896
Willy Tarreau37406352012-04-23 16:16:37 +02002897 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002898 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002899 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002900 if (stkctr_entry(stkctr) != NULL) {
2901 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002902 if (!ptr)
2903 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002904 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002905 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002906 return 1;
2907}
2908
Willy Tarreau87b09662015-04-03 00:22:06 +02002909/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002910 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2911 * only.
2912 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002913static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002914smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002915{
Willy Tarreaube508f12016-03-10 11:47:01 +01002916 struct stkctr *stkctr;
2917
Willy Tarreaube508f12016-03-10 11:47:01 +01002918 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002919 if (!stkctr)
2920 return 0;
2921
Willy Tarreau37406352012-04-23 16:16:37 +02002922 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002923 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002924 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002925 if (stkctr_entry(stkctr) != NULL) {
2926 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002927 if (!ptr)
2928 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002929 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002930 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002931 }
2932 return 1;
2933}
2934
Willy Tarreau87b09662015-04-03 00:22:06 +02002935/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002936 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002937 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002938 */
2939static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002940smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002941{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002942 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002943 struct stksess *ts;
2944 struct stktable_key *key;
2945 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002946 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002947
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002948 if (!conn)
2949 return 0;
2950
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002951 /* Fetch source adress in a sample. */
2952 if (!smp_fetch_src(NULL, smp, NULL, NULL))
2953 return 0;
2954
2955 /* Converts into key. */
2956 key = smp_to_stkey(smp, &args->data.prx->table);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002957 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002958 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002959
Willy Tarreau24e32d82012-04-23 23:55:44 +02002960 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002961
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002962 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2963 /* entry does not exist and could not be created */
2964 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002965
2966 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2967 if (!ptr)
2968 return 0; /* parameter not stored in this table */
2969
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002970 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002971 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02002972 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02002973 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002974 return 1;
2975}
2976
Willy Tarreau87b09662015-04-03 00:22:06 +02002977/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002978 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2979 * "src_conn_cur" only.
2980 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002981static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002982smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002983{
Willy Tarreaube508f12016-03-10 11:47:01 +01002984 struct stkctr *stkctr;
2985
Willy Tarreaube508f12016-03-10 11:47:01 +01002986 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002987 if (!stkctr)
2988 return 0;
2989
Willy Tarreau37406352012-04-23 16:16:37 +02002990 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002991 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002992 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002993 if (stkctr_entry(stkctr) != NULL) {
2994 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002995 if (!ptr)
2996 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002997 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002998 }
2999 return 1;
3000}
3001
Willy Tarreau87b09662015-04-03 00:22:06 +02003002/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02003003 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3004 * "src_sess_cnt" only.
3005 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003006static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003007smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003008{
Willy Tarreaube508f12016-03-10 11:47:01 +01003009 struct stkctr *stkctr;
3010
Willy Tarreaube508f12016-03-10 11:47:01 +01003011 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02003012 if (!stkctr)
3013 return 0;
3014
Willy Tarreau37406352012-04-23 16:16:37 +02003015 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003016 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003017 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003018 if (stkctr_entry(stkctr) != NULL) {
3019 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003020 if (!ptr)
3021 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003022 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003023 }
3024 return 1;
3025}
3026
Willy Tarreau87b09662015-04-03 00:22:06 +02003027/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003028 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3029 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003030static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003031smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003032{
Willy Tarreaube508f12016-03-10 11:47:01 +01003033 struct stkctr *stkctr;
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003034
Willy Tarreaube508f12016-03-10 11:47:01 +01003035 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003036 if (!stkctr)
3037 return 0;
3038
Willy Tarreau37406352012-04-23 16:16:37 +02003039 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003040 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003041 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003042 if (stkctr_entry(stkctr) != NULL) {
3043 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003044 if (!ptr)
3045 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003046 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003047 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003048 }
3049 return 1;
3050}
3051
Willy Tarreau87b09662015-04-03 00:22:06 +02003052/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02003053 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3054 * "src_http_req_cnt" only.
3055 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003056static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003057smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003058{
Willy Tarreaube508f12016-03-10 11:47:01 +01003059 struct stkctr *stkctr;
Willy Tarreau91200da2013-07-23 15:55:19 +02003060
Willy Tarreaube508f12016-03-10 11:47:01 +01003061 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02003062 if (!stkctr)
3063 return 0;
3064
Willy Tarreau37406352012-04-23 16:16:37 +02003065 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003066 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003067 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003068 if (stkctr_entry(stkctr) != NULL) {
3069 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003070 if (!ptr)
3071 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003072 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003073 }
3074 return 1;
3075}
3076
Willy Tarreau87b09662015-04-03 00:22:06 +02003077/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02003078 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3079 * "src_http_req_rate" only.
3080 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003081static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003082smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003083{
Willy Tarreaube508f12016-03-10 11:47:01 +01003084 struct stkctr *stkctr;
3085
Willy Tarreaube508f12016-03-10 11:47:01 +01003086 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02003087 if (!stkctr)
3088 return 0;
3089
Willy Tarreau37406352012-04-23 16:16:37 +02003090 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003091 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003092 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003093 if (stkctr_entry(stkctr) != NULL) {
3094 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003095 if (!ptr)
3096 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003097 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003098 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003099 }
3100 return 1;
3101}
3102
Willy Tarreau87b09662015-04-03 00:22:06 +02003103/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003104 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3105 * "src_http_err_cnt" only.
3106 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003107static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003108smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003109{
Willy Tarreaube508f12016-03-10 11:47:01 +01003110 struct stkctr *stkctr;
3111
Willy Tarreaube508f12016-03-10 11:47:01 +01003112 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003113 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{
Willy Tarreaube508f12016-03-10 11:47:01 +01003135 struct stkctr *stkctr;
3136
Willy Tarreaube508f12016-03-10 11:47:01 +01003137 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003138 if (!stkctr)
3139 return 0;
3140
Willy Tarreau37406352012-04-23 16:16:37 +02003141 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003142 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003143 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003144 if (stkctr_entry(stkctr) != NULL) {
3145 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003146 if (!ptr)
3147 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003148 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003149 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003150 }
3151 return 1;
3152}
3153
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003154/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003155 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003156 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3157 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003158static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003159smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003160{
Willy Tarreaube508f12016-03-10 11:47:01 +01003161 struct stkctr *stkctr;
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003162
Willy Tarreaube508f12016-03-10 11:47:01 +01003163 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003164 if (!stkctr)
3165 return 0;
3166
Willy Tarreau37406352012-04-23 16:16:37 +02003167 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003168 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003169 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003170 if (stkctr_entry(stkctr) != NULL) {
3171 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003172 if (!ptr)
3173 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003174 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003175 }
3176 return 1;
3177}
3178
Willy Tarreau613fe992013-07-23 17:39:19 +02003179/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003180 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003181 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003182 */
3183static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003184smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003185{
Willy Tarreaube508f12016-03-10 11:47:01 +01003186 struct stkctr *stkctr;
Willy Tarreau613fe992013-07-23 17:39:19 +02003187
Willy Tarreaube508f12016-03-10 11:47:01 +01003188 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003189 if (!stkctr)
3190 return 0;
3191
Willy Tarreau37406352012-04-23 16:16:37 +02003192 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003193 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003194 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003195 if (stkctr_entry(stkctr) != NULL) {
3196 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003197 if (!ptr)
3198 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003199 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003200 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003201 }
3202 return 1;
3203}
3204
Willy Tarreau53aea102013-07-23 17:39:02 +02003205/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003206 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003207 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3208 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003209static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003210smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003211{
Willy Tarreaube508f12016-03-10 11:47:01 +01003212 struct stkctr *stkctr;
3213
Willy Tarreaube508f12016-03-10 11:47:01 +01003214 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003215 if (!stkctr)
3216 return 0;
3217
Willy Tarreau37406352012-04-23 16:16:37 +02003218 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003219 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003220 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003221 if (stkctr_entry(stkctr) != NULL) {
3222 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003223 if (!ptr)
3224 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003225 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003226 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003227 return 1;
3228}
3229
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003230/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003231 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003232 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003233 */
3234static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003235smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003236{
Willy Tarreaube508f12016-03-10 11:47:01 +01003237 struct stkctr *stkctr;
3238
Willy Tarreaube508f12016-03-10 11:47:01 +01003239 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003240 if (!stkctr)
3241 return 0;
3242
Willy Tarreau37406352012-04-23 16:16:37 +02003243 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003244 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003245 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003246 if (stkctr_entry(stkctr) != NULL) {
3247 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003248 if (!ptr)
3249 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003250 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003251 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003252 }
3253 return 1;
3254}
3255
Willy Tarreau87b09662015-04-03 00:22:06 +02003256/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003257 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3258 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003259static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003260smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003261{
Willy Tarreaube508f12016-03-10 11:47:01 +01003262 struct stkctr *stkctr;
3263
Willy Tarreaube508f12016-03-10 11:47:01 +01003264 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau563eef42013-07-23 18:32:02 +02003265 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003266 return 0;
3267
Willy Tarreau563eef42013-07-23 18:32:02 +02003268 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003269 smp->data.type = SMP_T_SINT;
Willy Tarreau4d03ef72016-08-14 12:02:55 +02003270 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
Willy Tarreau563eef42013-07-23 18:32:02 +02003271 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003272}
3273
Willy Tarreau34db1082012-04-19 17:16:54 +02003274/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003275 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003276 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003277static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003278smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003279{
Willy Tarreau37406352012-04-23 16:16:37 +02003280 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003281 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003282 smp->data.u.sint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003283 return 1;
3284}
3285
Willy Tarreau34db1082012-04-19 17:16:54 +02003286/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003287 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003288 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003289static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003290smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003291{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003292 struct proxy *px;
3293
Willy Tarreau24e32d82012-04-23 23:55:44 +02003294 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003295 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003296 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003297 smp->data.u.sint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003298 return 1;
3299}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003300
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003301/* 0=OK, <0=Alert, >0=Warning */
3302static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
3303 struct proxy *px, struct act_rule *rule,
3304 char **err)
3305{
3306 struct action_kw *kw;
3307
3308 /* Check if the service name exists. */
3309 if (*(args[*cur_arg]) == 0) {
3310 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01003311 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003312 }
3313
3314 /* lookup for keyword corresponding to a service. */
3315 kw = action_lookup(&service_keywords, args[*cur_arg]);
3316 if (!kw) {
3317 memprintf(err, "'%s' unknown service name.", args[1]);
3318 return ACT_RET_PRS_ERR;
3319 }
3320 (*cur_arg)++;
3321
3322 /* executes specific rule parser. */
3323 rule->kw = kw;
3324 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
3325 return ACT_RET_PRS_ERR;
3326
3327 /* Register processing function. */
3328 rule->action_ptr = process_use_service;
3329 rule->action = ACT_CUSTOM;
3330
3331 return ACT_RET_PRS_OK;
3332}
3333
3334void service_keywords_register(struct action_kw_list *kw_list)
3335{
3336 LIST_ADDQ(&service_keywords, &kw_list->list);
3337}
3338
3339/* main configuration keyword registration. */
3340static struct action_kw_list stream_tcp_keywords = { ILH, {
3341 { "use-service", stream_parse_use_service },
3342 { /* END */ }
3343}};
3344
3345static struct action_kw_list stream_http_keywords = { ILH, {
3346 { "use-service", stream_parse_use_service },
3347 { /* END */ }
3348}};
3349
Willy Tarreau61612d42012-04-19 18:42:05 +02003350/* Note: must not be declared <const> as its list will be overwritten.
3351 * Please take care of keeping this list alphabetically sorted.
3352 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003353static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003354 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003355}};
3356
Willy Tarreau281c7992013-01-08 01:23:27 +01003357/* Note: must not be declared <const> as its list will be overwritten.
3358 * Please take care of keeping this list alphabetically sorted.
3359 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003360static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003361 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3362 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3363 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3364 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3365 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3366 { "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 +02003367 { "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 +02003368 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3369 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3370 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3371 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3372 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3373 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3374 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3375 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3376 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3377 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3378 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3379 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3380 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003381 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3382 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3383 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3384 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3385 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3386 { "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 +02003387 { "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 +02003388 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3389 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3390 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3391 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3392 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3393 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3394 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3395 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3396 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3397 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3398 { "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 +02003399 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003400 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3401 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3402 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3403 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3404 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3405 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3406 { "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 +02003407 { "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 +02003408 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3409 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3410 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3411 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3412 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3413 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3414 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3415 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3416 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3417 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3418 { "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 +02003419 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003420 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3421 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3422 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3423 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3424 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3425 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3426 { "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 +02003427 { "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 +02003428 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3429 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3430 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3431 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3432 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3433 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3434 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3435 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3436 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3437 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3438 { "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 +02003439 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003440 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3441 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3442 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3443 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3444 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3445 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3446 { "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 +02003447 { "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 +02003448 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3449 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3450 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3451 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3452 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3453 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3454 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3455 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3456 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3457 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3458 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3459 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3460 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3461 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003462 { /* END */ },
3463}};
3464
Willy Tarreau8b22a712010-06-18 17:46:06 +02003465__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003466static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003467{
Willy Tarreau281c7992013-01-08 01:23:27 +01003468 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003469 acl_register_keywords(&acl_kws);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003470 tcp_req_cont_keywords_register(&stream_tcp_keywords);
3471 http_req_keywords_register(&stream_http_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003472}
3473
Willy Tarreaubaaee002006-06-26 02:48:02 +02003474/*
3475 * Local variables:
3476 * c-indent-level: 8
3477 * c-basic-offset: 8
3478 * End:
3479 */