blob: 2b2d39939efbedf0c1a2c730a104ebda7cb5ef62 [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>
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020021#include <common/hathreads.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010022#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010023#include <common/initcall.h>
24#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020026#include <types/applet.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <types/capture.h>
William Lallemand4c5b4d52016-11-21 08:51:11 +010028#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020029#include <types/filters.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010030#include <types/global.h>
William Lallemand9ed62032016-11-21 17:49:11 +010031#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020033#include <proto/acl.h>
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020034#include <proto/action.h>
Willy Tarreau609aad92018-11-22 08:31:09 +010035#include <proto/activity.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020036#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010037#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020038#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010039#include <proto/checks.h>
William Lallemand4c5b4d52016-11-21 08:51:11 +010040#include <proto/cli.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020041#include <proto/connection.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010042#include <proto/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010043#include <proto/stats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020044#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020045#include <proto/filters.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020046#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020047#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010048#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010049#include <proto/hlua.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020050#include <proto/http_rules.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020051#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020052#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020053#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020054#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020055#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010056#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010057#include <proto/proto_http.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020058#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020061#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010062#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010063#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010064#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010065#include <proto/tcp_rules.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020066#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020067
Willy Tarreau8ceae722018-11-26 11:58:30 +010068DECLARE_POOL(pool_head_stream, "stream", sizeof(struct stream));
69
Willy Tarreaub6b3df32018-11-26 16:31:20 +010070struct list streams = LIST_HEAD_INIT(streams);
Willy Tarreau86abe442018-11-25 20:12:18 +010071__decl_spinlock(streams_lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +020072
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020073/* List of all use-service keywords. */
74static struct list service_keywords = LIST_HEAD_INIT(service_keywords);
75
Willy Tarreau5790eb02017-08-28 17:18:36 +020076
77/* Create a new stream for connection <conn>. Return < 0 on error. This is only
78 * valid right after the handshake, before the connection's data layer is
79 * initialized, because it relies on the session to be in conn->owner.
80 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +020081int stream_create_from_cs(struct conn_stream *cs)
Willy Tarreau5790eb02017-08-28 17:18:36 +020082{
83 struct stream *strm;
84
Olivier Houchard9aaf7782017-09-13 18:30:23 +020085 strm = stream_new(cs->conn->owner, &cs->obj_type);
Willy Tarreau5790eb02017-08-28 17:18:36 +020086 if (strm == NULL)
87 return -1;
88
89 task_wakeup(strm->task, TASK_WOKEN_INIT);
90 return 0;
91}
92
Willy Tarreaub882dd82018-11-06 15:50:21 +010093/* Callback used to wake up a stream when an input buffer is available. The
94 * stream <s>'s stream interfaces are checked for a failed buffer allocation
Willy Tarreaud0f5bbc2018-11-14 11:10:26 +010095 * as indicated by the presence of the SI_FL_RXBLK_ROOM flag and the lack of a
Willy Tarreaub882dd82018-11-06 15:50:21 +010096 * buffer, and and input buffer is assigned there (at most one). The function
97 * returns 1 and wakes the stream up if a buffer was taken, otherwise zero.
98 * It's designed to be called from __offer_buffer().
99 */
100int stream_buf_available(void *arg)
101{
102 struct stream *s = arg;
103
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100104 if (!s->req.buf.size && !s->req.pipe && (s->si[0].flags & SI_FL_RXBLK_BUFF) &&
Willy Tarreaub882dd82018-11-06 15:50:21 +0100105 b_alloc_margin(&s->req.buf, global.tune.reserved_bufs))
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100106 si_rx_buff_rdy(&s->si[0]);
107 else if (!s->res.buf.size && !s->res.pipe && (s->si[1].flags & SI_FL_RXBLK_BUFF) &&
Willy Tarreaub882dd82018-11-06 15:50:21 +0100108 b_alloc_margin(&s->res.buf, 0))
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100109 si_rx_buff_rdy(&s->si[1]);
Willy Tarreaub882dd82018-11-06 15:50:21 +0100110 else
111 return 0;
112
113 task_wakeup(s->task, TASK_WOKEN_RES);
114 return 1;
115
116}
117
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200118/* This function is called from the session handler which detects the end of
Willy Tarreau73b65ac2015-04-08 18:26:29 +0200119 * handshake, in order to complete initialization of a valid stream. It must be
Joseph Herlant4cc8d0d2018-11-15 09:14:14 -0800120 * called with a completely initialized session. It returns the pointer to
Willy Tarreau73b65ac2015-04-08 18:26:29 +0200121 * the newly created stream, or NULL in case of fatal error. The client-facing
Willy Tarreau87787ac2017-08-28 16:22:54 +0200122 * end point is assigned to <origin>, which must be valid. The stream's task
123 * is configured with a nice value inherited from the listener's nice if any.
124 * The task's context is set to the new stream, and its function is set to
125 * process_stream(). Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +0200126 */
Willy Tarreau87787ac2017-08-28 16:22:54 +0200127struct stream *stream_new(struct session *sess, enum obj_type *origin)
Willy Tarreau2542b532012-08-31 16:01:23 +0200128{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200129 struct stream *s;
Willy Tarreau87787ac2017-08-28 16:22:54 +0200130 struct task *t;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200131 struct conn_stream *cs = objt_cs(origin);
Willy Tarreau73b65ac2015-04-08 18:26:29 +0200132 struct appctx *appctx = objt_appctx(origin);
Christopher Fauletb3484d62018-11-29 15:19:05 +0100133 const struct cs_info *csinfo;
Willy Tarreau2542b532012-08-31 16:01:23 +0200134
Willy Tarreaubafbe012017-11-24 17:34:44 +0100135 if (unlikely((s = pool_alloc(pool_head_stream)) == NULL))
Willy Tarreau87787ac2017-08-28 16:22:54 +0200136 goto out_fail_alloc;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200137
138 /* minimum stream initialization required for an embryonic stream is
139 * fairly low. We need very little to execute L4 ACLs, then we need a
140 * task to make the client-side connection live on its own.
141 * - flags
142 * - stick-entry tracking
143 */
144 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200145 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200146 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200147 tv_zero(&s->logs.tv_request);
148 s->logs.t_queue = -1;
149 s->logs.t_connect = -1;
150 s->logs.t_data = -1;
151 s->logs.t_close = 0;
152 s->logs.bytes_in = s->logs.bytes_out = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -0400153 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
154 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Baptiste Assmann333939c2019-01-21 08:34:50 +0100155 s->obj_type = OBJ_TYPE_STREAM;
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200156
Christopher Fauletb3484d62018-11-29 15:19:05 +0100157 csinfo = si_get_cs_info(cs);
158 if (csinfo) {
159 s->logs.accept_date = csinfo->create_date;
160 s->logs.tv_accept = csinfo->tv_create;
161 s->logs.t_handshake = csinfo->t_handshake;
162 s->logs.t_idle = csinfo->t_idle;
163 }
164 else {
165 s->logs.accept_date = sess->accept_date;
166 s->logs.tv_accept = sess->tv_accept;
167 s->logs.t_handshake = sess->t_handshake;
168 s->logs.t_idle = -1;
169 }
170
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200171 /* default logging function */
172 s->do_log = strm_log;
173
174 /* default error reporting function, may be changed by analysers */
175 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200176
177 /* Initialise the current rule list pointer to NULL. We are sure that
178 * any rulelist match the NULL pointer.
179 */
180 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200181 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200182
Willy Tarreaua68f7622015-09-21 17:48:24 +0200183 /* Copy SC counters for the stream. We don't touch refcounts because
184 * any reference we have is inherited from the session. Since the stream
185 * doesn't exist without the session, the session's existence guarantees
186 * we don't lose the entry. During the store operation, the stream won't
187 * touch these ones.
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200188 */
Thierry FOURNIERc8fdb982015-08-16 12:03:39 +0200189 memcpy(s->stkctr, sess->stkctr, sizeof(s->stkctr));
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200190
191 s->sess = sess;
192 s->si[0].flags = SI_FL_NONE;
193 s->si[1].flags = SI_FL_ISBACK;
194
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100195 s->uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200196
Willy Tarreau87b09662015-04-03 00:22:06 +0200197 /* OK, we're keeping the stream, so let's properly initialize the stream */
Willy Tarreau2542b532012-08-31 16:01:23 +0200198 LIST_INIT(&s->back_refs);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100199
200 LIST_INIT(&s->buffer_wait.list);
201 s->buffer_wait.target = s;
Willy Tarreaub882dd82018-11-06 15:50:21 +0100202 s->buffer_wait.wakeup_cb = stream_buf_available;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200203
Willy Tarreau2e9c1d22019-04-24 08:28:31 +0200204 s->call_rate.curr_sec = s->call_rate.curr_ctr = s->call_rate.prev_ctr = 0;
William Lallemandcf62f7e2018-10-26 14:47:40 +0200205 s->pcli_next_pid = 0;
William Lallemandebf61802018-12-11 16:10:57 +0100206 s->pcli_flags = 0;
Willy Tarreau2542b532012-08-31 16:01:23 +0200207 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200208
Emeric Brunc60def82017-09-27 14:59:38 +0200209 if ((t = task_new(tid_bit)) == NULL)
Willy Tarreau87787ac2017-08-28 16:22:54 +0200210 goto out_fail_alloc;
211
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200212 s->task = t;
Christopher Faulet9d810ca2016-12-08 22:33:52 +0100213 s->pending_events = 0;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200214 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200215 t->context = s;
216 t->expire = TICK_ETERNITY;
Willy Tarreau87787ac2017-08-28 16:22:54 +0200217 if (sess->listener)
218 t->nice = sess->listener->nice;
Willy Tarreau2542b532012-08-31 16:01:23 +0200219
Willy Tarreau87b09662015-04-03 00:22:06 +0200220 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200221 * This changes later when switching rules are executed or
222 * when the default backend is assigned.
223 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200224 s->be = sess->fe;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200225 s->req.buf = BUF_NULL;
226 s->res.buf = BUF_NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200227 s->req_cap = NULL;
228 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200229
Willy Tarreauebcd4842015-06-19 11:59:02 +0200230 /* Initialise all the variables contexts even if not used.
231 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200232 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200233 vars_init(&s->vars_txn, SCOPE_TXN);
234 vars_init(&s->vars_reqres, SCOPE_REQ);
235
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200236 /* this part should be common with other protocols */
Olivier Houchard8f0b4c62018-08-02 18:21:38 +0200237 if (si_reset(&s->si[0]) < 0)
238 goto out_fail_alloc;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200239 si_set_state(&s->si[0], SI_ST_EST);
Hongbo Longe39683c2017-03-10 18:41:51 +0100240 s->si[0].hcto = sess->fe->timeout.clientfin;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200241
Christopher Faulet0e160ff2019-04-03 10:01:07 +0200242 if (cs && cs->conn->mux) {
243 if (cs->conn->mux->flags & MX_FL_CLEAN_ABRT)
244 s->si[0].flags |= SI_FL_CLEAN_ABRT;
245 if (cs->conn->mux->flags & MX_FL_HTX)
246 s->flags |= SF_HTX;
247 }
248 /* Set SF_HTX flag for HTX frontends. */
249 if (sess->fe->mode == PR_MODE_HTTP && sess->fe->options2 & PR_O2_USE_HTX)
250 s->flags |= SF_HTX;
Willy Tarreau984fca92017-12-20 16:31:43 +0100251
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200252 /* attach the incoming connection to the stream interface now. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200253 if (cs)
254 si_attach_cs(&s->si[0], cs);
Willy Tarreau470280e2015-04-05 01:33:13 +0200255 else if (appctx)
256 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200257
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200258 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200259 s->si[0].flags |= SI_FL_INDEP_STR;
260
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200261 /* pre-initialize the other side's stream interface to an INIT state. The
262 * callbacks will be initialized before attempting to connect.
263 */
Olivier Houchard8f0b4c62018-08-02 18:21:38 +0200264 if (si_reset(&s->si[1]) < 0)
265 goto out_fail_alloc_si1;
Hongbo Longe39683c2017-03-10 18:41:51 +0100266 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200267
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200268 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200269 s->si[1].flags |= SI_FL_INDEP_STR;
270
Willy Tarreau87b09662015-04-03 00:22:06 +0200271 stream_init_srv_conn(s);
Willy Tarreau9b82d942016-12-05 00:26:31 +0100272 s->target = sess->listener ? sess->listener->default_target : NULL;
273
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200274 s->pend_pos = NULL;
Patrick Hemmer268a7072018-05-11 12:52:31 -0400275 s->priority_class = 0;
276 s->priority_offset = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200277
278 /* init store persistence */
279 s->store_count = 0;
280
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100281 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100282 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau9b82d942016-12-05 00:26:31 +0100283 s->req.analysers = sess->listener ? sess->listener->analysers : 0;
Willy Tarreaue223e3b2017-11-18 15:39:10 +0100284
285 if (!sess->fe->fe_req_ana) {
286 channel_auto_connect(&s->req); /* don't wait to establish connection */
287 channel_auto_close(&s->req); /* let the producer forward close requests */
288 }
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200289
290 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100291 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100292 s->req.rex = TICK_ETERNITY;
293 s->req.wex = TICK_ETERNITY;
294 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200295
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100296 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100297 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100298 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200299
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200300 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100301 s->req.flags |= CF_NEVER_WAIT;
302 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200303 }
304
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200305 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100306 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100307 s->res.rex = TICK_ETERNITY;
308 s->res.wex = TICK_ETERNITY;
309 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200310
Willy Tarreaueee5b512015-04-03 23:46:31 +0200311 s->txn = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100312 s->hlua = NULL;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100313
Frédéric Lécaillebed883a2019-04-23 17:26:33 +0200314 s->dns_ctx.dns_requester = NULL;
315 s->dns_ctx.hostname_dn = NULL;
316 s->dns_ctx.hostname_dn_len = 0;
317 s->dns_ctx.parent = NULL;
318
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100319 HA_SPIN_LOCK(STRMS_LOCK, &streams_lock);
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200320 LIST_ADDQ(&streams, &s->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100321 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200322
Christopher Faulet92d36382015-11-05 13:35:03 +0100323 if (flt_stream_init(s) < 0 || flt_stream_start(s) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200324 goto out_fail_accept;
325
Olivier Houcharda254a372019-04-05 15:30:12 +0200326 s->si[1].l7_buffer = BUF_NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200327 /* finish initialization of the accepted file descriptor */
Olivier Houchard53216e72018-10-10 15:46:36 +0200328 if (appctx)
Willy Tarreau0cd3bd62018-11-06 18:46:37 +0100329 si_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200330
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200331 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200332 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200333
334 /* it is important not to call the wakeup function directly but to
335 * pass through task_wakeup(), because this one knows how to apply
Emeric Brun5f77fef2017-05-29 15:26:51 +0200336 * priorities to tasks. Using multi thread we must be sure that
337 * stream is fully initialized before calling task_wakeup. So
338 * the caller must handle the task_wakeup
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200339 */
Willy Tarreau02d86382015-04-05 12:00:52 +0200340 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200341
342 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200343 out_fail_accept:
Christopher Faulet92d36382015-11-05 13:35:03 +0100344 flt_stream_release(s, 0);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200345 task_destroy(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200346 tasklet_free(s->si[1].wait_event.task);
Willy Tarreaue5f229e2018-10-15 11:08:55 +0200347 LIST_DEL(&s->list);
Olivier Houchard8f0b4c62018-08-02 18:21:38 +0200348out_fail_alloc_si1:
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200349 tasklet_free(s->si[0].wait_event.task);
Willy Tarreau87787ac2017-08-28 16:22:54 +0200350 out_fail_alloc:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100351 pool_free(pool_head_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200352 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200353}
354
Willy Tarreaubaaee002006-06-26 02:48:02 +0200355/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200356 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200358static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200360 struct session *sess = strm_sess(s);
361 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100362 struct bref *bref, *back;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200363 struct conn_stream *cli_cs = objt_cs(s->si[0].end);
Willy Tarreau5bcfd562017-11-29 14:05:38 +0100364 int must_free_sess;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200365 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100366
Willy Tarreau0ad46fa2019-05-17 14:20:05 +0200367 /* detach the stream from its own task before even releasing it so
368 * that walking over a task list never exhibits a dying stream.
369 */
370 s->task->context = NULL;
371 __ha_barrier_store();
372
Willy Tarreaud0ad4a82018-07-25 11:13:53 +0200373 pendconn_free(s);
Willy Tarreau922a8062008-12-04 09:33:58 +0100374
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100375 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200376 if (s->flags & SF_CURR_SESS) {
377 s->flags &= ~SF_CURR_SESS;
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100378 _HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100379 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100380 if (may_dequeue_tasks(objt_server(s->target), s->be))
381 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100382 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100383
Willy Tarreau7c669d72008-06-20 15:04:11 +0200384 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200385 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200386 * it should normally be only the same as the one above,
387 * so this should not happen in fact.
388 */
389 sess_change_server(s, NULL);
390 }
391
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100392 if (s->req.pipe)
393 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100394
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100395 if (s->res.pipe)
396 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100397
Willy Tarreaubf883e02014-11-25 21:10:35 +0100398 /* We may still be present in the buffer wait queue */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100399 if (!LIST_ISEMPTY(&s->buffer_wait.list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100400 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100401 LIST_DEL(&s->buffer_wait.list);
402 LIST_INIT(&s->buffer_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100403 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100404 }
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200405 if (s->req.buf.size || s->res.buf.size) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100406 b_drop(&s->req.buf);
407 b_drop(&s->res.buf);
Olivier Houchard673867c2018-05-25 16:58:52 +0200408 offer_buffers(NULL, tasks_run_queue);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100409 }
Willy Tarreau9b28e032012-10-12 23:49:43 +0200410
Olivier Houchard32211a12019-02-01 18:10:46 +0100411 pool_free(pool_head_uniqueid, s->unique_id);
Willy Tarreau09c4bab2019-02-10 18:49:37 +0100412 s->unique_id = NULL;
Olivier Houchard32211a12019-02-01 18:10:46 +0100413
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100414 hlua_ctx_destroy(s->hlua);
415 s->hlua = NULL;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200416 if (s->txn)
417 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100418
Willy Tarreau1e954912012-10-12 17:50:05 +0200419 /* ensure the client-side transport layer is destroyed */
Willy Tarreau32560732017-10-08 11:33:44 +0200420 if (cli_cs)
421 cs_close(cli_cs);
Willy Tarreau1e954912012-10-12 17:50:05 +0200422
Willy Tarreaua4cda672010-06-06 18:28:49 +0200423 for (i = 0; i < s->store_count; i++) {
424 if (!s->store[i].ts)
425 continue;
426 stksess_free(s->store[i].table, s->store[i].ts);
427 s->store[i].ts = NULL;
428 }
429
Willy Tarreaueee5b512015-04-03 23:46:31 +0200430 if (s->txn) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100431 pool_free(pool_head_hdr_idx, s->txn->hdr_idx.v);
432 pool_free(pool_head_http_txn, s->txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200433 s->txn = NULL;
434 }
435
Baptiste Assmann333939c2019-01-21 08:34:50 +0100436 if (s->dns_ctx.dns_requester) {
437 free(s->dns_ctx.hostname_dn); s->dns_ctx.hostname_dn = NULL;
438 s->dns_ctx.hostname_dn_len = 0;
439 dns_unlink_resolution(s->dns_ctx.dns_requester);
440
441 pool_free(dns_requester_pool, s->dns_ctx.dns_requester);
442 s->dns_ctx.dns_requester = NULL;
443 }
444
Christopher Fauletd7c91962015-04-30 11:48:27 +0200445 flt_stream_stop(s);
Christopher Faulet92d36382015-11-05 13:35:03 +0100446 flt_stream_release(s, 0);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200447
Willy Tarreau92fb9832007-10-16 17:34:28 +0200448 if (fe) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100449 pool_free(fe->rsp_cap_pool, s->res_cap);
450 pool_free(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200451 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100452
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200453 /* Cleanup all variable contexts. */
Willy Tarreaucda7f3f2018-10-28 13:44:36 +0100454 if (!LIST_ISEMPTY(&s->vars_txn.head))
455 vars_prune(&s->vars_txn, s->sess, s);
456 if (!LIST_ISEMPTY(&s->vars_reqres.head))
457 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200458
Willy Tarreau87b09662015-04-03 00:22:06 +0200459 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200460
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100461 HA_SPIN_LOCK(STRMS_LOCK, &streams_lock);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100462 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100463 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200464 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100465 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100466 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100467 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200468 if (s->list.n != &streams)
469 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100470 bref->ref = s->list.n;
471 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100472 LIST_DEL(&s->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100473 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200474
Willy Tarreau5bcfd562017-11-29 14:05:38 +0100475 /* applets do not release session yet */
476 must_free_sess = objt_appctx(sess->origin) && sess->origin == s->si[0].end;
477
Olivier Houchard4fdec7a2018-10-11 17:09:14 +0200478
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200479 si_release_endpoint(&s->si[1]);
480 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200481
Olivier Houchardd7bd3e32018-10-18 16:22:02 +0200482 tasklet_free(s->si[0].wait_event.task);
483 tasklet_free(s->si[1].wait_event.task);
484
Olivier Houcharda254a372019-04-05 15:30:12 +0200485 b_free(&s->si[1].l7_buffer);
Olivier Houchard25607af2018-11-23 14:50:47 +0100486 if (must_free_sess) {
487 sess->origin = NULL;
Willy Tarreau5bcfd562017-11-29 14:05:38 +0100488 session_free(sess);
Olivier Houchard25607af2018-11-23 14:50:47 +0100489 }
Willy Tarreau5bcfd562017-11-29 14:05:38 +0100490
Willy Tarreaubafbe012017-11-24 17:34:44 +0100491 pool_free(pool_head_stream, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200492
493 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200494 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100495 pool_flush(pool_head_buffer);
496 pool_flush(pool_head_http_txn);
497 pool_flush(pool_head_hdr_idx);
498 pool_flush(pool_head_requri);
499 pool_flush(pool_head_capture);
500 pool_flush(pool_head_stream);
501 pool_flush(pool_head_session);
502 pool_flush(pool_head_connection);
503 pool_flush(pool_head_pendconn);
504 pool_flush(fe->req_cap_pool);
505 pool_flush(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200506 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200507}
508
Willy Tarreau656859d2014-11-25 19:46:36 +0100509
Willy Tarreau87b09662015-04-03 00:22:06 +0200510/* Allocates a work buffer for stream <s>. It is meant to be called inside
511 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200512 * to work fine, which is the response buffer so that an error message may be
513 * built and returned. Response buffers may be allocated from the reserve, this
514 * is critical to ensure that a response may always flow and will never block a
515 * server from releasing a connection. Returns 0 in case of failure, non-zero
516 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100517 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100518static int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100519{
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100520 if (!LIST_ISEMPTY(&s->buffer_wait.list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100521 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100522 LIST_DEL(&s->buffer_wait.list);
523 LIST_INIT(&s->buffer_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100524 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100525 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100526
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200527 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100528 return 1;
529
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100530 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100531 LIST_ADDQ(&buffer_wq, &s->buffer_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100532 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau656859d2014-11-25 19:46:36 +0100533 return 0;
534}
535
536/* releases unused buffers after processing. Typically used at the end of the
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100537 * update() functions. It will try to wake up as many tasks/applets as the
538 * number of buffers that it releases. In practice, most often streams are
539 * blocked on a single buffer, so it makes sense to try to wake two up when two
540 * buffers are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100541 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200542void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100543{
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100544 int offer = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100545
Willy Tarreau0c7ed5d2018-07-10 09:53:31 +0200546 if (c_size(&s->req) && c_empty(&s->req)) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100547 offer = 1;
548 b_free(&s->req.buf);
549 }
Willy Tarreau0c7ed5d2018-07-10 09:53:31 +0200550 if (c_size(&s->res) && c_empty(&s->res)) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100551 offer = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100552 b_free(&s->res.buf);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100553 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100554
Willy Tarreaubf883e02014-11-25 21:10:35 +0100555 /* if we're certain to have at least 1 buffer available, and there is
556 * someone waiting, we can wake up a waiter and offer them.
557 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100558 if (offer)
Olivier Houchard673867c2018-05-25 16:58:52 +0200559 offer_buffers(s, tasks_run_queue);
Willy Tarreau656859d2014-11-25 19:46:36 +0100560}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200561
Willy Tarreau87b09662015-04-03 00:22:06 +0200562void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100563{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200564 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100565 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200566 void *ptr1,*ptr2;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200567 struct stksess *ts;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100568 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100569
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100570 bytes = s->req.total - s->logs.bytes_in;
571 s->logs.bytes_in = s->req.total;
572 if (bytes) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100573 _HA_ATOMIC_ADD(&sess->fe->fe_counters.bytes_in, bytes);
574 _HA_ATOMIC_ADD(&s->be->be_counters.bytes_in, bytes);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100575
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100576 if (objt_server(s->target))
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100577 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.bytes_in, bytes);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200578
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200579 if (sess->listener && sess->listener->counters)
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100580 _HA_ATOMIC_ADD(&sess->listener->counters->bytes_in, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200581
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100582 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200583 struct stkctr *stkctr = &s->stkctr[i];
584
Emeric Brun819fc6f2017-06-13 19:37:32 +0200585 ts = stkctr_entry(stkctr);
586 if (!ts) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200587 stkctr = &sess->stkctr[i];
Emeric Brun819fc6f2017-06-13 19:37:32 +0200588 ts = stkctr_entry(stkctr);
589 if (!ts)
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200590 continue;
591 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200592
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100593 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200594 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_IN_CNT);
Emeric Brun57056f02015-06-15 18:29:57 +0200595 if (ptr1)
596 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200597
Emeric Brun819fc6f2017-06-13 19:37:32 +0200598 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_IN_RATE);
Emeric Brun57056f02015-06-15 18:29:57 +0200599 if (ptr2)
600 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200601 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100602 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun57056f02015-06-15 18:29:57 +0200603
604 /* If data was modified, we need to touch to re-schedule sync */
605 if (ptr1 || ptr2)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200606 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau30e71012007-11-26 20:15:35 +0100607 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100608 }
609
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100610 bytes = s->res.total - s->logs.bytes_out;
611 s->logs.bytes_out = s->res.total;
612 if (bytes) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100613 _HA_ATOMIC_ADD(&sess->fe->fe_counters.bytes_out, bytes);
614 _HA_ATOMIC_ADD(&s->be->be_counters.bytes_out, bytes);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100615
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100616 if (objt_server(s->target))
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100617 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.bytes_out, bytes);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200618
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200619 if (sess->listener && sess->listener->counters)
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100620 _HA_ATOMIC_ADD(&sess->listener->counters->bytes_out, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200621
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100622 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200623 struct stkctr *stkctr = &s->stkctr[i];
624
Emeric Brun819fc6f2017-06-13 19:37:32 +0200625 ts = stkctr_entry(stkctr);
626 if (!ts) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200627 stkctr = &sess->stkctr[i];
Emeric Brun819fc6f2017-06-13 19:37:32 +0200628 ts = stkctr_entry(stkctr);
629 if (!ts)
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200630 continue;
631 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200632
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100633 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200634 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Emeric Brun57056f02015-06-15 18:29:57 +0200635 if (ptr1)
636 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200637
Emeric Brun819fc6f2017-06-13 19:37:32 +0200638 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_OUT_RATE);
Emeric Brun57056f02015-06-15 18:29:57 +0200639 if (ptr2)
640 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200641 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100642 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun57056f02015-06-15 18:29:57 +0200643
644 /* If data was modified, we need to touch to re-schedule sync */
645 if (ptr1 || ptr2)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200646 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), 0);
Willy Tarreau30e71012007-11-26 20:15:35 +0100647 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100648 }
649}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100651/* This function is called with (si->state == SI_ST_CON) meaning that a
652 * connection was attempted and that the file descriptor is already allocated.
653 * We must check for establishment, error and abort. Possible output states
654 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
655 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200656 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100657 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200658static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100659{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100660 struct stream_interface *si = &s->si[1];
661 struct channel *req = &s->req;
662 struct channel *rep = &s->res;
Olivier Houchard14547b22018-11-29 18:01:46 +0100663 struct conn_stream *srv_cs = objt_cs(si->end);
Willy Tarreaubf66bd12019-01-23 15:15:09 +0100664 struct connection *conn = srv_cs ? srv_cs->conn : objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100665
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100666 /* If we got an error, or if nothing happened and the connection timed
667 * out, we must give up. The CER state handler will take care of retry
668 * attempts and error reports.
669 */
670 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Bin Wang95fad5b2017-09-15 14:56:40 +0800671 if (unlikely(req->flags & CF_WROTE_DATA)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100672 /* Some data were sent past the connection establishment,
673 * so we need to pretend we're established to log correctly
674 * and let later states handle the failure.
675 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100676 si->state = SI_ST_EST;
677 si->err_type = SI_ET_DATA_ERR;
Olivier Houchardf4bda992019-05-03 22:21:24 +0200678 /* Don't add CF_WRITE_ERROR if we're here because
679 * early data were rejected by the server, or
680 * http_wait_for_response() will never be called
681 * to send a 425.
682 */
683 if (conn->err_code != CO_ER_SSL_EARLY_FAILED)
684 req->flags |= CF_WRITE_ERROR;
Willy Tarreau28e581b2019-01-31 18:58:06 +0100685 rep->flags |= CF_READ_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100686 return 1;
687 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100688 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100689 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100690
Willy Tarreaubf66bd12019-01-23 15:15:09 +0100691 if (!(s->flags & SF_SRV_REUSED) && conn) {
Olivier Houchard14547b22018-11-29 18:01:46 +0100692 conn_stop_tracking(conn);
693 conn_full_close(conn);
Olivier Houchard14547b22018-11-29 18:01:46 +0100694 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100695
696 if (si->err_type)
697 return 0;
698
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100699 if (si->flags & SI_FL_ERR)
700 si->err_type = SI_ET_CONN_ERR;
701 else
702 si->err_type = SI_ET_CONN_TO;
703 return 0;
704 }
705
706 /* OK, maybe we want to abort */
Bin Wang95fad5b2017-09-15 14:56:40 +0800707 if (!(req->flags & CF_WROTE_DATA) &&
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100708 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200709 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauede3d882018-10-24 17:17:56 +0200710 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Christopher Faulet769d0e92019-03-22 14:23:18 +0100711 (s->be->options & PR_O_ABRT_CLOSE))))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100712 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200713 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100714 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100715 if (s->srv_error)
716 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100717 return 1;
718 }
719
Christopher Faulet7a3367c2019-05-09 11:46:52 +0200720 /* If the request channel is waiting for the connect(), we mark the read
721 * side as attached on the response channel and we wake up it once. So
722 * it will have a chance to forward data now.
723 */
724 if (req->flags & CF_WAKE_CONNECT) {
725 rep->flags |= CF_READ_ATTACHED;
726 req->flags |= CF_WAKE_ONCE;
727 req->flags &= ~CF_WAKE_CONNECT;
728 }
729
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100730 /* we need to wait a bit more if there was no activity either */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200731 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100732 return 1;
733
734 /* OK, this means that a connection succeeded. The caller will be
735 * responsible for handling the transition from CON to EST.
736 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 si->state = SI_ST_EST;
738 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739 return 1;
740}
741
742/* This function is called with (si->state == SI_ST_CER) meaning that a
743 * previous connection attempt has failed and that the file descriptor
744 * has already been released. Possible causes include asynchronous error
745 * notification and time out. Possible output states are SI_ST_CLO when
746 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
747 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
748 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
749 * marked as in error state. It returns 0.
750 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200751static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100752{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100753 struct stream_interface *si = &s->si[1];
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200754 struct conn_stream *cs = objt_cs(si->end);
755 struct connection *conn = cs_conn(cs);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100756
Willy Tarreau87b09662015-04-03 00:22:06 +0200757 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100758 if (objt_server(s->target)) {
759 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100760
Willy Tarreaue7dff022015-04-03 01:14:29 +0200761 if (s->flags & SF_CURR_SESS) {
762 s->flags &= ~SF_CURR_SESS;
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100763 _HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100764 }
Willy Tarreau46d5b082017-07-26 20:13:37 +0200765
766 if ((si->flags & SI_FL_ERR) &&
767 conn && conn->err_code == CO_ER_SSL_MISMATCH_SNI) {
768 /* We tried to connect to a server which is configured
769 * with "verify required" and which doesn't have the
770 * "verifyhost" directive. The server presented a wrong
771 * certificate (a certificate for an unexpected name),
772 * which implies that we have used SNI in the handshake,
773 * and that the server doesn't have the associated cert
774 * and presented a default one.
775 *
776 * This is a serious enough issue not to retry. It's
777 * especially important because this wrong name might
778 * either be the result of a configuration error, and
779 * retrying will only hammer the server, or is caused
780 * by the use of a wrong SNI value, most likely
781 * provided by the client and we don't want to let the
782 * client provoke retries.
783 */
784 si->conn_retries = 0;
785 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100786 }
787
788 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200789 si->conn_retries--;
Olivier Houcharda254a372019-04-05 15:30:12 +0200790 if (si->conn_retries < 0 || !(s->be->retry_type & PR_RE_CONN_FAILED)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100791 if (!si->err_type) {
792 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100793 }
794
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100795 if (objt_server(s->target))
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100796 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_conns, 1);
797 _HA_ATOMIC_ADD(&s->be->be_counters.failed_conns, 1);
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100798 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100799 if (may_dequeue_tasks(objt_server(s->target), s->be))
800 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100801
802 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200803 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100804 s->req.flags |= CF_WRITE_ERROR;
805 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100806
807 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100808 if (s->srv_error)
809 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100810 return 0;
811 }
812
813 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700814 * retry on another server. By default this redispatch occurs on the
815 * last retry, but if configured we allow redispatches to occur on
816 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200817 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100818 * bit to ignore any persistence cookie. We won't count a retry nor a
819 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200820 * If the connection is not persistent, the balancing algorithm is not
821 * determinist (round robin) and there is more than one active server,
822 * we accept to perform an immediate redispatch without waiting since
823 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100824 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200825 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700826 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200827 ((__objt_server(s->target)->cur_state < SRV_ST_RUNNING) ||
Willy Tarreau49008c12016-01-13 07:58:44 +0100828 (((s->be->redispatch_after > 0) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700829 ((s->be->conn_retries - si->conn_retries) %
830 s->be->redispatch_after == 0)) ||
831 ((s->be->redispatch_after < 0) &&
832 ((s->be->conn_retries - si->conn_retries) %
833 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200834 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700835 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100836 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100837 if (may_dequeue_tasks(objt_server(s->target), s->be))
838 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100839
Willy Tarreaue7dff022015-04-03 01:14:29 +0200840 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100841 si->state = SI_ST_REQ;
842 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100843 if (objt_server(s->target))
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100844 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
845 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100846 si->state = SI_ST_ASS;
847 }
848
849 if (si->flags & SI_FL_ERR) {
850 /* The error was an asynchronous connection error, and we will
851 * likely have to retry connecting to the same server, most
852 * likely leading to the same result. To avoid this, we wait
Willy Tarreauf7a259d2019-01-23 16:29:53 +0100853 * MIN(one second, connect timeout) before retrying. We don't
854 * do it when the failure happened on a reused connection
855 * though.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100856 */
857
Willy Tarreaub0290662014-06-13 17:04:44 +0200858 int delay = 1000;
859
860 if (s->be->timeout.connect && s->be->timeout.connect < delay)
861 delay = s->be->timeout.connect;
862
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100863 if (!si->err_type)
864 si->err_type = SI_ET_CONN_ERR;
865
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200866 /* only wait when we're retrying on the same server */
Willy Tarreauf7a259d2019-01-23 16:29:53 +0100867 if ((si->state == SI_ST_ASS ||
868 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
869 (s->be->srv_act <= 1)) && !(s->flags & SF_SRV_REUSED)) {
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200870 si->state = SI_ST_TAR;
871 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
872 }
Olivier Houchard39cc0202019-04-09 19:12:51 +0200873 si->flags &= ~SI_FL_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100874 return 0;
875 }
876 return 0;
877}
878
879/*
880 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200881 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200882 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100883 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200884static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100885{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100886 struct stream_interface *si = &s->si[1];
887 struct channel *req = &s->req;
888 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100889
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100890 /* First, centralize the timers information */
891 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
892 si->exp = TICK_ETERNITY;
893
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100894 if (objt_server(s->target))
895 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100896
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100897 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100898 /* if the user wants to log as soon as possible, without counting
899 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200900 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau66425e32018-07-25 06:55:12 +0200901 /* note: no pend_pos here, session is established */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100902 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100903 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100904 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100905 }
906 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100907 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100908 }
909
Willy Tarreau0007d0a2018-12-11 18:01:38 +0100910 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Christopher Faulet309c6412015-12-02 09:57:32 +0100911
Willy Tarreau0007d0a2018-12-11 18:01:38 +0100912 /* Be sure to filter response headers if the backend is an HTTP proxy
913 * and if there are filters attached to the stream. */
914 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
915 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
Christopher Faulet309c6412015-12-02 09:57:32 +0100916
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +0100917 si_rx_endp_more(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200918 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200919 if (objt_cs(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200920 /* real connections have timeouts */
921 req->wto = s->be->timeout.server;
922 rep->rto = s->be->timeout.server;
Olivier Houchard47e9a1a2018-11-07 17:55:19 +0100923 /* The connection is now established, try to read data from the
924 * underlying layer, and subscribe to recv events. We use a
925 * delayed recv here to give a chance to the data to flow back
926 * by the time we process other tasks.
927 */
928 si_chk_rcv(si);
Willy Tarreaud04e8582010-05-31 12:31:35 +0200929 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100930 req->wex = TICK_ETERNITY;
931}
932
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700933/* Check if the connection request is in such a state that it can be aborted. */
934static int check_req_may_abort(struct channel *req, struct stream *s)
935{
936 return ((req->flags & (CF_READ_ERROR)) ||
Willy Tarreaud651ba12018-01-12 10:42:12 +0100937 ((req->flags & (CF_SHUTW_NOW|CF_SHUTW)) && /* empty and client aborted */
Christopher Faulet769d0e92019-03-22 14:23:18 +0100938 (channel_is_empty(req) || (s->be->options & PR_O_ABRT_CLOSE))));
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700939}
940
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100941/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
942 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100943 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
944 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
945 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100946 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200947static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100948{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100949 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100950 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100951 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100952
Christopher Faulet45073512018-07-20 10:16:29 +0200953 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%lu rqt=%lu rph=%lu rpt=%lu cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100954 now_ms, __FUNCTION__,
955 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400956 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100957 req->rex, s->res.wex,
958 req->flags, s->res.flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200959 ci_data(req), co_data(req), ci_data(&s->res), co_data(&s->res), s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100960
961 if (si->state == SI_ST_ASS) {
962 /* Server assigned to connection request, we have to try to connect now */
963 int conn_err;
964
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700965 /* Before we try to initiate the connection, see if the
966 * request may be aborted instead.
967 */
968 if (check_req_may_abort(req, s)) {
969 si->err_type |= SI_ET_CONN_ABRT;
970 goto abort_connection;
971 }
972
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100973 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100974 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100975
Willy Tarreaue7dff022015-04-03 01:14:29 +0200976 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100977 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100978 if (srv)
979 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500980 if (srv)
981 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100982 return;
983 }
984
985 /* We have received a synchronous error. We might have to
986 * abort, retry immediately or redispatch.
987 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200988 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100989 if (!si->err_type) {
990 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100991 }
992
Willy Tarreau827aee92011-03-10 16:55:02 +0100993 if (srv)
994 srv_inc_sess_ctr(srv);
995 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500996 srv_set_sess_last(srv);
997 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100998 _HA_ATOMIC_ADD(&srv->counters.failed_conns, 1);
999 _HA_ATOMIC_ADD(&s->be->be_counters.failed_conns, 1);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001000
Willy Tarreau87b09662015-04-03 00:22:06 +02001001 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +01001002 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001003 if (may_dequeue_tasks(srv, s->be))
1004 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001005
1006 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001007 si_shutr(si);
1008 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001009 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001010
1011 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1012
Willy Tarreau66425e32018-07-25 06:55:12 +02001013 /* we may need to know the position in the queue for logging */
1014 pendconn_cond_unlink(s->pend_pos);
1015
Willy Tarreau87b09662015-04-03 00:22:06 +02001016 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001017 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001018 if (s->srv_error)
1019 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001020 return;
1021 }
1022
1023 /* We are facing a retryable error, but we don't want to run a
1024 * turn-around now, as the problem is likely a source port
1025 * allocation problem, so we want to retry now.
1026 */
1027 si->state = SI_ST_CER;
1028 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001029 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001030 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
1031 return;
1032 }
1033 else if (si->state == SI_ST_QUE) {
1034 /* connection request was queued, check for any update */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +01001035 if (!pendconn_dequeue(s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001036 /* The connection is not in the queue anymore. Either
1037 * we have a server connection slot available and we
1038 * go directly to the assigned state, or we need to
1039 * load-balance first and go to the INI state.
1040 */
1041 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001042 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001043 si->state = SI_ST_REQ;
1044 else {
1045 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1046 si->state = SI_ST_ASS;
1047 }
1048 return;
1049 }
1050
1051 /* Connection request still in queue... */
1052 if (si->flags & SI_FL_EXP) {
1053 /* ... and timeout expired */
1054 si->exp = TICK_ETERNITY;
1055 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau66425e32018-07-25 06:55:12 +02001056
1057 /* we may need to know the position in the queue for logging */
1058 pendconn_cond_unlink(s->pend_pos);
1059
Willy Tarreau827aee92011-03-10 16:55:02 +01001060 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001061 _HA_ATOMIC_ADD(&srv->counters.failed_conns, 1);
1062 _HA_ATOMIC_ADD(&s->be->be_counters.failed_conns, 1);
Willy Tarreau73b013b2012-05-21 16:31:45 +02001063 si_shutr(si);
1064 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001065 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001066 if (!si->err_type)
1067 si->err_type = SI_ET_QUEUE_TO;
1068 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001069 if (s->srv_error)
1070 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001071 return;
1072 }
1073
1074 /* Connection remains in queue, check if we have to abort it */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001075 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001076 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau66425e32018-07-25 06:55:12 +02001077
1078 /* we may need to know the position in the queue for logging */
1079 pendconn_cond_unlink(s->pend_pos);
1080
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001081 si->err_type |= SI_ET_QUEUE_ABRT;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001082 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001083 }
1084
1085 /* Nothing changed */
1086 return;
1087 }
1088 else if (si->state == SI_ST_TAR) {
1089 /* Connection request might be aborted */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001090 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001091 si->err_type |= SI_ET_CONN_ABRT;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001092 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001093 }
1094
1095 if (!(si->flags & SI_FL_EXP))
1096 return; /* still in turn-around */
1097
Olivier Houchard86dcad62019-04-09 19:17:41 +02001098 si->flags &= ~SI_FL_EXP;
1099
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001100 si->exp = TICK_ETERNITY;
1101
Willy Tarreau87b09662015-04-03 00:22:06 +02001102 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001103 * marked "assigned".
1104 * FIXME: Should we force a redispatch attempt when the server is down ?
1105 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001106 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001107 si->state = SI_ST_ASS;
1108 else
1109 si->state = SI_ST_REQ;
1110 return;
1111 }
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001112 return;
1113
1114abort_connection:
1115 /* give up */
1116 si->exp = TICK_ETERNITY;
1117 si_shutr(si);
1118 si_shutw(si);
1119 si->state = SI_ST_CLO;
1120 if (s->srv_error)
1121 s->srv_error(s, si);
1122 return;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001123}
1124
Willy Tarreau87b09662015-04-03 00:22:06 +02001125/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +09001126 * also counts a failed request if the server state has not reached the request
1127 * stage.
1128 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001129static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +09001130{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001131 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +09001132 if (s->si[1].state < SI_ST_REQ) {
1133
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001134 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.failed_req, 1);
Willy Tarreau2c1068c2015-09-23 12:21:21 +02001135 if (strm_li(s) && strm_li(s)->counters)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001136 _HA_ATOMIC_ADD(&strm_li(s)->counters->failed_req, 1);
Simon Hormandec5be42011-06-08 09:19:07 +09001137
Willy Tarreaue7dff022015-04-03 01:14:29 +02001138 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +09001139 }
1140 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001141 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +09001142 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001143 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +09001144 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001145 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +09001146 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02001147 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +09001148 }
1149}
1150
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001151/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001152 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
1153 * a real connection to a server, indicating that a server has been assigned,
1154 * or SI_ST_EST for a successful connection to an applet. It may also return
1155 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001156 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001157static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001158{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001159 struct stream_interface *si = &s->si[1];
1160
Christopher Faulet45073512018-07-20 10:16:29 +02001161 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%lu rqt=%lu rph=%lu rpt=%lu cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001162 now_ms, __FUNCTION__,
1163 s,
CJ Ess6eac32e2015-05-02 16:35:24 -04001164 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001165 s->req.rex, s->res.wex,
1166 s->req.flags, s->res.flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001167 ci_data(&s->req), co_data(&s->req), ci_data(&s->res), co_data(&s->res), s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001168
1169 if (si->state != SI_ST_REQ)
1170 return;
1171
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001172 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
1173 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001174 struct appctx *appctx = objt_appctx(si->end);
1175
1176 if (!appctx || appctx->applet != __objt_applet(s->target))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001177 appctx = si_register_handler(si, objt_applet(s->target));
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001178
1179 if (!appctx) {
1180 /* No more memory, let's immediately abort. Force the
1181 * error code to ignore the ERR_LOCAL which is not a
1182 * real error.
1183 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001184 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001185
1186 si_shutr(si);
1187 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001188 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001189 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001190 si->state = SI_ST_CLO;
1191 if (s->srv_error)
1192 s->srv_error(s, si);
1193 return;
1194 }
1195
Christopher Faulet7a3367c2019-05-09 11:46:52 +02001196 /* For applets, there is no connection establishment, but if the
1197 * request channel is waiting for it, we mark the read side as
1198 * attached on the response channel and we wake up it once. So
1199 * it will have a chance to forward data now.
1200 */
1201 if (s->req.flags & CF_WAKE_CONNECT) {
1202 s->res.flags |= CF_READ_ATTACHED;
1203 s->req.flags |= CF_WAKE_ONCE;
1204 s->req.flags &= ~CF_WAKE_CONNECT;
1205 }
1206
Willy Tarreauee8269e2017-11-23 17:34:29 +01001207 if (tv_iszero(&s->logs.tv_request))
1208 s->logs.tv_request = now;
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001209 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001210 si->state = SI_ST_EST;
1211 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001212 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001213 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001214 return;
1215 }
1216
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001217 /* Try to assign a server */
1218 if (srv_redispatch_connect(s) != 0) {
1219 /* We did not get a server. Either we queued the
1220 * connection request, or we encountered an error.
1221 */
1222 if (si->state == SI_ST_QUE)
1223 return;
1224
1225 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001226 si_shutr(si);
1227 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001228 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001229 if (!si->err_type)
1230 si->err_type = SI_ET_CONN_OTHER;
1231 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001232 if (s->srv_error)
1233 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001234 return;
1235 }
1236
1237 /* The server is assigned */
1238 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1239 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001240 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001241}
1242
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001243/* This function parses the use-service action ruleset. It executes
1244 * the associated ACL and set an applet as a stream or txn final node.
1245 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1246 * consistent state. It returns ACT_RET_STOP in succes case because
1247 * use-service must be a terminal action. Returns ACT_RET_YIELD
1248 * if the initialisation function require more data.
1249 */
1250enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1251 struct session *sess, struct stream *s, int flags)
1252
1253{
1254 struct appctx *appctx;
1255
1256 /* Initialises the applet if it is required. */
1257 if (flags & ACT_FLAG_FIRST) {
1258 /* Register applet. this function schedules the applet. */
1259 s->target = &rule->applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001260 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target))))
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001261 return ACT_RET_ERR;
1262
1263 /* Initialise the context. */
1264 appctx = si_appctx(&s->si[1]);
1265 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1266 appctx->rule = rule;
1267 }
1268 else
1269 appctx = si_appctx(&s->si[1]);
1270
1271 /* Stops the applet sheduling, in case of the init function miss
1272 * some data.
1273 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001274 si_stop_get(&s->si[1]);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001275
1276 /* Call initialisation. */
1277 if (rule->applet.init)
1278 switch (rule->applet.init(appctx, px, s)) {
1279 case 0: return ACT_RET_ERR;
1280 case 1: break;
1281 default: return ACT_RET_YIELD;
1282 }
1283
Christopher Faulet2571bc62019-03-01 11:44:26 +01001284 if (rule->from != ACT_F_HTTP_REQ) {
1285 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1286 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
1287
1288 /* The flag SF_ASSIGNED prevent from server assignment. */
1289 s->flags |= SF_ASSIGNED;
1290 }
1291
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001292 /* Now we can schedule the applet. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001293 si_cant_get(&s->si[1]);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001294 appctx_wakeup(appctx);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001295 return ACT_RET_STOP;
1296}
1297
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001298/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001299 * if appropriate. The default_backend rule is also considered, then the
1300 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001301 * It returns 1 if the processing can continue on next analysers, or zero if it
1302 * either needs more data or wants to immediately abort the request.
1303 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001304static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001305{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001306 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001307 struct session *sess = s->sess;
1308 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001309
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001310 req->analysers &= ~an_bit;
1311 req->analyse_exp = TICK_ETERNITY;
1312
Christopher Faulet45073512018-07-20 10:16:29 +02001313 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001314 now_ms, __FUNCTION__,
1315 s,
1316 req,
1317 req->rex, req->wex,
1318 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001319 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001320 req->analysers);
1321
1322 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001323 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001324 struct switching_rule *rule;
1325
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001326 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001327 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001328
Willy Tarreauf51658d2014-04-23 01:21:56 +02001329 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001330 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001331 ret = acl_pass(ret);
1332 if (rule->cond->pol == ACL_COND_UNLESS)
1333 ret = !ret;
1334 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001335
1336 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001337 /* If the backend name is dynamic, try to resolve the name.
1338 * If we can't resolve the name, or if any error occurs, break
1339 * the loop and fallback to the default backend.
1340 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001341 struct proxy *backend = NULL;
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001342
1343 if (rule->dynamic) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001344 struct buffer *tmp;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001345
1346 tmp = alloc_trash_chunk();
1347 if (!tmp)
1348 goto sw_failed;
1349
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001350 if (build_logline(s, tmp->area, tmp->size, &rule->be.expr))
1351 backend = proxy_be_by_name(tmp->area);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001352
1353 free_trash_chunk(tmp);
1354 tmp = NULL;
1355
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001356 if (!backend)
1357 break;
1358 }
1359 else
1360 backend = rule->be.backend;
1361
Willy Tarreau87b09662015-04-03 00:22:06 +02001362 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001363 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001364 break;
1365 }
1366 }
1367
1368 /* To ensure correct connection accounting on the backend, we
1369 * have to assign one if it was not set (eg: a listen). This
1370 * measure also takes care of correctly setting the default
1371 * backend if any.
1372 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001373 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001374 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001375 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001376 }
1377
Willy Tarreaufb356202010-08-03 14:02:05 +02001378 /* 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 +02001379 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001380 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1381 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001382 s->req.analysers &= ~AN_REQ_FLT_START_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001383 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001384
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001385 /* 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 +02001386 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001387 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001388 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001389 int ret = 1;
1390
1391 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001392 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 +01001393 ret = acl_pass(ret);
1394 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1395 ret = !ret;
1396 }
1397
1398 if (ret) {
1399 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001400 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001401 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001402 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001403 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001404 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001405 break;
1406 }
1407 }
1408
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001409 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001410
1411 sw_failed:
1412 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001413 channel_abort(&s->req);
1414 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001415
Willy Tarreaue7dff022015-04-03 01:14:29 +02001416 if (!(s->flags & SF_ERR_MASK))
1417 s->flags |= SF_ERR_RESOURCE;
1418 if (!(s->flags & SF_FINST_MASK))
1419 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001420
Willy Tarreaueee5b512015-04-03 23:46:31 +02001421 if (s->txn)
1422 s->txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001423 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001424 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001425 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001426}
1427
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001428/* This stream analyser works on a request. It applies all use-server rules on
1429 * it then returns 1. The data must already be present in the buffer otherwise
1430 * they won't match. It always returns 1.
1431 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001432static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001433{
1434 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001435 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001436 struct server_rule *rule;
1437
Christopher Faulet45073512018-07-20 10:16:29 +02001438 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%lu analysers=%02x\n",
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001439 now_ms, __FUNCTION__,
1440 s,
1441 req,
1442 req->rex, req->wex,
1443 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001444 c_data(req),
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001445 req->analysers);
1446
Willy Tarreaue7dff022015-04-03 01:14:29 +02001447 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001448 list_for_each_entry(rule, &px->server_rules, list) {
1449 int ret;
1450
Willy Tarreau192252e2015-04-04 01:47:55 +02001451 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001452 ret = acl_pass(ret);
1453 if (rule->cond->pol == ACL_COND_UNLESS)
1454 ret = !ret;
1455
1456 if (ret) {
1457 struct server *srv = rule->srv.ptr;
1458
Emeric Brun52a91d32017-08-31 14:41:55 +02001459 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001460 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001461 (s->flags & SF_FORCE_PRST)) {
1462 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001463 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001464 break;
1465 }
1466 /* if the server is not UP, let's go on with next rules
1467 * just in case another one is suited.
1468 */
1469 }
1470 }
1471 }
1472
1473 req->analysers &= ~an_bit;
1474 req->analyse_exp = TICK_ETERNITY;
1475 return 1;
1476}
1477
Emeric Brun1d33b292010-01-04 15:47:17 +01001478/* This stream analyser works on a request. It applies all sticking rules on
1479 * it then returns 1. The data must already be present in the buffer otherwise
1480 * they won't match. It always returns 1.
1481 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001482static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001483{
1484 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001485 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001486 struct sticking_rule *rule;
1487
Christopher Faulet45073512018-07-20 10:16:29 +02001488 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001489 now_ms, __FUNCTION__,
1490 s,
1491 req,
1492 req->rex, req->wex,
1493 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001494 ci_data(req),
Emeric Brun1d33b292010-01-04 15:47:17 +01001495 req->analysers);
1496
1497 list_for_each_entry(rule, &px->sticking_rules, list) {
1498 int ret = 1 ;
1499 int i;
1500
Willy Tarreau9667a802013-12-09 12:52:13 +01001501 /* Only the first stick store-request of each table is applied
1502 * and other ones are ignored. The purpose is to allow complex
1503 * configurations which look for multiple entries by decreasing
1504 * order of precision and to stop at the first which matches.
1505 * An example could be a store of the IP address from an HTTP
1506 * header first, then from the source if not found.
1507 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001508 for (i = 0; i < s->store_count; i++) {
1509 if (rule->table.t == s->store[i].table)
1510 break;
1511 }
1512
1513 if (i != s->store_count)
1514 continue;
1515
1516 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001517 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001518 ret = acl_pass(ret);
1519 if (rule->cond->pol == ACL_COND_UNLESS)
1520 ret = !ret;
1521 }
1522
1523 if (ret) {
1524 struct stktable_key *key;
1525
Willy Tarreau192252e2015-04-04 01:47:55 +02001526 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 +01001527 if (!key)
1528 continue;
1529
1530 if (rule->flags & STK_IS_MATCH) {
1531 struct stksess *ts;
1532
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001533 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001534 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001535 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001536 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001537
1538 /* srv found in table */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001539 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreaub8d42d02018-09-20 11:11:15 +02001540 ptr = __stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau13c29de2010-06-06 16:40:39 +02001541 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001542 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun1d33b292010-01-04 15:47:17 +01001543 if (node) {
1544 struct server *srv;
1545
1546 srv = container_of(node, struct server, conf.id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001547 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001548 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001549 (s->flags & SF_FORCE_PRST)) {
1550 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001551 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001552 }
1553 }
1554 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001555 stktable_touch_local(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001556 }
1557 }
1558 if (rule->flags & STK_IS_STORE) {
1559 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1560 struct stksess *ts;
1561
1562 ts = stksess_new(rule->table.t, key);
1563 if (ts) {
1564 s->store[s->store_count].table = rule->table.t;
1565 s->store[s->store_count++].ts = ts;
1566 }
1567 }
1568 }
1569 }
1570 }
1571
1572 req->analysers &= ~an_bit;
1573 req->analyse_exp = TICK_ETERNITY;
1574 return 1;
1575}
1576
1577/* This stream analyser works on a response. It applies all store rules on it
1578 * then returns 1. The data must already be present in the buffer otherwise
1579 * they won't match. It always returns 1.
1580 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001581static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001582{
1583 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001584 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001585 struct sticking_rule *rule;
1586 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001587 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001588
Christopher Faulet45073512018-07-20 10:16:29 +02001589 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001590 now_ms, __FUNCTION__,
1591 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001592 rep,
1593 rep->rex, rep->wex,
1594 rep->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001595 ci_data(rep),
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001596 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001597
1598 list_for_each_entry(rule, &px->storersp_rules, list) {
1599 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001600
Willy Tarreau9667a802013-12-09 12:52:13 +01001601 /* Only the first stick store-response of each table is applied
1602 * and other ones are ignored. The purpose is to allow complex
1603 * configurations which look for multiple entries by decreasing
1604 * order of precision and to stop at the first which matches.
1605 * An example could be a store of a set-cookie value, with a
1606 * fallback to a parameter found in a 302 redirect.
1607 *
1608 * The store-response rules are not allowed to override the
1609 * store-request rules for the same table, but they may coexist.
1610 * Thus we can have up to one store-request entry and one store-
1611 * response entry for the same table at any time.
1612 */
1613 for (i = nbreq; i < s->store_count; i++) {
1614 if (rule->table.t == s->store[i].table)
1615 break;
1616 }
1617
1618 /* skip existing entries for this table */
1619 if (i < s->store_count)
1620 continue;
1621
Emeric Brun1d33b292010-01-04 15:47:17 +01001622 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001623 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001624 ret = acl_pass(ret);
1625 if (rule->cond->pol == ACL_COND_UNLESS)
1626 ret = !ret;
1627 }
1628
1629 if (ret) {
1630 struct stktable_key *key;
1631
Willy Tarreau192252e2015-04-04 01:47:55 +02001632 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 +01001633 if (!key)
1634 continue;
1635
Willy Tarreau37e340c2013-12-06 23:05:21 +01001636 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001637 struct stksess *ts;
1638
1639 ts = stksess_new(rule->table.t, key);
1640 if (ts) {
1641 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001642 s->store[s->store_count++].ts = ts;
1643 }
1644 }
1645 }
1646 }
1647
1648 /* process store request and store response */
1649 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001650 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001651 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001652
Willy Tarreauc93cd162014-05-13 15:54:22 +02001653 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001654 stksess_free(s->store[i].table, s->store[i].ts);
1655 s->store[i].ts = NULL;
1656 continue;
1657 }
1658
Emeric Brun819fc6f2017-06-13 19:37:32 +02001659 ts = stktable_set_entry(s->store[i].table, s->store[i].ts);
1660 if (ts != s->store[i].ts) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001661 /* the entry already existed, we can free ours */
Emeric Brun1d33b292010-01-04 15:47:17 +01001662 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001663 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001664 s->store[i].ts = NULL;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001665
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001666 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreaub8d42d02018-09-20 11:11:15 +02001667 ptr = __stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1668 stktable_data_cast(ptr, server_id) = __objt_server(s->target)->puid;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001669 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001670 stktable_touch_local(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001671 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001672 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001673
1674 rep->analysers &= ~an_bit;
1675 rep->analyse_exp = TICK_ETERNITY;
1676 return 1;
1677}
1678
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001679/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001680 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001681 */
1682#define UPDATE_ANALYSERS(real, list, back, flag) { \
1683 list = (((list) & ~(flag)) | ~(back)) & (real); \
1684 back = real; \
1685 if (!(list)) \
1686 break; \
1687 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1688 continue; \
1689}
1690
Christopher Fauleta9215b72016-05-11 17:06:28 +02001691/* These 2 following macros call an analayzer for the specified channel if the
1692 * right flag is set. The first one is used for "filterable" analyzers. If a
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001693 * stream has some registered filters, pre and post analyaze callbacks are
Christopher Faulet0184ea72017-01-05 14:06:34 +01001694 * called. The second are used for other analyzers (AN_REQ/RES_FLT_* and
Christopher Fauleta9215b72016-05-11 17:06:28 +02001695 * AN_REQ/RES_HTTP_XFER_BODY) */
1696#define FLT_ANALYZE(strm, chn, fun, list, back, flag, ...) \
1697 { \
1698 if ((list) & (flag)) { \
1699 if (HAS_FILTERS(strm)) { \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001700 if (!flt_pre_analyze((strm), (chn), (flag))) \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001701 break; \
1702 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1703 break; \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001704 if (!flt_post_analyze((strm), (chn), (flag))) \
1705 break; \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001706 } \
1707 else { \
1708 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1709 break; \
1710 } \
1711 UPDATE_ANALYSERS((chn)->analysers, (list), \
1712 (back), (flag)); \
1713 } \
1714 }
1715
1716#define ANALYZE(strm, chn, fun, list, back, flag, ...) \
1717 { \
1718 if ((list) & (flag)) { \
1719 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1720 break; \
1721 UPDATE_ANALYSERS((chn)->analysers, (list), \
1722 (back), (flag)); \
1723 } \
1724 }
1725
Willy Tarreau87b09662015-04-03 00:22:06 +02001726/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001727 * then puts it back to the wait queue in a clean state, or cleans up its
1728 * resources if it must be deleted. Returns in <next> the date the task wants
1729 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1730 * nothing too many times, the request and response buffers flags are monitored
1731 * and each function is called only if at least another function has changed at
1732 * least one flag it is interested in.
1733 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001734struct task *process_stream(struct task *t, void *context, unsigned short state)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001735{
Willy Tarreau827aee92011-03-10 16:55:02 +01001736 struct server *srv;
Olivier Houchard9f6af332018-05-25 14:04:04 +02001737 struct stream *s = context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001738 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001739 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001740 unsigned int rq_prod_last, rq_cons_last;
1741 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001742 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001743 struct channel *req, *res;
1744 struct stream_interface *si_f, *si_b;
Willy Tarreau3d07a162019-04-25 19:15:20 +02001745 unsigned int rate;
Willy Tarreau4ad574f2019-04-25 19:48:10 +02001746 uint64_t cpu_time_before, cpu_time_after;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001747
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001748 activity[tid].stream++;
1749
Willy Tarreau8f128b42014-11-28 15:07:47 +01001750 req = &s->req;
1751 res = &s->res;
1752
1753 si_f = &s->si[0];
1754 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001755
Willy Tarreau0f8d3ab2018-10-25 10:42:39 +02001756 /* First, attempt to receive pending data from I/O layers */
Willy Tarreauade64782018-11-17 19:51:07 +01001757 si_sync_recv(si_f);
1758 si_sync_recv(si_b);
Willy Tarreau0f8d3ab2018-10-25 10:42:39 +02001759
Willy Tarreau4ad574f2019-04-25 19:48:10 +02001760 /* measure only the analysers' time */
1761 cpu_time_before = 0;
1762 if (task_profiling_mask & tid_bit)
1763 cpu_time_before = now_cpu_time();
1764
Olivier Houchard53216e72018-10-10 15:46:36 +02001765redo:
Willy Tarreau3d07a162019-04-25 19:15:20 +02001766 rate = update_freq_ctr(&s->call_rate, 1);
1767 if (rate >= 100000 && s->call_rate.prev_ctr) { // make sure to wait at least a full second
1768 stream_dump_and_crash(&s->obj_type, read_freq_ctr(&s->call_rate));
1769 }
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001770
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001771 //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 +01001772 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001773
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001774 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001775 if (s->txn)
1776 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001777
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001778 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001779 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1780 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001781
1782 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001783 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1784 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001785
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001786 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001787 si_f->flags |= SI_FL_DONT_WAKE;
1788 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001789
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001790 /* update pending events */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001791 s->pending_events |= (state & TASK_WOKEN_ANY);
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001792
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001793 /* 1a: Check for low level timeouts if needed. We just set a flag on
1794 * stream interfaces when their timeouts have expired.
1795 */
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001796 if (unlikely(s->pending_events & TASK_WOKEN_TIMER)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001797 si_check_timeouts(si_f);
1798 si_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001799
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001800 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001801 * for future reads or writes. Note: this will also concern upper layers
1802 * but we do not touch any other flag. We must be careful and correctly
1803 * detect state changes when calling them.
1804 */
1805
Willy Tarreau8f128b42014-11-28 15:07:47 +01001806 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001807
Willy Tarreau8f128b42014-11-28 15:07:47 +01001808 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1809 si_b->flags |= SI_FL_NOLINGER;
1810 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001811 }
1812
Willy Tarreau8f128b42014-11-28 15:07:47 +01001813 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1814 if (si_f->flags & SI_FL_NOHALF)
1815 si_f->flags |= SI_FL_NOLINGER;
1816 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001817 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001818
Willy Tarreau8f128b42014-11-28 15:07:47 +01001819 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001820
Willy Tarreau8f128b42014-11-28 15:07:47 +01001821 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1822 si_f->flags |= SI_FL_NOLINGER;
1823 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001824 }
1825
Willy Tarreau8f128b42014-11-28 15:07:47 +01001826 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1827 if (si_b->flags & SI_FL_NOHALF)
1828 si_b->flags |= SI_FL_NOLINGER;
1829 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001830 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001831
Christopher Fauleta00d8172016-11-10 14:58:05 +01001832 if (HAS_FILTERS(s))
1833 flt_stream_check_timeouts(s);
1834
Willy Tarreau798f4322012-11-08 14:49:17 +01001835 /* Once in a while we're woken up because the task expires. But
1836 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001837 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001838 * timeout needs to be refreshed.
1839 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001840 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001841 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
Willy Tarreauede3d882018-10-24 17:17:56 +02001842 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001843 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001844 ((s->pending_events & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER)) {
Willy Tarreau5fb04712016-05-04 10:18:37 +02001845 si_f->flags &= ~SI_FL_DONT_WAKE;
1846 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau798f4322012-11-08 14:49:17 +01001847 goto update_exp_and_leave;
Willy Tarreau5fb04712016-05-04 10:18:37 +02001848 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001849 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001850
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001851 /* below we may emit error messages so we have to ensure that we have
1852 * our buffers properly allocated.
1853 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001854 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001855 /* No buffer available, we've been subscribed to the list of
1856 * buffer waiters, let's wait for our turn.
1857 */
Willy Tarreau5fb04712016-05-04 10:18:37 +02001858 si_f->flags &= ~SI_FL_DONT_WAKE;
1859 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001860 goto update_exp_and_leave;
1861 }
1862
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001863 /* 1b: check for low-level errors reported at the stream interface.
1864 * First we check if it's a retryable error (in which case we don't
1865 * want to tell the buffer). Otherwise we report the error one level
1866 * upper by setting flags into the buffers. Note that the side towards
1867 * the client cannot have connect (hence retryable) errors. Also, the
1868 * connection setup code must be able to deal with any type of abort.
1869 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001870 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001871 if (unlikely(si_f->flags & SI_FL_ERR)) {
1872 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1873 si_shutr(si_f);
1874 si_shutw(si_f);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001875 si_report_error(si_f);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001876 if (!(req->analysers) && !(res->analysers)) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001877 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
1878 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01001879 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001880 _HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001881 if (!(s->flags & SF_ERR_MASK))
1882 s->flags |= SF_ERR_CLICL;
1883 if (!(s->flags & SF_FINST_MASK))
1884 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001885 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001886 }
1887 }
1888
Willy Tarreau8f128b42014-11-28 15:07:47 +01001889 if (unlikely(si_b->flags & SI_FL_ERR)) {
1890 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1891 si_shutr(si_b);
1892 si_shutw(si_b);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001893 si_report_error(si_b);
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001894 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01001895 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001896 _HA_ATOMIC_ADD(&srv->counters.failed_resp, 1);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001897 if (!(req->analysers) && !(res->analysers)) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001898 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
1899 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01001900 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001901 _HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001902 if (!(s->flags & SF_ERR_MASK))
1903 s->flags |= SF_ERR_SRVCL;
1904 if (!(s->flags & SF_FINST_MASK))
1905 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001906 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001907 }
1908 /* note: maybe we should process connection errors here ? */
1909 }
1910
Willy Tarreau8f128b42014-11-28 15:07:47 +01001911 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001912 /* we were trying to establish a connection on the server side,
1913 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1914 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001915 if (unlikely(!sess_update_st_con_tcp(s)))
1916 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001917 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001918 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001919
1920 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1921 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1922 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1923 */
1924 }
1925
Willy Tarreau8f128b42014-11-28 15:07:47 +01001926 rq_prod_last = si_f->state;
1927 rq_cons_last = si_b->state;
1928 rp_cons_last = si_f->state;
1929 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001930
1931 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001932 /* Check for connection closure */
1933
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001934 DPRINTF(stderr,
Christopher Faulet45073512018-07-20 10:16:29 +02001935 "[%u] %s:%d: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%lu rqt=%lu rph=%lu rpt=%lu cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001936 now_ms, __FUNCTION__, __LINE__,
1937 t,
1938 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001939 req, res,
1940 req->rex, res->wex,
1941 req->flags, res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001942 ci_data(req), co_data(req), ci_data(res), co_data(res), si_f->state, si_b->state,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001943 si_f->err_type, si_b->err_type,
1944 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001945
1946 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001947 if (unlikely(si_f->state == SI_ST_DIS))
1948 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001949
1950 /* When a server-side connection is released, we have to count it and
1951 * check for pending connections on this server.
1952 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001953 if (unlikely(si_b->state == SI_ST_DIS)) {
1954 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001955 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001956 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001957 if (s->flags & SF_CURR_SESS) {
1958 s->flags &= ~SF_CURR_SESS;
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001959 _HA_ATOMIC_SUB(&srv->cur_sess, 1);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001960 }
1961 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001962 if (may_dequeue_tasks(srv, s->be))
1963 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001964 }
1965 }
1966
1967 /*
1968 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1969 * at this point.
1970 */
1971
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001972 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001973 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001974 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1975 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
Willy Tarreau33982cb2017-11-20 15:37:13 +01001976 (req->analysers && (req->flags & CF_SHUTW)) ||
Willy Tarreau8f128b42014-11-28 15:07:47 +01001977 si_f->state != rq_prod_last ||
1978 si_b->state != rq_cons_last ||
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001979 s->pending_events & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001980 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001981
Willy Tarreau8f128b42014-11-28 15:07:47 +01001982 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001983 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001984 unsigned int ana_list;
1985 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001986
Willy Tarreau90deb182010-01-07 00:20:41 +01001987 /* it's up to the analysers to stop new connections,
1988 * disable reading or closing. Note: if an analyser
1989 * disables any of these bits, it is responsible for
1990 * enabling them again when it disables itself, so
1991 * that other analysers are called in similar conditions.
1992 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001993 channel_auto_read(req);
1994 channel_auto_connect(req);
1995 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001996
1997 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001998 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001999 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002000 * the list when not needed. Any analyser may return 0
2001 * to break out of the loop, either because of missing
2002 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02002003 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002004 * analyser, and we may loop again if other analysers
2005 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002006 *
2007 * We build a list of analysers to run. We evaluate all
2008 * of these analysers in the order of the lower bit to
2009 * the higher bit. This ordering is very important.
2010 * An analyser will often add/remove other analysers,
2011 * including itself. Any changes to itself have no effect
2012 * on the loop. If it removes any other analysers, we
2013 * want those analysers not to be called anymore during
2014 * this loop. If it adds an analyser that is located
2015 * after itself, we want it to be scheduled for being
2016 * processed during the loop. If it adds an analyser
2017 * which is located before it, we want it to switch to
2018 * it immediately, even if it has already been called
2019 * once but removed since.
2020 *
2021 * In order to achieve this, we compare the analyser
2022 * list after the call with a copy of it before the
2023 * call. The work list is fed with analyser bits that
2024 * appeared during the call. Then we compare previous
2025 * work list with the new one, and check the bits that
2026 * appeared. If the lowest of these bits is lower than
2027 * the current bit, it means we have enabled a previous
2028 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01002029 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002030
Willy Tarreau8f128b42014-11-28 15:07:47 +01002031 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01002032 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002033 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002034 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_REQ_FLT_START_FE);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002035 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_FE);
2036 FLT_ANALYZE(s, req, http_wait_for_request, ana_list, ana_back, AN_REQ_WAIT_HTTP);
2037 FLT_ANALYZE(s, req, http_wait_for_request_body, ana_list, ana_back, AN_REQ_HTTP_BODY);
2038 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE, sess->fe);
2039 FLT_ANALYZE(s, req, process_switching_rules, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002040 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_REQ_FLT_START_BE);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002041 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_BE);
2042 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE, s->be);
2043 FLT_ANALYZE(s, req, http_process_tarpit, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
2044 FLT_ANALYZE(s, req, process_server_rules, ana_list, ana_back, AN_REQ_SRV_RULES);
2045 FLT_ANALYZE(s, req, http_process_request, ana_list, ana_back, AN_REQ_HTTP_INNER);
2046 FLT_ANALYZE(s, req, tcp_persist_rdp_cookie, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
2047 FLT_ANALYZE(s, req, process_sticking_rules, ana_list, ana_back, AN_REQ_STICKING_RULES);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002048 ANALYZE (s, req, flt_analyze_http_headers, ana_list, ana_back, AN_REQ_FLT_HTTP_HDRS);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002049 ANALYZE (s, req, http_request_forward_body, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002050 ANALYZE (s, req, pcli_wait_for_request, ana_list, ana_back, AN_REQ_WAIT_CLI);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002051 ANALYZE (s, req, flt_xfer_data, ana_list, ana_back, AN_REQ_FLT_XFER_DATA);
2052 ANALYZE (s, req, flt_end_analyze, ana_list, ana_back, AN_REQ_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01002053 break;
2054 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002055 }
Willy Tarreau84455332009-03-15 22:34:05 +01002056
Willy Tarreau8f128b42014-11-28 15:07:47 +01002057 rq_prod_last = si_f->state;
2058 rq_cons_last = si_b->state;
2059 req->flags &= ~CF_WAKE_ONCE;
2060 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002061
Willy Tarreau8f128b42014-11-28 15:07:47 +01002062 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002063 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002064 }
2065
Willy Tarreau576507f2010-01-07 00:09:04 +01002066 /* we'll monitor the request analysers while parsing the response,
2067 * because some response analysers may indirectly enable new request
2068 * analysers (eg: HTTP keep-alive).
2069 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002070 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01002071
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002072 resync_response:
2073 /* Analyse response */
2074
Willy Tarreau8f128b42014-11-28 15:07:47 +01002075 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
2076 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
Willy Tarreau33982cb2017-11-20 15:37:13 +01002077 (res->analysers && (res->flags & CF_SHUTW)) ||
Willy Tarreau8f128b42014-11-28 15:07:47 +01002078 si_f->state != rp_cons_last ||
2079 si_b->state != rp_prod_last ||
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002080 s->pending_events & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002081 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002082
Willy Tarreau8f128b42014-11-28 15:07:47 +01002083 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01002084 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002085 unsigned int ana_list;
2086 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002087
Willy Tarreau90deb182010-01-07 00:20:41 +01002088 /* it's up to the analysers to stop disable reading or
2089 * closing. Note: if an analyser disables any of these
2090 * bits, it is responsible for enabling them again when
2091 * it disables itself, so that other analysers are called
2092 * in similar conditions.
2093 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002094 channel_auto_read(res);
2095 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002096
2097 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01002098 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002099 * to MSB. The analysers must remove themselves from
2100 * the list when not needed. Any analyser may return 0
2101 * to break out of the loop, either because of missing
2102 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02002103 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002104 * analyser, and we may loop again if other analysers
2105 * are added in the middle.
2106 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002107
Willy Tarreau8f128b42014-11-28 15:07:47 +01002108 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01002109 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002110 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002111 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_RES_FLT_START_FE);
2112 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_RES_FLT_START_BE);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002113 FLT_ANALYZE(s, res, tcp_inspect_response, ana_list, ana_back, AN_RES_INSPECT);
2114 FLT_ANALYZE(s, res, http_wait_for_response, ana_list, ana_back, AN_RES_WAIT_HTTP);
2115 FLT_ANALYZE(s, res, process_store_rules, ana_list, ana_back, AN_RES_STORE_RULES);
2116 FLT_ANALYZE(s, res, http_process_res_common, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE, s->be);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002117 ANALYZE (s, res, flt_analyze_http_headers, ana_list, ana_back, AN_RES_FLT_HTTP_HDRS);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002118 ANALYZE (s, res, http_response_forward_body, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002119 ANALYZE (s, res, pcli_wait_for_response, ana_list, ana_back, AN_RES_WAIT_CLI);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002120 ANALYZE (s, res, flt_xfer_data, ana_list, ana_back, AN_RES_FLT_XFER_DATA);
2121 ANALYZE (s, res, flt_end_analyze, ana_list, ana_back, AN_RES_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01002122 break;
2123 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002124 }
2125
Willy Tarreau8f128b42014-11-28 15:07:47 +01002126 rp_cons_last = si_f->state;
2127 rp_prod_last = si_b->state;
Christopher Fauletcdaea892017-07-06 15:49:30 +02002128 res->flags &= ~CF_WAKE_ONCE;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002129 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002130
Willy Tarreau8f128b42014-11-28 15:07:47 +01002131 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002132 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002133 }
2134
Willy Tarreau576507f2010-01-07 00:09:04 +01002135 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002136 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01002137 goto resync_request;
2138
Willy Tarreau8f128b42014-11-28 15:07:47 +01002139 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01002140 goto resync_request;
2141
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002142 /* FIXME: here we should call protocol handlers which rely on
2143 * both buffers.
2144 */
2145
2146
2147 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02002148 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01002149 * we're just in a data phase here since it means we have not
2150 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002151 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002152 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002153 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002154 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002155 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002156 req->analysers = 0;
2157 if (req->flags & CF_READ_ERROR) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002158 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2159 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002160 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002161 _HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002162 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002163 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002164 else if (req->flags & CF_READ_TIMEOUT) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002165 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2166 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002167 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002168 _HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002169 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002170 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002171 else if (req->flags & CF_WRITE_ERROR) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002172 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2173 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002174 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002175 _HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002176 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002177 }
2178 else {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002179 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2180 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002181 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002182 _HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002183 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002184 }
Willy Tarreau84455332009-03-15 22:34:05 +01002185 sess_set_term_flags(s);
Christopher Faulet5e1a9d72019-04-23 17:34:22 +02002186
2187 /* Abort the request if a client error occurred while
2188 * the backend stream-interface is in the SI_ST_INI
2189 * state. It is switched into the SI_ST_CLO state and
2190 * the request channel is erased. */
2191 if (si_b->state == SI_ST_INI) {
2192 si_b->state = SI_ST_CLO;
2193 channel_abort(req);
2194 if (IS_HTX_STRM(s))
2195 channel_htx_erase(req, htxbuf(&req->buf));
2196 else
2197 channel_erase(req);
2198 }
Willy Tarreau84455332009-03-15 22:34:05 +01002199 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002200 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002201 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002202 res->analysers = 0;
2203 if (res->flags & CF_READ_ERROR) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002204 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2205 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002206 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002207 _HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002208 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002209 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002210 else if (res->flags & CF_READ_TIMEOUT) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002211 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2212 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002213 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002214 _HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002215 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002216 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002217 else if (res->flags & CF_WRITE_ERROR) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002218 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2219 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002220 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002221 _HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002222 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002223 }
2224 else {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002225 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2226 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002227 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002228 _HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002229 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002230 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002231 sess_set_term_flags(s);
2232 }
Willy Tarreau84455332009-03-15 22:34:05 +01002233 }
2234
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002235 /*
2236 * Here we take care of forwarding unhandled data. This also includes
2237 * connection establishments and shutdown requests.
2238 */
2239
2240
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002241 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002242 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002243 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002244 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002245 */
Christopher Faulet35fe6992017-08-29 16:06:38 +02002246 if (unlikely((!req->analysers || (req->analysers == AN_REQ_FLT_END && !(req->flags & CF_FLT_ANALYZE))) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002247 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2248 (si_f->state >= SI_ST_EST) &&
2249 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002250 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002251 * attached to it. If any data are left in, we'll permit them to
2252 * move.
2253 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002254 channel_auto_read(req);
2255 channel_auto_connect(req);
2256 channel_auto_close(req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002257
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002258 if (IS_HTX_STRM(s)) {
2259 struct htx *htx = htxbuf(&req->buf);
2260
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002261 /* We'll let data flow between the producer (if still connected)
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002262 * to the consumer.
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002263 */
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002264 co_set_data(req, htx->data);
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002265 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002266 channel_htx_forward_forever(req, htx);
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002267 }
2268 else {
2269 /* We'll let data flow between the producer (if still connected)
2270 * to the consumer (which might possibly not be connected yet).
2271 */
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002272 c_adv(req, ci_data(req));
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002273 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2274 channel_forward_forever(req);
Willy Tarreau42529c32015-07-09 18:38:57 +02002275
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002276 /* Just in order to support fetching HTTP contents after start
2277 * of forwarding when the HTTP forwarding analyser is not used,
2278 * we simply reset msg->sov so that HTTP rewinding points to the
2279 * headers.
2280 */
Olivier Houchard1909f6d2018-12-06 13:28:30 +01002281 if (s->txn)
2282 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - co_data(req);
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002283 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002284 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002285
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002286 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002287 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2288 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002289 (global.tune.options & GTUNE_USE_SPLICE) &&
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002290 (objt_cs(si_f->end) && __objt_cs(si_f->end)->conn->xprt && __objt_cs(si_f->end)->conn->xprt->rcv_pipe) &&
2291 (objt_cs(si_b->end) && __objt_cs(si_b->end)->conn->xprt && __objt_cs(si_b->end)->conn->xprt->snd_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002292 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002293 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2294 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002295 (req->flags & CF_STREAMER_FAST)))) {
2296 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002297 }
2298
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002299 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002300 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002301
2302 /*
2303 * Now forward all shutdown requests between both sides of the buffer
2304 */
2305
Willy Tarreau520d95e2009-09-19 21:04:57 +02002306 /* first, let's check if the request buffer needs to shutdown(write), which may
2307 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002308 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2309 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002310 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002311 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002312 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002313 channel_shutw_now(req);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002314 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002315
2316 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002317 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2318 channel_is_empty(req))) {
2319 if (req->flags & CF_READ_ERROR)
2320 si_b->flags |= SI_FL_NOLINGER;
2321 si_shutw(si_b);
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002322 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002323
2324 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002325 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2326 !req->analysers))
2327 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002328
2329 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002330 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2331 if (si_f->flags & SI_FL_NOHALF)
2332 si_f->flags |= SI_FL_NOLINGER;
2333 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002334 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002335
Willy Tarreau520d95e2009-09-19 21:04:57 +02002336 /* it's possible that an upper layer has requested a connection setup or abort.
2337 * There are 2 situations where we decide to establish a new connection :
2338 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002339 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002340 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002341 if (si_b->state == SI_ST_INI) {
Christopher Faulet5e1a9d72019-04-23 17:34:22 +02002342 if (!(req->flags & CF_SHUTW)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002343 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002344 /* If we have an appctx, there is no connect method, so we
2345 * immediately switch to the connected state, otherwise we
2346 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002347 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002348 si_b->state = SI_ST_REQ; /* new connection requested */
2349 si_b->conn_retries = s->be->conn_retries;
Olivier Houchard602bf7d2019-05-10 13:59:15 +02002350 if ((s->be->retry_type &~ PR_RE_CONN_FAILED) &&
2351 !(si_b->flags & SI_FL_D_L7_RETRY))
Olivier Houcharda254a372019-04-05 15:30:12 +02002352 si_b->flags |= SI_FL_L7_RETRY;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002353 }
Willy Tarreau73201222009-08-16 18:27:24 +02002354 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002355 else {
Willy Tarreau0542c8b2017-11-24 15:01:10 +01002356 si_release_endpoint(si_b);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002357 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2358 channel_shutw_now(req); /* fix buffer flags upon abort */
2359 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002360 }
Willy Tarreau92795622009-03-06 12:51:23 +01002361 }
2362
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002363
2364 /* we may have a pending connection request, or a connection waiting
2365 * for completion.
2366 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002367 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002368
2369 /* prune the request variables and swap to the response variables. */
2370 if (s->vars_reqres.scope != SCOPE_RES) {
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01002371 if (!LIST_ISEMPTY(&s->vars_reqres.head)) {
2372 vars_prune(&s->vars_reqres, s->sess, s);
2373 vars_init(&s->vars_reqres, SCOPE_RES);
2374 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002375 }
2376
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002377 do {
2378 /* nb: step 1 might switch from QUE to ASS, but we first want
2379 * to give a chance to step 2 to perform a redirect if needed.
2380 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002381 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002382 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002383 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002384 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002385
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002386 /* applets directly go to the ESTABLISHED state. Similarly,
2387 * servers experience the same fate when their connection
2388 * is reused.
2389 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002390 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002391 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002392
Willy Tarreaub44c8732013-12-31 23:16:50 +01002393 /* Now we can add the server name to a header (if requested) */
2394 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002395 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002396 (s->be->server_id_hdr_name != NULL) &&
2397 (s->be->mode == PR_MODE_HTTP) &&
2398 objt_server(s->target)) {
Christopher Faulet27a3dc82018-10-23 15:34:07 +02002399 http_send_name_header(s, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002400 }
2401
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002402 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002403 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002404 http_perform_server_redirect(s, si_b);
2405 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002406 }
2407
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002408 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002409 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002410 goto resync_stream_interface;
2411
Willy Tarreau815a9b22010-07-27 17:15:12 +02002412 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002413 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002414 goto resync_request;
2415
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002416 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002417
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002418 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002419 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002420 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002421 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002422 */
Christopher Faulet35fe6992017-08-29 16:06:38 +02002423 if (unlikely((!res->analysers || (res->analysers == AN_RES_FLT_END && !(res->flags & CF_FLT_ANALYZE))) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002424 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2425 (si_b->state >= SI_ST_EST) &&
2426 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002427 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002428 * attached to it. If any data are left in, we'll permit them to
2429 * move.
2430 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002431 channel_auto_read(res);
2432 channel_auto_close(res);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002433
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002434 if (IS_HTX_STRM(s)) {
2435 struct htx *htx = htxbuf(&res->buf);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002436
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002437 /* We'll let data flow between the producer (if still connected)
2438 * to the consumer.
2439 */
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002440 co_set_data(res, htx->data);
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002441 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002442 channel_htx_forward_forever(res, htx);
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002443 }
2444 else {
2445 /* We'll let data flow between the producer (if still connected)
2446 * to the consumer.
2447 */
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002448 c_adv(res, ci_data(res));
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002449 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2450 channel_forward_forever(res);
2451
2452 /* Just in order to support fetching HTTP contents after start
2453 * of forwarding when the HTTP forwarding analyser is not used,
2454 * we simply reset msg->sov so that HTTP rewinding points to the
2455 * headers.
2456 */
Olivier Houchard1909f6d2018-12-06 13:28:30 +01002457 if (s->txn)
2458 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - co_data(res);
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002459 }
Willy Tarreau42529c32015-07-09 18:38:57 +02002460
Willy Tarreauce887fd2012-05-12 12:50:00 +02002461 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002462 * tunnel timeout set, use it now. Note that we must respect
2463 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002464 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002465 if (!req->analysers && s->be->timeout.tunnel) {
2466 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002467 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002468
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002469 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2470 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002471 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2472 res->rto = s->be->timeout.serverfin;
2473 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2474 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002475 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2476 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002477
Willy Tarreau8f128b42014-11-28 15:07:47 +01002478 req->rex = tick_add(now_ms, req->rto);
2479 req->wex = tick_add(now_ms, req->wto);
2480 res->rex = tick_add(now_ms, res->rto);
2481 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002482 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002483 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002484
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002485 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002486 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2487 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002488 (global.tune.options & GTUNE_USE_SPLICE) &&
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002489 (objt_cs(si_f->end) && __objt_cs(si_f->end)->conn->xprt && __objt_cs(si_f->end)->conn->xprt->snd_pipe) &&
2490 (objt_cs(si_b->end) && __objt_cs(si_b->end)->conn->xprt && __objt_cs(si_b->end)->conn->xprt->rcv_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002491 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002492 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2493 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002494 (res->flags & CF_STREAMER_FAST)))) {
2495 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002496 }
2497
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002498 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002499 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002500
2501 /*
2502 * Now forward all shutdown requests between both sides of the buffer
2503 */
2504
2505 /*
2506 * FIXME: this is probably where we should produce error responses.
2507 */
2508
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002509 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002510 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002511 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002512 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002513 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002514
2515 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002516 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2517 channel_is_empty(res))) {
2518 si_shutw(si_f);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002519 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002520
2521 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002522 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2523 !res->analysers)
2524 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002525
2526 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002527 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2528 if (si_b->flags & SI_FL_NOHALF)
2529 si_b->flags |= SI_FL_NOLINGER;
2530 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002531 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002532
Willy Tarreau8f128b42014-11-28 15:07:47 +01002533 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002534 goto resync_stream_interface;
2535
Willy Tarreau8f128b42014-11-28 15:07:47 +01002536 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002537 goto resync_request;
2538
Willy Tarreau8f128b42014-11-28 15:07:47 +01002539 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002540 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002541
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002542 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002543 si_f->flags &= ~SI_FL_DONT_WAKE;
2544 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002545
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002546 /* This is needed only when debugging is enabled, to indicate
2547 * client-side or server-side close. Please note that in the unlikely
2548 * event where both sides would close at once, the sequence is reported
2549 * on the server side first.
2550 */
2551 if (unlikely((global.mode & MODE_DEBUG) &&
2552 (!(global.mode & MODE_QUIET) ||
2553 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002554 if (si_b->state == SI_ST_CLO &&
2555 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002556 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002557 s->uniq_id, s->be->id,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002558 objt_cs(si_f->end) ? (unsigned short)objt_cs(si_f->end)->conn->handle.fd : -1,
2559 objt_cs(si_b->end) ? (unsigned short)objt_cs(si_b->end)->conn->handle.fd : -1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002560 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002561 }
2562
Willy Tarreau8f128b42014-11-28 15:07:47 +01002563 if (si_f->state == SI_ST_CLO &&
2564 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002565 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002566 s->uniq_id, s->be->id,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002567 objt_cs(si_f->end) ? (unsigned short)objt_cs(si_f->end)->conn->handle.fd : -1,
2568 objt_cs(si_b->end) ? (unsigned short)objt_cs(si_b->end)->conn->handle.fd : -1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002569 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002570 }
2571 }
2572
Willy Tarreau8f128b42014-11-28 15:07:47 +01002573 if (likely((si_f->state != SI_ST_CLO) ||
2574 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Olivier Houchard120f64a2019-04-09 19:14:59 +02002575 enum si_state si_b_prev_state, si_f_prev_state;
2576
2577 si_f_prev_state = si_f->prev_state;
2578 si_b_prev_state = si_b->prev_state;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002579
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002580 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002581 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002582
Willy Tarreaud14844a2018-11-08 18:15:29 +01002583 si_update_both(si_f, si_b);
Olivier Houchard53216e72018-10-10 15:46:36 +02002584
Olivier Houchard120f64a2019-04-09 19:14:59 +02002585 if (si_f->state == SI_ST_DIS || si_f->state != si_f_prev_state ||
2586 si_b->state == SI_ST_DIS || si_b->state != si_b_prev_state ||
Olivier Houchard56897e22019-04-10 13:51:37 +02002587 ((si_f->flags & SI_FL_ERR) && si_f->state != SI_ST_CLO) ||
2588 ((si_b->flags & SI_FL_ERR) && si_b->state != SI_ST_CLO) ||
Willy Tarreau85f89012018-10-25 11:06:57 +02002589 (((req->flags ^ rqf_last) | (res->flags ^ rpf_last)) & CF_MASK_ANALYSER))
Olivier Houchard53216e72018-10-10 15:46:36 +02002590 goto redo;
2591
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002592 /* Trick: if a request is being waiting for the server to respond,
2593 * and if we know the server can timeout, we don't want the timeout
2594 * to expire on the client side first, but we're still interested
2595 * in passing data from the client to the server (eg: POST). Thus,
2596 * we can cancel the client's request timeout if the server's
2597 * request timeout is set and the server has not yet sent a response.
2598 */
2599
Willy Tarreau8f128b42014-11-28 15:07:47 +01002600 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2601 (tick_isset(req->wex) || tick_isset(res->rex))) {
2602 req->flags |= CF_READ_NOEXP;
2603 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002604 }
2605
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002606 /* Reset pending events now */
2607 s->pending_events = 0;
2608
Willy Tarreau798f4322012-11-08 14:49:17 +01002609 update_exp_and_leave:
Willy Tarreau5fb04712016-05-04 10:18:37 +02002610 /* Note: please ensure that if you branch here you disable SI_FL_DONT_WAKE */
Christopher Fauleta00d8172016-11-10 14:58:05 +01002611 t->expire = tick_first((tick_is_expired(t->expire, now_ms) ? 0 : t->expire),
2612 tick_first(tick_first(req->rex, req->wex),
2613 tick_first(res->rex, res->wex)));
Willy Tarreaudef0d222016-11-08 22:03:00 +01002614 if (!req->analysers)
2615 req->analyse_exp = TICK_ETERNITY;
2616
2617 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED) &&
2618 (!tick_isset(req->analyse_exp) || tick_is_expired(req->analyse_exp, now_ms)))
2619 req->analyse_exp = tick_add(now_ms, 5000);
2620
2621 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002622
Willy Tarreau9a398be2017-11-10 17:14:23 +01002623 t->expire = tick_first(t->expire, res->analyse_exp);
2624
Willy Tarreau8f128b42014-11-28 15:07:47 +01002625 if (si_f->exp)
2626 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002627
Willy Tarreau8f128b42014-11-28 15:07:47 +01002628 if (si_b->exp)
2629 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002630
Christopher Faulet56803b12017-11-24 16:06:18 +01002631 DPRINTF(stderr,
Willy Tarreau127334e2009-03-28 10:47:26 +01002632 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2633 " 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 +01002634 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2635 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002636
2637#ifdef DEBUG_DEV
2638 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002639 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002640 ABORT_NOW();
2641#endif
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002642 s->pending_events &= ~(TASK_WOKEN_TIMER | TASK_WOKEN_RES);
Willy Tarreau87b09662015-04-03 00:22:06 +02002643 stream_release_buffers(s);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002644 /* We may have free'd some space in buffers, or have more to send/recv, try again */
Willy Tarreau4ad574f2019-04-25 19:48:10 +02002645
2646 if (cpu_time_before) {
2647 cpu_time_after = now_cpu_time();
2648 cpu_time_after -= cpu_time_before;
2649 if (cpu_time_after >= 100000000U) {
2650 /* more than 1 ms CPU time spent in analysers is already absolutely
2651 * insane, but on tiny machines where performance doesn't matter it
2652 * may happen when using tons of regex, so let's fix the threshold
2653 * to 100ms which must never ever be hit.
2654 */
2655 stream_dump_and_crash(&s->obj_type, -(cpu_time_after / 1000));
2656 }
2657 }
Willy Tarreau26c25062009-03-08 09:38:41 +01002658 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002659 }
2660
Willy Tarreaue7dff022015-04-03 01:14:29 +02002661 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002662 _HA_ATOMIC_SUB(&s->be->beconn, 1);
Willy Tarreau6f5e4b92017-09-15 09:07:56 +02002663
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002664 if (unlikely((global.mode & MODE_DEBUG) &&
2665 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002666 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002667 s->uniq_id, s->be->id,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002668 objt_cs(si_f->end) ? (unsigned short)objt_cs(si_f->end)->conn->handle.fd : -1,
2669 objt_cs(si_b->end) ? (unsigned short)objt_cs(si_b->end)->conn->handle.fd : -1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002670 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002671 }
2672
2673 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002674 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002675
Willy Tarreaueee5b512015-04-03 23:46:31 +02002676 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002677 int n;
2678
Willy Tarreaueee5b512015-04-03 23:46:31 +02002679 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002680 if (n < 1 || n > 5)
2681 n = 0;
2682
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002683 if (sess->fe->mode == PR_MODE_HTTP) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002684 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002685 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002686 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002687 (s->be->mode == PR_MODE_HTTP)) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002688 _HA_ATOMIC_ADD(&s->be->be_counters.p.http.rsp[n], 1);
2689 _HA_ATOMIC_ADD(&s->be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002690 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002691 }
2692
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002693 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002694 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002695 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002696 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreau66425e32018-07-25 06:55:12 +02002697 /* we may need to know the position in the queue */
2698 pendconn_free(s);
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002699 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002700 }
2701
Willy Tarreau87b09662015-04-03 00:22:06 +02002702 /* update time stats for this stream */
2703 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002704
Willy Tarreau4ad574f2019-04-25 19:48:10 +02002705 if (cpu_time_before) {
2706 cpu_time_after = now_cpu_time();
2707 cpu_time_after -= cpu_time_before;
2708 if (cpu_time_after >= 100000000U) {
2709 /* more than 1 ms CPU time spent in analysers is already absolutely
2710 * insane, but on tiny machines where performance doesn't matter it
2711 * may happen when using tons of regex, so let's fix the threshold
2712 * to 100ms which must never ever be hit.
2713 */
2714 stream_dump_and_crash(&s->obj_type, -(cpu_time_after / 1000));
2715 }
2716 }
2717
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002718 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002719 stream_free(s);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002720 task_destroy(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002721 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002722}
2723
Willy Tarreau87b09662015-04-03 00:22:06 +02002724/* Update the stream's backend and server time stats */
2725void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002726{
2727 int t_request;
2728 int t_queue;
2729 int t_connect;
2730 int t_data;
2731 int t_close;
2732 struct server *srv;
2733
2734 t_request = 0;
2735 t_queue = s->logs.t_queue;
2736 t_connect = s->logs.t_connect;
2737 t_close = s->logs.t_close;
2738 t_data = s->logs.t_data;
2739
2740 if (s->be->mode != PR_MODE_HTTP)
2741 t_data = t_connect;
2742
2743 if (t_connect < 0 || t_data < 0)
2744 return;
2745
2746 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2747 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2748
2749 t_data -= t_connect;
2750 t_connect -= t_queue;
2751 t_queue -= t_request;
2752
2753 srv = objt_server(s->target);
2754 if (srv) {
2755 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2756 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2757 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2758 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2759 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002760 HA_SPIN_LOCK(PROXY_LOCK, &s->be->lock);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002761 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2762 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2763 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2764 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002765 HA_SPIN_UNLOCK(PROXY_LOCK, &s->be->lock);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002766}
2767
Willy Tarreau7c669d72008-06-20 15:04:11 +02002768/*
2769 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002770 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002771 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002772 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002773 * server.
2774 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002775void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002776{
2777 if (sess->srv_conn == newsrv)
2778 return;
2779
2780 if (sess->srv_conn) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002781 _HA_ATOMIC_SUB(&sess->srv_conn->served, 1);
2782 _HA_ATOMIC_SUB(&sess->srv_conn->proxy->served, 1);
2783 __ha_barrier_atomic_store();
Willy Tarreau7c669d72008-06-20 15:04:11 +02002784 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2785 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002786 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002787 }
2788
2789 if (newsrv) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002790 _HA_ATOMIC_ADD(&newsrv->served, 1);
2791 _HA_ATOMIC_ADD(&newsrv->proxy->served, 1);
2792 __ha_barrier_atomic_store();
Willy Tarreau7c669d72008-06-20 15:04:11 +02002793 if (newsrv->proxy->lbprm.server_take_conn)
2794 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002795 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002796 }
2797}
2798
Willy Tarreau84455332009-03-15 22:34:05 +01002799/* Handle server-side errors for default protocols. It is called whenever a a
2800 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002801 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002802 * them. It's installed as ->srv_error for the server-side stream_interface.
2803 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002804void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002805{
2806 int err_type = si->err_type;
2807 int err = 0, fin = 0;
2808
2809 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002810 err = SF_ERR_CLICL;
2811 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002812 }
2813 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002814 err = SF_ERR_CLICL;
2815 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002816 }
2817 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002818 err = SF_ERR_SRVTO;
2819 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002820 }
2821 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002822 err = SF_ERR_SRVCL;
2823 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002824 }
2825 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002826 err = SF_ERR_SRVTO;
2827 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002828 }
2829 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002830 err = SF_ERR_SRVCL;
2831 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002832 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002833 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002834 err = SF_ERR_RESOURCE;
2835 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002836 }
Willy Tarreau84455332009-03-15 22:34:05 +01002837 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002838 err = SF_ERR_INTERNAL;
2839 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002840 }
2841
Willy Tarreaue7dff022015-04-03 01:14:29 +02002842 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002843 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002844 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002845 s->flags |= fin;
2846}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002847
Willy Tarreaue7dff022015-04-03 01:14:29 +02002848/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002849void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002850{
Willy Tarreau87b09662015-04-03 00:22:06 +02002851 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002852 return;
2853
Willy Tarreau87b09662015-04-03 00:22:06 +02002854 channel_shutw_now(&stream->req);
2855 channel_shutr_now(&stream->res);
2856 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002857 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002858 stream->flags |= why;
2859 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002860}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002861
Willy Tarreau71c07ac2019-04-25 19:08:48 +02002862/* dumps an error message for type <type> at ptr <ptr> related to stream <s>,
2863 * having reached loop rate <rate>, then aborts hoping to retrieve a core. If
2864 * <rate> is negative instead it reports a duration in microseconds.
2865 */
2866void stream_dump_and_crash(enum obj_type *obj, int rate)
2867{
2868 const struct conn_stream *csf, *csb;
2869 const struct connection *cof, *cob;
2870 const struct appctx *acf, *acb;
2871 const struct server *srv;
2872 const struct stream *s;
2873 const char *src = "unknown";
2874 const char *dst = "unknown";
2875 char pn[INET6_ADDRSTRLEN];
2876 char *msg = NULL;
2877 const char *fmt;
2878 const struct channel *req, *res;
2879 const struct stream_interface *si_f, *si_b;
2880 const void *ptr;
2881
2882 ptr = s = objt_stream(obj);
2883 if (!s) {
2884 const struct appctx *appctx = objt_appctx(obj);
2885 if (!appctx)
2886 return;
2887 ptr = appctx;
2888 s = si_strm(appctx->owner);
2889 if (!s)
2890 return;
2891 }
2892
2893 si_f = &s->si[0];
2894 si_b = &s->si[1];
2895 req = &s->req;
2896 res = &s->res;
2897
2898 csf = objt_cs(si_f->end);
2899 cof = cs_conn(csf);
2900 acf = objt_appctx(si_f->end);
2901 if (cof && addr_to_str(&cof->addr.from, pn, sizeof(pn)) >= 0)
2902 src = pn;
2903 else if (acf)
2904 src = acf->applet->name;
2905
2906 csb = objt_cs(si_b->end);
2907 cob = cs_conn(csb);
2908 acb = objt_appctx(si_b->end);
2909 srv = objt_server(s->target);
2910 if (srv)
2911 dst = srv->id;
2912 else if (acb)
2913 dst = acb->applet->name;
2914
2915 if (rate < 0) {
2916 fmt = "A bogus %s [%p] halted the whole thread for at least %d microseconds "
2917 "resulting in a complete freeze of all other tasks, aborting now! Please "
2918 "report this error to developers "
2919 "[src=%s fe=%s be=%s dst=%s rqf=%x rqa=%x rpf=%x rpa=%x sif=%s,%x sib=%s,%x "
2920 "cof=%p,%x:%s/%s/%s/%d cob=%p,%x:%s/%s/%s/%d csf=%p,%x csb=%p,%x af=%p,%u ab=%p,%u]\n";
2921 rate = -rate;
2922 }
2923 else {
2924 fmt = "A bogus %s [%p] is spinning at %d calls per second and refuses to die, "
2925 "aborting now! Please report this error to developers "
2926 "[src=%s fe=%s be=%s dst=%s rqf=%x rqa=%x rpf=%x rpa=%x sif=%s,%x sib=%s,%x "
2927 "cof=%p,%x:%s/%s/%s/%d cob=%p,%x:%s/%s/%s/%d csf=%p,%x csb=%p,%x af=%p,%u ab=%p,%u]\n";
2928 }
2929
2930 memprintf(&msg, fmt,
2931 obj_type_name(obj), ptr, rate, src, s->sess->fe->id, s->be->id, dst,
2932 req->flags, req->analysers, res->flags, res->analysers,
2933 si_state_str(si_f->state), si_f->flags,
2934 si_state_str(si_b->state), si_b->flags,
2935 cof, cof ? cof->flags : 0, conn_get_mux_name(cof), conn_get_xprt_name(cof), conn_get_ctrl_name(cof), cof?cof->handle.fd:0,
2936 cob, cob ? cob->flags : 0, conn_get_mux_name(cob), conn_get_xprt_name(cob), conn_get_ctrl_name(cob), cob?cob->handle.fd:0,
2937 csf, csf ? csf->flags : 0, csb, csb ? csb->flags : 0,
2938 acf, acf ? acf->st0 : 0, acb, acb ? acb->st0 : 0);
2939
2940 ha_alert("%s", msg);
2941 send_log(NULL, LOG_EMERG, "%s", msg);
2942 abort();
2943}
2944
Willy Tarreau8b22a712010-06-18 17:46:06 +02002945/************************************************************************/
2946/* All supported ACL keywords must be declared here. */
2947/************************************************************************/
2948
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02002949/* 0=OK, <0=Alert, >0=Warning */
2950static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
2951 struct proxy *px, struct act_rule *rule,
2952 char **err)
2953{
2954 struct action_kw *kw;
2955
2956 /* Check if the service name exists. */
2957 if (*(args[*cur_arg]) == 0) {
2958 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01002959 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02002960 }
2961
2962 /* lookup for keyword corresponding to a service. */
2963 kw = action_lookup(&service_keywords, args[*cur_arg]);
2964 if (!kw) {
2965 memprintf(err, "'%s' unknown service name.", args[1]);
2966 return ACT_RET_PRS_ERR;
2967 }
2968 (*cur_arg)++;
2969
2970 /* executes specific rule parser. */
2971 rule->kw = kw;
2972 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
2973 return ACT_RET_PRS_ERR;
2974
2975 /* Register processing function. */
2976 rule->action_ptr = process_use_service;
2977 rule->action = ACT_CUSTOM;
2978
2979 return ACT_RET_PRS_OK;
2980}
2981
2982void service_keywords_register(struct action_kw_list *kw_list)
2983{
2984 LIST_ADDQ(&service_keywords, &kw_list->list);
2985}
2986
Willy Tarreau679bba12019-03-19 08:08:10 +01002987/* Lists the known services on <out> */
2988void list_services(FILE *out)
2989{
2990 struct action_kw_list *kw_list;
2991 int found = 0;
2992 int i;
2993
2994 fprintf(out, "Available services :");
2995 list_for_each_entry(kw_list, &service_keywords, list) {
2996 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
2997 if (!found)
2998 fputc('\n', out);
2999 found = 1;
3000 fprintf(out, "\t%s\n", kw_list->kw[i].kw);
3001 }
3002 }
3003 if (!found)
3004 fprintf(out, " none\n");
3005}
William Lallemand4c5b4d52016-11-21 08:51:11 +01003006
3007/* This function dumps a complete stream state onto the stream interface's
3008 * read buffer. The stream has to be set in strm. It returns 0 if the output
3009 * buffer is full and it needs to be called again, otherwise non-zero. It is
3010 * designed to be called from stats_dump_strm_to_buffer() below.
3011 */
3012static int stats_dump_full_strm_to_buffer(struct stream_interface *si, struct stream *strm)
3013{
3014 struct appctx *appctx = __objt_appctx(si->end);
3015 struct tm tm;
3016 extern const char *monthname[12];
3017 char pn[INET6_ADDRSTRLEN];
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003018 struct conn_stream *cs;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003019 struct connection *conn;
3020 struct appctx *tmpctx;
3021
3022 chunk_reset(&trash);
3023
3024 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != strm->uniq_id) {
3025 /* stream changed, no need to go any further */
3026 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaue6e52362019-01-04 17:42:57 +01003027 if (ci_putchk(si_ic(si), &trash) == -1)
3028 goto full;
3029 goto done;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003030 }
3031
3032 switch (appctx->ctx.sess.section) {
3033 case 0: /* main status of the stream */
3034 appctx->ctx.sess.uid = strm->uniq_id;
3035 appctx->ctx.sess.section = 1;
3036 /* fall through */
3037
3038 case 1:
3039 get_localtime(strm->logs.accept_date.tv_sec, &tm);
3040 chunk_appendf(&trash,
3041 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
3042 strm,
3043 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3044 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(strm->logs.accept_date.tv_usec),
3045 strm->uniq_id,
3046 strm_li(strm) ? strm_li(strm)->proto->name : "?");
3047
3048 conn = objt_conn(strm_orig(strm));
3049 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
3050 case AF_INET:
3051 case AF_INET6:
3052 chunk_appendf(&trash, " source=%s:%d\n",
3053 pn, get_host_port(&conn->addr.from));
3054 break;
3055 case AF_UNIX:
3056 chunk_appendf(&trash, " source=unix:%d\n", strm_li(strm)->luid);
3057 break;
3058 default:
3059 /* no more information to print right now */
3060 chunk_appendf(&trash, "\n");
3061 break;
3062 }
3063
3064 chunk_appendf(&trash,
Willy Tarreauadf7a152019-01-07 10:10:07 +01003065 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p waiting=%d\n",
3066 strm->flags, strm->si[1].conn_retries, strm->srv_conn, strm->pend_pos,
3067 !LIST_ISEMPTY(&strm->buffer_wait.list));
William Lallemand4c5b4d52016-11-21 08:51:11 +01003068
3069 chunk_appendf(&trash,
3070 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
3071 strm_fe(strm)->id, strm_fe(strm)->uuid, strm_fe(strm)->mode ? "http" : "tcp",
3072 strm_li(strm) ? strm_li(strm)->name ? strm_li(strm)->name : "?" : "?",
3073 strm_li(strm) ? strm_li(strm)->luid : 0);
3074
3075 if (conn)
3076 conn_get_to_addr(conn);
3077
3078 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
3079 case AF_INET:
3080 case AF_INET6:
3081 chunk_appendf(&trash, " addr=%s:%d\n",
3082 pn, get_host_port(&conn->addr.to));
3083 break;
3084 case AF_UNIX:
3085 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(strm)->luid);
3086 break;
3087 default:
3088 /* no more information to print right now */
3089 chunk_appendf(&trash, "\n");
3090 break;
3091 }
3092
3093 if (strm->be->cap & PR_CAP_BE)
3094 chunk_appendf(&trash,
3095 " backend=%s (id=%u mode=%s)",
3096 strm->be->id,
3097 strm->be->uuid, strm->be->mode ? "http" : "tcp");
3098 else
3099 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
3100
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003101 cs = objt_cs(strm->si[1].end);
3102 conn = cs_conn(cs);
3103
William Lallemand4c5b4d52016-11-21 08:51:11 +01003104 if (conn)
3105 conn_get_from_addr(conn);
3106
3107 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
3108 case AF_INET:
3109 case AF_INET6:
3110 chunk_appendf(&trash, " addr=%s:%d\n",
3111 pn, get_host_port(&conn->addr.from));
3112 break;
3113 case AF_UNIX:
3114 chunk_appendf(&trash, " addr=unix\n");
3115 break;
3116 default:
3117 /* no more information to print right now */
3118 chunk_appendf(&trash, "\n");
3119 break;
3120 }
3121
3122 if (strm->be->cap & PR_CAP_BE)
3123 chunk_appendf(&trash,
3124 " server=%s (id=%u)",
3125 objt_server(strm->target) ? objt_server(strm->target)->id : "<none>",
3126 objt_server(strm->target) ? objt_server(strm->target)->puid : 0);
3127 else
3128 chunk_appendf(&trash, " server=<NONE> (id=-1)");
3129
3130 if (conn)
3131 conn_get_to_addr(conn);
3132
3133 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
3134 case AF_INET:
3135 case AF_INET6:
3136 chunk_appendf(&trash, " addr=%s:%d\n",
3137 pn, get_host_port(&conn->addr.to));
3138 break;
3139 case AF_UNIX:
3140 chunk_appendf(&trash, " addr=unix\n");
3141 break;
3142 default:
3143 /* no more information to print right now */
3144 chunk_appendf(&trash, "\n");
3145 break;
3146 }
3147
3148 chunk_appendf(&trash,
Willy Tarreau2e9c1d22019-04-24 08:28:31 +02003149 " task=%p (state=0x%02x nice=%d calls=%u rate=%u exp=%s tmask=0x%lx%s",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003150 strm->task,
3151 strm->task->state,
Willy Tarreau2e9c1d22019-04-24 08:28:31 +02003152 strm->task->nice, strm->task->calls, read_freq_ctr(&strm->call_rate),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003153 strm->task->expire ?
3154 tick_is_expired(strm->task->expire, now_ms) ? "<PAST>" :
3155 human_time(TICKS_TO_MS(strm->task->expire - now_ms),
3156 TICKS_TO_MS(1000)) : "<NEVER>",
Christopher Fauletf0205062017-11-15 20:56:43 +01003157 strm->task->thread_mask,
William Lallemand4c5b4d52016-11-21 08:51:11 +01003158 task_in_rq(strm->task) ? ", running" : "");
3159
3160 chunk_appendf(&trash,
3161 " age=%s)\n",
3162 human_time(now.tv_sec - strm->logs.accept_date.tv_sec, 1));
3163
3164 if (strm->txn)
3165 chunk_appendf(&trash,
Willy Tarreau7778b592019-01-07 10:38:10 +01003166 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n"
3167 " req.f=0x%02x blen=%llu chnk=%llu next=%u\n"
3168 " rsp.f=0x%02x blen=%llu chnk=%llu next=%u\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003169 strm->txn, strm->txn->flags, strm->txn->meth, strm->txn->status,
Willy Tarreau7778b592019-01-07 10:38:10 +01003170 h1_msg_state_str(strm->txn->req.msg_state), h1_msg_state_str(strm->txn->rsp.msg_state),
3171 strm->txn->req.flags, strm->txn->req.body_len, strm->txn->req.chunk_len, strm->txn->req.next,
3172 strm->txn->rsp.flags, strm->txn->rsp.body_len, strm->txn->rsp.chunk_len, strm->txn->rsp.next);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003173
3174 chunk_appendf(&trash,
Christopher Faulet224a2d72019-01-04 15:06:13 +01003175 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s et=0x%03x sub=%d)\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003176 &strm->si[0],
3177 si_state_str(strm->si[0].state),
3178 strm->si[0].flags,
3179 obj_type_name(strm->si[0].end),
3180 obj_base_ptr(strm->si[0].end),
3181 strm->si[0].exp ?
3182 tick_is_expired(strm->si[0].exp, now_ms) ? "<PAST>" :
3183 human_time(TICKS_TO_MS(strm->si[0].exp - now_ms),
3184 TICKS_TO_MS(1000)) : "<NEVER>",
Christopher Faulet224a2d72019-01-04 15:06:13 +01003185 strm->si[0].err_type, strm->si[0].wait_event.events);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003186
3187 chunk_appendf(&trash,
Christopher Faulet224a2d72019-01-04 15:06:13 +01003188 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s et=0x%03x sub=%d)\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003189 &strm->si[1],
3190 si_state_str(strm->si[1].state),
3191 strm->si[1].flags,
3192 obj_type_name(strm->si[1].end),
3193 obj_base_ptr(strm->si[1].end),
3194 strm->si[1].exp ?
3195 tick_is_expired(strm->si[1].exp, now_ms) ? "<PAST>" :
3196 human_time(TICKS_TO_MS(strm->si[1].exp - now_ms),
3197 TICKS_TO_MS(1000)) : "<NEVER>",
Christopher Faulet224a2d72019-01-04 15:06:13 +01003198 strm->si[1].err_type, strm->si[1].wait_event.events);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003199
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003200 if ((cs = objt_cs(strm->si[0].end)) != NULL) {
3201 conn = cs->conn;
3202
William Lallemand4c5b4d52016-11-21 08:51:11 +01003203 chunk_appendf(&trash,
Willy Tarreau53a47662017-08-28 10:53:00 +02003204 " co0=%p ctrl=%s xprt=%s mux=%s data=%s target=%s:%p\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003205 conn,
3206 conn_get_ctrl_name(conn),
3207 conn_get_xprt_name(conn),
Willy Tarreau53a47662017-08-28 10:53:00 +02003208 conn_get_mux_name(conn),
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003209 cs_get_data_name(cs),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003210 obj_type_name(conn->target),
3211 obj_base_ptr(conn->target));
3212
3213 chunk_appendf(&trash,
Christopher Fauletf0205062017-11-15 20:56:43 +01003214 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d fd.tmask=0x%lx\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003215 conn->flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003216 conn->handle.fd,
3217 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].state : 0,
Olivier Houchard12568362018-01-31 18:07:29 +01003218 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].cache.next >= -2 : 0,
Willy Tarreauebc78d72018-01-20 23:53:50 +01003219 conn->handle.fd >= 0 ? !!(fdtab[conn->handle.fd].update_mask & tid_bit) : 0,
Christopher Fauletf0205062017-11-15 20:56:43 +01003220 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].thread_mask: 0);
Willy Tarreaufb3b1b02018-12-18 14:28:24 +01003221
3222 chunk_appendf(&trash, " cs=%p csf=0x%08x ctx=%p\n", cs, cs->flags, cs->ctx);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003223 }
3224 else if ((tmpctx = objt_appctx(strm->si[0].end)) != NULL) {
3225 chunk_appendf(&trash,
Willy Tarreau22d63a22019-04-24 08:41:29 +02003226 " app0=%p st0=%d st1=%d st2=%d applet=%s tmask=0x%lx nice=%d calls=%u rate=%u cpu=%llu lat=%llu\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003227 tmpctx,
3228 tmpctx->st0,
3229 tmpctx->st1,
3230 tmpctx->st2,
Christopher Fauletf0205062017-11-15 20:56:43 +01003231 tmpctx->applet->name,
Willy Tarreau34b1fac2018-05-31 14:40:19 +02003232 tmpctx->thread_mask,
Willy Tarreau22d63a22019-04-24 08:41:29 +02003233 tmpctx->t->nice, tmpctx->t->calls, read_freq_ctr(&tmpctx->call_rate),
Willy Tarreau9efd7452018-05-31 14:48:54 +02003234 (unsigned long long)tmpctx->t->cpu_time, (unsigned long long)tmpctx->t->lat_time);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003235 }
3236
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003237 if ((cs = objt_cs(strm->si[1].end)) != NULL) {
3238 conn = cs->conn;
3239
William Lallemand4c5b4d52016-11-21 08:51:11 +01003240 chunk_appendf(&trash,
Willy Tarreau53a47662017-08-28 10:53:00 +02003241 " co1=%p ctrl=%s xprt=%s mux=%s data=%s target=%s:%p\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003242 conn,
3243 conn_get_ctrl_name(conn),
3244 conn_get_xprt_name(conn),
Willy Tarreau53a47662017-08-28 10:53:00 +02003245 conn_get_mux_name(conn),
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003246 cs_get_data_name(cs),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003247 obj_type_name(conn->target),
3248 obj_base_ptr(conn->target));
3249
3250 chunk_appendf(&trash,
Christopher Fauletf0205062017-11-15 20:56:43 +01003251 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d fd.tmask=0x%lx\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003252 conn->flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003253 conn->handle.fd,
3254 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].state : 0,
Olivier Houchard12568362018-01-31 18:07:29 +01003255 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].cache.next >= -2 : 0,
Willy Tarreauebc78d72018-01-20 23:53:50 +01003256 conn->handle.fd >= 0 ? !!(fdtab[conn->handle.fd].update_mask & tid_bit) : 0,
Christopher Fauletf0205062017-11-15 20:56:43 +01003257 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].thread_mask: 0);
Willy Tarreaufb3b1b02018-12-18 14:28:24 +01003258
3259 chunk_appendf(&trash, " cs=%p csf=0x%08x ctx=%p\n", cs, cs->flags, cs->ctx);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003260 }
3261 else if ((tmpctx = objt_appctx(strm->si[1].end)) != NULL) {
3262 chunk_appendf(&trash,
Willy Tarreau22d63a22019-04-24 08:41:29 +02003263 " app1=%p st0=%d st1=%d st2=%d applet=%s tmask=0x%lx nice=%d calls=%u rate=%u cpu=%llu lat=%llu\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003264 tmpctx,
3265 tmpctx->st0,
3266 tmpctx->st1,
3267 tmpctx->st2,
Christopher Fauletf0205062017-11-15 20:56:43 +01003268 tmpctx->applet->name,
Willy Tarreau34b1fac2018-05-31 14:40:19 +02003269 tmpctx->thread_mask,
Willy Tarreau22d63a22019-04-24 08:41:29 +02003270 tmpctx->t->nice, tmpctx->t->calls, read_freq_ctr(&tmpctx->call_rate),
Willy Tarreau9efd7452018-05-31 14:48:54 +02003271 (unsigned long long)tmpctx->t->cpu_time, (unsigned long long)tmpctx->t->lat_time);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003272 }
3273
3274 chunk_appendf(&trash,
3275 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
3276 " an_exp=%s",
3277 &strm->req,
3278 strm->req.flags, strm->req.analysers,
3279 strm->req.pipe ? strm->req.pipe->data : 0,
3280 strm->req.to_forward, strm->req.total,
3281 strm->req.analyse_exp ?
3282 human_time(TICKS_TO_MS(strm->req.analyse_exp - now_ms),
3283 TICKS_TO_MS(1000)) : "<NEVER>");
3284
3285 chunk_appendf(&trash,
3286 " rex=%s",
3287 strm->req.rex ?
3288 human_time(TICKS_TO_MS(strm->req.rex - now_ms),
3289 TICKS_TO_MS(1000)) : "<NEVER>");
3290
3291 chunk_appendf(&trash,
3292 " wex=%s\n"
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003293 " buf=%p data=%p o=%u p=%u req.next=%d i=%u size=%u\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003294 strm->req.wex ?
3295 human_time(TICKS_TO_MS(strm->req.wex - now_ms),
3296 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003297 &strm->req.buf,
3298 b_orig(&strm->req.buf), (unsigned int)co_data(&strm->req),
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003299 (unsigned int)ci_head_ofs(&strm->req),
Willy Tarreau591d4452018-06-15 17:21:00 +02003300 strm->txn ? strm->txn->req.next : 0, (unsigned int)ci_data(&strm->req),
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003301 (unsigned int)strm->req.buf.size);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003302
Christopher Fauletb9af8812019-01-04 14:30:44 +01003303 if (IS_HTX_STRM(strm)) {
3304 struct htx *htx = htxbuf(&strm->req.buf);
3305
3306 chunk_appendf(&trash,
Willy Tarreaub84e67f2019-01-07 10:01:34 +01003307 " htx=%p flags=0x%x size=%u data=%u used=%u wrap=%s extra=%llu\n",
3308 htx, htx->flags, htx->size, htx->data, htx->used,
Christopher Fauletb9af8812019-01-04 14:30:44 +01003309 (!htx->used || htx->tail+1 == htx->wrap) ? "NO" : "YES",
3310 (unsigned long long)htx->extra);
3311 }
3312
William Lallemand4c5b4d52016-11-21 08:51:11 +01003313 chunk_appendf(&trash,
3314 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
3315 " an_exp=%s",
3316 &strm->res,
3317 strm->res.flags, strm->res.analysers,
3318 strm->res.pipe ? strm->res.pipe->data : 0,
3319 strm->res.to_forward, strm->res.total,
3320 strm->res.analyse_exp ?
3321 human_time(TICKS_TO_MS(strm->res.analyse_exp - now_ms),
3322 TICKS_TO_MS(1000)) : "<NEVER>");
3323
3324 chunk_appendf(&trash,
3325 " rex=%s",
3326 strm->res.rex ?
3327 human_time(TICKS_TO_MS(strm->res.rex - now_ms),
3328 TICKS_TO_MS(1000)) : "<NEVER>");
3329
3330 chunk_appendf(&trash,
3331 " wex=%s\n"
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003332 " buf=%p data=%p o=%u p=%u rsp.next=%d i=%u size=%u\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003333 strm->res.wex ?
3334 human_time(TICKS_TO_MS(strm->res.wex - now_ms),
3335 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003336 &strm->res.buf,
3337 b_orig(&strm->res.buf), (unsigned int)co_data(&strm->res),
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003338 (unsigned int)ci_head_ofs(&strm->res),
Willy Tarreau591d4452018-06-15 17:21:00 +02003339 strm->txn ? strm->txn->rsp.next : 0, (unsigned int)ci_data(&strm->res),
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003340 (unsigned int)strm->res.buf.size);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003341
Christopher Fauletb9af8812019-01-04 14:30:44 +01003342 if (IS_HTX_STRM(strm)) {
3343 struct htx *htx = htxbuf(&strm->res.buf);
3344
3345 chunk_appendf(&trash,
3346 " htx=%p flags=0x%x size=%u data=%u used=%u wrap=%s extra=%llu\n",
3347 htx, htx->flags, htx->size, htx->data, htx->used,
3348 (!htx->used || htx->tail+1 == htx->wrap) ? "NO" : "YES",
3349 (unsigned long long)htx->extra);
3350 }
3351
Willy Tarreaue6e52362019-01-04 17:42:57 +01003352 if (ci_putchk(si_ic(si), &trash) == -1)
3353 goto full;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003354
3355 /* use other states to dump the contents */
3356 }
3357 /* end of dump */
Willy Tarreaue6e52362019-01-04 17:42:57 +01003358 done:
William Lallemand4c5b4d52016-11-21 08:51:11 +01003359 appctx->ctx.sess.uid = 0;
3360 appctx->ctx.sess.section = 0;
3361 return 1;
Willy Tarreaue6e52362019-01-04 17:42:57 +01003362 full:
3363 return 0;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003364}
3365
3366
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003367static int cli_parse_show_sess(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand4c5b4d52016-11-21 08:51:11 +01003368{
William Lallemand4c5b4d52016-11-21 08:51:11 +01003369 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3370 return 1;
3371
3372 if (*args[2] && strcmp(args[2], "all") == 0)
3373 appctx->ctx.sess.target = (void *)-1;
3374 else if (*args[2])
3375 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
3376 else
3377 appctx->ctx.sess.target = NULL;
3378 appctx->ctx.sess.section = 0; /* start with stream status */
3379 appctx->ctx.sess.pos = 0;
3380
3381 return 0;
3382}
3383
3384/* This function dumps all streams' states onto the stream interface's
3385 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreaue6e52362019-01-04 17:42:57 +01003386 * to be called again, otherwise non-zero. It proceeds in an isolated
3387 * thread so there is no thread safety issue here.
William Lallemand4c5b4d52016-11-21 08:51:11 +01003388 */
3389static int cli_io_handler_dump_sess(struct appctx *appctx)
3390{
3391 struct stream_interface *si = appctx->owner;
3392 struct connection *conn;
3393
Willy Tarreaue6e52362019-01-04 17:42:57 +01003394 thread_isolate();
3395
William Lallemand4c5b4d52016-11-21 08:51:11 +01003396 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
3397 /* If we're forced to shut down, we might have to remove our
3398 * reference to the last stream being dumped.
3399 */
3400 if (appctx->st2 == STAT_ST_LIST) {
3401 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
3402 LIST_DEL(&appctx->ctx.sess.bref.users);
3403 LIST_INIT(&appctx->ctx.sess.bref.users);
3404 }
3405 }
Willy Tarreaue6e52362019-01-04 17:42:57 +01003406 goto done;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003407 }
3408
3409 chunk_reset(&trash);
3410
3411 switch (appctx->st2) {
3412 case STAT_ST_INIT:
3413 /* the function had not been called yet, let's prepare the
3414 * buffer for a response. We initialize the current stream
3415 * pointer to the first in the global list. When a target
3416 * stream is being destroyed, it is responsible for updating
3417 * this pointer. We know we have reached the end when this
3418 * pointer points back to the head of the streams list.
3419 */
3420 LIST_INIT(&appctx->ctx.sess.bref.users);
3421 appctx->ctx.sess.bref.ref = streams.n;
3422 appctx->st2 = STAT_ST_LIST;
3423 /* fall through */
3424
3425 case STAT_ST_LIST:
3426 /* first, let's detach the back-ref from a possible previous stream */
3427 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
3428 LIST_DEL(&appctx->ctx.sess.bref.users);
3429 LIST_INIT(&appctx->ctx.sess.bref.users);
3430 }
3431
3432 /* and start from where we stopped */
3433 while (appctx->ctx.sess.bref.ref != &streams) {
3434 char pn[INET6_ADDRSTRLEN];
3435 struct stream *curr_strm;
3436
3437 curr_strm = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
3438
3439 if (appctx->ctx.sess.target) {
3440 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_strm)
3441 goto next_sess;
3442
3443 LIST_ADDQ(&curr_strm->back_refs, &appctx->ctx.sess.bref.users);
3444 /* call the proper dump() function and return if we're missing space */
Willy Tarreaue6e52362019-01-04 17:42:57 +01003445 if (!stats_dump_full_strm_to_buffer(si, curr_strm))
3446 goto full;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003447
3448 /* stream dump complete */
3449 LIST_DEL(&appctx->ctx.sess.bref.users);
3450 LIST_INIT(&appctx->ctx.sess.bref.users);
3451 if (appctx->ctx.sess.target != (void *)-1) {
3452 appctx->ctx.sess.target = NULL;
3453 break;
3454 }
3455 else
3456 goto next_sess;
3457 }
3458
3459 chunk_appendf(&trash,
3460 "%p: proto=%s",
3461 curr_strm,
3462 strm_li(curr_strm) ? strm_li(curr_strm)->proto->name : "?");
3463
3464 conn = objt_conn(strm_orig(curr_strm));
3465 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
3466 case AF_INET:
3467 case AF_INET6:
3468 chunk_appendf(&trash,
3469 " src=%s:%d fe=%s be=%s srv=%s",
3470 pn,
3471 get_host_port(&conn->addr.from),
3472 strm_fe(curr_strm)->id,
3473 (curr_strm->be->cap & PR_CAP_BE) ? curr_strm->be->id : "<NONE>",
3474 objt_server(curr_strm->target) ? objt_server(curr_strm->target)->id : "<none>"
3475 );
3476 break;
3477 case AF_UNIX:
3478 chunk_appendf(&trash,
3479 " src=unix:%d fe=%s be=%s srv=%s",
3480 strm_li(curr_strm)->luid,
3481 strm_fe(curr_strm)->id,
3482 (curr_strm->be->cap & PR_CAP_BE) ? curr_strm->be->id : "<NONE>",
3483 objt_server(curr_strm->target) ? objt_server(curr_strm->target)->id : "<none>"
3484 );
3485 break;
3486 }
3487
3488 chunk_appendf(&trash,
Willy Tarreau2e9c1d22019-04-24 08:28:31 +02003489 " ts=%02x age=%s calls=%u rate=%u cpu=%llu lat=%llu",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003490 curr_strm->task->state,
3491 human_time(now.tv_sec - curr_strm->logs.tv_accept.tv_sec, 1),
Willy Tarreau2e9c1d22019-04-24 08:28:31 +02003492 curr_strm->task->calls, read_freq_ctr(&curr_strm->call_rate),
Willy Tarreau9efd7452018-05-31 14:48:54 +02003493 (unsigned long long)curr_strm->task->cpu_time, (unsigned long long)curr_strm->task->lat_time);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003494
3495 chunk_appendf(&trash,
Willy Tarreau506a29a2018-07-18 10:07:58 +02003496 " rq[f=%06xh,i=%u,an=%02xh,rx=%s",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003497 curr_strm->req.flags,
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003498 (unsigned int)ci_data(&curr_strm->req),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003499 curr_strm->req.analysers,
3500 curr_strm->req.rex ?
3501 human_time(TICKS_TO_MS(curr_strm->req.rex - now_ms),
3502 TICKS_TO_MS(1000)) : "");
3503
3504 chunk_appendf(&trash,
3505 ",wx=%s",
3506 curr_strm->req.wex ?
3507 human_time(TICKS_TO_MS(curr_strm->req.wex - now_ms),
3508 TICKS_TO_MS(1000)) : "");
3509
3510 chunk_appendf(&trash,
3511 ",ax=%s]",
3512 curr_strm->req.analyse_exp ?
3513 human_time(TICKS_TO_MS(curr_strm->req.analyse_exp - now_ms),
3514 TICKS_TO_MS(1000)) : "");
3515
3516 chunk_appendf(&trash,
Willy Tarreau506a29a2018-07-18 10:07:58 +02003517 " rp[f=%06xh,i=%u,an=%02xh,rx=%s",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003518 curr_strm->res.flags,
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003519 (unsigned int)ci_data(&curr_strm->res),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003520 curr_strm->res.analysers,
3521 curr_strm->res.rex ?
3522 human_time(TICKS_TO_MS(curr_strm->res.rex - now_ms),
3523 TICKS_TO_MS(1000)) : "");
3524
3525 chunk_appendf(&trash,
3526 ",wx=%s",
3527 curr_strm->res.wex ?
3528 human_time(TICKS_TO_MS(curr_strm->res.wex - now_ms),
3529 TICKS_TO_MS(1000)) : "");
3530
3531 chunk_appendf(&trash,
3532 ",ax=%s]",
3533 curr_strm->res.analyse_exp ?
3534 human_time(TICKS_TO_MS(curr_strm->res.analyse_exp - now_ms),
3535 TICKS_TO_MS(1000)) : "");
3536
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003537 conn = cs_conn(objt_cs(curr_strm->si[0].end));
William Lallemand4c5b4d52016-11-21 08:51:11 +01003538 chunk_appendf(&trash,
3539 " s0=[%d,%1xh,fd=%d,ex=%s]",
3540 curr_strm->si[0].state,
3541 curr_strm->si[0].flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003542 conn ? conn->handle.fd : -1,
William Lallemand4c5b4d52016-11-21 08:51:11 +01003543 curr_strm->si[0].exp ?
3544 human_time(TICKS_TO_MS(curr_strm->si[0].exp - now_ms),
3545 TICKS_TO_MS(1000)) : "");
3546
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003547 conn = cs_conn(objt_cs(curr_strm->si[1].end));
William Lallemand4c5b4d52016-11-21 08:51:11 +01003548 chunk_appendf(&trash,
3549 " s1=[%d,%1xh,fd=%d,ex=%s]",
3550 curr_strm->si[1].state,
3551 curr_strm->si[1].flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003552 conn ? conn->handle.fd : -1,
William Lallemand4c5b4d52016-11-21 08:51:11 +01003553 curr_strm->si[1].exp ?
3554 human_time(TICKS_TO_MS(curr_strm->si[1].exp - now_ms),
3555 TICKS_TO_MS(1000)) : "");
3556
3557 chunk_appendf(&trash,
3558 " exp=%s",
3559 curr_strm->task->expire ?
3560 human_time(TICKS_TO_MS(curr_strm->task->expire - now_ms),
3561 TICKS_TO_MS(1000)) : "");
3562 if (task_in_rq(curr_strm->task))
3563 chunk_appendf(&trash, " run(nice=%d)", curr_strm->task->nice);
3564
3565 chunk_appendf(&trash, "\n");
3566
Willy Tarreau06d80a92017-10-19 14:32:15 +02003567 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand4c5b4d52016-11-21 08:51:11 +01003568 /* let's try again later from this stream. We add ourselves into
3569 * this stream's users so that it can remove us upon termination.
3570 */
William Lallemand4c5b4d52016-11-21 08:51:11 +01003571 LIST_ADDQ(&curr_strm->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreaue6e52362019-01-04 17:42:57 +01003572 goto full;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003573 }
3574
3575 next_sess:
3576 appctx->ctx.sess.bref.ref = curr_strm->list.n;
3577 }
3578
3579 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
3580 /* specified stream not found */
3581 if (appctx->ctx.sess.section > 0)
3582 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3583 else
3584 chunk_appendf(&trash, "Session not found.\n");
3585
Willy Tarreaue6e52362019-01-04 17:42:57 +01003586 if (ci_putchk(si_ic(si), &trash) == -1)
3587 goto full;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003588
3589 appctx->ctx.sess.target = NULL;
3590 appctx->ctx.sess.uid = 0;
Willy Tarreaue6e52362019-01-04 17:42:57 +01003591 goto done;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003592 }
William Lallemand4c5b4d52016-11-21 08:51:11 +01003593 /* fall through */
3594
3595 default:
3596 appctx->st2 = STAT_ST_FIN;
Willy Tarreaue6e52362019-01-04 17:42:57 +01003597 goto done;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003598 }
Willy Tarreaue6e52362019-01-04 17:42:57 +01003599 done:
3600 thread_release();
3601 return 1;
3602 full:
3603 thread_release();
3604 si_rx_room_blk(si);
3605 return 0;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003606}
3607
3608static void cli_release_show_sess(struct appctx *appctx)
3609{
3610 if (appctx->st2 == STAT_ST_LIST) {
Willy Tarreau7ce3f092017-11-10 16:24:41 +01003611 HA_SPIN_LOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003612 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
3613 LIST_DEL(&appctx->ctx.sess.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003614 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003615 }
3616}
3617
Willy Tarreau61b65212016-11-24 11:09:25 +01003618/* Parses the "shutdown session" directive, it always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003619static int cli_parse_shutdown_session(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau61b65212016-11-24 11:09:25 +01003620{
3621 struct stream *strm, *ptr;
3622
3623 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3624 return 1;
3625
3626 if (!*args[2]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003627 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau61b65212016-11-24 11:09:25 +01003628 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003629 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau61b65212016-11-24 11:09:25 +01003630 return 1;
3631 }
3632
3633 ptr = (void *)strtoul(args[2], NULL, 0);
3634
3635 /* first, look for the requested stream in the stream table */
3636 list_for_each_entry(strm, &streams, list) {
3637 if (strm == ptr)
3638 break;
3639 }
3640
3641 /* do we have the stream ? */
3642 if (strm != ptr) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003643 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau61b65212016-11-24 11:09:25 +01003644 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003645 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau61b65212016-11-24 11:09:25 +01003646 return 1;
3647 }
3648
3649 stream_shutdown(strm, SF_ERR_KILLED);
3650 return 1;
3651}
3652
Willy Tarreau4e46b622016-11-23 16:50:48 +01003653/* Parses the "shutdown session server" directive, it always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003654static int cli_parse_shutdown_sessions_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau4e46b622016-11-23 16:50:48 +01003655{
3656 struct server *sv;
3657 struct stream *strm, *strm_bck;
3658
3659 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3660 return 1;
3661
3662 sv = cli_find_server(appctx, args[3]);
3663 if (!sv)
3664 return 1;
3665
3666 /* kill all the stream that are on this server */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003667 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau4e46b622016-11-23 16:50:48 +01003668 list_for_each_entry_safe(strm, strm_bck, &sv->actconns, by_srv)
3669 if (strm->srv_conn == sv)
3670 stream_shutdown(strm, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003671 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau4e46b622016-11-23 16:50:48 +01003672 return 1;
3673}
3674
William Lallemand4c5b4d52016-11-21 08:51:11 +01003675/* register cli keywords */
3676static struct cli_kw_list cli_kws = {{ },{
3677 { { "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 },
Willy Tarreau61b65212016-11-24 11:09:25 +01003678 { { "shutdown", "session", NULL }, "shutdown session : kill a specific session", cli_parse_shutdown_session, NULL, NULL },
Willy Tarreau4e46b622016-11-23 16:50:48 +01003679 { { "shutdown", "sessions", "server" }, "shutdown sessions server : kill sessions on a server", cli_parse_shutdown_sessions_server, NULL, NULL },
William Lallemand4c5b4d52016-11-21 08:51:11 +01003680 {{},}
3681}};
3682
Willy Tarreau0108d902018-11-25 19:14:37 +01003683INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3684
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003685/* main configuration keyword registration. */
3686static struct action_kw_list stream_tcp_keywords = { ILH, {
3687 { "use-service", stream_parse_use_service },
3688 { /* END */ }
3689}};
3690
Willy Tarreau0108d902018-11-25 19:14:37 +01003691INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &stream_tcp_keywords);
3692
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003693static struct action_kw_list stream_http_keywords = { ILH, {
3694 { "use-service", stream_parse_use_service },
3695 { /* END */ }
3696}};
3697
Willy Tarreau0108d902018-11-25 19:14:37 +01003698INITCALL1(STG_REGISTER, http_req_keywords_register, &stream_http_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003699
Willy Tarreaubaaee002006-06-26 02:48:02 +02003700/*
3701 * Local variables:
3702 * c-indent-level: 8
3703 * c-basic-offset: 8
3704 * End:
3705 */