blob: 288d36defea98d742ae222a7a5abf6d01ef05baf [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 Tarreau350135c2016-11-17 12:05:13 +0100563 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100564
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100565 /* If we got an error, or if nothing happened and the connection timed
566 * out, we must give up. The CER state handler will take care of retry
567 * attempts and error reports.
568 */
569 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100570 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100571 /* Some data were sent past the connection establishment,
572 * so we need to pretend we're established to log correctly
573 * and let later states handle the failure.
574 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100575 si->state = SI_ST_EST;
576 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100577 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100578 return 1;
579 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100580 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100581 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100582
Willy Tarreau350135c2016-11-17 12:05:13 +0100583 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100584
585 if (si->err_type)
586 return 0;
587
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100588 if (si->flags & SI_FL_ERR)
589 si->err_type = SI_ET_CONN_ERR;
590 else
591 si->err_type = SI_ET_CONN_TO;
592 return 0;
593 }
594
595 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100596 if (!(req->flags & CF_WRITE_PARTIAL) &&
597 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200598 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
599 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100600 s->be->options & PR_O_ABRT_CLOSE)))) {
601 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200602 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100603 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100604 if (s->srv_error)
605 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100606 return 1;
607 }
608
609 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200610 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100611 return 1;
612
613 /* OK, this means that a connection succeeded. The caller will be
614 * responsible for handling the transition from CON to EST.
615 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100616 si->state = SI_ST_EST;
617 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100618 return 1;
619}
620
621/* This function is called with (si->state == SI_ST_CER) meaning that a
622 * previous connection attempt has failed and that the file descriptor
623 * has already been released. Possible causes include asynchronous error
624 * notification and time out. Possible output states are SI_ST_CLO when
625 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
626 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
627 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
628 * marked as in error state. It returns 0.
629 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200630static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100631{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100632 struct stream_interface *si = &s->si[1];
633
Willy Tarreau87b09662015-04-03 00:22:06 +0200634 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100635 if (objt_server(s->target)) {
636 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100637
Willy Tarreaue7dff022015-04-03 01:14:29 +0200638 if (s->flags & SF_CURR_SESS) {
639 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100640 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100641 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100642 }
643
644 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200645 si->conn_retries--;
646 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100647 if (!si->err_type) {
648 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100649 }
650
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100651 if (objt_server(s->target))
652 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100653 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100654 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100655 if (may_dequeue_tasks(objt_server(s->target), s->be))
656 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100657
658 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200659 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100660 s->req.flags |= CF_WRITE_ERROR;
661 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100662
663 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100664 if (s->srv_error)
665 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100666 return 0;
667 }
668
669 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700670 * retry on another server. By default this redispatch occurs on the
671 * last retry, but if configured we allow redispatches to occur on
672 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200673 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100674 * bit to ignore any persistence cookie. We won't count a retry nor a
675 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200676 * If the connection is not persistent, the balancing algorithm is not
677 * determinist (round robin) and there is more than one active server,
678 * we accept to perform an immediate redispatch without waiting since
679 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100680 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200681 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700682 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
Willy Tarreau49008c12016-01-13 07:58:44 +0100683 ((__objt_server(s->target)->state < SRV_ST_RUNNING) ||
684 (((s->be->redispatch_after > 0) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700685 ((s->be->conn_retries - si->conn_retries) %
686 s->be->redispatch_after == 0)) ||
687 ((s->be->redispatch_after < 0) &&
688 ((s->be->conn_retries - si->conn_retries) %
689 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200690 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700691 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100692 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100693 if (may_dequeue_tasks(objt_server(s->target), s->be))
694 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100695
Willy Tarreaue7dff022015-04-03 01:14:29 +0200696 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100697 si->state = SI_ST_REQ;
698 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100699 if (objt_server(s->target))
700 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100701 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100702 si->state = SI_ST_ASS;
703 }
704
705 if (si->flags & SI_FL_ERR) {
706 /* The error was an asynchronous connection error, and we will
707 * likely have to retry connecting to the same server, most
708 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200709 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100710 */
711
Willy Tarreaub0290662014-06-13 17:04:44 +0200712 int delay = 1000;
713
714 if (s->be->timeout.connect && s->be->timeout.connect < delay)
715 delay = s->be->timeout.connect;
716
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100717 if (!si->err_type)
718 si->err_type = SI_ET_CONN_ERR;
719
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200720 /* only wait when we're retrying on the same server */
721 if (si->state == SI_ST_ASS ||
722 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
723 (s->be->srv_act <= 1)) {
724 si->state = SI_ST_TAR;
725 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
726 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100727 return 0;
728 }
729 return 0;
730}
731
732/*
733 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200734 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200735 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200737static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100738{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100739 struct stream_interface *si = &s->si[1];
740 struct channel *req = &s->req;
741 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100742
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100743 /* First, centralize the timers information */
744 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
745 si->exp = TICK_ETERNITY;
746
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100747 if (objt_server(s->target))
748 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100749
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100750 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751 /* if the user wants to log as soon as possible, without counting
752 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200753 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100754 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100755 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100756 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100757 }
758 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100759 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100760 }
761
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200762 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Christopher Faulet309c6412015-12-02 09:57:32 +0100763
764 /* Be sure to filter response headers if the backend is an HTTP proxy
765 * and if there are filters attached to the stream. */
766 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
767 rep->analysers |= AN_FLT_HTTP_HDRS;
768
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200769 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200770 if (req->flags & CF_WAKE_CONNECT) {
771 req->flags |= CF_WAKE_ONCE;
772 req->flags &= ~CF_WAKE_CONNECT;
773 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200774 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200775 /* real connections have timeouts */
776 req->wto = s->be->timeout.server;
777 rep->rto = s->be->timeout.server;
778 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100779 req->wex = TICK_ETERNITY;
780}
781
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700782/* Check if the connection request is in such a state that it can be aborted. */
783static int check_req_may_abort(struct channel *req, struct stream *s)
784{
785 return ((req->flags & (CF_READ_ERROR)) ||
786 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
787 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE)));
788}
789
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100790/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
791 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100792 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
793 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
794 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100795 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200796static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100797{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100798 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100799 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100800 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100801
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100802 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 +0100803 now_ms, __FUNCTION__,
804 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400805 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100806 req->rex, s->res.wex,
807 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400808 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 +0100809
810 if (si->state == SI_ST_ASS) {
811 /* Server assigned to connection request, we have to try to connect now */
812 int conn_err;
813
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700814 /* Before we try to initiate the connection, see if the
815 * request may be aborted instead.
816 */
817 if (check_req_may_abort(req, s)) {
818 si->err_type |= SI_ET_CONN_ABRT;
819 goto abort_connection;
820 }
821
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100822 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100823 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100824
Willy Tarreaue7dff022015-04-03 01:14:29 +0200825 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100826 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100827 if (srv)
828 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500829 if (srv)
830 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100831 return;
832 }
833
834 /* We have received a synchronous error. We might have to
835 * abort, retry immediately or redispatch.
836 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200837 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100838 if (!si->err_type) {
839 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100840 }
841
Willy Tarreau827aee92011-03-10 16:55:02 +0100842 if (srv)
843 srv_inc_sess_ctr(srv);
844 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500845 srv_set_sess_last(srv);
846 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100847 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100848 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100849
Willy Tarreau87b09662015-04-03 00:22:06 +0200850 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100851 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100852 if (may_dequeue_tasks(srv, s->be))
853 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100854
855 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200856 si_shutr(si);
857 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100858 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100859
860 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
861
Willy Tarreau87b09662015-04-03 00:22:06 +0200862 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100863 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100864 if (s->srv_error)
865 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100866 return;
867 }
868
869 /* We are facing a retryable error, but we don't want to run a
870 * turn-around now, as the problem is likely a source port
871 * allocation problem, so we want to retry now.
872 */
873 si->state = SI_ST_CER;
874 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100875 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100876 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
877 return;
878 }
879 else if (si->state == SI_ST_QUE) {
880 /* connection request was queued, check for any update */
881 if (!s->pend_pos) {
882 /* The connection is not in the queue anymore. Either
883 * we have a server connection slot available and we
884 * go directly to the assigned state, or we need to
885 * load-balance first and go to the INI state.
886 */
887 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200888 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100889 si->state = SI_ST_REQ;
890 else {
891 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
892 si->state = SI_ST_ASS;
893 }
894 return;
895 }
896
897 /* Connection request still in queue... */
898 if (si->flags & SI_FL_EXP) {
899 /* ... and timeout expired */
900 si->exp = TICK_ETERNITY;
901 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100902 if (srv)
903 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100904 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200905 si_shutr(si);
906 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100907 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100908 if (!si->err_type)
909 si->err_type = SI_ET_QUEUE_TO;
910 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100911 if (s->srv_error)
912 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100913 return;
914 }
915
916 /* Connection remains in queue, check if we have to abort it */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700917 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100918 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100919 si->err_type |= SI_ET_QUEUE_ABRT;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700920 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100921 }
922
923 /* Nothing changed */
924 return;
925 }
926 else if (si->state == SI_ST_TAR) {
927 /* Connection request might be aborted */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700928 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100929 si->err_type |= SI_ET_CONN_ABRT;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700930 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100931 }
932
933 if (!(si->flags & SI_FL_EXP))
934 return; /* still in turn-around */
935
936 si->exp = TICK_ETERNITY;
937
Willy Tarreau87b09662015-04-03 00:22:06 +0200938 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100939 * marked "assigned".
940 * FIXME: Should we force a redispatch attempt when the server is down ?
941 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200942 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100943 si->state = SI_ST_ASS;
944 else
945 si->state = SI_ST_REQ;
946 return;
947 }
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700948 return;
949
950abort_connection:
951 /* give up */
952 si->exp = TICK_ETERNITY;
953 si_shutr(si);
954 si_shutw(si);
955 si->state = SI_ST_CLO;
956 if (s->srv_error)
957 s->srv_error(s, si);
958 return;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100959}
960
Willy Tarreau87b09662015-04-03 00:22:06 +0200961/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900962 * also counts a failed request if the server state has not reached the request
963 * stage.
964 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200965static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900966{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200967 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900968 if (s->si[1].state < SI_ST_REQ) {
969
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200970 strm_fe(s)->fe_counters.failed_req++;
Willy Tarreau2c1068c2015-09-23 12:21:21 +0200971 if (strm_li(s) && strm_li(s)->counters)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200972 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900973
Willy Tarreaue7dff022015-04-03 01:14:29 +0200974 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900975 }
976 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200977 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900978 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200979 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900980 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200981 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900982 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200983 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900984 }
985}
986
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100987/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100988 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
989 * a real connection to a server, indicating that a server has been assigned,
990 * or SI_ST_EST for a successful connection to an applet. It may also return
991 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100992 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200993static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100994{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100995 struct stream_interface *si = &s->si[1];
996
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100997 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 +0100998 now_ms, __FUNCTION__,
999 s,
CJ Ess6eac32e2015-05-02 16:35:24 -04001000 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001001 s->req.rex, s->res.wex,
1002 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -04001003 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 +01001004
1005 if (si->state != SI_ST_REQ)
1006 return;
1007
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001008 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
1009 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001010 struct appctx *appctx = objt_appctx(si->end);
1011
1012 if (!appctx || appctx->applet != __objt_applet(s->target))
1013 appctx = stream_int_register_handler(si, objt_applet(s->target));
1014
1015 if (!appctx) {
1016 /* No more memory, let's immediately abort. Force the
1017 * error code to ignore the ERR_LOCAL which is not a
1018 * real error.
1019 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001020 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001021
1022 si_shutr(si);
1023 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001024 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001025 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001026 si->state = SI_ST_CLO;
1027 if (s->srv_error)
1028 s->srv_error(s, si);
1029 return;
1030 }
1031
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001032 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001033 si->state = SI_ST_EST;
1034 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001035 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001036 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001037 return;
1038 }
1039
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001040 /* Try to assign a server */
1041 if (srv_redispatch_connect(s) != 0) {
1042 /* We did not get a server. Either we queued the
1043 * connection request, or we encountered an error.
1044 */
1045 if (si->state == SI_ST_QUE)
1046 return;
1047
1048 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001049 si_shutr(si);
1050 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001051 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001052 if (!si->err_type)
1053 si->err_type = SI_ET_CONN_OTHER;
1054 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001055 if (s->srv_error)
1056 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001057 return;
1058 }
1059
1060 /* The server is assigned */
1061 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1062 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001063 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001064}
1065
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001066/* This function parses the use-service action ruleset. It executes
1067 * the associated ACL and set an applet as a stream or txn final node.
1068 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1069 * consistent state. It returns ACT_RET_STOP in succes case because
1070 * use-service must be a terminal action. Returns ACT_RET_YIELD
1071 * if the initialisation function require more data.
1072 */
1073enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1074 struct session *sess, struct stream *s, int flags)
1075
1076{
1077 struct appctx *appctx;
1078
1079 /* Initialises the applet if it is required. */
1080 if (flags & ACT_FLAG_FIRST) {
1081 /* Register applet. this function schedules the applet. */
1082 s->target = &rule->applet.obj_type;
1083 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target))))
1084 return ACT_RET_ERR;
1085
1086 /* Initialise the context. */
1087 appctx = si_appctx(&s->si[1]);
1088 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1089 appctx->rule = rule;
1090 }
1091 else
1092 appctx = si_appctx(&s->si[1]);
1093
1094 /* Stops the applet sheduling, in case of the init function miss
1095 * some data.
1096 */
1097 appctx_pause(appctx);
1098 si_applet_stop_get(&s->si[1]);
1099
1100 /* Call initialisation. */
1101 if (rule->applet.init)
1102 switch (rule->applet.init(appctx, px, s)) {
1103 case 0: return ACT_RET_ERR;
1104 case 1: break;
1105 default: return ACT_RET_YIELD;
1106 }
1107
1108 /* Now we can schedule the applet. */
1109 si_applet_cant_get(&s->si[1]);
1110 appctx_wakeup(appctx);
1111
1112 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1113 sess->fe->fe_counters.intercepted_req++;
1114
1115 /* The flag SF_ASSIGNED prevent from server assignment. */
1116 s->flags |= SF_ASSIGNED;
1117
1118 return ACT_RET_STOP;
1119}
1120
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001121/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001122 * if appropriate. The default_backend rule is also considered, then the
1123 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001124 * It returns 1 if the processing can continue on next analysers, or zero if it
1125 * either needs more data or wants to immediately abort the request.
1126 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001127static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001128{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001129 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001130 struct session *sess = s->sess;
1131 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001132
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001133 req->analysers &= ~an_bit;
1134 req->analyse_exp = TICK_ETERNITY;
1135
Willy Tarreau87b09662015-04-03 00:22:06 +02001136 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 +02001137 now_ms, __FUNCTION__,
1138 s,
1139 req,
1140 req->rex, req->wex,
1141 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001142 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001143 req->analysers);
1144
1145 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001146 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001147 struct switching_rule *rule;
1148
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001149 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001150 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001151
Willy Tarreauf51658d2014-04-23 01:21:56 +02001152 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001153 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001154 ret = acl_pass(ret);
1155 if (rule->cond->pol == ACL_COND_UNLESS)
1156 ret = !ret;
1157 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001158
1159 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001160 /* If the backend name is dynamic, try to resolve the name.
1161 * If we can't resolve the name, or if any error occurs, break
1162 * the loop and fallback to the default backend.
1163 */
1164 struct proxy *backend;
1165
1166 if (rule->dynamic) {
1167 struct chunk *tmp = get_trash_chunk();
1168 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1169 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001170 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001171 if (!backend)
1172 break;
1173 }
1174 else
1175 backend = rule->be.backend;
1176
Willy Tarreau87b09662015-04-03 00:22:06 +02001177 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001178 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001179 break;
1180 }
1181 }
1182
1183 /* To ensure correct connection accounting on the backend, we
1184 * have to assign one if it was not set (eg: a listen). This
1185 * measure also takes care of correctly setting the default
1186 * backend if any.
1187 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001188 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001189 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001190 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001191 }
1192
Willy Tarreaufb356202010-08-03 14:02:05 +02001193 /* 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 +02001194 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001195 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1196 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001197 s->req.analysers &= ~AN_FLT_START_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001198 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001199
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001200 /* 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 +02001201 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001202 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001203 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001204 int ret = 1;
1205
1206 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001207 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 +01001208 ret = acl_pass(ret);
1209 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1210 ret = !ret;
1211 }
1212
1213 if (ret) {
1214 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001215 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001216 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001217 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001218 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001219 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001220 break;
1221 }
1222 }
1223
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001224 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001225
1226 sw_failed:
1227 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001228 channel_abort(&s->req);
1229 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001230
Willy Tarreaue7dff022015-04-03 01:14:29 +02001231 if (!(s->flags & SF_ERR_MASK))
1232 s->flags |= SF_ERR_RESOURCE;
1233 if (!(s->flags & SF_FINST_MASK))
1234 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001235
Willy Tarreaueee5b512015-04-03 23:46:31 +02001236 if (s->txn)
1237 s->txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001238 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001239 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001240 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001241}
1242
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001243/* This stream analyser works on a request. It applies all use-server rules on
1244 * it then returns 1. The data must already be present in the buffer otherwise
1245 * they won't match. It always returns 1.
1246 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001247static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001248{
1249 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001250 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001251 struct server_rule *rule;
1252
Willy Tarreau87b09662015-04-03 00:22:06 +02001253 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 +02001254 now_ms, __FUNCTION__,
1255 s,
1256 req,
1257 req->rex, req->wex,
1258 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001259 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001260 req->analysers);
1261
Willy Tarreaue7dff022015-04-03 01:14:29 +02001262 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001263 list_for_each_entry(rule, &px->server_rules, list) {
1264 int ret;
1265
Willy Tarreau192252e2015-04-04 01:47:55 +02001266 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001267 ret = acl_pass(ret);
1268 if (rule->cond->pol == ACL_COND_UNLESS)
1269 ret = !ret;
1270
1271 if (ret) {
1272 struct server *srv = rule->srv.ptr;
1273
Willy Tarreau892337c2014-05-13 23:41:20 +02001274 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001275 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001276 (s->flags & SF_FORCE_PRST)) {
1277 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001278 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001279 break;
1280 }
1281 /* if the server is not UP, let's go on with next rules
1282 * just in case another one is suited.
1283 */
1284 }
1285 }
1286 }
1287
1288 req->analysers &= ~an_bit;
1289 req->analyse_exp = TICK_ETERNITY;
1290 return 1;
1291}
1292
Emeric Brun1d33b292010-01-04 15:47:17 +01001293/* This stream analyser works on a request. It applies all sticking rules on
1294 * it then returns 1. The data must already be present in the buffer otherwise
1295 * they won't match. It always returns 1.
1296 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001297static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001298{
1299 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001300 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001301 struct sticking_rule *rule;
1302
Willy Tarreau87b09662015-04-03 00:22:06 +02001303 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 +01001304 now_ms, __FUNCTION__,
1305 s,
1306 req,
1307 req->rex, req->wex,
1308 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001309 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001310 req->analysers);
1311
1312 list_for_each_entry(rule, &px->sticking_rules, list) {
1313 int ret = 1 ;
1314 int i;
1315
Willy Tarreau9667a802013-12-09 12:52:13 +01001316 /* Only the first stick store-request of each table is applied
1317 * and other ones are ignored. The purpose is to allow complex
1318 * configurations which look for multiple entries by decreasing
1319 * order of precision and to stop at the first which matches.
1320 * An example could be a store of the IP address from an HTTP
1321 * header first, then from the source if not found.
1322 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001323 for (i = 0; i < s->store_count; i++) {
1324 if (rule->table.t == s->store[i].table)
1325 break;
1326 }
1327
1328 if (i != s->store_count)
1329 continue;
1330
1331 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001332 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001333 ret = acl_pass(ret);
1334 if (rule->cond->pol == ACL_COND_UNLESS)
1335 ret = !ret;
1336 }
1337
1338 if (ret) {
1339 struct stktable_key *key;
1340
Willy Tarreau192252e2015-04-04 01:47:55 +02001341 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 +01001342 if (!key)
1343 continue;
1344
1345 if (rule->flags & STK_IS_MATCH) {
1346 struct stksess *ts;
1347
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001348 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001349 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001350 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001351 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001352
1353 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001354 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1355 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001356 if (node) {
1357 struct server *srv;
1358
1359 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001360 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001361 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001362 (s->flags & SF_FORCE_PRST)) {
1363 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001364 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001365 }
1366 }
1367 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001368 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001369 }
1370 }
1371 if (rule->flags & STK_IS_STORE) {
1372 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1373 struct stksess *ts;
1374
1375 ts = stksess_new(rule->table.t, key);
1376 if (ts) {
1377 s->store[s->store_count].table = rule->table.t;
1378 s->store[s->store_count++].ts = ts;
1379 }
1380 }
1381 }
1382 }
1383 }
1384
1385 req->analysers &= ~an_bit;
1386 req->analyse_exp = TICK_ETERNITY;
1387 return 1;
1388}
1389
1390/* This stream analyser works on a response. It applies all store rules on it
1391 * then returns 1. The data must already be present in the buffer otherwise
1392 * they won't match. It always returns 1.
1393 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001394static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001395{
1396 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001397 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001398 struct sticking_rule *rule;
1399 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001400 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001401
Willy Tarreau87b09662015-04-03 00:22:06 +02001402 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 +01001403 now_ms, __FUNCTION__,
1404 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001405 rep,
1406 rep->rex, rep->wex,
1407 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001408 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001409 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001410
1411 list_for_each_entry(rule, &px->storersp_rules, list) {
1412 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001413
Willy Tarreau9667a802013-12-09 12:52:13 +01001414 /* Only the first stick store-response of each table is applied
1415 * and other ones are ignored. The purpose is to allow complex
1416 * configurations which look for multiple entries by decreasing
1417 * order of precision and to stop at the first which matches.
1418 * An example could be a store of a set-cookie value, with a
1419 * fallback to a parameter found in a 302 redirect.
1420 *
1421 * The store-response rules are not allowed to override the
1422 * store-request rules for the same table, but they may coexist.
1423 * Thus we can have up to one store-request entry and one store-
1424 * response entry for the same table at any time.
1425 */
1426 for (i = nbreq; i < s->store_count; i++) {
1427 if (rule->table.t == s->store[i].table)
1428 break;
1429 }
1430
1431 /* skip existing entries for this table */
1432 if (i < s->store_count)
1433 continue;
1434
Emeric Brun1d33b292010-01-04 15:47:17 +01001435 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001436 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001437 ret = acl_pass(ret);
1438 if (rule->cond->pol == ACL_COND_UNLESS)
1439 ret = !ret;
1440 }
1441
1442 if (ret) {
1443 struct stktable_key *key;
1444
Willy Tarreau192252e2015-04-04 01:47:55 +02001445 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 +01001446 if (!key)
1447 continue;
1448
Willy Tarreau37e340c2013-12-06 23:05:21 +01001449 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001450 struct stksess *ts;
1451
1452 ts = stksess_new(rule->table.t, key);
1453 if (ts) {
1454 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001455 s->store[s->store_count++].ts = ts;
1456 }
1457 }
1458 }
1459 }
1460
1461 /* process store request and store response */
1462 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001463 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001464 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001465
Willy Tarreauc93cd162014-05-13 15:54:22 +02001466 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001467 stksess_free(s->store[i].table, s->store[i].ts);
1468 s->store[i].ts = NULL;
1469 continue;
1470 }
1471
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001472 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1473 if (ts) {
1474 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001475 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001476 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001477 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001478 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001479 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001480
1481 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001482 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001483 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001484 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001485 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001486
1487 rep->analysers &= ~an_bit;
1488 rep->analyse_exp = TICK_ETERNITY;
1489 return 1;
1490}
1491
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001492/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001493 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001494 */
1495#define UPDATE_ANALYSERS(real, list, back, flag) { \
1496 list = (((list) & ~(flag)) | ~(back)) & (real); \
1497 back = real; \
1498 if (!(list)) \
1499 break; \
1500 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1501 continue; \
1502}
1503
Christopher Fauleta9215b72016-05-11 17:06:28 +02001504/* These 2 following macros call an analayzer for the specified channel if the
1505 * right flag is set. The first one is used for "filterable" analyzers. If a
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001506 * stream has some registered filters, pre and post analyaze callbacks are
1507 * called. The second are used for other analyzers (AN_FLT_* and
Christopher Fauleta9215b72016-05-11 17:06:28 +02001508 * AN_REQ/RES_HTTP_XFER_BODY) */
1509#define FLT_ANALYZE(strm, chn, fun, list, back, flag, ...) \
1510 { \
1511 if ((list) & (flag)) { \
1512 if (HAS_FILTERS(strm)) { \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001513 if (!flt_pre_analyze((strm), (chn), (flag))) \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001514 break; \
1515 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1516 break; \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001517 if (!flt_post_analyze((strm), (chn), (flag))) \
1518 break; \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001519 } \
1520 else { \
1521 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1522 break; \
1523 } \
1524 UPDATE_ANALYSERS((chn)->analysers, (list), \
1525 (back), (flag)); \
1526 } \
1527 }
1528
1529#define ANALYZE(strm, chn, fun, list, back, flag, ...) \
1530 { \
1531 if ((list) & (flag)) { \
1532 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1533 break; \
1534 UPDATE_ANALYSERS((chn)->analysers, (list), \
1535 (back), (flag)); \
1536 } \
1537 }
1538
Willy Tarreau87b09662015-04-03 00:22:06 +02001539/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001540 * then puts it back to the wait queue in a clean state, or cleans up its
1541 * resources if it must be deleted. Returns in <next> the date the task wants
1542 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1543 * nothing too many times, the request and response buffers flags are monitored
1544 * and each function is called only if at least another function has changed at
1545 * least one flag it is interested in.
1546 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001547struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001548{
Willy Tarreau827aee92011-03-10 16:55:02 +01001549 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001550 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001551 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001552 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001553 unsigned int rq_prod_last, rq_cons_last;
1554 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001555 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001556 struct channel *req, *res;
1557 struct stream_interface *si_f, *si_b;
1558
1559 req = &s->req;
1560 res = &s->res;
1561
1562 si_f = &s->si[0];
1563 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001564
1565 //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 +01001566 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001567
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001568 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001569 if (s->txn)
1570 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001571
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001572 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001573 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1574 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001575
1576 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001577 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1578 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001579
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001580 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001581 si_f->flags |= SI_FL_DONT_WAKE;
1582 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001583
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001584 /* 1a: Check for low level timeouts if needed. We just set a flag on
1585 * stream interfaces when their timeouts have expired.
1586 */
1587 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001588 stream_int_check_timeouts(si_f);
1589 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001590
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001591 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001592 * for future reads or writes. Note: this will also concern upper layers
1593 * but we do not touch any other flag. We must be careful and correctly
1594 * detect state changes when calling them.
1595 */
1596
Willy Tarreau8f128b42014-11-28 15:07:47 +01001597 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001598
Willy Tarreau8f128b42014-11-28 15:07:47 +01001599 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1600 si_b->flags |= SI_FL_NOLINGER;
1601 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001602 }
1603
Willy Tarreau8f128b42014-11-28 15:07:47 +01001604 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1605 if (si_f->flags & SI_FL_NOHALF)
1606 si_f->flags |= SI_FL_NOLINGER;
1607 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001608 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001609
Willy Tarreau8f128b42014-11-28 15:07:47 +01001610 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001611
Willy Tarreau8f128b42014-11-28 15:07:47 +01001612 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1613 si_f->flags |= SI_FL_NOLINGER;
1614 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001615 }
1616
Willy Tarreau8f128b42014-11-28 15:07:47 +01001617 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1618 if (si_b->flags & SI_FL_NOHALF)
1619 si_b->flags |= SI_FL_NOLINGER;
1620 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001621 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001622
Christopher Fauleta00d8172016-11-10 14:58:05 +01001623 if (HAS_FILTERS(s))
1624 flt_stream_check_timeouts(s);
1625
Willy Tarreau798f4322012-11-08 14:49:17 +01001626 /* Once in a while we're woken up because the task expires. But
1627 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001628 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001629 * timeout needs to be refreshed.
1630 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001631 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001632 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1633 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001634 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau5fb04712016-05-04 10:18:37 +02001635 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER)) {
1636 si_f->flags &= ~SI_FL_DONT_WAKE;
1637 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau798f4322012-11-08 14:49:17 +01001638 goto update_exp_and_leave;
Willy Tarreau5fb04712016-05-04 10:18:37 +02001639 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001640 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001641
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001642 /* below we may emit error messages so we have to ensure that we have
1643 * our buffers properly allocated.
1644 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001645 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001646 /* No buffer available, we've been subscribed to the list of
1647 * buffer waiters, let's wait for our turn.
1648 */
Willy Tarreau5fb04712016-05-04 10:18:37 +02001649 si_f->flags &= ~SI_FL_DONT_WAKE;
1650 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001651 goto update_exp_and_leave;
1652 }
1653
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001654 /* 1b: check for low-level errors reported at the stream interface.
1655 * First we check if it's a retryable error (in which case we don't
1656 * want to tell the buffer). Otherwise we report the error one level
1657 * upper by setting flags into the buffers. Note that the side towards
1658 * the client cannot have connect (hence retryable) errors. Also, the
1659 * connection setup code must be able to deal with any type of abort.
1660 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001661 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001662 if (unlikely(si_f->flags & SI_FL_ERR)) {
1663 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1664 si_shutr(si_f);
1665 si_shutw(si_f);
1666 stream_int_report_error(si_f);
1667 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001668 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001669 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001670 if (srv)
1671 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001672 if (!(s->flags & SF_ERR_MASK))
1673 s->flags |= SF_ERR_CLICL;
1674 if (!(s->flags & SF_FINST_MASK))
1675 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001676 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001677 }
1678 }
1679
Willy Tarreau8f128b42014-11-28 15:07:47 +01001680 if (unlikely(si_b->flags & SI_FL_ERR)) {
1681 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1682 si_shutr(si_b);
1683 si_shutw(si_b);
1684 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001685 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001686 if (srv)
1687 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001688 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001689 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001690 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001691 if (srv)
1692 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001693 if (!(s->flags & SF_ERR_MASK))
1694 s->flags |= SF_ERR_SRVCL;
1695 if (!(s->flags & SF_FINST_MASK))
1696 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001697 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001698 }
1699 /* note: maybe we should process connection errors here ? */
1700 }
1701
Willy Tarreau8f128b42014-11-28 15:07:47 +01001702 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001703 /* we were trying to establish a connection on the server side,
1704 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1705 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001706 if (unlikely(!sess_update_st_con_tcp(s)))
1707 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001708 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001709 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001710
1711 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1712 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1713 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1714 */
1715 }
1716
Willy Tarreau8f128b42014-11-28 15:07:47 +01001717 rq_prod_last = si_f->state;
1718 rq_cons_last = si_b->state;
1719 rp_cons_last = si_f->state;
1720 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001721
1722 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001723 /* Check for connection closure */
1724
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001725 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001726 "[%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 +01001727 now_ms, __FUNCTION__, __LINE__,
1728 t,
1729 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001730 req, res,
1731 req->rex, res->wex,
1732 req->flags, res->flags,
1733 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1734 si_f->err_type, si_b->err_type,
1735 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001736
1737 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001738 if (unlikely(si_f->state == SI_ST_DIS))
1739 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001740
1741 /* When a server-side connection is released, we have to count it and
1742 * check for pending connections on this server.
1743 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001744 if (unlikely(si_b->state == SI_ST_DIS)) {
1745 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001746 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001747 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001748 if (s->flags & SF_CURR_SESS) {
1749 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001750 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001751 }
1752 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001753 if (may_dequeue_tasks(srv, s->be))
1754 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001755 }
1756 }
1757
1758 /*
1759 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1760 * at this point.
1761 */
1762
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001763 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001764 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001765 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1766 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1767 si_f->state != rq_prod_last ||
1768 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001769 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001770 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001771
Willy Tarreau8f128b42014-11-28 15:07:47 +01001772 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001773 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001774 unsigned int ana_list;
1775 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001776
Willy Tarreau90deb182010-01-07 00:20:41 +01001777 /* it's up to the analysers to stop new connections,
1778 * disable reading or closing. Note: if an analyser
1779 * disables any of these bits, it is responsible for
1780 * enabling them again when it disables itself, so
1781 * that other analysers are called in similar conditions.
1782 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001783 channel_auto_read(req);
1784 channel_auto_connect(req);
1785 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001786
1787 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001788 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001789 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001790 * the list when not needed. Any analyser may return 0
1791 * to break out of the loop, either because of missing
1792 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001793 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001794 * analyser, and we may loop again if other analysers
1795 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001796 *
1797 * We build a list of analysers to run. We evaluate all
1798 * of these analysers in the order of the lower bit to
1799 * the higher bit. This ordering is very important.
1800 * An analyser will often add/remove other analysers,
1801 * including itself. Any changes to itself have no effect
1802 * on the loop. If it removes any other analysers, we
1803 * want those analysers not to be called anymore during
1804 * this loop. If it adds an analyser that is located
1805 * after itself, we want it to be scheduled for being
1806 * processed during the loop. If it adds an analyser
1807 * which is located before it, we want it to switch to
1808 * it immediately, even if it has already been called
1809 * once but removed since.
1810 *
1811 * In order to achieve this, we compare the analyser
1812 * list after the call with a copy of it before the
1813 * call. The work list is fed with analyser bits that
1814 * appeared during the call. Then we compare previous
1815 * work list with the new one, and check the bits that
1816 * appeared. If the lowest of these bits is lower than
1817 * the current bit, it means we have enabled a previous
1818 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001819 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001820
Willy Tarreau8f128b42014-11-28 15:07:47 +01001821 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001822 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001823 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauleta9215b72016-05-11 17:06:28 +02001824 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_FLT_START_FE);
1825 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_FE);
1826 FLT_ANALYZE(s, req, http_wait_for_request, ana_list, ana_back, AN_REQ_WAIT_HTTP);
1827 FLT_ANALYZE(s, req, http_wait_for_request_body, ana_list, ana_back, AN_REQ_HTTP_BODY);
1828 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE, sess->fe);
1829 FLT_ANALYZE(s, req, process_switching_rules, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
1830 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_FLT_START_BE);
1831 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_BE);
1832 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE, s->be);
1833 FLT_ANALYZE(s, req, http_process_tarpit, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
1834 FLT_ANALYZE(s, req, process_server_rules, ana_list, ana_back, AN_REQ_SRV_RULES);
1835 FLT_ANALYZE(s, req, http_process_request, ana_list, ana_back, AN_REQ_HTTP_INNER);
1836 FLT_ANALYZE(s, req, tcp_persist_rdp_cookie, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
1837 FLT_ANALYZE(s, req, process_sticking_rules, ana_list, ana_back, AN_REQ_STICKING_RULES);
1838 ANALYZE (s, req, flt_analyze_http_headers, ana_list, ana_back, AN_FLT_HTTP_HDRS);
1839 ANALYZE (s, req, flt_xfer_data, ana_list, ana_back, AN_FLT_XFER_DATA);
1840 ANALYZE (s, req, http_request_forward_body, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
1841 ANALYZE (s, req, flt_end_analyze, ana_list, ana_back, AN_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01001842 break;
1843 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001844 }
Willy Tarreau84455332009-03-15 22:34:05 +01001845
Willy Tarreau8f128b42014-11-28 15:07:47 +01001846 rq_prod_last = si_f->state;
1847 rq_cons_last = si_b->state;
1848 req->flags &= ~CF_WAKE_ONCE;
1849 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001850
Willy Tarreau8f128b42014-11-28 15:07:47 +01001851 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001852 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001853 }
1854
Willy Tarreau576507f2010-01-07 00:09:04 +01001855 /* we'll monitor the request analysers while parsing the response,
1856 * because some response analysers may indirectly enable new request
1857 * analysers (eg: HTTP keep-alive).
1858 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001859 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001860
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001861 resync_response:
1862 /* Analyse response */
1863
Willy Tarreau8f128b42014-11-28 15:07:47 +01001864 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1865 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1866 si_f->state != rp_cons_last ||
1867 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001868 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001869 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001870
Willy Tarreau8f128b42014-11-28 15:07:47 +01001871 if ((res->flags & CF_MASK_ANALYSER) &&
1872 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001873 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1874 * for some free space in the response buffer, so we might need to call
1875 * it when something changes in the response buffer, but still we pass
1876 * it the request buffer. Note that the SI state might very well still
1877 * be zero due to us returning a flow of redirects!
1878 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001879 res->analysers &= ~AN_REQ_ALL;
1880 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001881 }
1882
Willy Tarreau8f128b42014-11-28 15:07:47 +01001883 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001884 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001885 unsigned int ana_list;
1886 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001887
Willy Tarreau90deb182010-01-07 00:20:41 +01001888 /* it's up to the analysers to stop disable reading or
1889 * closing. Note: if an analyser disables any of these
1890 * bits, it is responsible for enabling them again when
1891 * it disables itself, so that other analysers are called
1892 * in similar conditions.
1893 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001894 channel_auto_read(res);
1895 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001896
1897 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001898 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001899 * to MSB. The analysers must remove themselves from
1900 * the list when not needed. Any analyser may return 0
1901 * to break out of the loop, either because of missing
1902 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001903 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001904 * analyser, and we may loop again if other analysers
1905 * are added in the middle.
1906 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001907
Willy Tarreau8f128b42014-11-28 15:07:47 +01001908 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001909 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001910 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauleta9215b72016-05-11 17:06:28 +02001911 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_FLT_START_FE);
1912 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_FLT_START_BE);
1913 FLT_ANALYZE(s, res, tcp_inspect_response, ana_list, ana_back, AN_RES_INSPECT);
1914 FLT_ANALYZE(s, res, http_wait_for_response, ana_list, ana_back, AN_RES_WAIT_HTTP);
1915 FLT_ANALYZE(s, res, process_store_rules, ana_list, ana_back, AN_RES_STORE_RULES);
1916 FLT_ANALYZE(s, res, http_process_res_common, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE, s->be);
1917 ANALYZE (s, res, flt_analyze_http_headers, ana_list, ana_back, AN_FLT_HTTP_HDRS);
1918 ANALYZE (s, res, flt_xfer_data, ana_list, ana_back, AN_FLT_XFER_DATA);
1919 ANALYZE (s, res, http_response_forward_body, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
1920 ANALYZE (s, res, flt_end_analyze, ana_list, ana_back, AN_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01001921 break;
1922 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001923 }
1924
Willy Tarreau8f128b42014-11-28 15:07:47 +01001925 rp_cons_last = si_f->state;
1926 rp_prod_last = si_b->state;
1927 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001928
Willy Tarreau8f128b42014-11-28 15:07:47 +01001929 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001930 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001931 }
1932
Willy Tarreau576507f2010-01-07 00:09:04 +01001933 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001934 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001935 goto resync_request;
1936
Willy Tarreau8f128b42014-11-28 15:07:47 +01001937 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001938 goto resync_request;
1939
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001940 /* FIXME: here we should call protocol handlers which rely on
1941 * both buffers.
1942 */
1943
1944
1945 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001946 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001947 * we're just in a data phase here since it means we have not
1948 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001949 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001950 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001951 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001952 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001953 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001954 req->analysers = 0;
1955 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001956 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001957 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001958 if (srv)
1959 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001960 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001961 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001962 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001963 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001964 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001965 if (srv)
1966 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001967 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001968 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001969 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001970 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001971 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001972 if (srv)
1973 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001974 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001975 }
1976 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001977 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001978 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001979 if (srv)
1980 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001981 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001982 }
Willy Tarreau84455332009-03-15 22:34:05 +01001983 sess_set_term_flags(s);
1984 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001985 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001986 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001987 res->analysers = 0;
1988 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001989 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001990 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001991 if (srv)
1992 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001993 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001994 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001995 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001996 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001997 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001998 if (srv)
1999 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002000 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002001 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002002 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002003 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002004 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002005 if (srv)
2006 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002007 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002008 }
2009 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002010 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002011 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002012 if (srv)
2013 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002014 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002015 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002016 sess_set_term_flags(s);
2017 }
Willy Tarreau84455332009-03-15 22:34:05 +01002018 }
2019
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002020 /*
2021 * Here we take care of forwarding unhandled data. This also includes
2022 * connection establishments and shutdown requests.
2023 */
2024
2025
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002026 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002027 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002028 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002029 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002030 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002031 if (unlikely(!req->analysers &&
2032 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2033 (si_f->state >= SI_ST_EST) &&
2034 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002035 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002036 * attached to it. If any data are left in, we'll permit them to
2037 * move.
2038 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002039 channel_auto_read(req);
2040 channel_auto_connect(req);
2041 channel_auto_close(req);
2042 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002043
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002044 /* We'll let data flow between the producer (if still connected)
2045 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002046 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002047 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8bf242b2016-05-04 14:05:58 +02002048 channel_forward_forever(req);
Willy Tarreau42529c32015-07-09 18:38:57 +02002049
2050 /* Just in order to support fetching HTTP contents after start
2051 * of forwarding when the HTTP forwarding analyser is not used,
2052 * we simply reset msg->sov so that HTTP rewinding points to the
2053 * headers.
2054 */
2055 if (s->txn)
2056 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - req->buf->o;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002057 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002058
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002059 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002060 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2061 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002062 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002063 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2064 (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 +01002065 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002066 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2067 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002068 (req->flags & CF_STREAMER_FAST)))) {
2069 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002070 }
2071
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002072 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002073 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002074
2075 /*
2076 * Now forward all shutdown requests between both sides of the buffer
2077 */
2078
Willy Tarreau520d95e2009-09-19 21:04:57 +02002079 /* first, let's check if the request buffer needs to shutdown(write), which may
2080 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002081 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2082 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002083 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002084 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002085 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002086 channel_shutw_now(req);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002087 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002088
2089 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002090 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2091 channel_is_empty(req))) {
2092 if (req->flags & CF_READ_ERROR)
2093 si_b->flags |= SI_FL_NOLINGER;
2094 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002095 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002096 res->rto = s->be->timeout.serverfin;
2097 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002098 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002099 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002100
2101 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002102 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2103 !req->analysers))
2104 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002105
2106 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002107 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2108 if (si_f->flags & SI_FL_NOHALF)
2109 si_f->flags |= SI_FL_NOLINGER;
2110 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002111 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002112
Willy Tarreau520d95e2009-09-19 21:04:57 +02002113 /* it's possible that an upper layer has requested a connection setup or abort.
2114 * There are 2 situations where we decide to establish a new connection :
2115 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002116 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002117 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002118 if (si_b->state == SI_ST_INI) {
2119 if (!(req->flags & CF_SHUTW)) {
2120 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002121 /* If we have an appctx, there is no connect method, so we
2122 * immediately switch to the connected state, otherwise we
2123 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002124 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002125 si_b->state = SI_ST_REQ; /* new connection requested */
2126 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002127 }
Willy Tarreau73201222009-08-16 18:27:24 +02002128 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002129 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002130 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2131 channel_shutw_now(req); /* fix buffer flags upon abort */
2132 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002133 }
Willy Tarreau92795622009-03-06 12:51:23 +01002134 }
2135
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002136
2137 /* we may have a pending connection request, or a connection waiting
2138 * for completion.
2139 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002140 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002141
2142 /* prune the request variables and swap to the response variables. */
2143 if (s->vars_reqres.scope != SCOPE_RES) {
Willy Tarreau6204cd92016-03-10 16:33:04 +01002144 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002145 vars_init(&s->vars_reqres, SCOPE_RES);
2146 }
2147
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002148 do {
2149 /* nb: step 1 might switch from QUE to ASS, but we first want
2150 * to give a chance to step 2 to perform a redirect if needed.
2151 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002152 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002153 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002154 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002155 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002156
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002157 /* applets directly go to the ESTABLISHED state. Similarly,
2158 * servers experience the same fate when their connection
2159 * is reused.
2160 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002161 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002162 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002163
Willy Tarreaub44c8732013-12-31 23:16:50 +01002164 /* Now we can add the server name to a header (if requested) */
2165 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002166 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002167 (s->be->server_id_hdr_name != NULL) &&
2168 (s->be->mode == PR_MODE_HTTP) &&
2169 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002170 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002171 }
2172
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002173 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002174 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002175 http_perform_server_redirect(s, si_b);
2176 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002177 }
2178
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002179 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002180 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002181 goto resync_stream_interface;
2182
Willy Tarreau815a9b22010-07-27 17:15:12 +02002183 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002184 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002185 goto resync_request;
2186
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002187 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002188
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002189 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002190 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002191 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002192 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002193 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002194 if (unlikely(!res->analysers &&
2195 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2196 (si_b->state >= SI_ST_EST) &&
2197 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002198 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002199 * attached to it. If any data are left in, we'll permit them to
2200 * move.
2201 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002202 channel_auto_read(res);
2203 channel_auto_close(res);
2204 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002205
2206 /* We'll let data flow between the producer (if still connected)
2207 * to the consumer.
2208 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002209 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8bf242b2016-05-04 14:05:58 +02002210 channel_forward_forever(res);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002211
Willy Tarreau42529c32015-07-09 18:38:57 +02002212 /* Just in order to support fetching HTTP contents after start
2213 * of forwarding when the HTTP forwarding analyser is not used,
2214 * we simply reset msg->sov so that HTTP rewinding points to the
2215 * headers.
2216 */
2217 if (s->txn)
2218 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2219
Willy Tarreauce887fd2012-05-12 12:50:00 +02002220 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002221 * tunnel timeout set, use it now. Note that we must respect
2222 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002223 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002224 if (!req->analysers && s->be->timeout.tunnel) {
2225 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002226 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002227
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002228 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2229 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002230 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2231 res->rto = s->be->timeout.serverfin;
2232 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2233 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002234 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2235 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002236
Willy Tarreau8f128b42014-11-28 15:07:47 +01002237 req->rex = tick_add(now_ms, req->rto);
2238 req->wex = tick_add(now_ms, req->wto);
2239 res->rex = tick_add(now_ms, res->rto);
2240 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002241 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002242 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002243
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002244 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002245 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2246 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002247 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002248 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2249 (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 +01002250 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002251 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2252 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002253 (res->flags & CF_STREAMER_FAST)))) {
2254 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002255 }
2256
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002257 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002258 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002259
2260 /*
2261 * Now forward all shutdown requests between both sides of the buffer
2262 */
2263
2264 /*
2265 * FIXME: this is probably where we should produce error responses.
2266 */
2267
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002268 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002269 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002270 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002271 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002272 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002273
2274 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002275 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2276 channel_is_empty(res))) {
2277 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002278 if (tick_isset(sess->fe->timeout.clientfin)) {
2279 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002280 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002281 }
2282 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002283
2284 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002285 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2286 !res->analysers)
2287 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002288
2289 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002290 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2291 if (si_b->flags & SI_FL_NOHALF)
2292 si_b->flags |= SI_FL_NOLINGER;
2293 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002294 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002295
Willy Tarreau8f128b42014-11-28 15:07:47 +01002296 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002297 goto resync_stream_interface;
2298
Willy Tarreau8f128b42014-11-28 15:07:47 +01002299 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002300 goto resync_request;
2301
Willy Tarreau8f128b42014-11-28 15:07:47 +01002302 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002303 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002304
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002305 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002306 si_f->flags &= ~SI_FL_DONT_WAKE;
2307 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002308
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002309 /* This is needed only when debugging is enabled, to indicate
2310 * client-side or server-side close. Please note that in the unlikely
2311 * event where both sides would close at once, the sequence is reported
2312 * on the server side first.
2313 */
2314 if (unlikely((global.mode & MODE_DEBUG) &&
2315 (!(global.mode & MODE_QUIET) ||
2316 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002317 if (si_b->state == SI_ST_CLO &&
2318 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002319 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002320 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002321 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2322 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002323 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002324 }
2325
Willy Tarreau8f128b42014-11-28 15:07:47 +01002326 if (si_f->state == SI_ST_CLO &&
2327 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002328 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002329 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002330 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2331 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002332 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002333 }
2334 }
2335
Willy Tarreau8f128b42014-11-28 15:07:47 +01002336 if (likely((si_f->state != SI_ST_CLO) ||
2337 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002338
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002339 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002340 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002341
Willy Tarreau563cc372015-04-19 18:13:56 +02002342 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002343 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002344
Willy Tarreau563cc372015-04-19 18:13:56 +02002345 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002346 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002347
Willy Tarreau8f128b42014-11-28 15:07:47 +01002348 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2349 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2350 si_f->prev_state = si_f->state;
2351 si_b->prev_state = si_b->state;
2352 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2353 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002354
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002355 /* Trick: if a request is being waiting for the server to respond,
2356 * and if we know the server can timeout, we don't want the timeout
2357 * to expire on the client side first, but we're still interested
2358 * in passing data from the client to the server (eg: POST). Thus,
2359 * we can cancel the client's request timeout if the server's
2360 * request timeout is set and the server has not yet sent a response.
2361 */
2362
Willy Tarreau8f128b42014-11-28 15:07:47 +01002363 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2364 (tick_isset(req->wex) || tick_isset(res->rex))) {
2365 req->flags |= CF_READ_NOEXP;
2366 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002367 }
2368
Willy Tarreau798f4322012-11-08 14:49:17 +01002369 update_exp_and_leave:
Willy Tarreau5fb04712016-05-04 10:18:37 +02002370 /* Note: please ensure that if you branch here you disable SI_FL_DONT_WAKE */
Christopher Fauleta00d8172016-11-10 14:58:05 +01002371 t->expire = tick_first((tick_is_expired(t->expire, now_ms) ? 0 : t->expire),
2372 tick_first(tick_first(req->rex, req->wex),
2373 tick_first(res->rex, res->wex)));
Willy Tarreaudef0d222016-11-08 22:03:00 +01002374 if (!req->analysers)
2375 req->analyse_exp = TICK_ETERNITY;
2376
2377 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED) &&
2378 (!tick_isset(req->analyse_exp) || tick_is_expired(req->analyse_exp, now_ms)))
2379 req->analyse_exp = tick_add(now_ms, 5000);
2380
2381 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002382
Willy Tarreau8f128b42014-11-28 15:07:47 +01002383 if (si_f->exp)
2384 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002385
Willy Tarreau8f128b42014-11-28 15:07:47 +01002386 if (si_b->exp)
2387 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002388
2389#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002390 fprintf(stderr,
2391 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2392 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
Willy Tarreau8f128b42014-11-28 15:07:47 +01002393 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2394 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002395#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002396
2397#ifdef DEBUG_DEV
2398 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002399 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002400 ABORT_NOW();
2401#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002402 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002403 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002404 }
2405
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002406 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002407 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002408 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002409 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002410 if (sess->listener) {
2411 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002412 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002413 sess->listener->nbconn--;
2414 if (sess->listener->state == LI_FULL)
2415 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002416
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002417 /* Dequeues all of the listeners waiting for a resource */
2418 if (!LIST_ISEMPTY(&global_listener_queue))
2419 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002420
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002421 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2422 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2423 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002424 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002425
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002426 if (unlikely((global.mode & MODE_DEBUG) &&
2427 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002428 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002429 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002430 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2431 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002432 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002433 }
2434
2435 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002436 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002437
Willy Tarreaueee5b512015-04-03 23:46:31 +02002438 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002439 int n;
2440
Willy Tarreaueee5b512015-04-03 23:46:31 +02002441 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002442 if (n < 1 || n > 5)
2443 n = 0;
2444
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002445 if (sess->fe->mode == PR_MODE_HTTP) {
2446 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002447 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002448 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002449 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002450 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002451 s->be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002452 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002453 }
2454
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002455 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002456 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002457 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002458 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002459 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002460 }
2461
Willy Tarreau87b09662015-04-03 00:22:06 +02002462 /* update time stats for this stream */
2463 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002464
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002465 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002466 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002467 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002468 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002469 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002470}
2471
Willy Tarreau87b09662015-04-03 00:22:06 +02002472/* Update the stream's backend and server time stats */
2473void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002474{
2475 int t_request;
2476 int t_queue;
2477 int t_connect;
2478 int t_data;
2479 int t_close;
2480 struct server *srv;
2481
2482 t_request = 0;
2483 t_queue = s->logs.t_queue;
2484 t_connect = s->logs.t_connect;
2485 t_close = s->logs.t_close;
2486 t_data = s->logs.t_data;
2487
2488 if (s->be->mode != PR_MODE_HTTP)
2489 t_data = t_connect;
2490
2491 if (t_connect < 0 || t_data < 0)
2492 return;
2493
2494 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2495 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2496
2497 t_data -= t_connect;
2498 t_connect -= t_queue;
2499 t_queue -= t_request;
2500
2501 srv = objt_server(s->target);
2502 if (srv) {
2503 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2504 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2505 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2506 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2507 }
2508 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2509 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2510 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2511 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2512}
2513
Willy Tarreau7c669d72008-06-20 15:04:11 +02002514/*
2515 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002516 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002517 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002518 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002519 * server.
2520 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002521void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002522{
2523 if (sess->srv_conn == newsrv)
2524 return;
2525
2526 if (sess->srv_conn) {
2527 sess->srv_conn->served--;
Andrew Rodlande168feb2016-10-25 12:48:17 -04002528 sess->srv_conn->proxy->served--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002529 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2530 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002531 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002532 }
2533
2534 if (newsrv) {
2535 newsrv->served++;
Andrew Rodlande168feb2016-10-25 12:48:17 -04002536 newsrv->proxy->served++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002537 if (newsrv->proxy->lbprm.server_take_conn)
2538 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002539 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002540 }
2541}
2542
Willy Tarreau84455332009-03-15 22:34:05 +01002543/* Handle server-side errors for default protocols. It is called whenever a a
2544 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002545 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002546 * them. It's installed as ->srv_error for the server-side stream_interface.
2547 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002548void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002549{
2550 int err_type = si->err_type;
2551 int err = 0, fin = 0;
2552
2553 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002554 err = SF_ERR_CLICL;
2555 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002556 }
2557 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002558 err = SF_ERR_CLICL;
2559 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002560 }
2561 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002562 err = SF_ERR_SRVTO;
2563 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002564 }
2565 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002566 err = SF_ERR_SRVCL;
2567 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002568 }
2569 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002570 err = SF_ERR_SRVTO;
2571 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002572 }
2573 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002574 err = SF_ERR_SRVCL;
2575 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002576 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002577 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002578 err = SF_ERR_RESOURCE;
2579 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002580 }
Willy Tarreau84455332009-03-15 22:34:05 +01002581 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002582 err = SF_ERR_INTERNAL;
2583 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002584 }
2585
Willy Tarreaue7dff022015-04-03 01:14:29 +02002586 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002587 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002588 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002589 s->flags |= fin;
2590}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002591
Willy Tarreaue7dff022015-04-03 01:14:29 +02002592/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002593void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002594{
Willy Tarreau87b09662015-04-03 00:22:06 +02002595 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002596 return;
2597
Willy Tarreau87b09662015-04-03 00:22:06 +02002598 channel_shutw_now(&stream->req);
2599 channel_shutr_now(&stream->res);
2600 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002601 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002602 stream->flags |= why;
2603 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002604}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002605
Willy Tarreau8b22a712010-06-18 17:46:06 +02002606/************************************************************************/
2607/* All supported ACL keywords must be declared here. */
2608/************************************************************************/
2609
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002610/* Returns a pointer to a stkctr depending on the fetch keyword name.
2611 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002612 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002613 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002614 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002615 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002616 * NULL may be returned if the designated stkctr is not tracked. For
2617 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2618 * passed. When present, the currently tracked key is then looked up
2619 * in the specified table instead of the current table. The purpose is
2620 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002621 * multiple tables). <strm> is allowed to be NULL, in which case only
2622 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002623 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002624struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002625smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002626{
2627 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002628 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002629 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002630 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002631 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002632
Willy Tarreau0f791d42013-07-23 19:56:43 +02002633 if (num == '_' - '0') {
2634 /* sc_* variant, args[0] = ctr# (mandatory) */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002635 num = args[arg++].data.sint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002636 if (num >= MAX_SESS_STKCTR)
2637 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002638 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002639 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002640 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002641 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002642 struct sample smp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002643
2644 if (!conn)
2645 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002646
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002647 /* Fetch source adress in a sample. */
2648 smp.px = NULL;
2649 smp.sess = sess;
2650 smp.strm = strm;
2651 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2652 return NULL;
2653
2654 /* Converts into key. */
2655 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002656 if (!key)
2657 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002658
Willy Tarreaua65536c2013-07-22 22:40:11 +02002659 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002660 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002661 return &stkctr;
2662 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002663
2664 /* Here, <num> contains the counter number from 0 to 9 for
2665 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002666 * args[arg] is the first optional argument. We first lookup the
2667 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002668 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002669
Thierry Fournier40e1d512016-03-29 21:27:36 +02002670 if (strm)
2671 stkptr = &strm->stkctr[num];
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002672 if (!strm || !stkctr_entry(stkptr)) {
2673 stkptr = &sess->stkctr[num];
2674 if (!stkctr_entry(stkptr))
2675 return NULL;
2676 }
2677
2678 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002679 if (!stksess)
2680 return NULL;
2681
Willy Tarreau0f791d42013-07-23 19:56:43 +02002682 if (unlikely(args[arg].type == ARGT_TAB)) {
2683 /* an alternate table was specified, let's look up the same key there */
2684 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002685 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002686 return &stkctr;
2687 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002688 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002689}
2690
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002691/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2692 * the entry if it doesn't exist yet. This is needed for a few fetch
2693 * functions which need to create an entry, such as src_inc_gpc* and
2694 * src_clr_gpc*.
2695 */
2696struct stkctr *
2697smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
2698{
2699 static struct stkctr stkctr;
2700 struct stktable_key *key;
2701 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002702 struct sample smp;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002703
2704 if (strncmp(kw, "src_", 4) != 0)
2705 return NULL;
2706
2707 if (!conn)
2708 return NULL;
2709
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002710 /* Fetch source adress in a sample. */
2711 smp.px = NULL;
2712 smp.sess = sess;
2713 smp.strm = strm;
2714 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2715 return NULL;
2716
2717 /* Converts into key. */
2718 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002719 if (!key)
2720 return NULL;
2721
2722 stkctr.table = &args->data.prx->table;
2723 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
2724 return &stkctr;
2725}
2726
Willy Tarreau87b09662015-04-03 00:22:06 +02002727/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002728 * currently being tracked or not.
2729 * Supports being called as "sc[0-9]_tracked" only.
2730 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002731static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002732smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002733{
2734 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002735 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002736 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002737 return 1;
2738}
2739
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002740/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2741 * frontend counters or from the src.
2742 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2743 * zero is returned if the key is new.
2744 */
2745static int
2746smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2747{
Willy Tarreaube508f12016-03-10 11:47:01 +01002748 struct stkctr *stkctr;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002749
Willy Tarreaube508f12016-03-10 11:47:01 +01002750 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002751 if (!stkctr)
2752 return 0;
2753
2754 smp->flags = SMP_F_VOL_TEST;
2755 smp->data.type = SMP_T_SINT;
2756 smp->data.u.sint = 0;
2757
2758 if (stkctr_entry(stkctr) != NULL) {
2759 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2760 if (!ptr)
2761 return 0; /* parameter not stored */
2762 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
2763 }
2764 return 1;
2765}
2766
Willy Tarreau87b09662015-04-03 00:22:06 +02002767/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002768 * frontend counters or from the src.
2769 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2770 * zero is returned if the key is new.
2771 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002772static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002773smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002774{
Willy Tarreaube508f12016-03-10 11:47:01 +01002775 struct stkctr *stkctr;
2776
Willy Tarreaube508f12016-03-10 11:47:01 +01002777 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002778 if (!stkctr)
2779 return 0;
2780
Willy Tarreau37406352012-04-23 16:16:37 +02002781 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002782 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002783 smp->data.u.sint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002784
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002785 if (stkctr_entry(stkctr) != NULL) {
2786 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002787 if (!ptr)
2788 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002789 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002790 }
2791 return 1;
2792}
2793
Willy Tarreau87b09662015-04-03 00:22:06 +02002794/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002795 * tracked frontend counters or from the src.
2796 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2797 * Value zero is returned if the key is new.
2798 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002799static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002800smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002801{
Willy Tarreaube508f12016-03-10 11:47:01 +01002802 struct stkctr *stkctr;
2803
Willy Tarreaube508f12016-03-10 11:47:01 +01002804 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002805 if (!stkctr)
2806 return 0;
2807
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002808 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002809 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002810 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002811 if (stkctr_entry(stkctr) != NULL) {
2812 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002813 if (!ptr)
2814 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002815 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002816 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002817 }
2818 return 1;
2819}
2820
Willy Tarreau87b09662015-04-03 00:22:06 +02002821/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002822 * frontend counters and return it into temp integer.
2823 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002824 */
2825static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002826smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002827{
Willy Tarreaube508f12016-03-10 11:47:01 +01002828 struct stkctr *stkctr;
2829
Willy Tarreaube508f12016-03-10 11:47:01 +01002830 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002831 if (!stkctr)
2832 return 0;
2833
Willy Tarreau37406352012-04-23 16:16:37 +02002834 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002835 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002836 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002837
2838 if (stkctr_entry(stkctr) == NULL)
2839 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2840
Thierry Fournier6fc340f2016-06-06 18:28:05 +02002841 if (stkctr && stkctr_entry(stkctr)) {
Emeric Brun57056f02015-06-15 18:29:57 +02002842 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002843
2844 /* First, update gpc0_rate if it's tracked. Second, update its
2845 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2846 */
Emeric Brun57056f02015-06-15 18:29:57 +02002847 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2848 if (ptr1) {
2849 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002850 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002851 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002852 }
2853
Emeric Brun57056f02015-06-15 18:29:57 +02002854 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2855 if (ptr2)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002856 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002857
Emeric Brun57056f02015-06-15 18:29:57 +02002858 /* If data was modified, we need to touch to re-schedule sync */
2859 if (ptr1 || ptr2)
2860 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002861 }
2862 return 1;
2863}
2864
Willy Tarreau87b09662015-04-03 00:22:06 +02002865/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002866 * frontend counters and return its previous value into temp integer.
2867 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002868 */
2869static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002870smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002871{
Willy Tarreaube508f12016-03-10 11:47:01 +01002872 struct stkctr *stkctr;
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002873
Willy Tarreaube508f12016-03-10 11:47:01 +01002874 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002875 if (!stkctr)
2876 return 0;
2877
Willy Tarreau37406352012-04-23 16:16:37 +02002878 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002879 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002880 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002881
2882 if (stkctr_entry(stkctr) == NULL)
2883 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2884
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002885 if (stkctr_entry(stkctr) != NULL) {
2886 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002887 if (!ptr)
2888 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002889 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002890 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002891 /* If data was modified, we need to touch to re-schedule sync */
2892 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002893 }
2894 return 1;
2895}
2896
Willy Tarreau87b09662015-04-03 00:22:06 +02002897/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002898 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2899 * "src_conn_cnt" only.
2900 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002901static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002902smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002903{
Willy Tarreaube508f12016-03-10 11:47:01 +01002904 struct stkctr *stkctr;
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002905
Willy Tarreaube508f12016-03-10 11:47:01 +01002906 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002907 if (!stkctr)
2908 return 0;
2909
Willy Tarreau37406352012-04-23 16:16:37 +02002910 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002911 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002912 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002913 if (stkctr_entry(stkctr) != NULL) {
2914 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002915 if (!ptr)
2916 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002917 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002918 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002919 return 1;
2920}
2921
Willy Tarreau87b09662015-04-03 00:22:06 +02002922/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002923 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2924 * only.
2925 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002926static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002927smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002928{
Willy Tarreaube508f12016-03-10 11:47:01 +01002929 struct stkctr *stkctr;
2930
Willy Tarreaube508f12016-03-10 11:47:01 +01002931 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002932 if (!stkctr)
2933 return 0;
2934
Willy Tarreau37406352012-04-23 16:16:37 +02002935 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002936 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002937 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002938 if (stkctr_entry(stkctr) != NULL) {
2939 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002940 if (!ptr)
2941 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002942 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002943 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002944 }
2945 return 1;
2946}
2947
Willy Tarreau87b09662015-04-03 00:22:06 +02002948/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002949 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002950 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002951 */
2952static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002953smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002954{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002955 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002956 struct stksess *ts;
2957 struct stktable_key *key;
2958 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002959 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002960
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002961 if (!conn)
2962 return 0;
2963
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002964 /* Fetch source adress in a sample. */
2965 if (!smp_fetch_src(NULL, smp, NULL, NULL))
2966 return 0;
2967
2968 /* Converts into key. */
2969 key = smp_to_stkey(smp, &args->data.prx->table);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002970 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002971 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002972
Willy Tarreau24e32d82012-04-23 23:55:44 +02002973 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002974
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002975 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2976 /* entry does not exist and could not be created */
2977 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002978
2979 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2980 if (!ptr)
2981 return 0; /* parameter not stored in this table */
2982
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002983 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002984 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02002985 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02002986 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002987 return 1;
2988}
2989
Willy Tarreau87b09662015-04-03 00:22:06 +02002990/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002991 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2992 * "src_conn_cur" only.
2993 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002994static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002995smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002996{
Willy Tarreaube508f12016-03-10 11:47:01 +01002997 struct stkctr *stkctr;
2998
Willy Tarreaube508f12016-03-10 11:47:01 +01002999 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02003000 if (!stkctr)
3001 return 0;
3002
Willy Tarreau37406352012-04-23 16:16:37 +02003003 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003004 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003005 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003006 if (stkctr_entry(stkctr) != NULL) {
3007 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003008 if (!ptr)
3009 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003010 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003011 }
3012 return 1;
3013}
3014
Willy Tarreau87b09662015-04-03 00:22:06 +02003015/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02003016 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3017 * "src_sess_cnt" only.
3018 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003019static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003020smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003021{
Willy Tarreaube508f12016-03-10 11:47:01 +01003022 struct stkctr *stkctr;
3023
Willy Tarreaube508f12016-03-10 11:47:01 +01003024 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02003025 if (!stkctr)
3026 return 0;
3027
Willy Tarreau37406352012-04-23 16:16:37 +02003028 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003029 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003030 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003031 if (stkctr_entry(stkctr) != NULL) {
3032 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003033 if (!ptr)
3034 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003035 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003036 }
3037 return 1;
3038}
3039
Willy Tarreau87b09662015-04-03 00:22:06 +02003040/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003041 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3042 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003043static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003044smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003045{
Willy Tarreaube508f12016-03-10 11:47:01 +01003046 struct stkctr *stkctr;
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003047
Willy Tarreaube508f12016-03-10 11:47:01 +01003048 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003049 if (!stkctr)
3050 return 0;
3051
Willy Tarreau37406352012-04-23 16:16:37 +02003052 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003053 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003054 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003055 if (stkctr_entry(stkctr) != NULL) {
3056 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003057 if (!ptr)
3058 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003059 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003060 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003061 }
3062 return 1;
3063}
3064
Willy Tarreau87b09662015-04-03 00:22:06 +02003065/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02003066 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3067 * "src_http_req_cnt" only.
3068 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003069static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003070smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003071{
Willy Tarreaube508f12016-03-10 11:47:01 +01003072 struct stkctr *stkctr;
Willy Tarreau91200da2013-07-23 15:55:19 +02003073
Willy Tarreaube508f12016-03-10 11:47:01 +01003074 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02003075 if (!stkctr)
3076 return 0;
3077
Willy Tarreau37406352012-04-23 16:16:37 +02003078 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003079 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003080 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003081 if (stkctr_entry(stkctr) != NULL) {
3082 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003083 if (!ptr)
3084 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003085 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003086 }
3087 return 1;
3088}
3089
Willy Tarreau87b09662015-04-03 00:22:06 +02003090/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02003091 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3092 * "src_http_req_rate" only.
3093 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003094static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003095smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003096{
Willy Tarreaube508f12016-03-10 11:47:01 +01003097 struct stkctr *stkctr;
3098
Willy Tarreaube508f12016-03-10 11:47:01 +01003099 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02003100 if (!stkctr)
3101 return 0;
3102
Willy Tarreau37406352012-04-23 16:16:37 +02003103 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003104 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003105 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003106 if (stkctr_entry(stkctr) != NULL) {
3107 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003108 if (!ptr)
3109 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003110 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003111 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003112 }
3113 return 1;
3114}
3115
Willy Tarreau87b09662015-04-03 00:22:06 +02003116/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003117 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3118 * "src_http_err_cnt" only.
3119 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003120static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003121smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003122{
Willy Tarreaube508f12016-03-10 11:47:01 +01003123 struct stkctr *stkctr;
3124
Willy Tarreaube508f12016-03-10 11:47:01 +01003125 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003126 if (!stkctr)
3127 return 0;
3128
Willy Tarreau37406352012-04-23 16:16:37 +02003129 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003130 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003131 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003132 if (stkctr_entry(stkctr) != NULL) {
3133 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003134 if (!ptr)
3135 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003136 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003137 }
3138 return 1;
3139}
3140
Willy Tarreau87b09662015-04-03 00:22:06 +02003141/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003142 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3143 * "src_http_err_rate" only.
3144 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003145static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003146smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003147{
Willy Tarreaube508f12016-03-10 11:47:01 +01003148 struct stkctr *stkctr;
3149
Willy Tarreaube508f12016-03-10 11:47:01 +01003150 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003151 if (!stkctr)
3152 return 0;
3153
Willy Tarreau37406352012-04-23 16:16:37 +02003154 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003155 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003156 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003157 if (stkctr_entry(stkctr) != NULL) {
3158 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003159 if (!ptr)
3160 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003161 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003162 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003163 }
3164 return 1;
3165}
3166
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003167/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003168 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003169 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3170 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003171static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003172smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003173{
Willy Tarreaube508f12016-03-10 11:47:01 +01003174 struct stkctr *stkctr;
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003175
Willy Tarreaube508f12016-03-10 11:47:01 +01003176 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003177 if (!stkctr)
3178 return 0;
3179
Willy Tarreau37406352012-04-23 16:16:37 +02003180 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003181 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003182 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003183 if (stkctr_entry(stkctr) != NULL) {
3184 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003185 if (!ptr)
3186 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003187 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003188 }
3189 return 1;
3190}
3191
Willy Tarreau613fe992013-07-23 17:39:19 +02003192/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003193 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003194 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003195 */
3196static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003197smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003198{
Willy Tarreaube508f12016-03-10 11:47:01 +01003199 struct stkctr *stkctr;
Willy Tarreau613fe992013-07-23 17:39:19 +02003200
Willy Tarreaube508f12016-03-10 11:47:01 +01003201 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003202 if (!stkctr)
3203 return 0;
3204
Willy Tarreau37406352012-04-23 16:16:37 +02003205 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003206 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003207 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003208 if (stkctr_entry(stkctr) != NULL) {
3209 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003210 if (!ptr)
3211 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003212 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003213 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003214 }
3215 return 1;
3216}
3217
Willy Tarreau53aea102013-07-23 17:39:02 +02003218/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003219 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003220 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3221 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003222static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003223smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003224{
Willy Tarreaube508f12016-03-10 11:47:01 +01003225 struct stkctr *stkctr;
3226
Willy Tarreaube508f12016-03-10 11:47:01 +01003227 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003228 if (!stkctr)
3229 return 0;
3230
Willy Tarreau37406352012-04-23 16:16:37 +02003231 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003232 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003233 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003234 if (stkctr_entry(stkctr) != NULL) {
3235 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003236 if (!ptr)
3237 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003238 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003239 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003240 return 1;
3241}
3242
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003243/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003244 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003245 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003246 */
3247static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003248smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003249{
Willy Tarreaube508f12016-03-10 11:47:01 +01003250 struct stkctr *stkctr;
3251
Willy Tarreaube508f12016-03-10 11:47:01 +01003252 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003253 if (!stkctr)
3254 return 0;
3255
Willy Tarreau37406352012-04-23 16:16:37 +02003256 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003257 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003258 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003259 if (stkctr_entry(stkctr) != NULL) {
3260 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003261 if (!ptr)
3262 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003263 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003264 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003265 }
3266 return 1;
3267}
3268
Willy Tarreau87b09662015-04-03 00:22:06 +02003269/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003270 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3271 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003272static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003273smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003274{
Willy Tarreaube508f12016-03-10 11:47:01 +01003275 struct stkctr *stkctr;
3276
Willy Tarreaube508f12016-03-10 11:47:01 +01003277 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau563eef42013-07-23 18:32:02 +02003278 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003279 return 0;
3280
Willy Tarreau563eef42013-07-23 18:32:02 +02003281 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003282 smp->data.type = SMP_T_SINT;
Willy Tarreau4d03ef72016-08-14 12:02:55 +02003283 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
Willy Tarreau563eef42013-07-23 18:32:02 +02003284 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003285}
3286
Willy Tarreau34db1082012-04-19 17:16:54 +02003287/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003288 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003289 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003290static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003291smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003292{
Willy Tarreau37406352012-04-23 16:16:37 +02003293 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003294 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003295 smp->data.u.sint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003296 return 1;
3297}
3298
Willy Tarreau34db1082012-04-19 17:16:54 +02003299/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003300 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003301 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003302static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003303smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003304{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003305 struct proxy *px;
3306
Willy Tarreau24e32d82012-04-23 23:55:44 +02003307 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003308 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003309 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003310 smp->data.u.sint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003311 return 1;
3312}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003313
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003314/* 0=OK, <0=Alert, >0=Warning */
3315static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
3316 struct proxy *px, struct act_rule *rule,
3317 char **err)
3318{
3319 struct action_kw *kw;
3320
3321 /* Check if the service name exists. */
3322 if (*(args[*cur_arg]) == 0) {
3323 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01003324 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003325 }
3326
3327 /* lookup for keyword corresponding to a service. */
3328 kw = action_lookup(&service_keywords, args[*cur_arg]);
3329 if (!kw) {
3330 memprintf(err, "'%s' unknown service name.", args[1]);
3331 return ACT_RET_PRS_ERR;
3332 }
3333 (*cur_arg)++;
3334
3335 /* executes specific rule parser. */
3336 rule->kw = kw;
3337 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
3338 return ACT_RET_PRS_ERR;
3339
3340 /* Register processing function. */
3341 rule->action_ptr = process_use_service;
3342 rule->action = ACT_CUSTOM;
3343
3344 return ACT_RET_PRS_OK;
3345}
3346
3347void service_keywords_register(struct action_kw_list *kw_list)
3348{
3349 LIST_ADDQ(&service_keywords, &kw_list->list);
3350}
3351
3352/* main configuration keyword registration. */
3353static struct action_kw_list stream_tcp_keywords = { ILH, {
3354 { "use-service", stream_parse_use_service },
3355 { /* END */ }
3356}};
3357
3358static struct action_kw_list stream_http_keywords = { ILH, {
3359 { "use-service", stream_parse_use_service },
3360 { /* END */ }
3361}};
3362
Willy Tarreau61612d42012-04-19 18:42:05 +02003363/* Note: must not be declared <const> as its list will be overwritten.
3364 * Please take care of keeping this list alphabetically sorted.
3365 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003366static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003367 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003368}};
3369
Willy Tarreau281c7992013-01-08 01:23:27 +01003370/* Note: must not be declared <const> as its list will be overwritten.
3371 * Please take care of keeping this list alphabetically sorted.
3372 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003373static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003374 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3375 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3376 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3377 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3378 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3379 { "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 +02003380 { "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 +02003381 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3382 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3383 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3384 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3385 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3386 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3387 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3388 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3389 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3390 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3391 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3392 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3393 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003394 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3395 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3396 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3397 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3398 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3399 { "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 +02003400 { "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 +02003401 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3402 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3403 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3404 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3405 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3406 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3407 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3408 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3409 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3410 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3411 { "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 +02003412 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003413 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3414 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3415 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3416 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3417 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3418 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3419 { "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 +02003420 { "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 +02003421 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3422 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3423 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3424 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3425 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3426 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3427 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3428 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3429 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3430 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3431 { "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 +02003432 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003433 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3434 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3435 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3436 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3437 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3438 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3439 { "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 +02003440 { "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 +02003441 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3442 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3443 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3444 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3445 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3446 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3447 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3448 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3449 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3450 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3451 { "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 +02003452 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003453 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3454 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3455 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3456 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3457 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3458 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3459 { "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 +02003460 { "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 +02003461 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3462 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3463 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3464 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3465 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3466 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3467 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3468 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3469 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3470 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3471 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3472 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3473 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3474 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003475 { /* END */ },
3476}};
3477
Willy Tarreau8b22a712010-06-18 17:46:06 +02003478__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003479static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003480{
Willy Tarreau281c7992013-01-08 01:23:27 +01003481 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003482 acl_register_keywords(&acl_kws);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003483 tcp_req_cont_keywords_register(&stream_tcp_keywords);
3484 http_req_keywords_register(&stream_http_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003485}
3486
Willy Tarreaubaaee002006-06-26 02:48:02 +02003487/*
3488 * Local variables:
3489 * c-indent-level: 8
3490 * c-basic-offset: 8
3491 * End:
3492 */