blob: 0d987452b4f9d1b2f44982b312923fa9b111a90f [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>
William Lallemand4c5b4d52016-11-21 08:51:11 +010025#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020026#include <types/filters.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010027#include <types/global.h>
William Lallemand9ed62032016-11-21 17:49:11 +010028#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020030#include <proto/acl.h>
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020031#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020032#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010033#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020034#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010035#include <proto/checks.h>
William Lallemand4c5b4d52016-11-21 08:51:11 +010036#include <proto/cli.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020037#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010038#include <proto/stats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020039#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020040#include <proto/filters.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020041#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020042#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010043#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010044#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020045#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020046#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020047#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020048#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020049#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010050#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010051#include <proto/proto_http.h>
52#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020053#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010055#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020056#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010057#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010058#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010059#include <proto/task.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020060#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
Willy Tarreau87b09662015-04-03 00:22:06 +020062struct pool_head *pool2_stream;
63struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020064
Willy Tarreau87b09662015-04-03 00:22:06 +020065/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010066struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
67
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020068/* List of all use-service keywords. */
69static struct list service_keywords = LIST_HEAD_INIT(service_keywords);
70
Willy Tarreau9903f0e2015-04-04 18:50:31 +020071/* This function is called from the session handler which detects the end of
Willy Tarreau73b65ac2015-04-08 18:26:29 +020072 * handshake, in order to complete initialization of a valid stream. It must be
73 * called with a session (which may be embryonic). It returns the pointer to
74 * the newly created stream, or NULL in case of fatal error. The client-facing
75 * end point is assigned to <origin>, which must be valid. The task's context
76 * is set to the new stream, and its function is set to process_stream().
77 * Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +020078 */
Willy Tarreau73b65ac2015-04-08 18:26:29 +020079struct stream *stream_new(struct session *sess, struct task *t, enum obj_type *origin)
Willy Tarreau2542b532012-08-31 16:01:23 +020080{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020081 struct stream *s;
Willy Tarreau73b65ac2015-04-08 18:26:29 +020082 struct connection *conn = objt_conn(origin);
83 struct appctx *appctx = objt_appctx(origin);
Willy Tarreau2542b532012-08-31 16:01:23 +020084
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020085 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020086 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020087
88 /* minimum stream initialization required for an embryonic stream is
89 * fairly low. We need very little to execute L4 ACLs, then we need a
90 * task to make the client-side connection live on its own.
91 * - flags
92 * - stick-entry tracking
93 */
94 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020095 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020096 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020097 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
98 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +020099 /* This function is called just after the handshake, so the handshake duration is
100 * between the accept time and now.
101 */
102 s->logs.t_handshake = tv_ms_elapsed(&sess->tv_accept, &now);
103 s->logs.t_idle = -1;
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200104 tv_zero(&s->logs.tv_request);
105 s->logs.t_queue = -1;
106 s->logs.t_connect = -1;
107 s->logs.t_data = -1;
108 s->logs.t_close = 0;
109 s->logs.bytes_in = s->logs.bytes_out = 0;
110 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
111 s->logs.srv_queue_size = 0; /* we will get this number soon */
112
113 /* default logging function */
114 s->do_log = strm_log;
115
116 /* default error reporting function, may be changed by analysers */
117 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200118
119 /* Initialise the current rule list pointer to NULL. We are sure that
120 * any rulelist match the NULL pointer.
121 */
122 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200123 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200124
Willy Tarreaua68f7622015-09-21 17:48:24 +0200125 /* Copy SC counters for the stream. We don't touch refcounts because
126 * any reference we have is inherited from the session. Since the stream
127 * doesn't exist without the session, the session's existence guarantees
128 * we don't lose the entry. During the store operation, the stream won't
129 * touch these ones.
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200130 */
Thierry FOURNIERc8fdb982015-08-16 12:03:39 +0200131 memcpy(s->stkctr, sess->stkctr, sizeof(s->stkctr));
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200132
133 s->sess = sess;
134 s->si[0].flags = SI_FL_NONE;
135 s->si[1].flags = SI_FL_ISBACK;
136
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200137 s->uniq_id = global.req_count++;
138
Willy Tarreau87b09662015-04-03 00:22:06 +0200139 /* OK, we're keeping the stream, so let's properly initialize the stream */
140 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200141 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100142 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200143
Willy Tarreaue7dff022015-04-03 01:14:29 +0200144 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200145 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200146
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200147 s->task = t;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200148 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200149 t->context = s;
150 t->expire = TICK_ETERNITY;
151
Willy Tarreau87b09662015-04-03 00:22:06 +0200152 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200153 * This changes later when switching rules are executed or
154 * when the default backend is assigned.
155 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200156 s->be = sess->fe;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100157 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200158 s->req_cap = NULL;
159 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200160
Willy Tarreauebcd4842015-06-19 11:59:02 +0200161 /* Initialise all the variables contexts even if not used.
162 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200163 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200164 vars_init(&s->vars_txn, SCOPE_TXN);
165 vars_init(&s->vars_reqres, SCOPE_REQ);
166
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200167 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100168 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200169 si_set_state(&s->si[0], SI_ST_EST);
170
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200171 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200172 if (conn)
173 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200174 else if (appctx)
175 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200176
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200177 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200178 s->si[0].flags |= SI_FL_INDEP_STR;
179
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200180 /* pre-initialize the other side's stream interface to an INIT state. The
181 * callbacks will be initialized before attempting to connect.
182 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100183 si_reset(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200184
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200185 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200186 s->si[1].flags |= SI_FL_INDEP_STR;
187
Willy Tarreau87b09662015-04-03 00:22:06 +0200188 stream_init_srv_conn(s);
Willy Tarreaud1769b82015-04-06 00:25:48 +0200189 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200190 s->pend_pos = NULL;
191
192 /* init store persistence */
193 s->store_count = 0;
194
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100195 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100196 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200197 s->req.analysers = 0;
198 channel_auto_connect(&s->req); /* don't wait to establish connection */
199 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200200
201 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100202 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100203 s->req.rex = TICK_ETERNITY;
204 s->req.wex = TICK_ETERNITY;
205 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200206
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100207 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100208 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100209 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200210
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200211 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100212 s->req.flags |= CF_NEVER_WAIT;
213 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200214 }
215
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200216 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100217 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100218 s->res.rex = TICK_ETERNITY;
219 s->res.wex = TICK_ETERNITY;
220 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200221
Willy Tarreaueee5b512015-04-03 23:46:31 +0200222 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100223
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100224 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100225
Christopher Faulet92d36382015-11-05 13:35:03 +0100226 if (flt_stream_init(s) < 0 || flt_stream_start(s) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200227 goto out_fail_accept;
228
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200229 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200230 if (conn)
231 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200232 else if (appctx)
Willy Tarreaufe127932015-04-21 19:23:39 +0200233 si_applet_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200234
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200235 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200236 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200237
238 /* it is important not to call the wakeup function directly but to
239 * pass through task_wakeup(), because this one knows how to apply
240 * priorities to tasks.
241 */
242 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200243 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200244
245 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200246 out_fail_accept:
Christopher Faulet92d36382015-11-05 13:35:03 +0100247 flt_stream_release(s, 0);
Willy Tarreau3b246412014-11-25 17:10:33 +0100248 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200249 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200250 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200251}
252
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200254 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200256static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200257{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200258 struct session *sess = strm_sess(s);
259 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100260 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200261 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200262 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100263
Willy Tarreaubaaee002006-06-26 02:48:02 +0200264 if (s->pend_pos)
265 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100266
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100267 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200268 if (s->flags & SF_CURR_SESS) {
269 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100270 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100271 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100272 if (may_dequeue_tasks(objt_server(s->target), s->be))
273 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100274 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100275
Willy Tarreau7c669d72008-06-20 15:04:11 +0200276 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200277 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200278 * it should normally be only the same as the one above,
279 * so this should not happen in fact.
280 */
281 sess_change_server(s, NULL);
282 }
283
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100284 if (s->req.pipe)
285 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100286
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100287 if (s->res.pipe)
288 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100289
Willy Tarreaubf883e02014-11-25 21:10:35 +0100290 /* We may still be present in the buffer wait queue */
291 if (!LIST_ISEMPTY(&s->buffer_wait)) {
292 LIST_DEL(&s->buffer_wait);
293 LIST_INIT(&s->buffer_wait);
294 }
295
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100296 b_drop(&s->req.buf);
297 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100298 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200299 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200300
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100301 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200302 if (s->txn)
303 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100304
Willy Tarreau1e954912012-10-12 17:50:05 +0200305 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200306 if (cli_conn)
307 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200308
Willy Tarreaua4cda672010-06-06 18:28:49 +0200309 for (i = 0; i < s->store_count; i++) {
310 if (!s->store[i].ts)
311 continue;
312 stksess_free(s->store[i].table, s->store[i].ts);
313 s->store[i].ts = NULL;
314 }
315
Willy Tarreaueee5b512015-04-03 23:46:31 +0200316 if (s->txn) {
317 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
318 pool_free2(pool2_http_txn, s->txn);
319 s->txn = NULL;
320 }
321
Christopher Fauletd7c91962015-04-30 11:48:27 +0200322 flt_stream_stop(s);
Christopher Faulet92d36382015-11-05 13:35:03 +0100323 flt_stream_release(s, 0);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200324
Willy Tarreau92fb9832007-10-16 17:34:28 +0200325 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200326 pool_free2(fe->rsp_cap_pool, s->res_cap);
327 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100329
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200330 /* Cleanup all variable contexts. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100331 vars_prune(&s->vars_txn, s->sess, s);
332 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200333
Willy Tarreau87b09662015-04-03 00:22:06 +0200334 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200335
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100336 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100337 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200338 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100339 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100340 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100341 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200342 if (s->list.n != &streams)
343 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100344 bref->ref = s->list.n;
345 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100346 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200347 si_release_endpoint(&s->si[1]);
348 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200349
350 /* FIXME: for now we have a 1:1 relation between stream and session so
351 * the stream must free the session.
352 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200353 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200354 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200355
356 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200357 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200358 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200359 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200360 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200361 pool_flush2(pool2_requri);
362 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200363 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200364 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100365 pool_flush2(pool2_connection);
366 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200367 pool_flush2(fe->req_cap_pool);
368 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200369 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200370}
371
Willy Tarreau78955f42014-12-28 13:09:02 +0100372/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
373 * that it's not the last available buffer or it's the response buffer. Unless
374 * the buffer is the response buffer, an extra control is made so that we always
375 * keep <tune.buffers.reserved> buffers available after this allocation. To be
376 * called at the beginning of recv() callbacks to ensure that the required
377 * buffers are properly allocated. Returns 0 in case of failure, non-zero
378 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100379 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200380int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100381{
Willy Tarreau87b09662015-04-03 00:22:06 +0200382 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100383 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100384 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100385
Willy Tarreau78955f42014-12-28 13:09:02 +0100386 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100387 margin = global.tune.reserved_bufs;
388
Thierry FOURNIER27929fb2015-09-25 08:36:11 +0200389 s = chn_strm(chn);
Willy Tarreau78955f42014-12-28 13:09:02 +0100390
391 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100392 if (b)
393 return 1;
394
Willy Tarreaubf883e02014-11-25 21:10:35 +0100395 if (LIST_ISEMPTY(&s->buffer_wait))
396 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100397 return 0;
398}
399
Willy Tarreau87b09662015-04-03 00:22:06 +0200400/* Allocates a work buffer for stream <s>. It is meant to be called inside
401 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200402 * to work fine, which is the response buffer so that an error message may be
403 * built and returned. Response buffers may be allocated from the reserve, this
404 * is critical to ensure that a response may always flow and will never block a
405 * server from releasing a connection. Returns 0 in case of failure, non-zero
406 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100407 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200408int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100409{
Willy Tarreaubf883e02014-11-25 21:10:35 +0100410 if (!LIST_ISEMPTY(&s->buffer_wait)) {
411 LIST_DEL(&s->buffer_wait);
412 LIST_INIT(&s->buffer_wait);
413 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100414
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200415 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100416 return 1;
417
Willy Tarreaubf883e02014-11-25 21:10:35 +0100418 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100419 return 0;
420}
421
422/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100423 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200424 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100425 * a single buffer, so it makes sense to try to wake two up when two buffers
426 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100427 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200428void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100429{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100430 if (s->req.buf->size && buffer_empty(s->req.buf))
431 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100432
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100433 if (s->res.buf->size && buffer_empty(s->res.buf))
434 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100435
Willy Tarreaubf883e02014-11-25 21:10:35 +0100436 /* if we're certain to have at least 1 buffer available, and there is
437 * someone waiting, we can wake up a waiter and offer them.
438 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100439 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200440 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100441}
442
Willy Tarreau87b09662015-04-03 00:22:06 +0200443/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100444 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200445 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100446 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200447void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100448{
Willy Tarreau87b09662015-04-03 00:22:06 +0200449 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100450
451 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100452 if (rqlimit <= run_queue)
453 break;
454
Willy Tarreaubf883e02014-11-25 21:10:35 +0100455 if (sess->task->state & TASK_RUNNING)
456 continue;
457
458 LIST_DEL(&sess->buffer_wait);
459 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100460 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100461 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100462}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200463
464/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200465int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200466{
Willy Tarreau87b09662015-04-03 00:22:06 +0200467 LIST_INIT(&streams);
468 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
469 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200470}
471
Willy Tarreau87b09662015-04-03 00:22:06 +0200472void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100473{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200474 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100475 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200476 void *ptr1,*ptr2;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100477 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100478
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100479 bytes = s->req.total - s->logs.bytes_in;
480 s->logs.bytes_in = s->req.total;
481 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200482 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100483
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100484 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100485
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100486 if (objt_server(s->target))
487 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200488
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200489 if (sess->listener && sess->listener->counters)
490 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200491
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100492 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200493 struct stkctr *stkctr = &s->stkctr[i];
494
495 if (!stkctr_entry(stkctr)) {
496 stkctr = &sess->stkctr[i];
497 if (!stkctr_entry(stkctr))
498 continue;
499 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200500
Emeric Brun57056f02015-06-15 18:29:57 +0200501 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
502 if (ptr1)
503 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200504
Emeric Brun57056f02015-06-15 18:29:57 +0200505 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
506 if (ptr2)
507 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200508 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200509
510 /* If data was modified, we need to touch to re-schedule sync */
511 if (ptr1 || ptr2)
512 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100513 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100514 }
515
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100516 bytes = s->res.total - s->logs.bytes_out;
517 s->logs.bytes_out = s->res.total;
518 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200519 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100520
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100521 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100522
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100523 if (objt_server(s->target))
524 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200525
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200526 if (sess->listener && sess->listener->counters)
527 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200528
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100529 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200530 struct stkctr *stkctr = &s->stkctr[i];
531
532 if (!stkctr_entry(stkctr)) {
533 stkctr = &sess->stkctr[i];
534 if (!stkctr_entry(stkctr))
535 continue;
536 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200537
Emeric Brun57056f02015-06-15 18:29:57 +0200538 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
539 if (ptr1)
540 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200541
Emeric Brun57056f02015-06-15 18:29:57 +0200542 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
543 if (ptr2)
544 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200545 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200546
547 /* If data was modified, we need to touch to re-schedule sync */
548 if (ptr1 || ptr2)
549 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100550 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100551 }
552}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100554/* This function is called with (si->state == SI_ST_CON) meaning that a
555 * connection was attempted and that the file descriptor is already allocated.
556 * We must check for establishment, error and abort. Possible output states
557 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
558 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200559 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100560 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200561static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100562{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100563 struct stream_interface *si = &s->si[1];
564 struct channel *req = &s->req;
565 struct channel *rep = &s->res;
Willy Tarreau350135c2016-11-17 12:05:13 +0100566 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100567
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100568 /* If we got an error, or if nothing happened and the connection timed
569 * out, we must give up. The CER state handler will take care of retry
570 * attempts and error reports.
571 */
572 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100573 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100574 /* Some data were sent past the connection establishment,
575 * so we need to pretend we're established to log correctly
576 * and let later states handle the failure.
577 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100578 si->state = SI_ST_EST;
579 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100580 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100581 return 1;
582 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100583 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100584 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100585
Willy Tarreau350135c2016-11-17 12:05:13 +0100586 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100587
588 if (si->err_type)
589 return 0;
590
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100591 if (si->flags & SI_FL_ERR)
592 si->err_type = SI_ET_CONN_ERR;
593 else
594 si->err_type = SI_ET_CONN_TO;
595 return 0;
596 }
597
598 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100599 if (!(req->flags & CF_WRITE_PARTIAL) &&
600 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200601 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
602 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100603 s->be->options & PR_O_ABRT_CLOSE)))) {
604 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200605 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100606 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100607 if (s->srv_error)
608 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100609 return 1;
610 }
611
612 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200613 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100614 return 1;
615
616 /* OK, this means that a connection succeeded. The caller will be
617 * responsible for handling the transition from CON to EST.
618 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100619 si->state = SI_ST_EST;
620 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100621 return 1;
622}
623
624/* This function is called with (si->state == SI_ST_CER) meaning that a
625 * previous connection attempt has failed and that the file descriptor
626 * has already been released. Possible causes include asynchronous error
627 * notification and time out. Possible output states are SI_ST_CLO when
628 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
629 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
630 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
631 * marked as in error state. It returns 0.
632 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200633static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100635 struct stream_interface *si = &s->si[1];
636
Willy Tarreau87b09662015-04-03 00:22:06 +0200637 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100638 if (objt_server(s->target)) {
639 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100640
Willy Tarreaue7dff022015-04-03 01:14:29 +0200641 if (s->flags & SF_CURR_SESS) {
642 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100643 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100645 }
646
647 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200648 si->conn_retries--;
649 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100650 if (!si->err_type) {
651 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100652 }
653
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100654 if (objt_server(s->target))
655 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100656 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100657 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100658 if (may_dequeue_tasks(objt_server(s->target), s->be))
659 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100660
661 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200662 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100663 s->req.flags |= CF_WRITE_ERROR;
664 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100665
666 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100667 if (s->srv_error)
668 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100669 return 0;
670 }
671
672 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700673 * retry on another server. By default this redispatch occurs on the
674 * last retry, but if configured we allow redispatches to occur on
675 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200676 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100677 * bit to ignore any persistence cookie. We won't count a retry nor a
678 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200679 * If the connection is not persistent, the balancing algorithm is not
680 * determinist (round robin) and there is more than one active server,
681 * we accept to perform an immediate redispatch without waiting since
682 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100683 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200684 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700685 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
Willy Tarreau49008c12016-01-13 07:58:44 +0100686 ((__objt_server(s->target)->state < SRV_ST_RUNNING) ||
687 (((s->be->redispatch_after > 0) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700688 ((s->be->conn_retries - si->conn_retries) %
689 s->be->redispatch_after == 0)) ||
690 ((s->be->redispatch_after < 0) &&
691 ((s->be->conn_retries - si->conn_retries) %
692 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200693 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700694 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100695 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100696 if (may_dequeue_tasks(objt_server(s->target), s->be))
697 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100698
Willy Tarreaue7dff022015-04-03 01:14:29 +0200699 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100700 si->state = SI_ST_REQ;
701 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100702 if (objt_server(s->target))
703 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100704 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100705 si->state = SI_ST_ASS;
706 }
707
708 if (si->flags & SI_FL_ERR) {
709 /* The error was an asynchronous connection error, and we will
710 * likely have to retry connecting to the same server, most
711 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200712 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100713 */
714
Willy Tarreaub0290662014-06-13 17:04:44 +0200715 int delay = 1000;
716
717 if (s->be->timeout.connect && s->be->timeout.connect < delay)
718 delay = s->be->timeout.connect;
719
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100720 if (!si->err_type)
721 si->err_type = SI_ET_CONN_ERR;
722
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200723 /* only wait when we're retrying on the same server */
724 if (si->state == SI_ST_ASS ||
725 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
726 (s->be->srv_act <= 1)) {
727 si->state = SI_ST_TAR;
728 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
729 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100730 return 0;
731 }
732 return 0;
733}
734
735/*
736 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200737 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200738 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200740static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100741{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100742 struct stream_interface *si = &s->si[1];
743 struct channel *req = &s->req;
744 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100745
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100746 /* First, centralize the timers information */
747 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
748 si->exp = TICK_ETERNITY;
749
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100750 if (objt_server(s->target))
751 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100752
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100753 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100754 /* if the user wants to log as soon as possible, without counting
755 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200756 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100757 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100758 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100759 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100760 }
761 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100762 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100763 }
764
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200765 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Christopher Faulet309c6412015-12-02 09:57:32 +0100766
767 /* Be sure to filter response headers if the backend is an HTTP proxy
768 * and if there are filters attached to the stream. */
769 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
770 rep->analysers |= AN_FLT_HTTP_HDRS;
771
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200772 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200773 if (req->flags & CF_WAKE_CONNECT) {
774 req->flags |= CF_WAKE_ONCE;
775 req->flags &= ~CF_WAKE_CONNECT;
776 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200777 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200778 /* real connections have timeouts */
779 req->wto = s->be->timeout.server;
780 rep->rto = s->be->timeout.server;
781 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100782 req->wex = TICK_ETERNITY;
783}
784
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700785/* Check if the connection request is in such a state that it can be aborted. */
786static int check_req_may_abort(struct channel *req, struct stream *s)
787{
788 return ((req->flags & (CF_READ_ERROR)) ||
789 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
790 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE)));
791}
792
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100793/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
794 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100795 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
796 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
797 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100798 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200799static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100800{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100801 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100802 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100803 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100804
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100805 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 +0100806 now_ms, __FUNCTION__,
807 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400808 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100809 req->rex, s->res.wex,
810 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400811 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 +0100812
813 if (si->state == SI_ST_ASS) {
814 /* Server assigned to connection request, we have to try to connect now */
815 int conn_err;
816
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700817 /* Before we try to initiate the connection, see if the
818 * request may be aborted instead.
819 */
820 if (check_req_may_abort(req, s)) {
821 si->err_type |= SI_ET_CONN_ABRT;
822 goto abort_connection;
823 }
824
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100825 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100826 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100827
Willy Tarreaue7dff022015-04-03 01:14:29 +0200828 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100829 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100830 if (srv)
831 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500832 if (srv)
833 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100834 return;
835 }
836
837 /* We have received a synchronous error. We might have to
838 * abort, retry immediately or redispatch.
839 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200840 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100841 if (!si->err_type) {
842 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100843 }
844
Willy Tarreau827aee92011-03-10 16:55:02 +0100845 if (srv)
846 srv_inc_sess_ctr(srv);
847 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500848 srv_set_sess_last(srv);
849 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100850 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100851 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100852
Willy Tarreau87b09662015-04-03 00:22:06 +0200853 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100854 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100855 if (may_dequeue_tasks(srv, s->be))
856 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100857
858 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200859 si_shutr(si);
860 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100861 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100862
863 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
864
Willy Tarreau87b09662015-04-03 00:22:06 +0200865 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100866 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100867 if (s->srv_error)
868 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100869 return;
870 }
871
872 /* We are facing a retryable error, but we don't want to run a
873 * turn-around now, as the problem is likely a source port
874 * allocation problem, so we want to retry now.
875 */
876 si->state = SI_ST_CER;
877 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100878 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100879 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
880 return;
881 }
882 else if (si->state == SI_ST_QUE) {
883 /* connection request was queued, check for any update */
884 if (!s->pend_pos) {
885 /* The connection is not in the queue anymore. Either
886 * we have a server connection slot available and we
887 * go directly to the assigned state, or we need to
888 * load-balance first and go to the INI state.
889 */
890 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200891 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100892 si->state = SI_ST_REQ;
893 else {
894 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
895 si->state = SI_ST_ASS;
896 }
897 return;
898 }
899
900 /* Connection request still in queue... */
901 if (si->flags & SI_FL_EXP) {
902 /* ... and timeout expired */
903 si->exp = TICK_ETERNITY;
904 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100905 if (srv)
906 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100907 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200908 si_shutr(si);
909 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100910 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100911 if (!si->err_type)
912 si->err_type = SI_ET_QUEUE_TO;
913 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100914 if (s->srv_error)
915 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100916 return;
917 }
918
919 /* Connection remains in queue, check if we have to abort it */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700920 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100921 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100922 si->err_type |= SI_ET_QUEUE_ABRT;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700923 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100924 }
925
926 /* Nothing changed */
927 return;
928 }
929 else if (si->state == SI_ST_TAR) {
930 /* Connection request might be aborted */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700931 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100932 si->err_type |= SI_ET_CONN_ABRT;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700933 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100934 }
935
936 if (!(si->flags & SI_FL_EXP))
937 return; /* still in turn-around */
938
939 si->exp = TICK_ETERNITY;
940
Willy Tarreau87b09662015-04-03 00:22:06 +0200941 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100942 * marked "assigned".
943 * FIXME: Should we force a redispatch attempt when the server is down ?
944 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200945 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100946 si->state = SI_ST_ASS;
947 else
948 si->state = SI_ST_REQ;
949 return;
950 }
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700951 return;
952
953abort_connection:
954 /* give up */
955 si->exp = TICK_ETERNITY;
956 si_shutr(si);
957 si_shutw(si);
958 si->state = SI_ST_CLO;
959 if (s->srv_error)
960 s->srv_error(s, si);
961 return;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100962}
963
Willy Tarreau87b09662015-04-03 00:22:06 +0200964/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900965 * also counts a failed request if the server state has not reached the request
966 * stage.
967 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200968static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900969{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200970 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900971 if (s->si[1].state < SI_ST_REQ) {
972
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200973 strm_fe(s)->fe_counters.failed_req++;
Willy Tarreau2c1068c2015-09-23 12:21:21 +0200974 if (strm_li(s) && strm_li(s)->counters)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200975 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900976
Willy Tarreaue7dff022015-04-03 01:14:29 +0200977 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900978 }
979 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200980 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900981 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200982 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900983 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200984 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900985 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200986 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900987 }
988}
989
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100990/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100991 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
992 * a real connection to a server, indicating that a server has been assigned,
993 * or SI_ST_EST for a successful connection to an applet. It may also return
994 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100995 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200996static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100997{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100998 struct stream_interface *si = &s->si[1];
999
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001000 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 +01001001 now_ms, __FUNCTION__,
1002 s,
CJ Ess6eac32e2015-05-02 16:35:24 -04001003 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001004 s->req.rex, s->res.wex,
1005 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -04001006 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 +01001007
1008 if (si->state != SI_ST_REQ)
1009 return;
1010
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001011 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
1012 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001013 struct appctx *appctx = objt_appctx(si->end);
1014
1015 if (!appctx || appctx->applet != __objt_applet(s->target))
1016 appctx = stream_int_register_handler(si, objt_applet(s->target));
1017
1018 if (!appctx) {
1019 /* No more memory, let's immediately abort. Force the
1020 * error code to ignore the ERR_LOCAL which is not a
1021 * real error.
1022 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001023 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001024
1025 si_shutr(si);
1026 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001027 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001028 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001029 si->state = SI_ST_CLO;
1030 if (s->srv_error)
1031 s->srv_error(s, si);
1032 return;
1033 }
1034
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001035 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001036 si->state = SI_ST_EST;
1037 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001038 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001039 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001040 return;
1041 }
1042
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001043 /* Try to assign a server */
1044 if (srv_redispatch_connect(s) != 0) {
1045 /* We did not get a server. Either we queued the
1046 * connection request, or we encountered an error.
1047 */
1048 if (si->state == SI_ST_QUE)
1049 return;
1050
1051 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001052 si_shutr(si);
1053 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001054 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001055 if (!si->err_type)
1056 si->err_type = SI_ET_CONN_OTHER;
1057 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001058 if (s->srv_error)
1059 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001060 return;
1061 }
1062
1063 /* The server is assigned */
1064 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1065 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001066 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001067}
1068
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001069/* This function parses the use-service action ruleset. It executes
1070 * the associated ACL and set an applet as a stream or txn final node.
1071 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1072 * consistent state. It returns ACT_RET_STOP in succes case because
1073 * use-service must be a terminal action. Returns ACT_RET_YIELD
1074 * if the initialisation function require more data.
1075 */
1076enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1077 struct session *sess, struct stream *s, int flags)
1078
1079{
1080 struct appctx *appctx;
1081
1082 /* Initialises the applet if it is required. */
1083 if (flags & ACT_FLAG_FIRST) {
1084 /* Register applet. this function schedules the applet. */
1085 s->target = &rule->applet.obj_type;
1086 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target))))
1087 return ACT_RET_ERR;
1088
1089 /* Initialise the context. */
1090 appctx = si_appctx(&s->si[1]);
1091 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1092 appctx->rule = rule;
1093 }
1094 else
1095 appctx = si_appctx(&s->si[1]);
1096
1097 /* Stops the applet sheduling, in case of the init function miss
1098 * some data.
1099 */
1100 appctx_pause(appctx);
1101 si_applet_stop_get(&s->si[1]);
1102
1103 /* Call initialisation. */
1104 if (rule->applet.init)
1105 switch (rule->applet.init(appctx, px, s)) {
1106 case 0: return ACT_RET_ERR;
1107 case 1: break;
1108 default: return ACT_RET_YIELD;
1109 }
1110
1111 /* Now we can schedule the applet. */
1112 si_applet_cant_get(&s->si[1]);
1113 appctx_wakeup(appctx);
1114
1115 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1116 sess->fe->fe_counters.intercepted_req++;
1117
1118 /* The flag SF_ASSIGNED prevent from server assignment. */
1119 s->flags |= SF_ASSIGNED;
1120
1121 return ACT_RET_STOP;
1122}
1123
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001124/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001125 * if appropriate. The default_backend rule is also considered, then the
1126 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001127 * It returns 1 if the processing can continue on next analysers, or zero if it
1128 * either needs more data or wants to immediately abort the request.
1129 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001130static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001131{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001132 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001133 struct session *sess = s->sess;
1134 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001135
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001136 req->analysers &= ~an_bit;
1137 req->analyse_exp = TICK_ETERNITY;
1138
Willy Tarreau87b09662015-04-03 00:22:06 +02001139 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 +02001140 now_ms, __FUNCTION__,
1141 s,
1142 req,
1143 req->rex, req->wex,
1144 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001145 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001146 req->analysers);
1147
1148 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001149 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001150 struct switching_rule *rule;
1151
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001152 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001153 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001154
Willy Tarreauf51658d2014-04-23 01:21:56 +02001155 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001156 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001157 ret = acl_pass(ret);
1158 if (rule->cond->pol == ACL_COND_UNLESS)
1159 ret = !ret;
1160 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001161
1162 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001163 /* If the backend name is dynamic, try to resolve the name.
1164 * If we can't resolve the name, or if any error occurs, break
1165 * the loop and fallback to the default backend.
1166 */
1167 struct proxy *backend;
1168
1169 if (rule->dynamic) {
1170 struct chunk *tmp = get_trash_chunk();
1171 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1172 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001173 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001174 if (!backend)
1175 break;
1176 }
1177 else
1178 backend = rule->be.backend;
1179
Willy Tarreau87b09662015-04-03 00:22:06 +02001180 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001181 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001182 break;
1183 }
1184 }
1185
1186 /* To ensure correct connection accounting on the backend, we
1187 * have to assign one if it was not set (eg: a listen). This
1188 * measure also takes care of correctly setting the default
1189 * backend if any.
1190 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001191 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001192 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001193 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001194 }
1195
Willy Tarreaufb356202010-08-03 14:02:05 +02001196 /* 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 +02001197 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001198 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1199 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001200 s->req.analysers &= ~AN_FLT_START_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001201 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001202
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001203 /* 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 +02001204 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001205 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001206 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001207 int ret = 1;
1208
1209 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001210 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 +01001211 ret = acl_pass(ret);
1212 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1213 ret = !ret;
1214 }
1215
1216 if (ret) {
1217 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001218 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001219 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001220 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001221 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001222 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001223 break;
1224 }
1225 }
1226
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001227 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001228
1229 sw_failed:
1230 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001231 channel_abort(&s->req);
1232 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001233
Willy Tarreaue7dff022015-04-03 01:14:29 +02001234 if (!(s->flags & SF_ERR_MASK))
1235 s->flags |= SF_ERR_RESOURCE;
1236 if (!(s->flags & SF_FINST_MASK))
1237 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001238
Willy Tarreaueee5b512015-04-03 23:46:31 +02001239 if (s->txn)
1240 s->txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001241 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001242 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001243 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001244}
1245
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001246/* This stream analyser works on a request. It applies all use-server rules on
1247 * it then returns 1. The data must already be present in the buffer otherwise
1248 * they won't match. It always returns 1.
1249 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001250static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001251{
1252 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001253 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001254 struct server_rule *rule;
1255
Willy Tarreau87b09662015-04-03 00:22:06 +02001256 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 +02001257 now_ms, __FUNCTION__,
1258 s,
1259 req,
1260 req->rex, req->wex,
1261 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001262 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001263 req->analysers);
1264
Willy Tarreaue7dff022015-04-03 01:14:29 +02001265 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001266 list_for_each_entry(rule, &px->server_rules, list) {
1267 int ret;
1268
Willy Tarreau192252e2015-04-04 01:47:55 +02001269 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001270 ret = acl_pass(ret);
1271 if (rule->cond->pol == ACL_COND_UNLESS)
1272 ret = !ret;
1273
1274 if (ret) {
1275 struct server *srv = rule->srv.ptr;
1276
Willy Tarreau892337c2014-05-13 23:41:20 +02001277 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001278 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001279 (s->flags & SF_FORCE_PRST)) {
1280 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001281 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001282 break;
1283 }
1284 /* if the server is not UP, let's go on with next rules
1285 * just in case another one is suited.
1286 */
1287 }
1288 }
1289 }
1290
1291 req->analysers &= ~an_bit;
1292 req->analyse_exp = TICK_ETERNITY;
1293 return 1;
1294}
1295
Emeric Brun1d33b292010-01-04 15:47:17 +01001296/* This stream analyser works on a request. It applies all sticking rules on
1297 * it then returns 1. The data must already be present in the buffer otherwise
1298 * they won't match. It always returns 1.
1299 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001300static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001301{
1302 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001303 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001304 struct sticking_rule *rule;
1305
Willy Tarreau87b09662015-04-03 00:22:06 +02001306 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 +01001307 now_ms, __FUNCTION__,
1308 s,
1309 req,
1310 req->rex, req->wex,
1311 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001312 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001313 req->analysers);
1314
1315 list_for_each_entry(rule, &px->sticking_rules, list) {
1316 int ret = 1 ;
1317 int i;
1318
Willy Tarreau9667a802013-12-09 12:52:13 +01001319 /* Only the first stick store-request of each table is applied
1320 * and other ones are ignored. The purpose is to allow complex
1321 * configurations which look for multiple entries by decreasing
1322 * order of precision and to stop at the first which matches.
1323 * An example could be a store of the IP address from an HTTP
1324 * header first, then from the source if not found.
1325 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001326 for (i = 0; i < s->store_count; i++) {
1327 if (rule->table.t == s->store[i].table)
1328 break;
1329 }
1330
1331 if (i != s->store_count)
1332 continue;
1333
1334 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001335 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001336 ret = acl_pass(ret);
1337 if (rule->cond->pol == ACL_COND_UNLESS)
1338 ret = !ret;
1339 }
1340
1341 if (ret) {
1342 struct stktable_key *key;
1343
Willy Tarreau192252e2015-04-04 01:47:55 +02001344 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 +01001345 if (!key)
1346 continue;
1347
1348 if (rule->flags & STK_IS_MATCH) {
1349 struct stksess *ts;
1350
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001351 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001352 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001353 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001354 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001355
1356 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001357 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1358 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001359 if (node) {
1360 struct server *srv;
1361
1362 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001363 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001364 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001365 (s->flags & SF_FORCE_PRST)) {
1366 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001367 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001368 }
1369 }
1370 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001371 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001372 }
1373 }
1374 if (rule->flags & STK_IS_STORE) {
1375 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1376 struct stksess *ts;
1377
1378 ts = stksess_new(rule->table.t, key);
1379 if (ts) {
1380 s->store[s->store_count].table = rule->table.t;
1381 s->store[s->store_count++].ts = ts;
1382 }
1383 }
1384 }
1385 }
1386 }
1387
1388 req->analysers &= ~an_bit;
1389 req->analyse_exp = TICK_ETERNITY;
1390 return 1;
1391}
1392
1393/* This stream analyser works on a response. It applies all store rules on it
1394 * then returns 1. The data must already be present in the buffer otherwise
1395 * they won't match. It always returns 1.
1396 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001397static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001398{
1399 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001400 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001401 struct sticking_rule *rule;
1402 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001403 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001404
Willy Tarreau87b09662015-04-03 00:22:06 +02001405 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 +01001406 now_ms, __FUNCTION__,
1407 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001408 rep,
1409 rep->rex, rep->wex,
1410 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001411 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001412 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001413
1414 list_for_each_entry(rule, &px->storersp_rules, list) {
1415 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001416
Willy Tarreau9667a802013-12-09 12:52:13 +01001417 /* Only the first stick store-response of each table is applied
1418 * and other ones are ignored. The purpose is to allow complex
1419 * configurations which look for multiple entries by decreasing
1420 * order of precision and to stop at the first which matches.
1421 * An example could be a store of a set-cookie value, with a
1422 * fallback to a parameter found in a 302 redirect.
1423 *
1424 * The store-response rules are not allowed to override the
1425 * store-request rules for the same table, but they may coexist.
1426 * Thus we can have up to one store-request entry and one store-
1427 * response entry for the same table at any time.
1428 */
1429 for (i = nbreq; i < s->store_count; i++) {
1430 if (rule->table.t == s->store[i].table)
1431 break;
1432 }
1433
1434 /* skip existing entries for this table */
1435 if (i < s->store_count)
1436 continue;
1437
Emeric Brun1d33b292010-01-04 15:47:17 +01001438 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001439 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001440 ret = acl_pass(ret);
1441 if (rule->cond->pol == ACL_COND_UNLESS)
1442 ret = !ret;
1443 }
1444
1445 if (ret) {
1446 struct stktable_key *key;
1447
Willy Tarreau192252e2015-04-04 01:47:55 +02001448 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 +01001449 if (!key)
1450 continue;
1451
Willy Tarreau37e340c2013-12-06 23:05:21 +01001452 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001453 struct stksess *ts;
1454
1455 ts = stksess_new(rule->table.t, key);
1456 if (ts) {
1457 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001458 s->store[s->store_count++].ts = ts;
1459 }
1460 }
1461 }
1462 }
1463
1464 /* process store request and store response */
1465 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001466 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001467 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001468
Willy Tarreauc93cd162014-05-13 15:54:22 +02001469 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001470 stksess_free(s->store[i].table, s->store[i].ts);
1471 s->store[i].ts = NULL;
1472 continue;
1473 }
1474
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001475 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1476 if (ts) {
1477 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001478 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001479 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001480 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001481 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001482 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001483
1484 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001485 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001486 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001487 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001488 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001489
1490 rep->analysers &= ~an_bit;
1491 rep->analyse_exp = TICK_ETERNITY;
1492 return 1;
1493}
1494
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001495/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001496 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001497 */
1498#define UPDATE_ANALYSERS(real, list, back, flag) { \
1499 list = (((list) & ~(flag)) | ~(back)) & (real); \
1500 back = real; \
1501 if (!(list)) \
1502 break; \
1503 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1504 continue; \
1505}
1506
Christopher Fauleta9215b72016-05-11 17:06:28 +02001507/* These 2 following macros call an analayzer for the specified channel if the
1508 * right flag is set. The first one is used for "filterable" analyzers. If a
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001509 * stream has some registered filters, pre and post analyaze callbacks are
1510 * called. The second are used for other analyzers (AN_FLT_* and
Christopher Fauleta9215b72016-05-11 17:06:28 +02001511 * AN_REQ/RES_HTTP_XFER_BODY) */
1512#define FLT_ANALYZE(strm, chn, fun, list, back, flag, ...) \
1513 { \
1514 if ((list) & (flag)) { \
1515 if (HAS_FILTERS(strm)) { \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001516 if (!flt_pre_analyze((strm), (chn), (flag))) \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001517 break; \
1518 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1519 break; \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001520 if (!flt_post_analyze((strm), (chn), (flag))) \
1521 break; \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001522 } \
1523 else { \
1524 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1525 break; \
1526 } \
1527 UPDATE_ANALYSERS((chn)->analysers, (list), \
1528 (back), (flag)); \
1529 } \
1530 }
1531
1532#define ANALYZE(strm, chn, fun, list, back, flag, ...) \
1533 { \
1534 if ((list) & (flag)) { \
1535 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1536 break; \
1537 UPDATE_ANALYSERS((chn)->analysers, (list), \
1538 (back), (flag)); \
1539 } \
1540 }
1541
Willy Tarreau87b09662015-04-03 00:22:06 +02001542/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001543 * then puts it back to the wait queue in a clean state, or cleans up its
1544 * resources if it must be deleted. Returns in <next> the date the task wants
1545 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1546 * nothing too many times, the request and response buffers flags are monitored
1547 * and each function is called only if at least another function has changed at
1548 * least one flag it is interested in.
1549 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001550struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001551{
Willy Tarreau827aee92011-03-10 16:55:02 +01001552 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001553 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001554 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001555 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001556 unsigned int rq_prod_last, rq_cons_last;
1557 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001558 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001559 struct channel *req, *res;
1560 struct stream_interface *si_f, *si_b;
1561
1562 req = &s->req;
1563 res = &s->res;
1564
1565 si_f = &s->si[0];
1566 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001567
1568 //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 +01001569 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001570
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001571 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001572 if (s->txn)
1573 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001574
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001575 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001576 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1577 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001578
1579 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001580 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1581 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001582
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001583 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001584 si_f->flags |= SI_FL_DONT_WAKE;
1585 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001586
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001587 /* 1a: Check for low level timeouts if needed. We just set a flag on
1588 * stream interfaces when their timeouts have expired.
1589 */
1590 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001591 stream_int_check_timeouts(si_f);
1592 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001593
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001594 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001595 * for future reads or writes. Note: this will also concern upper layers
1596 * but we do not touch any other flag. We must be careful and correctly
1597 * detect state changes when calling them.
1598 */
1599
Willy Tarreau8f128b42014-11-28 15:07:47 +01001600 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001601
Willy Tarreau8f128b42014-11-28 15:07:47 +01001602 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1603 si_b->flags |= SI_FL_NOLINGER;
1604 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001605 }
1606
Willy Tarreau8f128b42014-11-28 15:07:47 +01001607 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1608 if (si_f->flags & SI_FL_NOHALF)
1609 si_f->flags |= SI_FL_NOLINGER;
1610 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001611 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001612
Willy Tarreau8f128b42014-11-28 15:07:47 +01001613 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001614
Willy Tarreau8f128b42014-11-28 15:07:47 +01001615 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1616 si_f->flags |= SI_FL_NOLINGER;
1617 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001618 }
1619
Willy Tarreau8f128b42014-11-28 15:07:47 +01001620 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1621 if (si_b->flags & SI_FL_NOHALF)
1622 si_b->flags |= SI_FL_NOLINGER;
1623 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001624 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001625
Christopher Fauleta00d8172016-11-10 14:58:05 +01001626 if (HAS_FILTERS(s))
1627 flt_stream_check_timeouts(s);
1628
Willy Tarreau798f4322012-11-08 14:49:17 +01001629 /* Once in a while we're woken up because the task expires. But
1630 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001631 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001632 * timeout needs to be refreshed.
1633 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001634 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001635 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1636 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001637 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau5fb04712016-05-04 10:18:37 +02001638 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER)) {
1639 si_f->flags &= ~SI_FL_DONT_WAKE;
1640 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau798f4322012-11-08 14:49:17 +01001641 goto update_exp_and_leave;
Willy Tarreau5fb04712016-05-04 10:18:37 +02001642 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001643 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001644
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001645 /* below we may emit error messages so we have to ensure that we have
1646 * our buffers properly allocated.
1647 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001648 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001649 /* No buffer available, we've been subscribed to the list of
1650 * buffer waiters, let's wait for our turn.
1651 */
Willy Tarreau5fb04712016-05-04 10:18:37 +02001652 si_f->flags &= ~SI_FL_DONT_WAKE;
1653 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001654 goto update_exp_and_leave;
1655 }
1656
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001657 /* 1b: check for low-level errors reported at the stream interface.
1658 * First we check if it's a retryable error (in which case we don't
1659 * want to tell the buffer). Otherwise we report the error one level
1660 * upper by setting flags into the buffers. Note that the side towards
1661 * the client cannot have connect (hence retryable) errors. Also, the
1662 * connection setup code must be able to deal with any type of abort.
1663 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001664 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001665 if (unlikely(si_f->flags & SI_FL_ERR)) {
1666 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1667 si_shutr(si_f);
1668 si_shutw(si_f);
1669 stream_int_report_error(si_f);
1670 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001671 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001672 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001673 if (srv)
1674 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001675 if (!(s->flags & SF_ERR_MASK))
1676 s->flags |= SF_ERR_CLICL;
1677 if (!(s->flags & SF_FINST_MASK))
1678 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001679 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001680 }
1681 }
1682
Willy Tarreau8f128b42014-11-28 15:07:47 +01001683 if (unlikely(si_b->flags & SI_FL_ERR)) {
1684 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1685 si_shutr(si_b);
1686 si_shutw(si_b);
1687 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001688 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001689 if (srv)
1690 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001691 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001692 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001693 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001694 if (srv)
1695 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001696 if (!(s->flags & SF_ERR_MASK))
1697 s->flags |= SF_ERR_SRVCL;
1698 if (!(s->flags & SF_FINST_MASK))
1699 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001700 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001701 }
1702 /* note: maybe we should process connection errors here ? */
1703 }
1704
Willy Tarreau8f128b42014-11-28 15:07:47 +01001705 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001706 /* we were trying to establish a connection on the server side,
1707 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1708 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001709 if (unlikely(!sess_update_st_con_tcp(s)))
1710 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001711 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001712 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001713
1714 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1715 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1716 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1717 */
1718 }
1719
Willy Tarreau8f128b42014-11-28 15:07:47 +01001720 rq_prod_last = si_f->state;
1721 rq_cons_last = si_b->state;
1722 rp_cons_last = si_f->state;
1723 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001724
1725 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001726 /* Check for connection closure */
1727
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001728 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001729 "[%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 +01001730 now_ms, __FUNCTION__, __LINE__,
1731 t,
1732 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001733 req, res,
1734 req->rex, res->wex,
1735 req->flags, res->flags,
1736 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1737 si_f->err_type, si_b->err_type,
1738 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001739
1740 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001741 if (unlikely(si_f->state == SI_ST_DIS))
1742 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001743
1744 /* When a server-side connection is released, we have to count it and
1745 * check for pending connections on this server.
1746 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001747 if (unlikely(si_b->state == SI_ST_DIS)) {
1748 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001749 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001750 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001751 if (s->flags & SF_CURR_SESS) {
1752 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001753 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001754 }
1755 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001756 if (may_dequeue_tasks(srv, s->be))
1757 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001758 }
1759 }
1760
1761 /*
1762 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1763 * at this point.
1764 */
1765
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001766 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001767 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001768 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1769 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1770 si_f->state != rq_prod_last ||
1771 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001772 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001773 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001774
Willy Tarreau8f128b42014-11-28 15:07:47 +01001775 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001776 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001777 unsigned int ana_list;
1778 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001779
Willy Tarreau90deb182010-01-07 00:20:41 +01001780 /* it's up to the analysers to stop new connections,
1781 * disable reading or closing. Note: if an analyser
1782 * disables any of these bits, it is responsible for
1783 * enabling them again when it disables itself, so
1784 * that other analysers are called in similar conditions.
1785 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001786 channel_auto_read(req);
1787 channel_auto_connect(req);
1788 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001789
1790 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001791 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001792 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001793 * the list when not needed. Any analyser may return 0
1794 * to break out of the loop, either because of missing
1795 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001796 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001797 * analyser, and we may loop again if other analysers
1798 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001799 *
1800 * We build a list of analysers to run. We evaluate all
1801 * of these analysers in the order of the lower bit to
1802 * the higher bit. This ordering is very important.
1803 * An analyser will often add/remove other analysers,
1804 * including itself. Any changes to itself have no effect
1805 * on the loop. If it removes any other analysers, we
1806 * want those analysers not to be called anymore during
1807 * this loop. If it adds an analyser that is located
1808 * after itself, we want it to be scheduled for being
1809 * processed during the loop. If it adds an analyser
1810 * which is located before it, we want it to switch to
1811 * it immediately, even if it has already been called
1812 * once but removed since.
1813 *
1814 * In order to achieve this, we compare the analyser
1815 * list after the call with a copy of it before the
1816 * call. The work list is fed with analyser bits that
1817 * appeared during the call. Then we compare previous
1818 * work list with the new one, and check the bits that
1819 * appeared. If the lowest of these bits is lower than
1820 * the current bit, it means we have enabled a previous
1821 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001822 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001823
Willy Tarreau8f128b42014-11-28 15:07:47 +01001824 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001825 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001826 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauleta9215b72016-05-11 17:06:28 +02001827 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_FLT_START_FE);
1828 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_FE);
1829 FLT_ANALYZE(s, req, http_wait_for_request, ana_list, ana_back, AN_REQ_WAIT_HTTP);
1830 FLT_ANALYZE(s, req, http_wait_for_request_body, ana_list, ana_back, AN_REQ_HTTP_BODY);
1831 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE, sess->fe);
1832 FLT_ANALYZE(s, req, process_switching_rules, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
1833 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_FLT_START_BE);
1834 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_BE);
1835 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE, s->be);
1836 FLT_ANALYZE(s, req, http_process_tarpit, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
1837 FLT_ANALYZE(s, req, process_server_rules, ana_list, ana_back, AN_REQ_SRV_RULES);
1838 FLT_ANALYZE(s, req, http_process_request, ana_list, ana_back, AN_REQ_HTTP_INNER);
1839 FLT_ANALYZE(s, req, tcp_persist_rdp_cookie, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
1840 FLT_ANALYZE(s, req, process_sticking_rules, ana_list, ana_back, AN_REQ_STICKING_RULES);
1841 ANALYZE (s, req, flt_analyze_http_headers, ana_list, ana_back, AN_FLT_HTTP_HDRS);
1842 ANALYZE (s, req, flt_xfer_data, ana_list, ana_back, AN_FLT_XFER_DATA);
1843 ANALYZE (s, req, http_request_forward_body, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
1844 ANALYZE (s, req, flt_end_analyze, ana_list, ana_back, AN_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01001845 break;
1846 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001847 }
Willy Tarreau84455332009-03-15 22:34:05 +01001848
Willy Tarreau8f128b42014-11-28 15:07:47 +01001849 rq_prod_last = si_f->state;
1850 rq_cons_last = si_b->state;
1851 req->flags &= ~CF_WAKE_ONCE;
1852 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001853
Willy Tarreau8f128b42014-11-28 15:07:47 +01001854 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001855 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001856 }
1857
Willy Tarreau576507f2010-01-07 00:09:04 +01001858 /* we'll monitor the request analysers while parsing the response,
1859 * because some response analysers may indirectly enable new request
1860 * analysers (eg: HTTP keep-alive).
1861 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001862 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001863
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001864 resync_response:
1865 /* Analyse response */
1866
Willy Tarreau8f128b42014-11-28 15:07:47 +01001867 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1868 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1869 si_f->state != rp_cons_last ||
1870 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001871 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001872 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001873
Willy Tarreau8f128b42014-11-28 15:07:47 +01001874 if ((res->flags & CF_MASK_ANALYSER) &&
1875 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001876 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1877 * for some free space in the response buffer, so we might need to call
1878 * it when something changes in the response buffer, but still we pass
1879 * it the request buffer. Note that the SI state might very well still
1880 * be zero due to us returning a flow of redirects!
1881 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001882 res->analysers &= ~AN_REQ_ALL;
1883 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001884 }
1885
Willy Tarreau8f128b42014-11-28 15:07:47 +01001886 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001887 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001888 unsigned int ana_list;
1889 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001890
Willy Tarreau90deb182010-01-07 00:20:41 +01001891 /* it's up to the analysers to stop disable reading or
1892 * closing. Note: if an analyser disables any of these
1893 * bits, it is responsible for enabling them again when
1894 * it disables itself, so that other analysers are called
1895 * in similar conditions.
1896 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001897 channel_auto_read(res);
1898 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001899
1900 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001901 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001902 * to MSB. The analysers must remove themselves from
1903 * the list when not needed. Any analyser may return 0
1904 * to break out of the loop, either because of missing
1905 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001906 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001907 * analyser, and we may loop again if other analysers
1908 * are added in the middle.
1909 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001910
Willy Tarreau8f128b42014-11-28 15:07:47 +01001911 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001912 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001913 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauleta9215b72016-05-11 17:06:28 +02001914 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_FLT_START_FE);
1915 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_FLT_START_BE);
1916 FLT_ANALYZE(s, res, tcp_inspect_response, ana_list, ana_back, AN_RES_INSPECT);
1917 FLT_ANALYZE(s, res, http_wait_for_response, ana_list, ana_back, AN_RES_WAIT_HTTP);
1918 FLT_ANALYZE(s, res, process_store_rules, ana_list, ana_back, AN_RES_STORE_RULES);
1919 FLT_ANALYZE(s, res, http_process_res_common, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE, s->be);
1920 ANALYZE (s, res, flt_analyze_http_headers, ana_list, ana_back, AN_FLT_HTTP_HDRS);
1921 ANALYZE (s, res, flt_xfer_data, ana_list, ana_back, AN_FLT_XFER_DATA);
1922 ANALYZE (s, res, http_response_forward_body, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
1923 ANALYZE (s, res, flt_end_analyze, ana_list, ana_back, AN_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01001924 break;
1925 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001926 }
1927
Willy Tarreau8f128b42014-11-28 15:07:47 +01001928 rp_cons_last = si_f->state;
1929 rp_prod_last = si_b->state;
1930 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001931
Willy Tarreau8f128b42014-11-28 15:07:47 +01001932 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001933 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001934 }
1935
Willy Tarreau576507f2010-01-07 00:09:04 +01001936 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001937 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001938 goto resync_request;
1939
Willy Tarreau8f128b42014-11-28 15:07:47 +01001940 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001941 goto resync_request;
1942
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001943 /* FIXME: here we should call protocol handlers which rely on
1944 * both buffers.
1945 */
1946
1947
1948 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001949 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001950 * we're just in a data phase here since it means we have not
1951 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001952 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001953 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001954 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001955 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001956 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001957 req->analysers = 0;
1958 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001959 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001960 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001961 if (srv)
1962 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001963 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001964 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001965 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001966 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001967 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001968 if (srv)
1969 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001970 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001971 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001972 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001973 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001974 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001975 if (srv)
1976 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001977 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001978 }
1979 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001980 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001981 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001982 if (srv)
1983 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001984 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001985 }
Willy Tarreau84455332009-03-15 22:34:05 +01001986 sess_set_term_flags(s);
1987 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001988 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001989 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001990 res->analysers = 0;
1991 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001992 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001993 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001994 if (srv)
1995 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001996 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001997 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001998 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001999 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002000 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002001 if (srv)
2002 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002003 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002004 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002005 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002006 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002007 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002008 if (srv)
2009 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002010 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002011 }
2012 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002013 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002014 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002015 if (srv)
2016 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002017 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002018 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002019 sess_set_term_flags(s);
2020 }
Willy Tarreau84455332009-03-15 22:34:05 +01002021 }
2022
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002023 /*
2024 * Here we take care of forwarding unhandled data. This also includes
2025 * connection establishments and shutdown requests.
2026 */
2027
2028
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002029 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002030 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002031 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002032 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002033 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002034 if (unlikely(!req->analysers &&
2035 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2036 (si_f->state >= SI_ST_EST) &&
2037 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002038 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002039 * attached to it. If any data are left in, we'll permit them to
2040 * move.
2041 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002042 channel_auto_read(req);
2043 channel_auto_connect(req);
2044 channel_auto_close(req);
2045 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002046
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002047 /* We'll let data flow between the producer (if still connected)
2048 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002049 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002050 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8bf242b2016-05-04 14:05:58 +02002051 channel_forward_forever(req);
Willy Tarreau42529c32015-07-09 18:38:57 +02002052
2053 /* Just in order to support fetching HTTP contents after start
2054 * of forwarding when the HTTP forwarding analyser is not used,
2055 * we simply reset msg->sov so that HTTP rewinding points to the
2056 * headers.
2057 */
2058 if (s->txn)
2059 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - req->buf->o;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002060 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002061
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002062 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002063 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2064 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002065 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002066 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2067 (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 +01002068 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002069 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2070 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002071 (req->flags & CF_STREAMER_FAST)))) {
2072 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002073 }
2074
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002075 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002076 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002077
2078 /*
2079 * Now forward all shutdown requests between both sides of the buffer
2080 */
2081
Willy Tarreau520d95e2009-09-19 21:04:57 +02002082 /* first, let's check if the request buffer needs to shutdown(write), which may
2083 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002084 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2085 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002086 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002087 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002088 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002089 channel_shutw_now(req);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002090 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002091
2092 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002093 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2094 channel_is_empty(req))) {
2095 if (req->flags & CF_READ_ERROR)
2096 si_b->flags |= SI_FL_NOLINGER;
2097 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002098 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002099 res->rto = s->be->timeout.serverfin;
2100 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002101 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002102 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002103
2104 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002105 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2106 !req->analysers))
2107 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002108
2109 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002110 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2111 if (si_f->flags & SI_FL_NOHALF)
2112 si_f->flags |= SI_FL_NOLINGER;
2113 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002114 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002115
Willy Tarreau520d95e2009-09-19 21:04:57 +02002116 /* it's possible that an upper layer has requested a connection setup or abort.
2117 * There are 2 situations where we decide to establish a new connection :
2118 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002119 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002120 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002121 if (si_b->state == SI_ST_INI) {
2122 if (!(req->flags & CF_SHUTW)) {
2123 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002124 /* If we have an appctx, there is no connect method, so we
2125 * immediately switch to the connected state, otherwise we
2126 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002127 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002128 si_b->state = SI_ST_REQ; /* new connection requested */
2129 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002130 }
Willy Tarreau73201222009-08-16 18:27:24 +02002131 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002132 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002133 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2134 channel_shutw_now(req); /* fix buffer flags upon abort */
2135 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002136 }
Willy Tarreau92795622009-03-06 12:51:23 +01002137 }
2138
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002139
2140 /* we may have a pending connection request, or a connection waiting
2141 * for completion.
2142 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002143 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002144
2145 /* prune the request variables and swap to the response variables. */
2146 if (s->vars_reqres.scope != SCOPE_RES) {
Willy Tarreau6204cd92016-03-10 16:33:04 +01002147 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002148 vars_init(&s->vars_reqres, SCOPE_RES);
2149 }
2150
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002151 do {
2152 /* nb: step 1 might switch from QUE to ASS, but we first want
2153 * to give a chance to step 2 to perform a redirect if needed.
2154 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002155 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002156 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002157 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002158 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002159
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002160 /* applets directly go to the ESTABLISHED state. Similarly,
2161 * servers experience the same fate when their connection
2162 * is reused.
2163 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002164 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002165 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002166
Willy Tarreaub44c8732013-12-31 23:16:50 +01002167 /* Now we can add the server name to a header (if requested) */
2168 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002169 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002170 (s->be->server_id_hdr_name != NULL) &&
2171 (s->be->mode == PR_MODE_HTTP) &&
2172 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002173 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002174 }
2175
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002176 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002177 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002178 http_perform_server_redirect(s, si_b);
2179 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002180 }
2181
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002182 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002183 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002184 goto resync_stream_interface;
2185
Willy Tarreau815a9b22010-07-27 17:15:12 +02002186 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002187 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002188 goto resync_request;
2189
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002190 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002191
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002192 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002193 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002194 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002195 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002196 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002197 if (unlikely(!res->analysers &&
2198 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2199 (si_b->state >= SI_ST_EST) &&
2200 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002201 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002202 * attached to it. If any data are left in, we'll permit them to
2203 * move.
2204 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002205 channel_auto_read(res);
2206 channel_auto_close(res);
2207 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002208
2209 /* We'll let data flow between the producer (if still connected)
2210 * to the consumer.
2211 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002212 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8bf242b2016-05-04 14:05:58 +02002213 channel_forward_forever(res);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002214
Willy Tarreau42529c32015-07-09 18:38:57 +02002215 /* Just in order to support fetching HTTP contents after start
2216 * of forwarding when the HTTP forwarding analyser is not used,
2217 * we simply reset msg->sov so that HTTP rewinding points to the
2218 * headers.
2219 */
2220 if (s->txn)
2221 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2222
Willy Tarreauce887fd2012-05-12 12:50:00 +02002223 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002224 * tunnel timeout set, use it now. Note that we must respect
2225 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002226 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002227 if (!req->analysers && s->be->timeout.tunnel) {
2228 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002229 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002230
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002231 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2232 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002233 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2234 res->rto = s->be->timeout.serverfin;
2235 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2236 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002237 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2238 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002239
Willy Tarreau8f128b42014-11-28 15:07:47 +01002240 req->rex = tick_add(now_ms, req->rto);
2241 req->wex = tick_add(now_ms, req->wto);
2242 res->rex = tick_add(now_ms, res->rto);
2243 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002244 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002245 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002246
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002247 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002248 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2249 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002250 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002251 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2252 (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 +01002253 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002254 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2255 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002256 (res->flags & CF_STREAMER_FAST)))) {
2257 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002258 }
2259
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002260 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002261 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002262
2263 /*
2264 * Now forward all shutdown requests between both sides of the buffer
2265 */
2266
2267 /*
2268 * FIXME: this is probably where we should produce error responses.
2269 */
2270
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002271 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002272 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002273 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002274 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002275 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002276
2277 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002278 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2279 channel_is_empty(res))) {
2280 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002281 if (tick_isset(sess->fe->timeout.clientfin)) {
2282 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002283 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002284 }
2285 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002286
2287 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002288 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2289 !res->analysers)
2290 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002291
2292 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002293 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2294 if (si_b->flags & SI_FL_NOHALF)
2295 si_b->flags |= SI_FL_NOLINGER;
2296 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002297 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002298
Willy Tarreau8f128b42014-11-28 15:07:47 +01002299 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002300 goto resync_stream_interface;
2301
Willy Tarreau8f128b42014-11-28 15:07:47 +01002302 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002303 goto resync_request;
2304
Willy Tarreau8f128b42014-11-28 15:07:47 +01002305 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002306 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002307
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002308 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002309 si_f->flags &= ~SI_FL_DONT_WAKE;
2310 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002311
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002312 /* This is needed only when debugging is enabled, to indicate
2313 * client-side or server-side close. Please note that in the unlikely
2314 * event where both sides would close at once, the sequence is reported
2315 * on the server side first.
2316 */
2317 if (unlikely((global.mode & MODE_DEBUG) &&
2318 (!(global.mode & MODE_QUIET) ||
2319 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002320 if (si_b->state == SI_ST_CLO &&
2321 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002322 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002323 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002324 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2325 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002326 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002327 }
2328
Willy Tarreau8f128b42014-11-28 15:07:47 +01002329 if (si_f->state == SI_ST_CLO &&
2330 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002331 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002332 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002333 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2334 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002335 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002336 }
2337 }
2338
Willy Tarreau8f128b42014-11-28 15:07:47 +01002339 if (likely((si_f->state != SI_ST_CLO) ||
2340 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002341
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002342 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002343 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002344
Willy Tarreau563cc372015-04-19 18:13:56 +02002345 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002346 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002347
Willy Tarreau563cc372015-04-19 18:13:56 +02002348 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002349 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002350
Willy Tarreau8f128b42014-11-28 15:07:47 +01002351 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2352 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2353 si_f->prev_state = si_f->state;
2354 si_b->prev_state = si_b->state;
2355 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2356 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002357
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002358 /* Trick: if a request is being waiting for the server to respond,
2359 * and if we know the server can timeout, we don't want the timeout
2360 * to expire on the client side first, but we're still interested
2361 * in passing data from the client to the server (eg: POST). Thus,
2362 * we can cancel the client's request timeout if the server's
2363 * request timeout is set and the server has not yet sent a response.
2364 */
2365
Willy Tarreau8f128b42014-11-28 15:07:47 +01002366 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2367 (tick_isset(req->wex) || tick_isset(res->rex))) {
2368 req->flags |= CF_READ_NOEXP;
2369 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002370 }
2371
Willy Tarreau798f4322012-11-08 14:49:17 +01002372 update_exp_and_leave:
Willy Tarreau5fb04712016-05-04 10:18:37 +02002373 /* Note: please ensure that if you branch here you disable SI_FL_DONT_WAKE */
Christopher Fauleta00d8172016-11-10 14:58:05 +01002374 t->expire = tick_first((tick_is_expired(t->expire, now_ms) ? 0 : t->expire),
2375 tick_first(tick_first(req->rex, req->wex),
2376 tick_first(res->rex, res->wex)));
Willy Tarreaudef0d222016-11-08 22:03:00 +01002377 if (!req->analysers)
2378 req->analyse_exp = TICK_ETERNITY;
2379
2380 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED) &&
2381 (!tick_isset(req->analyse_exp) || tick_is_expired(req->analyse_exp, now_ms)))
2382 req->analyse_exp = tick_add(now_ms, 5000);
2383
2384 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002385
Willy Tarreau8f128b42014-11-28 15:07:47 +01002386 if (si_f->exp)
2387 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002388
Willy Tarreau8f128b42014-11-28 15:07:47 +01002389 if (si_b->exp)
2390 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002391
2392#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002393 fprintf(stderr,
2394 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2395 " 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 +01002396 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2397 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002398#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002399
2400#ifdef DEBUG_DEV
2401 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002402 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002403 ABORT_NOW();
2404#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002405 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002406 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002407 }
2408
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002409 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002410 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002411 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002412 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002413 if (sess->listener) {
2414 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002415 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002416 sess->listener->nbconn--;
2417 if (sess->listener->state == LI_FULL)
2418 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002419
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002420 /* Dequeues all of the listeners waiting for a resource */
2421 if (!LIST_ISEMPTY(&global_listener_queue))
2422 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002423
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002424 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2425 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2426 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002427 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002428
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002429 if (unlikely((global.mode & MODE_DEBUG) &&
2430 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002431 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002432 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002433 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2434 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002435 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002436 }
2437
2438 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002439 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002440
Willy Tarreaueee5b512015-04-03 23:46:31 +02002441 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002442 int n;
2443
Willy Tarreaueee5b512015-04-03 23:46:31 +02002444 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002445 if (n < 1 || n > 5)
2446 n = 0;
2447
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002448 if (sess->fe->mode == PR_MODE_HTTP) {
2449 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002450 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002451 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002452 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002453 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002454 s->be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002455 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002456 }
2457
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002458 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002459 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002460 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002461 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002462 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002463 }
2464
Willy Tarreau87b09662015-04-03 00:22:06 +02002465 /* update time stats for this stream */
2466 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002467
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002468 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002469 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002470 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002471 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002472 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002473}
2474
Willy Tarreau87b09662015-04-03 00:22:06 +02002475/* Update the stream's backend and server time stats */
2476void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002477{
2478 int t_request;
2479 int t_queue;
2480 int t_connect;
2481 int t_data;
2482 int t_close;
2483 struct server *srv;
2484
2485 t_request = 0;
2486 t_queue = s->logs.t_queue;
2487 t_connect = s->logs.t_connect;
2488 t_close = s->logs.t_close;
2489 t_data = s->logs.t_data;
2490
2491 if (s->be->mode != PR_MODE_HTTP)
2492 t_data = t_connect;
2493
2494 if (t_connect < 0 || t_data < 0)
2495 return;
2496
2497 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2498 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2499
2500 t_data -= t_connect;
2501 t_connect -= t_queue;
2502 t_queue -= t_request;
2503
2504 srv = objt_server(s->target);
2505 if (srv) {
2506 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2507 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2508 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2509 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2510 }
2511 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2512 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2513 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2514 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2515}
2516
Willy Tarreau7c669d72008-06-20 15:04:11 +02002517/*
2518 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002519 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002520 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002521 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002522 * server.
2523 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002524void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002525{
2526 if (sess->srv_conn == newsrv)
2527 return;
2528
2529 if (sess->srv_conn) {
2530 sess->srv_conn->served--;
Andrew Rodlande168feb2016-10-25 12:48:17 -04002531 sess->srv_conn->proxy->served--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002532 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2533 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002534 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002535 }
2536
2537 if (newsrv) {
2538 newsrv->served++;
Andrew Rodlande168feb2016-10-25 12:48:17 -04002539 newsrv->proxy->served++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002540 if (newsrv->proxy->lbprm.server_take_conn)
2541 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002542 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002543 }
2544}
2545
Willy Tarreau84455332009-03-15 22:34:05 +01002546/* Handle server-side errors for default protocols. It is called whenever a a
2547 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002548 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002549 * them. It's installed as ->srv_error for the server-side stream_interface.
2550 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002551void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002552{
2553 int err_type = si->err_type;
2554 int err = 0, fin = 0;
2555
2556 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002557 err = SF_ERR_CLICL;
2558 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002559 }
2560 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002561 err = SF_ERR_CLICL;
2562 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002563 }
2564 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002565 err = SF_ERR_SRVTO;
2566 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002567 }
2568 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002569 err = SF_ERR_SRVCL;
2570 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002571 }
2572 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002573 err = SF_ERR_SRVTO;
2574 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002575 }
2576 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002577 err = SF_ERR_SRVCL;
2578 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002579 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002580 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002581 err = SF_ERR_RESOURCE;
2582 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002583 }
Willy Tarreau84455332009-03-15 22:34:05 +01002584 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002585 err = SF_ERR_INTERNAL;
2586 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002587 }
2588
Willy Tarreaue7dff022015-04-03 01:14:29 +02002589 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002590 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002591 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002592 s->flags |= fin;
2593}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002594
Willy Tarreaue7dff022015-04-03 01:14:29 +02002595/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002596void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002597{
Willy Tarreau87b09662015-04-03 00:22:06 +02002598 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002599 return;
2600
Willy Tarreau87b09662015-04-03 00:22:06 +02002601 channel_shutw_now(&stream->req);
2602 channel_shutr_now(&stream->res);
2603 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002604 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002605 stream->flags |= why;
2606 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002607}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002608
Willy Tarreau8b22a712010-06-18 17:46:06 +02002609/************************************************************************/
2610/* All supported ACL keywords must be declared here. */
2611/************************************************************************/
2612
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002613/* Returns a pointer to a stkctr depending on the fetch keyword name.
2614 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002615 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002616 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002617 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002618 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002619 * NULL may be returned if the designated stkctr is not tracked. For
2620 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2621 * passed. When present, the currently tracked key is then looked up
2622 * in the specified table instead of the current table. The purpose is
2623 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002624 * multiple tables). <strm> is allowed to be NULL, in which case only
2625 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002626 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002627struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002628smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002629{
2630 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002631 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002632 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002633 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002634 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002635
Willy Tarreau0f791d42013-07-23 19:56:43 +02002636 if (num == '_' - '0') {
2637 /* sc_* variant, args[0] = ctr# (mandatory) */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002638 num = args[arg++].data.sint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002639 if (num >= MAX_SESS_STKCTR)
2640 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002641 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002642 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002643 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002644 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002645 struct sample smp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002646
2647 if (!conn)
2648 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002649
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002650 /* Fetch source adress in a sample. */
2651 smp.px = NULL;
2652 smp.sess = sess;
2653 smp.strm = strm;
2654 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2655 return NULL;
2656
2657 /* Converts into key. */
2658 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002659 if (!key)
2660 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002661
Willy Tarreaua65536c2013-07-22 22:40:11 +02002662 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002663 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002664 return &stkctr;
2665 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002666
2667 /* Here, <num> contains the counter number from 0 to 9 for
2668 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002669 * args[arg] is the first optional argument. We first lookup the
2670 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002671 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002672
Thierry Fournier40e1d512016-03-29 21:27:36 +02002673 if (strm)
2674 stkptr = &strm->stkctr[num];
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002675 if (!strm || !stkctr_entry(stkptr)) {
2676 stkptr = &sess->stkctr[num];
2677 if (!stkctr_entry(stkptr))
2678 return NULL;
2679 }
2680
2681 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002682 if (!stksess)
2683 return NULL;
2684
Willy Tarreau0f791d42013-07-23 19:56:43 +02002685 if (unlikely(args[arg].type == ARGT_TAB)) {
2686 /* an alternate table was specified, let's look up the same key there */
2687 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002688 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002689 return &stkctr;
2690 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002691 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002692}
2693
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002694/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2695 * the entry if it doesn't exist yet. This is needed for a few fetch
2696 * functions which need to create an entry, such as src_inc_gpc* and
2697 * src_clr_gpc*.
2698 */
2699struct stkctr *
2700smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
2701{
2702 static struct stkctr stkctr;
2703 struct stktable_key *key;
2704 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002705 struct sample smp;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002706
2707 if (strncmp(kw, "src_", 4) != 0)
2708 return NULL;
2709
2710 if (!conn)
2711 return NULL;
2712
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002713 /* Fetch source adress in a sample. */
2714 smp.px = NULL;
2715 smp.sess = sess;
2716 smp.strm = strm;
2717 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2718 return NULL;
2719
2720 /* Converts into key. */
2721 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002722 if (!key)
2723 return NULL;
2724
2725 stkctr.table = &args->data.prx->table;
2726 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
2727 return &stkctr;
2728}
2729
Willy Tarreau87b09662015-04-03 00:22:06 +02002730/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002731 * currently being tracked or not.
2732 * Supports being called as "sc[0-9]_tracked" only.
2733 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002734static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002735smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002736{
2737 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002738 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002739 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002740 return 1;
2741}
2742
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002743/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2744 * frontend counters or from the src.
2745 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2746 * zero is returned if the key is new.
2747 */
2748static int
2749smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2750{
Willy Tarreaube508f12016-03-10 11:47:01 +01002751 struct stkctr *stkctr;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002752
Willy Tarreaube508f12016-03-10 11:47:01 +01002753 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002754 if (!stkctr)
2755 return 0;
2756
2757 smp->flags = SMP_F_VOL_TEST;
2758 smp->data.type = SMP_T_SINT;
2759 smp->data.u.sint = 0;
2760
2761 if (stkctr_entry(stkctr) != NULL) {
2762 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2763 if (!ptr)
2764 return 0; /* parameter not stored */
2765 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
2766 }
2767 return 1;
2768}
2769
Willy Tarreau87b09662015-04-03 00:22:06 +02002770/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002771 * frontend counters or from the src.
2772 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2773 * zero is returned if the key is new.
2774 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002775static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002776smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002777{
Willy Tarreaube508f12016-03-10 11:47:01 +01002778 struct stkctr *stkctr;
2779
Willy Tarreaube508f12016-03-10 11:47:01 +01002780 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002781 if (!stkctr)
2782 return 0;
2783
Willy Tarreau37406352012-04-23 16:16:37 +02002784 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002785 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002786 smp->data.u.sint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002787
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002788 if (stkctr_entry(stkctr) != NULL) {
2789 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002790 if (!ptr)
2791 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002792 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002793 }
2794 return 1;
2795}
2796
Willy Tarreau87b09662015-04-03 00:22:06 +02002797/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002798 * tracked frontend counters or from the src.
2799 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2800 * Value zero is returned if the key is new.
2801 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002802static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002803smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002804{
Willy Tarreaube508f12016-03-10 11:47:01 +01002805 struct stkctr *stkctr;
2806
Willy Tarreaube508f12016-03-10 11:47:01 +01002807 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002808 if (!stkctr)
2809 return 0;
2810
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002811 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002812 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002813 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002814 if (stkctr_entry(stkctr) != NULL) {
2815 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002816 if (!ptr)
2817 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002818 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002819 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002820 }
2821 return 1;
2822}
2823
Willy Tarreau87b09662015-04-03 00:22:06 +02002824/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002825 * frontend counters and return it into temp integer.
2826 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002827 */
2828static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002829smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002830{
Willy Tarreaube508f12016-03-10 11:47:01 +01002831 struct stkctr *stkctr;
2832
Willy Tarreaube508f12016-03-10 11:47:01 +01002833 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002834 if (!stkctr)
2835 return 0;
2836
Willy Tarreau37406352012-04-23 16:16:37 +02002837 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002838 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002839 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002840
2841 if (stkctr_entry(stkctr) == NULL)
2842 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2843
Thierry Fournier6fc340f2016-06-06 18:28:05 +02002844 if (stkctr && stkctr_entry(stkctr)) {
Emeric Brun57056f02015-06-15 18:29:57 +02002845 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002846
2847 /* First, update gpc0_rate if it's tracked. Second, update its
2848 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2849 */
Emeric Brun57056f02015-06-15 18:29:57 +02002850 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2851 if (ptr1) {
2852 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002853 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002854 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002855 }
2856
Emeric Brun57056f02015-06-15 18:29:57 +02002857 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2858 if (ptr2)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002859 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002860
Emeric Brun57056f02015-06-15 18:29:57 +02002861 /* If data was modified, we need to touch to re-schedule sync */
2862 if (ptr1 || ptr2)
2863 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002864 }
2865 return 1;
2866}
2867
Willy Tarreau87b09662015-04-03 00:22:06 +02002868/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002869 * frontend counters and return its previous value into temp integer.
2870 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002871 */
2872static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002873smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002874{
Willy Tarreaube508f12016-03-10 11:47:01 +01002875 struct stkctr *stkctr;
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002876
Willy Tarreaube508f12016-03-10 11:47:01 +01002877 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002878 if (!stkctr)
2879 return 0;
2880
Willy Tarreau37406352012-04-23 16:16:37 +02002881 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002882 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002883 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002884
2885 if (stkctr_entry(stkctr) == NULL)
2886 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2887
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002888 if (stkctr_entry(stkctr) != NULL) {
2889 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002890 if (!ptr)
2891 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002892 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002893 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002894 /* If data was modified, we need to touch to re-schedule sync */
2895 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002896 }
2897 return 1;
2898}
2899
Willy Tarreau87b09662015-04-03 00:22:06 +02002900/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002901 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2902 * "src_conn_cnt" only.
2903 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002904static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002905smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002906{
Willy Tarreaube508f12016-03-10 11:47:01 +01002907 struct stkctr *stkctr;
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002908
Willy Tarreaube508f12016-03-10 11:47:01 +01002909 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002910 if (!stkctr)
2911 return 0;
2912
Willy Tarreau37406352012-04-23 16:16:37 +02002913 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002914 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002915 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002916 if (stkctr_entry(stkctr) != NULL) {
2917 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002918 if (!ptr)
2919 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002920 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002921 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002922 return 1;
2923}
2924
Willy Tarreau87b09662015-04-03 00:22:06 +02002925/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002926 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2927 * only.
2928 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002929static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002930smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002931{
Willy Tarreaube508f12016-03-10 11:47:01 +01002932 struct stkctr *stkctr;
2933
Willy Tarreaube508f12016-03-10 11:47:01 +01002934 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002935 if (!stkctr)
2936 return 0;
2937
Willy Tarreau37406352012-04-23 16:16:37 +02002938 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002939 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002940 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002941 if (stkctr_entry(stkctr) != NULL) {
2942 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002943 if (!ptr)
2944 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002945 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002946 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002947 }
2948 return 1;
2949}
2950
Willy Tarreau87b09662015-04-03 00:22:06 +02002951/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002952 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002953 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002954 */
2955static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002956smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002957{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002958 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002959 struct stksess *ts;
2960 struct stktable_key *key;
2961 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002962 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002963
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002964 if (!conn)
2965 return 0;
2966
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002967 /* Fetch source adress in a sample. */
2968 if (!smp_fetch_src(NULL, smp, NULL, NULL))
2969 return 0;
2970
2971 /* Converts into key. */
2972 key = smp_to_stkey(smp, &args->data.prx->table);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002973 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002974 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002975
Willy Tarreau24e32d82012-04-23 23:55:44 +02002976 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002977
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002978 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2979 /* entry does not exist and could not be created */
2980 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002981
2982 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2983 if (!ptr)
2984 return 0; /* parameter not stored in this table */
2985
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002986 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002987 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02002988 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02002989 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002990 return 1;
2991}
2992
Willy Tarreau87b09662015-04-03 00:22:06 +02002993/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002994 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2995 * "src_conn_cur" only.
2996 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002997static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002998smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002999{
Willy Tarreaube508f12016-03-10 11:47:01 +01003000 struct stkctr *stkctr;
3001
Willy Tarreaube508f12016-03-10 11:47:01 +01003002 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02003003 if (!stkctr)
3004 return 0;
3005
Willy Tarreau37406352012-04-23 16:16:37 +02003006 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003007 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003008 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003009 if (stkctr_entry(stkctr) != NULL) {
3010 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003011 if (!ptr)
3012 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003013 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003014 }
3015 return 1;
3016}
3017
Willy Tarreau87b09662015-04-03 00:22:06 +02003018/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02003019 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3020 * "src_sess_cnt" only.
3021 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003022static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003023smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003024{
Willy Tarreaube508f12016-03-10 11:47:01 +01003025 struct stkctr *stkctr;
3026
Willy Tarreaube508f12016-03-10 11:47:01 +01003027 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02003028 if (!stkctr)
3029 return 0;
3030
Willy Tarreau37406352012-04-23 16:16:37 +02003031 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003032 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003033 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003034 if (stkctr_entry(stkctr) != NULL) {
3035 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003036 if (!ptr)
3037 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003038 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003039 }
3040 return 1;
3041}
3042
Willy Tarreau87b09662015-04-03 00:22:06 +02003043/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003044 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3045 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003046static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003047smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003048{
Willy Tarreaube508f12016-03-10 11:47:01 +01003049 struct stkctr *stkctr;
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003050
Willy Tarreaube508f12016-03-10 11:47:01 +01003051 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003052 if (!stkctr)
3053 return 0;
3054
Willy Tarreau37406352012-04-23 16:16:37 +02003055 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003056 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003057 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003058 if (stkctr_entry(stkctr) != NULL) {
3059 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003060 if (!ptr)
3061 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003062 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003063 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003064 }
3065 return 1;
3066}
3067
Willy Tarreau87b09662015-04-03 00:22:06 +02003068/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02003069 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3070 * "src_http_req_cnt" only.
3071 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003072static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003073smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003074{
Willy Tarreaube508f12016-03-10 11:47:01 +01003075 struct stkctr *stkctr;
Willy Tarreau91200da2013-07-23 15:55:19 +02003076
Willy Tarreaube508f12016-03-10 11:47:01 +01003077 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02003078 if (!stkctr)
3079 return 0;
3080
Willy Tarreau37406352012-04-23 16:16:37 +02003081 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003082 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003083 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003084 if (stkctr_entry(stkctr) != NULL) {
3085 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003086 if (!ptr)
3087 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003088 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003089 }
3090 return 1;
3091}
3092
Willy Tarreau87b09662015-04-03 00:22:06 +02003093/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02003094 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3095 * "src_http_req_rate" only.
3096 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003097static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003098smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003099{
Willy Tarreaube508f12016-03-10 11:47:01 +01003100 struct stkctr *stkctr;
3101
Willy Tarreaube508f12016-03-10 11:47:01 +01003102 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02003103 if (!stkctr)
3104 return 0;
3105
Willy Tarreau37406352012-04-23 16:16:37 +02003106 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003107 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003108 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003109 if (stkctr_entry(stkctr) != NULL) {
3110 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003111 if (!ptr)
3112 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003113 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003114 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003115 }
3116 return 1;
3117}
3118
Willy Tarreau87b09662015-04-03 00:22:06 +02003119/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003120 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3121 * "src_http_err_cnt" only.
3122 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003123static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003124smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003125{
Willy Tarreaube508f12016-03-10 11:47:01 +01003126 struct stkctr *stkctr;
3127
Willy Tarreaube508f12016-03-10 11:47:01 +01003128 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003129 if (!stkctr)
3130 return 0;
3131
Willy Tarreau37406352012-04-23 16:16:37 +02003132 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003133 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003134 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003135 if (stkctr_entry(stkctr) != NULL) {
3136 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003137 if (!ptr)
3138 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003139 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003140 }
3141 return 1;
3142}
3143
Willy Tarreau87b09662015-04-03 00:22:06 +02003144/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003145 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3146 * "src_http_err_rate" only.
3147 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003148static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003149smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003150{
Willy Tarreaube508f12016-03-10 11:47:01 +01003151 struct stkctr *stkctr;
3152
Willy Tarreaube508f12016-03-10 11:47:01 +01003153 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003154 if (!stkctr)
3155 return 0;
3156
Willy Tarreau37406352012-04-23 16:16:37 +02003157 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003158 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003159 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003160 if (stkctr_entry(stkctr) != NULL) {
3161 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003162 if (!ptr)
3163 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003164 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003165 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003166 }
3167 return 1;
3168}
3169
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003170/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003171 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003172 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3173 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003174static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003175smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003176{
Willy Tarreaube508f12016-03-10 11:47:01 +01003177 struct stkctr *stkctr;
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003178
Willy Tarreaube508f12016-03-10 11:47:01 +01003179 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003180 if (!stkctr)
3181 return 0;
3182
Willy Tarreau37406352012-04-23 16:16:37 +02003183 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003184 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003185 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003186 if (stkctr_entry(stkctr) != NULL) {
3187 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003188 if (!ptr)
3189 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003190 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003191 }
3192 return 1;
3193}
3194
Willy Tarreau613fe992013-07-23 17:39:19 +02003195/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003196 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003197 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003198 */
3199static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003200smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003201{
Willy Tarreaube508f12016-03-10 11:47:01 +01003202 struct stkctr *stkctr;
Willy Tarreau613fe992013-07-23 17:39:19 +02003203
Willy Tarreaube508f12016-03-10 11:47:01 +01003204 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003205 if (!stkctr)
3206 return 0;
3207
Willy Tarreau37406352012-04-23 16:16:37 +02003208 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003209 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003210 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003211 if (stkctr_entry(stkctr) != NULL) {
3212 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003213 if (!ptr)
3214 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003215 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003216 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003217 }
3218 return 1;
3219}
3220
Willy Tarreau53aea102013-07-23 17:39:02 +02003221/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003222 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003223 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3224 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003225static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003226smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003227{
Willy Tarreaube508f12016-03-10 11:47:01 +01003228 struct stkctr *stkctr;
3229
Willy Tarreaube508f12016-03-10 11:47:01 +01003230 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003231 if (!stkctr)
3232 return 0;
3233
Willy Tarreau37406352012-04-23 16:16:37 +02003234 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003235 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003236 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003237 if (stkctr_entry(stkctr) != NULL) {
3238 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003239 if (!ptr)
3240 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003241 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003242 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003243 return 1;
3244}
3245
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003246/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003247 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003248 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003249 */
3250static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003251smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003252{
Willy Tarreaube508f12016-03-10 11:47:01 +01003253 struct stkctr *stkctr;
3254
Willy Tarreaube508f12016-03-10 11:47:01 +01003255 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003256 if (!stkctr)
3257 return 0;
3258
Willy Tarreau37406352012-04-23 16:16:37 +02003259 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003260 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003261 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003262 if (stkctr_entry(stkctr) != NULL) {
3263 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003264 if (!ptr)
3265 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003266 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003267 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003268 }
3269 return 1;
3270}
3271
Willy Tarreau87b09662015-04-03 00:22:06 +02003272/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003273 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3274 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003275static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003276smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003277{
Willy Tarreaube508f12016-03-10 11:47:01 +01003278 struct stkctr *stkctr;
3279
Willy Tarreaube508f12016-03-10 11:47:01 +01003280 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau563eef42013-07-23 18:32:02 +02003281 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003282 return 0;
3283
Willy Tarreau563eef42013-07-23 18:32:02 +02003284 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003285 smp->data.type = SMP_T_SINT;
Willy Tarreau4d03ef72016-08-14 12:02:55 +02003286 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
Willy Tarreau563eef42013-07-23 18:32:02 +02003287 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003288}
3289
Willy Tarreau34db1082012-04-19 17:16:54 +02003290/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003291 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003292 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003293static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003294smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003295{
Willy Tarreau37406352012-04-23 16:16:37 +02003296 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003297 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003298 smp->data.u.sint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003299 return 1;
3300}
3301
Willy Tarreau34db1082012-04-19 17:16:54 +02003302/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003303 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003304 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003305static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003306smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003307{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003308 struct proxy *px;
3309
Willy Tarreau24e32d82012-04-23 23:55:44 +02003310 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003311 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003312 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003313 smp->data.u.sint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003314 return 1;
3315}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003316
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003317/* 0=OK, <0=Alert, >0=Warning */
3318static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
3319 struct proxy *px, struct act_rule *rule,
3320 char **err)
3321{
3322 struct action_kw *kw;
3323
3324 /* Check if the service name exists. */
3325 if (*(args[*cur_arg]) == 0) {
3326 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01003327 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003328 }
3329
3330 /* lookup for keyword corresponding to a service. */
3331 kw = action_lookup(&service_keywords, args[*cur_arg]);
3332 if (!kw) {
3333 memprintf(err, "'%s' unknown service name.", args[1]);
3334 return ACT_RET_PRS_ERR;
3335 }
3336 (*cur_arg)++;
3337
3338 /* executes specific rule parser. */
3339 rule->kw = kw;
3340 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
3341 return ACT_RET_PRS_ERR;
3342
3343 /* Register processing function. */
3344 rule->action_ptr = process_use_service;
3345 rule->action = ACT_CUSTOM;
3346
3347 return ACT_RET_PRS_OK;
3348}
3349
3350void service_keywords_register(struct action_kw_list *kw_list)
3351{
3352 LIST_ADDQ(&service_keywords, &kw_list->list);
3353}
3354
William Lallemand4c5b4d52016-11-21 08:51:11 +01003355
3356/* This function dumps a complete stream state onto the stream interface's
3357 * read buffer. The stream has to be set in strm. It returns 0 if the output
3358 * buffer is full and it needs to be called again, otherwise non-zero. It is
3359 * designed to be called from stats_dump_strm_to_buffer() below.
3360 */
3361static int stats_dump_full_strm_to_buffer(struct stream_interface *si, struct stream *strm)
3362{
3363 struct appctx *appctx = __objt_appctx(si->end);
3364 struct tm tm;
3365 extern const char *monthname[12];
3366 char pn[INET6_ADDRSTRLEN];
3367 struct connection *conn;
3368 struct appctx *tmpctx;
3369
3370 chunk_reset(&trash);
3371
3372 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != strm->uniq_id) {
3373 /* stream changed, no need to go any further */
3374 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3375 if (bi_putchk(si_ic(si), &trash) == -1) {
3376 si_applet_cant_put(si);
3377 return 0;
3378 }
3379 appctx->ctx.sess.uid = 0;
3380 appctx->ctx.sess.section = 0;
3381 return 1;
3382 }
3383
3384 switch (appctx->ctx.sess.section) {
3385 case 0: /* main status of the stream */
3386 appctx->ctx.sess.uid = strm->uniq_id;
3387 appctx->ctx.sess.section = 1;
3388 /* fall through */
3389
3390 case 1:
3391 get_localtime(strm->logs.accept_date.tv_sec, &tm);
3392 chunk_appendf(&trash,
3393 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
3394 strm,
3395 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3396 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(strm->logs.accept_date.tv_usec),
3397 strm->uniq_id,
3398 strm_li(strm) ? strm_li(strm)->proto->name : "?");
3399
3400 conn = objt_conn(strm_orig(strm));
3401 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
3402 case AF_INET:
3403 case AF_INET6:
3404 chunk_appendf(&trash, " source=%s:%d\n",
3405 pn, get_host_port(&conn->addr.from));
3406 break;
3407 case AF_UNIX:
3408 chunk_appendf(&trash, " source=unix:%d\n", strm_li(strm)->luid);
3409 break;
3410 default:
3411 /* no more information to print right now */
3412 chunk_appendf(&trash, "\n");
3413 break;
3414 }
3415
3416 chunk_appendf(&trash,
3417 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
3418 strm->flags, strm->si[1].conn_retries, strm->srv_conn, strm->pend_pos);
3419
3420 chunk_appendf(&trash,
3421 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
3422 strm_fe(strm)->id, strm_fe(strm)->uuid, strm_fe(strm)->mode ? "http" : "tcp",
3423 strm_li(strm) ? strm_li(strm)->name ? strm_li(strm)->name : "?" : "?",
3424 strm_li(strm) ? strm_li(strm)->luid : 0);
3425
3426 if (conn)
3427 conn_get_to_addr(conn);
3428
3429 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
3430 case AF_INET:
3431 case AF_INET6:
3432 chunk_appendf(&trash, " addr=%s:%d\n",
3433 pn, get_host_port(&conn->addr.to));
3434 break;
3435 case AF_UNIX:
3436 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(strm)->luid);
3437 break;
3438 default:
3439 /* no more information to print right now */
3440 chunk_appendf(&trash, "\n");
3441 break;
3442 }
3443
3444 if (strm->be->cap & PR_CAP_BE)
3445 chunk_appendf(&trash,
3446 " backend=%s (id=%u mode=%s)",
3447 strm->be->id,
3448 strm->be->uuid, strm->be->mode ? "http" : "tcp");
3449 else
3450 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
3451
3452 conn = objt_conn(strm->si[1].end);
3453 if (conn)
3454 conn_get_from_addr(conn);
3455
3456 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
3457 case AF_INET:
3458 case AF_INET6:
3459 chunk_appendf(&trash, " addr=%s:%d\n",
3460 pn, get_host_port(&conn->addr.from));
3461 break;
3462 case AF_UNIX:
3463 chunk_appendf(&trash, " addr=unix\n");
3464 break;
3465 default:
3466 /* no more information to print right now */
3467 chunk_appendf(&trash, "\n");
3468 break;
3469 }
3470
3471 if (strm->be->cap & PR_CAP_BE)
3472 chunk_appendf(&trash,
3473 " server=%s (id=%u)",
3474 objt_server(strm->target) ? objt_server(strm->target)->id : "<none>",
3475 objt_server(strm->target) ? objt_server(strm->target)->puid : 0);
3476 else
3477 chunk_appendf(&trash, " server=<NONE> (id=-1)");
3478
3479 if (conn)
3480 conn_get_to_addr(conn);
3481
3482 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
3483 case AF_INET:
3484 case AF_INET6:
3485 chunk_appendf(&trash, " addr=%s:%d\n",
3486 pn, get_host_port(&conn->addr.to));
3487 break;
3488 case AF_UNIX:
3489 chunk_appendf(&trash, " addr=unix\n");
3490 break;
3491 default:
3492 /* no more information to print right now */
3493 chunk_appendf(&trash, "\n");
3494 break;
3495 }
3496
3497 chunk_appendf(&trash,
3498 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
3499 strm->task,
3500 strm->task->state,
3501 strm->task->nice, strm->task->calls,
3502 strm->task->expire ?
3503 tick_is_expired(strm->task->expire, now_ms) ? "<PAST>" :
3504 human_time(TICKS_TO_MS(strm->task->expire - now_ms),
3505 TICKS_TO_MS(1000)) : "<NEVER>",
3506 task_in_rq(strm->task) ? ", running" : "");
3507
3508 chunk_appendf(&trash,
3509 " age=%s)\n",
3510 human_time(now.tv_sec - strm->logs.accept_date.tv_sec, 1));
3511
3512 if (strm->txn)
3513 chunk_appendf(&trash,
3514 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s waiting=%d\n",
3515 strm->txn, strm->txn->flags, strm->txn->meth, strm->txn->status,
3516 http_msg_state_str(strm->txn->req.msg_state), http_msg_state_str(strm->txn->rsp.msg_state), !LIST_ISEMPTY(&strm->buffer_wait));
3517
3518 chunk_appendf(&trash,
3519 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
3520 &strm->si[0],
3521 si_state_str(strm->si[0].state),
3522 strm->si[0].flags,
3523 obj_type_name(strm->si[0].end),
3524 obj_base_ptr(strm->si[0].end),
3525 strm->si[0].exp ?
3526 tick_is_expired(strm->si[0].exp, now_ms) ? "<PAST>" :
3527 human_time(TICKS_TO_MS(strm->si[0].exp - now_ms),
3528 TICKS_TO_MS(1000)) : "<NEVER>",
3529 strm->si[0].err_type);
3530
3531 chunk_appendf(&trash,
3532 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
3533 &strm->si[1],
3534 si_state_str(strm->si[1].state),
3535 strm->si[1].flags,
3536 obj_type_name(strm->si[1].end),
3537 obj_base_ptr(strm->si[1].end),
3538 strm->si[1].exp ?
3539 tick_is_expired(strm->si[1].exp, now_ms) ? "<PAST>" :
3540 human_time(TICKS_TO_MS(strm->si[1].exp - now_ms),
3541 TICKS_TO_MS(1000)) : "<NEVER>",
3542 strm->si[1].err_type);
3543
3544 if ((conn = objt_conn(strm->si[0].end)) != NULL) {
3545 chunk_appendf(&trash,
3546 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
3547 conn,
3548 conn_get_ctrl_name(conn),
3549 conn_get_xprt_name(conn),
3550 conn_get_data_name(conn),
3551 obj_type_name(conn->target),
3552 obj_base_ptr(conn->target));
3553
3554 chunk_appendf(&trash,
3555 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
3556 conn->flags,
3557 conn->t.sock.fd,
3558 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
3559 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
3560 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
3561 }
3562 else if ((tmpctx = objt_appctx(strm->si[0].end)) != NULL) {
3563 chunk_appendf(&trash,
3564 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
3565 tmpctx,
3566 tmpctx->st0,
3567 tmpctx->st1,
3568 tmpctx->st2,
3569 tmpctx->applet->name);
3570 }
3571
3572 if ((conn = objt_conn(strm->si[1].end)) != NULL) {
3573 chunk_appendf(&trash,
3574 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
3575 conn,
3576 conn_get_ctrl_name(conn),
3577 conn_get_xprt_name(conn),
3578 conn_get_data_name(conn),
3579 obj_type_name(conn->target),
3580 obj_base_ptr(conn->target));
3581
3582 chunk_appendf(&trash,
3583 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
3584 conn->flags,
3585 conn->t.sock.fd,
3586 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
3587 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
3588 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
3589 }
3590 else if ((tmpctx = objt_appctx(strm->si[1].end)) != NULL) {
3591 chunk_appendf(&trash,
3592 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
3593 tmpctx,
3594 tmpctx->st0,
3595 tmpctx->st1,
3596 tmpctx->st2,
3597 tmpctx->applet->name);
3598 }
3599
3600 chunk_appendf(&trash,
3601 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
3602 " an_exp=%s",
3603 &strm->req,
3604 strm->req.flags, strm->req.analysers,
3605 strm->req.pipe ? strm->req.pipe->data : 0,
3606 strm->req.to_forward, strm->req.total,
3607 strm->req.analyse_exp ?
3608 human_time(TICKS_TO_MS(strm->req.analyse_exp - now_ms),
3609 TICKS_TO_MS(1000)) : "<NEVER>");
3610
3611 chunk_appendf(&trash,
3612 " rex=%s",
3613 strm->req.rex ?
3614 human_time(TICKS_TO_MS(strm->req.rex - now_ms),
3615 TICKS_TO_MS(1000)) : "<NEVER>");
3616
3617 chunk_appendf(&trash,
3618 " wex=%s\n"
3619 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
3620 strm->req.wex ?
3621 human_time(TICKS_TO_MS(strm->req.wex - now_ms),
3622 TICKS_TO_MS(1000)) : "<NEVER>",
3623 strm->req.buf,
3624 strm->req.buf->data, strm->req.buf->o,
3625 (int)(strm->req.buf->p - strm->req.buf->data),
3626 strm->txn ? strm->txn->req.next : 0, strm->req.buf->i,
3627 strm->req.buf->size);
3628
3629 chunk_appendf(&trash,
3630 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
3631 " an_exp=%s",
3632 &strm->res,
3633 strm->res.flags, strm->res.analysers,
3634 strm->res.pipe ? strm->res.pipe->data : 0,
3635 strm->res.to_forward, strm->res.total,
3636 strm->res.analyse_exp ?
3637 human_time(TICKS_TO_MS(strm->res.analyse_exp - now_ms),
3638 TICKS_TO_MS(1000)) : "<NEVER>");
3639
3640 chunk_appendf(&trash,
3641 " rex=%s",
3642 strm->res.rex ?
3643 human_time(TICKS_TO_MS(strm->res.rex - now_ms),
3644 TICKS_TO_MS(1000)) : "<NEVER>");
3645
3646 chunk_appendf(&trash,
3647 " wex=%s\n"
3648 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
3649 strm->res.wex ?
3650 human_time(TICKS_TO_MS(strm->res.wex - now_ms),
3651 TICKS_TO_MS(1000)) : "<NEVER>",
3652 strm->res.buf,
3653 strm->res.buf->data, strm->res.buf->o,
3654 (int)(strm->res.buf->p - strm->res.buf->data),
3655 strm->txn ? strm->txn->rsp.next : 0, strm->res.buf->i,
3656 strm->res.buf->size);
3657
3658 if (bi_putchk(si_ic(si), &trash) == -1) {
3659 si_applet_cant_put(si);
3660 return 0;
3661 }
3662
3663 /* use other states to dump the contents */
3664 }
3665 /* end of dump */
3666 appctx->ctx.sess.uid = 0;
3667 appctx->ctx.sess.section = 0;
3668 return 1;
3669}
3670
3671
3672static int cli_parse_show_sess(char **args, struct appctx *appctx, void *private)
3673{
3674 appctx->st2 = STAT_ST_INIT;
3675 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3676 return 1;
3677
3678 if (*args[2] && strcmp(args[2], "all") == 0)
3679 appctx->ctx.sess.target = (void *)-1;
3680 else if (*args[2])
3681 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
3682 else
3683 appctx->ctx.sess.target = NULL;
3684 appctx->ctx.sess.section = 0; /* start with stream status */
3685 appctx->ctx.sess.pos = 0;
3686
3687 return 0;
3688}
3689
3690/* This function dumps all streams' states onto the stream interface's
3691 * read buffer. It returns 0 if the output buffer is full and it needs
3692 * to be called again, otherwise non-zero. It is designed to be called
3693 * from stats_dump_sess_to_buffer() below.
3694 */
3695static int cli_io_handler_dump_sess(struct appctx *appctx)
3696{
3697 struct stream_interface *si = appctx->owner;
3698 struct connection *conn;
3699
3700 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
3701 /* If we're forced to shut down, we might have to remove our
3702 * reference to the last stream being dumped.
3703 */
3704 if (appctx->st2 == STAT_ST_LIST) {
3705 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
3706 LIST_DEL(&appctx->ctx.sess.bref.users);
3707 LIST_INIT(&appctx->ctx.sess.bref.users);
3708 }
3709 }
3710 return 1;
3711 }
3712
3713 chunk_reset(&trash);
3714
3715 switch (appctx->st2) {
3716 case STAT_ST_INIT:
3717 /* the function had not been called yet, let's prepare the
3718 * buffer for a response. We initialize the current stream
3719 * pointer to the first in the global list. When a target
3720 * stream is being destroyed, it is responsible for updating
3721 * this pointer. We know we have reached the end when this
3722 * pointer points back to the head of the streams list.
3723 */
3724 LIST_INIT(&appctx->ctx.sess.bref.users);
3725 appctx->ctx.sess.bref.ref = streams.n;
3726 appctx->st2 = STAT_ST_LIST;
3727 /* fall through */
3728
3729 case STAT_ST_LIST:
3730 /* first, let's detach the back-ref from a possible previous stream */
3731 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
3732 LIST_DEL(&appctx->ctx.sess.bref.users);
3733 LIST_INIT(&appctx->ctx.sess.bref.users);
3734 }
3735
3736 /* and start from where we stopped */
3737 while (appctx->ctx.sess.bref.ref != &streams) {
3738 char pn[INET6_ADDRSTRLEN];
3739 struct stream *curr_strm;
3740
3741 curr_strm = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
3742
3743 if (appctx->ctx.sess.target) {
3744 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_strm)
3745 goto next_sess;
3746
3747 LIST_ADDQ(&curr_strm->back_refs, &appctx->ctx.sess.bref.users);
3748 /* call the proper dump() function and return if we're missing space */
3749 if (!stats_dump_full_strm_to_buffer(si, curr_strm))
3750 return 0;
3751
3752 /* stream dump complete */
3753 LIST_DEL(&appctx->ctx.sess.bref.users);
3754 LIST_INIT(&appctx->ctx.sess.bref.users);
3755 if (appctx->ctx.sess.target != (void *)-1) {
3756 appctx->ctx.sess.target = NULL;
3757 break;
3758 }
3759 else
3760 goto next_sess;
3761 }
3762
3763 chunk_appendf(&trash,
3764 "%p: proto=%s",
3765 curr_strm,
3766 strm_li(curr_strm) ? strm_li(curr_strm)->proto->name : "?");
3767
3768 conn = objt_conn(strm_orig(curr_strm));
3769 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
3770 case AF_INET:
3771 case AF_INET6:
3772 chunk_appendf(&trash,
3773 " src=%s:%d fe=%s be=%s srv=%s",
3774 pn,
3775 get_host_port(&conn->addr.from),
3776 strm_fe(curr_strm)->id,
3777 (curr_strm->be->cap & PR_CAP_BE) ? curr_strm->be->id : "<NONE>",
3778 objt_server(curr_strm->target) ? objt_server(curr_strm->target)->id : "<none>"
3779 );
3780 break;
3781 case AF_UNIX:
3782 chunk_appendf(&trash,
3783 " src=unix:%d fe=%s be=%s srv=%s",
3784 strm_li(curr_strm)->luid,
3785 strm_fe(curr_strm)->id,
3786 (curr_strm->be->cap & PR_CAP_BE) ? curr_strm->be->id : "<NONE>",
3787 objt_server(curr_strm->target) ? objt_server(curr_strm->target)->id : "<none>"
3788 );
3789 break;
3790 }
3791
3792 chunk_appendf(&trash,
3793 " ts=%02x age=%s calls=%d",
3794 curr_strm->task->state,
3795 human_time(now.tv_sec - curr_strm->logs.tv_accept.tv_sec, 1),
3796 curr_strm->task->calls);
3797
3798 chunk_appendf(&trash,
3799 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
3800 curr_strm->req.flags,
3801 curr_strm->req.buf->i,
3802 curr_strm->req.analysers,
3803 curr_strm->req.rex ?
3804 human_time(TICKS_TO_MS(curr_strm->req.rex - now_ms),
3805 TICKS_TO_MS(1000)) : "");
3806
3807 chunk_appendf(&trash,
3808 ",wx=%s",
3809 curr_strm->req.wex ?
3810 human_time(TICKS_TO_MS(curr_strm->req.wex - now_ms),
3811 TICKS_TO_MS(1000)) : "");
3812
3813 chunk_appendf(&trash,
3814 ",ax=%s]",
3815 curr_strm->req.analyse_exp ?
3816 human_time(TICKS_TO_MS(curr_strm->req.analyse_exp - now_ms),
3817 TICKS_TO_MS(1000)) : "");
3818
3819 chunk_appendf(&trash,
3820 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
3821 curr_strm->res.flags,
3822 curr_strm->res.buf->i,
3823 curr_strm->res.analysers,
3824 curr_strm->res.rex ?
3825 human_time(TICKS_TO_MS(curr_strm->res.rex - now_ms),
3826 TICKS_TO_MS(1000)) : "");
3827
3828 chunk_appendf(&trash,
3829 ",wx=%s",
3830 curr_strm->res.wex ?
3831 human_time(TICKS_TO_MS(curr_strm->res.wex - now_ms),
3832 TICKS_TO_MS(1000)) : "");
3833
3834 chunk_appendf(&trash,
3835 ",ax=%s]",
3836 curr_strm->res.analyse_exp ?
3837 human_time(TICKS_TO_MS(curr_strm->res.analyse_exp - now_ms),
3838 TICKS_TO_MS(1000)) : "");
3839
3840 conn = objt_conn(curr_strm->si[0].end);
3841 chunk_appendf(&trash,
3842 " s0=[%d,%1xh,fd=%d,ex=%s]",
3843 curr_strm->si[0].state,
3844 curr_strm->si[0].flags,
3845 conn ? conn->t.sock.fd : -1,
3846 curr_strm->si[0].exp ?
3847 human_time(TICKS_TO_MS(curr_strm->si[0].exp - now_ms),
3848 TICKS_TO_MS(1000)) : "");
3849
3850 conn = objt_conn(curr_strm->si[1].end);
3851 chunk_appendf(&trash,
3852 " s1=[%d,%1xh,fd=%d,ex=%s]",
3853 curr_strm->si[1].state,
3854 curr_strm->si[1].flags,
3855 conn ? conn->t.sock.fd : -1,
3856 curr_strm->si[1].exp ?
3857 human_time(TICKS_TO_MS(curr_strm->si[1].exp - now_ms),
3858 TICKS_TO_MS(1000)) : "");
3859
3860 chunk_appendf(&trash,
3861 " exp=%s",
3862 curr_strm->task->expire ?
3863 human_time(TICKS_TO_MS(curr_strm->task->expire - now_ms),
3864 TICKS_TO_MS(1000)) : "");
3865 if (task_in_rq(curr_strm->task))
3866 chunk_appendf(&trash, " run(nice=%d)", curr_strm->task->nice);
3867
3868 chunk_appendf(&trash, "\n");
3869
3870 if (bi_putchk(si_ic(si), &trash) == -1) {
3871 /* let's try again later from this stream. We add ourselves into
3872 * this stream's users so that it can remove us upon termination.
3873 */
3874 si_applet_cant_put(si);
3875 LIST_ADDQ(&curr_strm->back_refs, &appctx->ctx.sess.bref.users);
3876 return 0;
3877 }
3878
3879 next_sess:
3880 appctx->ctx.sess.bref.ref = curr_strm->list.n;
3881 }
3882
3883 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
3884 /* specified stream not found */
3885 if (appctx->ctx.sess.section > 0)
3886 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3887 else
3888 chunk_appendf(&trash, "Session not found.\n");
3889
3890 if (bi_putchk(si_ic(si), &trash) == -1) {
3891 si_applet_cant_put(si);
3892 return 0;
3893 }
3894
3895 appctx->ctx.sess.target = NULL;
3896 appctx->ctx.sess.uid = 0;
3897 return 1;
3898 }
3899
3900 appctx->st2 = STAT_ST_FIN;
3901 /* fall through */
3902
3903 default:
3904 appctx->st2 = STAT_ST_FIN;
3905 return 1;
3906 }
3907}
3908
3909static void cli_release_show_sess(struct appctx *appctx)
3910{
3911 if (appctx->st2 == STAT_ST_LIST) {
3912 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
3913 LIST_DEL(&appctx->ctx.sess.bref.users);
3914 }
3915}
3916
3917/* register cli keywords */
3918static struct cli_kw_list cli_kws = {{ },{
3919 { { "show", "sess", NULL }, "show sess [id] : report the list of current sessions or dump this session", cli_parse_show_sess, cli_io_handler_dump_sess, cli_release_show_sess },
3920 {{},}
3921}};
3922
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003923/* main configuration keyword registration. */
3924static struct action_kw_list stream_tcp_keywords = { ILH, {
3925 { "use-service", stream_parse_use_service },
3926 { /* END */ }
3927}};
3928
3929static struct action_kw_list stream_http_keywords = { ILH, {
3930 { "use-service", stream_parse_use_service },
3931 { /* END */ }
3932}};
3933
Willy Tarreau61612d42012-04-19 18:42:05 +02003934/* Note: must not be declared <const> as its list will be overwritten.
3935 * Please take care of keeping this list alphabetically sorted.
3936 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003937static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003938 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003939}};
3940
Willy Tarreau281c7992013-01-08 01:23:27 +01003941/* Note: must not be declared <const> as its list will be overwritten.
3942 * Please take care of keeping this list alphabetically sorted.
3943 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003944static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003945 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3946 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3947 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3948 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3949 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3950 { "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 +02003951 { "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 +02003952 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3953 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3954 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3955 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3956 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3957 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3958 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3959 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3960 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3961 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3962 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3963 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3964 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003965 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3966 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3967 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3968 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3969 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3970 { "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 +02003971 { "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 +02003972 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3973 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3974 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3975 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3976 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3977 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3978 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3979 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3980 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3981 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3982 { "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 +02003983 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003984 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3985 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3986 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3987 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3988 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3989 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3990 { "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 +02003991 { "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 +02003992 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3993 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3994 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3995 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3996 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3997 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3998 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3999 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4000 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4001 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4002 { "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 +02004003 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004004 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4005 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4006 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4007 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4008 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4009 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4010 { "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 +02004011 { "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 +02004012 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4013 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4014 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4015 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4016 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4017 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4018 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4019 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4020 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4021 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4022 { "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 +02004023 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02004024 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4025 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4026 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4027 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4028 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4029 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4030 { "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 +02004031 { "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 +02004032 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4033 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4034 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4035 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4036 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4037 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4038 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4039 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4040 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4041 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4042 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4043 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4044 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4045 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01004046 { /* END */ },
4047}};
4048
Willy Tarreau8b22a712010-06-18 17:46:06 +02004049__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02004050static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02004051{
Willy Tarreau281c7992013-01-08 01:23:27 +01004052 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02004053 acl_register_keywords(&acl_kws);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02004054 tcp_req_cont_keywords_register(&stream_tcp_keywords);
4055 http_req_keywords_register(&stream_http_keywords);
William Lallemand4c5b4d52016-11-21 08:51:11 +01004056 cli_register_kw(&cli_kws);
Willy Tarreau8b22a712010-06-18 17:46:06 +02004057}
4058
Willy Tarreaubaaee002006-06-26 02:48:02 +02004059/*
4060 * Local variables:
4061 * c-indent-level: 8
4062 * c-basic-offset: 8
4063 * End:
4064 */