blob: 8646ce84f5ae9f15e53215252a4c90c6c9841153 [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>
Frédéric Lécaille03cdf552019-05-20 10:08:27 +020042#include <proto/dict.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010043#include <proto/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <proto/stats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020045#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020046#include <proto/filters.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020047#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020048#include <proto/frontend.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>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020057#include <proto/http_ana.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
Christopher Fauleteea8fc72019-11-05 16:18:10 +010077/* trace source and events */
78static void strm_trace(enum trace_level level, uint64_t mask,
79 const struct trace_source *src,
80 const struct ist where, const struct ist func,
81 const void *a1, const void *a2, const void *a3, const void *a4);
82
83/* The event representation is split like this :
84 * strm - stream
85 * si - stream interface
86 * http - http analyzis
87 * tcp - tcp analyzis
88 *
89 * STRM_EV_* macros are defined in <proto/stream.h>
90 */
91static const struct trace_event strm_trace_events[] = {
92 { .mask = STRM_EV_STRM_NEW, .name = "strm_new", .desc = "new stream" },
93 { .mask = STRM_EV_STRM_FREE, .name = "strm_free", .desc = "release stream" },
94 { .mask = STRM_EV_STRM_ERR, .name = "strm_err", .desc = "error during stream processing" },
95 { .mask = STRM_EV_STRM_ANA, .name = "strm_ana", .desc = "stream analyzers" },
96 { .mask = STRM_EV_STRM_PROC, .name = "strm_proc", .desc = "stream processing" },
97
98 { .mask = STRM_EV_SI_ST, .name = "si_state", .desc = "processing stream-interface states" },
99
100 { .mask = STRM_EV_HTTP_ANA, .name = "http_ana", .desc = "HTTP analyzers" },
101 { .mask = STRM_EV_HTTP_ERR, .name = "http_err", .desc = "error during HTTP analyzis" },
102
103 { .mask = STRM_EV_TCP_ANA, .name = "tcp_ana", .desc = "TCP analyzers" },
104 { .mask = STRM_EV_TCP_ERR, .name = "tcp_err", .desc = "error during TCP analyzis" },
105 {}
106};
107
108static const struct name_desc strm_trace_lockon_args[4] = {
109 /* arg1 */ { /* already used by the stream */ },
110 /* arg2 */ { },
111 /* arg3 */ { },
112 /* arg4 */ { }
113};
114
115static const struct name_desc strm_trace_decoding[] = {
116#define STRM_VERB_CLEAN 1
117 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
118#define STRM_VERB_MINIMAL 2
119 { .name="minimal", .desc="report info on stream and stream-interfaces" },
120#define STRM_VERB_SIMPLE 3
121 { .name="simple", .desc="add info on request and response channels" },
122#define STRM_VERB_ADVANCED 4
123 { .name="advanced", .desc="add info on channel's buffer for data and developer levels only" },
124#define STRM_VERB_COMPLETE 5
125 { .name="complete", .desc="add info on channel's buffer" },
126 { /* end */ }
127};
128
129struct trace_source trace_strm = {
130 .name = IST("stream"),
131 .desc = "Applicative stream",
132 .arg_def = TRC_ARG1_STRM, // TRACE()'s first argument is always a stream
133 .default_cb = strm_trace,
134 .known_events = strm_trace_events,
135 .lockon_args = strm_trace_lockon_args,
136 .decoding = strm_trace_decoding,
137 .report_events = ~0, // report everything by default
138};
139
140#define TRACE_SOURCE &trace_strm
141INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
142
143/* the stream traces always expect that arg1, if non-null, is of a stream (from
144 * which we can derive everything), that arg2, if non-null, is an http
145 * transaction, that arg3, if non-null, is an http message.
146 */
147static void strm_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
148 const struct ist where, const struct ist func,
149 const void *a1, const void *a2, const void *a3, const void *a4)
150{
151 const struct stream *s = a1;
152 const struct http_txn *txn = a2;
153 const struct http_msg *msg = a3;
154 struct task *task;
155 const struct stream_interface *si_f, *si_b;
156 const struct channel *req, *res;
157 struct htx *htx;
158
159 if (!s || src->verbosity < STRM_VERB_CLEAN)
160 return;
161
162 task = s->task;
163 si_f = &s->si[0];
164 si_b = &s->si[1];
165 req = &s->req;
166 res = &s->res;
167 htx = (msg ? htxbuf(&msg->chn->buf) : NULL);
168
169 /* General info about the stream (htx/tcp, id...) */
170 chunk_appendf(&trace_buf, " : [%u,%s]",
171 s->uniq_id, ((s->flags & SF_HTX) ? "HTX" : "TCP"));
172 if (s->unique_id)
173 chunk_appendf(&trace_buf, " id=%s", s->unique_id);
174
175 /* Front and back stream-int state */
176 chunk_appendf(&trace_buf, " SI=(%s,%s)",
177 si_state_str(si_f->state), si_state_str(si_b->state));
178
179 /* If txn is defined, HTTP req/rep states */
180 if (txn)
181 chunk_appendf(&trace_buf, " HTTP=(%s,%s)",
182 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state));
183 if (msg)
184 chunk_appendf(&trace_buf, " %s", ((msg->chn->flags & CF_ISRESP) ? "RESPONSE" : "REQUEST"));
185
186 if (src->verbosity == STRM_VERB_CLEAN)
187 return;
188
189 /* If msg defined, display status-line if possible (verbosity > MINIMAL) */
190 if (src->verbosity > STRM_VERB_MINIMAL && htx && htx_nbblks(htx)) {
191 const struct htx_blk *blk = htx_get_head_blk(htx);
192 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
193 enum htx_blk_type type = htx_get_blk_type(blk);
194
195 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
196 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
197 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
198 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
199 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
200 }
201
202
203 /* If txn defined info about HTTP msgs, otherwise info about SI. */
204 if (txn) {
205 chunk_appendf(&trace_buf, " - t=%p s=(%p,0x%08x) txn.flags=0x%08x, http.flags=(0x%08x,0x%08x) status=%d",
206 task, s, s->flags, txn->flags, txn->req.flags, txn->rsp.flags, txn->status);
207 }
208 else {
209 chunk_appendf(&trace_buf, " - t=%p s=(%p,0x%08x) si_f=(%p,0x%08x,0x%x) si_b=(%p,0x%08x,0x%x) retries=%d",
210 task, s, s->flags, si_f, si_f->flags, si_f->err_type,
211 si_b, si_b->flags, si_b->err_type, si_b->conn_retries);
212 }
213
214 if (src->verbosity == STRM_VERB_MINIMAL)
215 return;
216
217
218 /* If txn defined, don't display all channel info */
219 if (src->verbosity == STRM_VERB_SIMPLE || txn) {
220 chunk_appendf(&trace_buf, " req=(%p .fl=0x%08x .exp(r,w,a)=(%u,%u,%u))",
221 req, req->flags, req->rex, req->wex, req->analyse_exp);
222 chunk_appendf(&trace_buf, " res=(%p .fl=0x%08x .exp(r,w,a)=(%u,%u,%u))",
223 res, res->flags, res->rex, res->wex, res->analyse_exp);
224 }
225 else {
226 chunk_appendf(&trace_buf, " req=(%p .fl=0x%08x .ana=0x%08x .exp(r,w,a)=(%u,%u,%u) .o=%lu .tot=%llu .to_fwd=%u)",
227 req, req->flags, req->analysers, req->rex, req->wex, req->analyse_exp,
228 req->output, req->total, req->to_forward);
229 chunk_appendf(&trace_buf, " res=(%p .fl=0x%08x .ana=0x%08x .exp(r,w,a)=(%u,%u,%u) .o=%lu .tot=%llu .to_fwd=%u)",
230 res, res->flags, res->analysers, res->rex, res->wex, res->analyse_exp,
231 res->output, res->total, res->to_forward);
232 }
233
234 if (src->verbosity == STRM_VERB_SIMPLE ||
235 (src->verbosity == STRM_VERB_ADVANCED && src->level < TRACE_LEVEL_DATA))
236 return;
237
238 /* channels' buffer info */
239 if (s->flags & SF_HTX) {
240 struct htx *rqhtx = htxbuf(&req->buf);
241 struct htx *rphtx = htxbuf(&res->buf);
242
243 chunk_appendf(&trace_buf, " htx=(%u/%u#%u, %u/%u#%u)",
244 rqhtx->data, rqhtx->size, htx_nbblks(rqhtx),
245 rphtx->data, rphtx->size, htx_nbblks(rphtx));
246 }
247 else {
248 chunk_appendf(&trace_buf, " buf=(%u@%p+%u/%u, %u@%p+%u/%u)",
249 (unsigned int)b_data(&req->buf), b_orig(&req->buf),
250 (unsigned int)b_head_ofs(&req->buf), (unsigned int)b_size(&req->buf),
251 (unsigned int)b_data(&req->buf), b_orig(&req->buf),
252 (unsigned int)b_head_ofs(&req->buf), (unsigned int)b_size(&req->buf));
253 }
254
255 /* If msg defined, display htx info if defined (level > USER) */
256 if (src->level > TRACE_LEVEL_USER && htx && htx_nbblks(htx)) {
257 int full = 0;
258
259 /* Full htx info (level > STATE && verbosity > SIMPLE) */
260 if (src->level > TRACE_LEVEL_STATE) {
261 if (src->verbosity == STRM_VERB_COMPLETE)
262 full = 1;
263 }
264
265 chunk_memcat(&trace_buf, "\n\t", 2);
266 htx_dump(&trace_buf, htx, full);
267 }
268}
269
Willy Tarreau5790eb02017-08-28 17:18:36 +0200270/* Create a new stream for connection <conn>. Return < 0 on error. This is only
271 * valid right after the handshake, before the connection's data layer is
272 * initialized, because it relies on the session to be in conn->owner.
273 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200274int stream_create_from_cs(struct conn_stream *cs)
Willy Tarreau5790eb02017-08-28 17:18:36 +0200275{
276 struct stream *strm;
277
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200278 strm = stream_new(cs->conn->owner, &cs->obj_type);
Willy Tarreau5790eb02017-08-28 17:18:36 +0200279 if (strm == NULL)
280 return -1;
281
282 task_wakeup(strm->task, TASK_WOKEN_INIT);
283 return 0;
284}
285
Willy Tarreaub882dd82018-11-06 15:50:21 +0100286/* Callback used to wake up a stream when an input buffer is available. The
287 * stream <s>'s stream interfaces are checked for a failed buffer allocation
Willy Tarreaud0f5bbc2018-11-14 11:10:26 +0100288 * as indicated by the presence of the SI_FL_RXBLK_ROOM flag and the lack of a
Willy Tarreaub882dd82018-11-06 15:50:21 +0100289 * buffer, and and input buffer is assigned there (at most one). The function
290 * returns 1 and wakes the stream up if a buffer was taken, otherwise zero.
291 * It's designed to be called from __offer_buffer().
292 */
293int stream_buf_available(void *arg)
294{
295 struct stream *s = arg;
296
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100297 if (!s->req.buf.size && !s->req.pipe && (s->si[0].flags & SI_FL_RXBLK_BUFF) &&
Willy Tarreaub882dd82018-11-06 15:50:21 +0100298 b_alloc_margin(&s->req.buf, global.tune.reserved_bufs))
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100299 si_rx_buff_rdy(&s->si[0]);
300 else if (!s->res.buf.size && !s->res.pipe && (s->si[1].flags & SI_FL_RXBLK_BUFF) &&
Willy Tarreaub882dd82018-11-06 15:50:21 +0100301 b_alloc_margin(&s->res.buf, 0))
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100302 si_rx_buff_rdy(&s->si[1]);
Willy Tarreaub882dd82018-11-06 15:50:21 +0100303 else
304 return 0;
305
306 task_wakeup(s->task, TASK_WOKEN_RES);
307 return 1;
308
309}
310
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200311/* This function is called from the session handler which detects the end of
Willy Tarreau73b65ac2015-04-08 18:26:29 +0200312 * handshake, in order to complete initialization of a valid stream. It must be
Joseph Herlant4cc8d0d2018-11-15 09:14:14 -0800313 * called with a completely initialized session. It returns the pointer to
Willy Tarreau73b65ac2015-04-08 18:26:29 +0200314 * the newly created stream, or NULL in case of fatal error. The client-facing
Willy Tarreau87787ac2017-08-28 16:22:54 +0200315 * end point is assigned to <origin>, which must be valid. The stream's task
316 * is configured with a nice value inherited from the listener's nice if any.
317 * The task's context is set to the new stream, and its function is set to
318 * process_stream(). Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +0200319 */
Willy Tarreau87787ac2017-08-28 16:22:54 +0200320struct stream *stream_new(struct session *sess, enum obj_type *origin)
Willy Tarreau2542b532012-08-31 16:01:23 +0200321{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200322 struct stream *s;
Willy Tarreau87787ac2017-08-28 16:22:54 +0200323 struct task *t;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200324 struct conn_stream *cs = objt_cs(origin);
Willy Tarreau73b65ac2015-04-08 18:26:29 +0200325 struct appctx *appctx = objt_appctx(origin);
Christopher Fauletb3484d62018-11-29 15:19:05 +0100326 const struct cs_info *csinfo;
Willy Tarreau2542b532012-08-31 16:01:23 +0200327
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100328 DBG_TRACE_ENTER(STRM_EV_STRM_NEW);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100329 if (unlikely((s = pool_alloc(pool_head_stream)) == NULL))
Willy Tarreau87787ac2017-08-28 16:22:54 +0200330 goto out_fail_alloc;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200331
332 /* minimum stream initialization required for an embryonic stream is
333 * fairly low. We need very little to execute L4 ACLs, then we need a
334 * task to make the client-side connection live on its own.
335 * - flags
336 * - stick-entry tracking
337 */
338 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200339 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200340 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200341 tv_zero(&s->logs.tv_request);
342 s->logs.t_queue = -1;
343 s->logs.t_connect = -1;
344 s->logs.t_data = -1;
345 s->logs.t_close = 0;
346 s->logs.bytes_in = s->logs.bytes_out = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -0400347 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
348 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Baptiste Assmann333939c2019-01-21 08:34:50 +0100349 s->obj_type = OBJ_TYPE_STREAM;
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200350
Christopher Fauletb3484d62018-11-29 15:19:05 +0100351 csinfo = si_get_cs_info(cs);
352 if (csinfo) {
353 s->logs.accept_date = csinfo->create_date;
354 s->logs.tv_accept = csinfo->tv_create;
355 s->logs.t_handshake = csinfo->t_handshake;
356 s->logs.t_idle = csinfo->t_idle;
357 }
358 else {
359 s->logs.accept_date = sess->accept_date;
360 s->logs.tv_accept = sess->tv_accept;
361 s->logs.t_handshake = sess->t_handshake;
362 s->logs.t_idle = -1;
363 }
364
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200365 /* default logging function */
366 s->do_log = strm_log;
367
368 /* default error reporting function, may be changed by analysers */
369 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200370
371 /* Initialise the current rule list pointer to NULL. We are sure that
372 * any rulelist match the NULL pointer.
373 */
374 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200375 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200376
Willy Tarreaua68f7622015-09-21 17:48:24 +0200377 /* Copy SC counters for the stream. We don't touch refcounts because
378 * any reference we have is inherited from the session. Since the stream
379 * doesn't exist without the session, the session's existence guarantees
380 * we don't lose the entry. During the store operation, the stream won't
381 * touch these ones.
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200382 */
Thierry FOURNIERc8fdb982015-08-16 12:03:39 +0200383 memcpy(s->stkctr, sess->stkctr, sizeof(s->stkctr));
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200384
385 s->sess = sess;
386 s->si[0].flags = SI_FL_NONE;
387 s->si[1].flags = SI_FL_ISBACK;
388
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100389 s->uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200390
Willy Tarreau87b09662015-04-03 00:22:06 +0200391 /* OK, we're keeping the stream, so let's properly initialize the stream */
Willy Tarreau2542b532012-08-31 16:01:23 +0200392 LIST_INIT(&s->back_refs);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100393
394 LIST_INIT(&s->buffer_wait.list);
395 s->buffer_wait.target = s;
Willy Tarreaub882dd82018-11-06 15:50:21 +0100396 s->buffer_wait.wakeup_cb = stream_buf_available;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200397
Willy Tarreau2e9c1d22019-04-24 08:28:31 +0200398 s->call_rate.curr_sec = s->call_rate.curr_ctr = s->call_rate.prev_ctr = 0;
William Lallemandcf62f7e2018-10-26 14:47:40 +0200399 s->pcli_next_pid = 0;
William Lallemandebf61802018-12-11 16:10:57 +0100400 s->pcli_flags = 0;
Willy Tarreau2542b532012-08-31 16:01:23 +0200401 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200402
Emeric Brunc60def82017-09-27 14:59:38 +0200403 if ((t = task_new(tid_bit)) == NULL)
Willy Tarreau87787ac2017-08-28 16:22:54 +0200404 goto out_fail_alloc;
405
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200406 s->task = t;
Christopher Faulet9d810ca2016-12-08 22:33:52 +0100407 s->pending_events = 0;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200408 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200409 t->context = s;
410 t->expire = TICK_ETERNITY;
Willy Tarreau87787ac2017-08-28 16:22:54 +0200411 if (sess->listener)
412 t->nice = sess->listener->nice;
Willy Tarreau2542b532012-08-31 16:01:23 +0200413
Willy Tarreau87b09662015-04-03 00:22:06 +0200414 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200415 * This changes later when switching rules are executed or
416 * when the default backend is assigned.
417 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200418 s->be = sess->fe;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200419 s->req.buf = BUF_NULL;
420 s->res.buf = BUF_NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200421 s->req_cap = NULL;
422 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200423
Willy Tarreauebcd4842015-06-19 11:59:02 +0200424 /* Initialise all the variables contexts even if not used.
425 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200426 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200427 vars_init(&s->vars_txn, SCOPE_TXN);
428 vars_init(&s->vars_reqres, SCOPE_REQ);
429
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200430 /* this part should be common with other protocols */
Olivier Houchard8f0b4c62018-08-02 18:21:38 +0200431 if (si_reset(&s->si[0]) < 0)
432 goto out_fail_alloc;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200433 si_set_state(&s->si[0], SI_ST_EST);
Hongbo Longe39683c2017-03-10 18:41:51 +0100434 s->si[0].hcto = sess->fe->timeout.clientfin;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200435
Christopher Faulet0e160ff2019-04-03 10:01:07 +0200436 if (cs && cs->conn->mux) {
437 if (cs->conn->mux->flags & MX_FL_CLEAN_ABRT)
438 s->si[0].flags |= SI_FL_CLEAN_ABRT;
439 if (cs->conn->mux->flags & MX_FL_HTX)
440 s->flags |= SF_HTX;
441 }
Christopher Faulet3494c632019-07-15 14:58:26 +0200442 /* Set SF_HTX flag for HTTP frontends. */
443 if (sess->fe->mode == PR_MODE_HTTP)
Christopher Faulet0e160ff2019-04-03 10:01:07 +0200444 s->flags |= SF_HTX;
Willy Tarreau984fca92017-12-20 16:31:43 +0100445
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200446 /* attach the incoming connection to the stream interface now. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200447 if (cs)
448 si_attach_cs(&s->si[0], cs);
Willy Tarreau470280e2015-04-05 01:33:13 +0200449 else if (appctx)
450 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200451
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200452 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200453 s->si[0].flags |= SI_FL_INDEP_STR;
454
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200455 /* pre-initialize the other side's stream interface to an INIT state. The
456 * callbacks will be initialized before attempting to connect.
457 */
Olivier Houchard8f0b4c62018-08-02 18:21:38 +0200458 if (si_reset(&s->si[1]) < 0)
459 goto out_fail_alloc_si1;
Hongbo Longe39683c2017-03-10 18:41:51 +0100460 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200461
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200462 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200463 s->si[1].flags |= SI_FL_INDEP_STR;
464
Willy Tarreau87b09662015-04-03 00:22:06 +0200465 stream_init_srv_conn(s);
Willy Tarreau9b82d942016-12-05 00:26:31 +0100466 s->target = sess->listener ? sess->listener->default_target : NULL;
Willy Tarreau90420602019-07-18 15:09:57 +0200467 s->target_addr = NULL;
Willy Tarreau9b82d942016-12-05 00:26:31 +0100468
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200469 s->pend_pos = NULL;
Patrick Hemmer268a7072018-05-11 12:52:31 -0400470 s->priority_class = 0;
471 s->priority_offset = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200472
473 /* init store persistence */
474 s->store_count = 0;
475
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100476 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100477 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau9b82d942016-12-05 00:26:31 +0100478 s->req.analysers = sess->listener ? sess->listener->analysers : 0;
Willy Tarreaue223e3b2017-11-18 15:39:10 +0100479
480 if (!sess->fe->fe_req_ana) {
481 channel_auto_connect(&s->req); /* don't wait to establish connection */
482 channel_auto_close(&s->req); /* let the producer forward close requests */
483 }
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200484
485 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100486 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100487 s->req.rex = TICK_ETERNITY;
488 s->req.wex = TICK_ETERNITY;
489 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200490
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100491 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100492 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100493 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200494
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200495 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100496 s->req.flags |= CF_NEVER_WAIT;
497 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200498 }
499
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200500 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100501 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100502 s->res.rex = TICK_ETERNITY;
503 s->res.wex = TICK_ETERNITY;
504 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200505
Willy Tarreaueee5b512015-04-03 23:46:31 +0200506 s->txn = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100507 s->hlua = NULL;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100508
Frédéric Lécaillebed883a2019-04-23 17:26:33 +0200509 s->dns_ctx.dns_requester = NULL;
510 s->dns_ctx.hostname_dn = NULL;
511 s->dns_ctx.hostname_dn_len = 0;
512 s->dns_ctx.parent = NULL;
513
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100514 HA_SPIN_LOCK(STRMS_LOCK, &streams_lock);
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200515 LIST_ADDQ(&streams, &s->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100516 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200517
Christopher Faulet92d36382015-11-05 13:35:03 +0100518 if (flt_stream_init(s) < 0 || flt_stream_start(s) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200519 goto out_fail_accept;
520
Olivier Houcharda254a372019-04-05 15:30:12 +0200521 s->si[1].l7_buffer = BUF_NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200522 /* finish initialization of the accepted file descriptor */
Olivier Houchard53216e72018-10-10 15:46:36 +0200523 if (appctx)
Willy Tarreau0cd3bd62018-11-06 18:46:37 +0100524 si_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200525
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200526 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200527 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200528
529 /* it is important not to call the wakeup function directly but to
530 * pass through task_wakeup(), because this one knows how to apply
Emeric Brun5f77fef2017-05-29 15:26:51 +0200531 * priorities to tasks. Using multi thread we must be sure that
532 * stream is fully initialized before calling task_wakeup. So
533 * the caller must handle the task_wakeup
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200534 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100535 DBG_TRACE_LEAVE(STRM_EV_STRM_NEW, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200536 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200537
538 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200539 out_fail_accept:
Christopher Faulet92d36382015-11-05 13:35:03 +0100540 flt_stream_release(s, 0);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200541 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200542 tasklet_free(s->si[1].wait_event.tasklet);
Willy Tarreaue5f229e2018-10-15 11:08:55 +0200543 LIST_DEL(&s->list);
Olivier Houchard8f0b4c62018-08-02 18:21:38 +0200544out_fail_alloc_si1:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200545 tasklet_free(s->si[0].wait_event.tasklet);
Willy Tarreau87787ac2017-08-28 16:22:54 +0200546 out_fail_alloc:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100547 pool_free(pool_head_stream, s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100548 DBG_TRACE_DEVEL("leaving on error", STRM_EV_STRM_NEW|STRM_EV_STRM_ERR);
Willy Tarreau02d86382015-04-05 12:00:52 +0200549 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200550}
551
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200553 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200555static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200556{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200557 struct session *sess = strm_sess(s);
558 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100559 struct bref *bref, *back;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200560 struct conn_stream *cli_cs = objt_cs(s->si[0].end);
Willy Tarreau5bcfd562017-11-29 14:05:38 +0100561 int must_free_sess;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200562 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100563
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100564 DBG_TRACE_POINT(STRM_EV_STRM_FREE, s);
565
Willy Tarreau0ad46fa2019-05-17 14:20:05 +0200566 /* detach the stream from its own task before even releasing it so
567 * that walking over a task list never exhibits a dying stream.
568 */
569 s->task->context = NULL;
570 __ha_barrier_store();
571
Willy Tarreaud0ad4a82018-07-25 11:13:53 +0200572 pendconn_free(s);
Willy Tarreau922a8062008-12-04 09:33:58 +0100573
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100574 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200575 if (s->flags & SF_CURR_SESS) {
576 s->flags &= ~SF_CURR_SESS;
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100577 _HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100578 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100579 if (may_dequeue_tasks(objt_server(s->target), s->be))
580 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100581 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100582
Willy Tarreau7c669d72008-06-20 15:04:11 +0200583 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200584 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200585 * it should normally be only the same as the one above,
586 * so this should not happen in fact.
587 */
588 sess_change_server(s, NULL);
589 }
590
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100591 if (s->req.pipe)
592 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100593
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100594 if (s->res.pipe)
595 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100596
Willy Tarreaubf883e02014-11-25 21:10:35 +0100597 /* We may still be present in the buffer wait queue */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100598 if (!LIST_ISEMPTY(&s->buffer_wait.list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100599 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100600 LIST_DEL(&s->buffer_wait.list);
601 LIST_INIT(&s->buffer_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100602 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100603 }
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200604 if (s->req.buf.size || s->res.buf.size) {
Willy Tarreaue0d0b402019-08-08 08:06:27 +0200605 b_free(&s->req.buf);
606 b_free(&s->res.buf);
Olivier Houchard673867c2018-05-25 16:58:52 +0200607 offer_buffers(NULL, tasks_run_queue);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100608 }
Willy Tarreau9b28e032012-10-12 23:49:43 +0200609
Olivier Houchard32211a12019-02-01 18:10:46 +0100610 pool_free(pool_head_uniqueid, s->unique_id);
Willy Tarreau09c4bab2019-02-10 18:49:37 +0100611 s->unique_id = NULL;
Olivier Houchard32211a12019-02-01 18:10:46 +0100612
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100613 hlua_ctx_destroy(s->hlua);
614 s->hlua = NULL;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200615 if (s->txn)
616 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100617
Willy Tarreau1e954912012-10-12 17:50:05 +0200618 /* ensure the client-side transport layer is destroyed */
Willy Tarreau32560732017-10-08 11:33:44 +0200619 if (cli_cs)
620 cs_close(cli_cs);
Willy Tarreau1e954912012-10-12 17:50:05 +0200621
Willy Tarreaua4cda672010-06-06 18:28:49 +0200622 for (i = 0; i < s->store_count; i++) {
623 if (!s->store[i].ts)
624 continue;
625 stksess_free(s->store[i].table, s->store[i].ts);
626 s->store[i].ts = NULL;
627 }
628
Willy Tarreaueee5b512015-04-03 23:46:31 +0200629 if (s->txn) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100630 pool_free(pool_head_http_txn, s->txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200631 s->txn = NULL;
632 }
633
Baptiste Assmann333939c2019-01-21 08:34:50 +0100634 if (s->dns_ctx.dns_requester) {
635 free(s->dns_ctx.hostname_dn); s->dns_ctx.hostname_dn = NULL;
636 s->dns_ctx.hostname_dn_len = 0;
637 dns_unlink_resolution(s->dns_ctx.dns_requester);
638
639 pool_free(dns_requester_pool, s->dns_ctx.dns_requester);
640 s->dns_ctx.dns_requester = NULL;
641 }
642
Christopher Fauletd7c91962015-04-30 11:48:27 +0200643 flt_stream_stop(s);
Christopher Faulet92d36382015-11-05 13:35:03 +0100644 flt_stream_release(s, 0);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200645
Willy Tarreau92fb9832007-10-16 17:34:28 +0200646 if (fe) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100647 pool_free(fe->rsp_cap_pool, s->res_cap);
648 pool_free(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100650
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200651 /* Cleanup all variable contexts. */
Willy Tarreaucda7f3f2018-10-28 13:44:36 +0100652 if (!LIST_ISEMPTY(&s->vars_txn.head))
653 vars_prune(&s->vars_txn, s->sess, s);
654 if (!LIST_ISEMPTY(&s->vars_reqres.head))
655 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200656
Willy Tarreau87b09662015-04-03 00:22:06 +0200657 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200658
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100659 HA_SPIN_LOCK(STRMS_LOCK, &streams_lock);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100660 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100661 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200662 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100663 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100664 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100665 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200666 if (s->list.n != &streams)
667 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100668 bref->ref = s->list.n;
669 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100670 LIST_DEL(&s->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100671 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200672
Willy Tarreau5bcfd562017-11-29 14:05:38 +0100673 /* applets do not release session yet */
674 must_free_sess = objt_appctx(sess->origin) && sess->origin == s->si[0].end;
675
Olivier Houchard4fdec7a2018-10-11 17:09:14 +0200676
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200677 si_release_endpoint(&s->si[1]);
678 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200679
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200680 tasklet_free(s->si[0].wait_event.tasklet);
681 tasklet_free(s->si[1].wait_event.tasklet);
Olivier Houchardd7bd3e32018-10-18 16:22:02 +0200682
Olivier Houcharda254a372019-04-05 15:30:12 +0200683 b_free(&s->si[1].l7_buffer);
Olivier Houchard25607af2018-11-23 14:50:47 +0100684 if (must_free_sess) {
685 sess->origin = NULL;
Willy Tarreau5bcfd562017-11-29 14:05:38 +0100686 session_free(sess);
Olivier Houchard25607af2018-11-23 14:50:47 +0100687 }
Willy Tarreau5bcfd562017-11-29 14:05:38 +0100688
Willy Tarreau90420602019-07-18 15:09:57 +0200689 sockaddr_free(&s->target_addr);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100690 pool_free(pool_head_stream, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200691
692 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200693 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100694 pool_flush(pool_head_buffer);
695 pool_flush(pool_head_http_txn);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100696 pool_flush(pool_head_requri);
697 pool_flush(pool_head_capture);
698 pool_flush(pool_head_stream);
699 pool_flush(pool_head_session);
700 pool_flush(pool_head_connection);
701 pool_flush(pool_head_pendconn);
702 pool_flush(fe->req_cap_pool);
703 pool_flush(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200704 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200705}
706
Willy Tarreau656859d2014-11-25 19:46:36 +0100707
Willy Tarreau87b09662015-04-03 00:22:06 +0200708/* Allocates a work buffer for stream <s>. It is meant to be called inside
709 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200710 * to work fine, which is the response buffer so that an error message may be
711 * built and returned. Response buffers may be allocated from the reserve, this
712 * is critical to ensure that a response may always flow and will never block a
713 * server from releasing a connection. Returns 0 in case of failure, non-zero
714 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100715 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100716static int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100717{
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100718 if (!LIST_ISEMPTY(&s->buffer_wait.list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100719 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100720 LIST_DEL(&s->buffer_wait.list);
721 LIST_INIT(&s->buffer_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100722 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100723 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100724
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200725 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100726 return 1;
727
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100728 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100729 LIST_ADDQ(&buffer_wq, &s->buffer_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100730 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau656859d2014-11-25 19:46:36 +0100731 return 0;
732}
733
734/* releases unused buffers after processing. Typically used at the end of the
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100735 * update() functions. It will try to wake up as many tasks/applets as the
736 * number of buffers that it releases. In practice, most often streams are
737 * blocked on a single buffer, so it makes sense to try to wake two up when two
738 * buffers are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100739 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200740void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100741{
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100742 int offer = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100743
Willy Tarreau0c7ed5d2018-07-10 09:53:31 +0200744 if (c_size(&s->req) && c_empty(&s->req)) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100745 offer = 1;
746 b_free(&s->req.buf);
747 }
Willy Tarreau0c7ed5d2018-07-10 09:53:31 +0200748 if (c_size(&s->res) && c_empty(&s->res)) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100749 offer = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100750 b_free(&s->res.buf);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100751 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100752
Willy Tarreaubf883e02014-11-25 21:10:35 +0100753 /* if we're certain to have at least 1 buffer available, and there is
754 * someone waiting, we can wake up a waiter and offer them.
755 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100756 if (offer)
Olivier Houchard673867c2018-05-25 16:58:52 +0200757 offer_buffers(s, tasks_run_queue);
Willy Tarreau656859d2014-11-25 19:46:36 +0100758}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200759
Willy Tarreau87b09662015-04-03 00:22:06 +0200760void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100761{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200762 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100763 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200764 void *ptr1,*ptr2;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200765 struct stksess *ts;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100766 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100767
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100768 bytes = s->req.total - s->logs.bytes_in;
769 s->logs.bytes_in = s->req.total;
770 if (bytes) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100771 _HA_ATOMIC_ADD(&sess->fe->fe_counters.bytes_in, bytes);
772 _HA_ATOMIC_ADD(&s->be->be_counters.bytes_in, bytes);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100773
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100774 if (objt_server(s->target))
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100775 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.bytes_in, bytes);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200776
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200777 if (sess->listener && sess->listener->counters)
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100778 _HA_ATOMIC_ADD(&sess->listener->counters->bytes_in, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200779
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100780 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200781 struct stkctr *stkctr = &s->stkctr[i];
782
Emeric Brun819fc6f2017-06-13 19:37:32 +0200783 ts = stkctr_entry(stkctr);
784 if (!ts) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200785 stkctr = &sess->stkctr[i];
Emeric Brun819fc6f2017-06-13 19:37:32 +0200786 ts = stkctr_entry(stkctr);
787 if (!ts)
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200788 continue;
789 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200790
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100791 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200792 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_IN_CNT);
Emeric Brun57056f02015-06-15 18:29:57 +0200793 if (ptr1)
794 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200795
Emeric Brun819fc6f2017-06-13 19:37:32 +0200796 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_IN_RATE);
Emeric Brun57056f02015-06-15 18:29:57 +0200797 if (ptr2)
798 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200799 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100800 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun57056f02015-06-15 18:29:57 +0200801
802 /* If data was modified, we need to touch to re-schedule sync */
803 if (ptr1 || ptr2)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200804 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau30e71012007-11-26 20:15:35 +0100805 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100806 }
807
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100808 bytes = s->res.total - s->logs.bytes_out;
809 s->logs.bytes_out = s->res.total;
810 if (bytes) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100811 _HA_ATOMIC_ADD(&sess->fe->fe_counters.bytes_out, bytes);
812 _HA_ATOMIC_ADD(&s->be->be_counters.bytes_out, bytes);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100813
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100814 if (objt_server(s->target))
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100815 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.bytes_out, bytes);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200816
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200817 if (sess->listener && sess->listener->counters)
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100818 _HA_ATOMIC_ADD(&sess->listener->counters->bytes_out, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200819
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100820 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200821 struct stkctr *stkctr = &s->stkctr[i];
822
Emeric Brun819fc6f2017-06-13 19:37:32 +0200823 ts = stkctr_entry(stkctr);
824 if (!ts) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200825 stkctr = &sess->stkctr[i];
Emeric Brun819fc6f2017-06-13 19:37:32 +0200826 ts = stkctr_entry(stkctr);
827 if (!ts)
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200828 continue;
829 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200830
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100831 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200832 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Emeric Brun57056f02015-06-15 18:29:57 +0200833 if (ptr1)
834 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200835
Emeric Brun819fc6f2017-06-13 19:37:32 +0200836 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_OUT_RATE);
Emeric Brun57056f02015-06-15 18:29:57 +0200837 if (ptr2)
838 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200839 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100840 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun57056f02015-06-15 18:29:57 +0200841
842 /* If data was modified, we need to touch to re-schedule sync */
843 if (ptr1 || ptr2)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200844 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), 0);
Willy Tarreau30e71012007-11-26 20:15:35 +0100845 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100846 }
847}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200848
Willy Tarreaud66ed882019-06-05 18:02:04 +0200849/* This function is called with (si->state == SI_ST_CON) meaning that a
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100850 * connection was attempted and that the file descriptor is already allocated.
Willy Tarreaud66ed882019-06-05 18:02:04 +0200851 * We must check for timeout, error and abort. Possible output states are
852 * SI_ST_CER (error), SI_ST_DIS (abort), and SI_ST_CON (no change). This only
853 * works with connection-based streams. We know that there were no I/O event
854 * when reaching this function. Timeouts and errors are *not* cleared.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100855 */
Willy Tarreaud66ed882019-06-05 18:02:04 +0200856static void sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100857{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100858 struct stream_interface *si = &s->si[1];
859 struct channel *req = &s->req;
860 struct channel *rep = &s->res;
Olivier Houchard14547b22018-11-29 18:01:46 +0100861 struct conn_stream *srv_cs = objt_cs(si->end);
Willy Tarreaubf66bd12019-01-23 15:15:09 +0100862 struct connection *conn = srv_cs ? srv_cs->conn : objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100863
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100864 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
865
Willy Tarreaud66ed882019-06-05 18:02:04 +0200866 /* the client might want to abort */
867 if ((rep->flags & CF_SHUTW) ||
868 ((req->flags & CF_SHUTW_NOW) &&
869 (channel_is_empty(req) || (s->be->options & PR_O_ABRT_CLOSE)))) {
870 si->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200871 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100872 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100873 if (s->srv_error)
874 s->srv_error(s, si);
Willy Tarreaud66ed882019-06-05 18:02:04 +0200875 /* Note: state = SI_ST_DIS now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100876 DBG_TRACE_STATE("client abort during connection attempt", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100877 }
878
Willy Tarreaud66ed882019-06-05 18:02:04 +0200879 /* retryable error ? */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100880 else if (si->flags & (SI_FL_EXP|SI_FL_ERR)) {
Willy Tarreaud66ed882019-06-05 18:02:04 +0200881 if (!(s->flags & SF_SRV_REUSED) && conn) {
882 conn_stop_tracking(conn);
883 conn_full_close(conn);
884 }
Christopher Faulet7a3367c2019-05-09 11:46:52 +0200885
Willy Tarreaud66ed882019-06-05 18:02:04 +0200886 if (!si->err_type) {
887 if (si->flags & SI_FL_ERR)
888 si->err_type = SI_ET_CONN_ERR;
889 else
890 si->err_type = SI_ET_CONN_TO;
891 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100892
Willy Tarreaud66ed882019-06-05 18:02:04 +0200893 si->state = SI_ST_CER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100894 DBG_TRACE_STATE("connection failed, retry", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
Willy Tarreaud66ed882019-06-05 18:02:04 +0200895 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100896
897 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100898}
899
900/* This function is called with (si->state == SI_ST_CER) meaning that a
901 * previous connection attempt has failed and that the file descriptor
902 * has already been released. Possible causes include asynchronous error
903 * notification and time out. Possible output states are SI_ST_CLO when
904 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
905 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
906 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
Willy Tarreaud66ed882019-06-05 18:02:04 +0200907 * marked as in error state. Timeouts and errors are cleared before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100908 */
Willy Tarreaud66ed882019-06-05 18:02:04 +0200909static void sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100910{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100911 struct stream_interface *si = &s->si[1];
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200912 struct conn_stream *cs = objt_cs(si->end);
913 struct connection *conn = cs_conn(cs);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100914
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100915 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
916
Willy Tarreaud66ed882019-06-05 18:02:04 +0200917 si->exp = TICK_ETERNITY;
918 si->flags &= ~SI_FL_EXP;
919
Willy Tarreau87b09662015-04-03 00:22:06 +0200920 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100921 if (objt_server(s->target)) {
922 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100923
Willy Tarreaue7dff022015-04-03 01:14:29 +0200924 if (s->flags & SF_CURR_SESS) {
925 s->flags &= ~SF_CURR_SESS;
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100926 _HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100927 }
Willy Tarreau46d5b082017-07-26 20:13:37 +0200928
929 if ((si->flags & SI_FL_ERR) &&
930 conn && conn->err_code == CO_ER_SSL_MISMATCH_SNI) {
931 /* We tried to connect to a server which is configured
932 * with "verify required" and which doesn't have the
933 * "verifyhost" directive. The server presented a wrong
934 * certificate (a certificate for an unexpected name),
935 * which implies that we have used SNI in the handshake,
936 * and that the server doesn't have the associated cert
937 * and presented a default one.
938 *
939 * This is a serious enough issue not to retry. It's
940 * especially important because this wrong name might
941 * either be the result of a configuration error, and
942 * retrying will only hammer the server, or is caused
943 * by the use of a wrong SNI value, most likely
944 * provided by the client and we don't want to let the
945 * client provoke retries.
946 */
947 si->conn_retries = 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100948 DBG_TRACE_DEVEL("Bad SSL cert, disable connection retries", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
Willy Tarreau46d5b082017-07-26 20:13:37 +0200949 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100950 }
951
952 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200953 si->conn_retries--;
Olivier Houcharda254a372019-04-05 15:30:12 +0200954 if (si->conn_retries < 0 || !(s->be->retry_type & PR_RE_CONN_FAILED)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100955 if (!si->err_type) {
956 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100957 }
958
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100959 if (objt_server(s->target))
Olivier Houcharddc6111e2019-03-08 18:54:51 +0100960 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_conns, 1);
961 _HA_ATOMIC_ADD(&s->be->be_counters.failed_conns, 1);
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100962 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100963 if (may_dequeue_tasks(objt_server(s->target), s->be))
964 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100965
966 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200967 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100968 s->req.flags |= CF_WRITE_ERROR;
969 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100970
971 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100972 if (s->srv_error)
973 s->srv_error(s, si);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100974
975 DBG_TRACE_STATE("connection failed", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
976 goto end;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100977 }
978
Olivier Houchard4bd58672019-07-12 16:16:59 +0200979 stream_choose_redispatch(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100980
981 if (si->flags & SI_FL_ERR) {
982 /* The error was an asynchronous connection error, and we will
983 * likely have to retry connecting to the same server, most
984 * likely leading to the same result. To avoid this, we wait
Willy Tarreauf7a259d2019-01-23 16:29:53 +0100985 * MIN(one second, connect timeout) before retrying. We don't
986 * do it when the failure happened on a reused connection
987 * though.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100988 */
989
Willy Tarreaub0290662014-06-13 17:04:44 +0200990 int delay = 1000;
991
992 if (s->be->timeout.connect && s->be->timeout.connect < delay)
993 delay = s->be->timeout.connect;
994
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100995 if (!si->err_type)
996 si->err_type = SI_ET_CONN_ERR;
997
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200998 /* only wait when we're retrying on the same server */
Willy Tarreauf7a259d2019-01-23 16:29:53 +0100999 if ((si->state == SI_ST_ASS ||
1000 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
1001 (s->be->srv_act <= 1)) && !(s->flags & SF_SRV_REUSED)) {
Willy Tarreaudb6d0122014-06-13 17:40:15 +02001002 si->state = SI_ST_TAR;
1003 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
1004 }
Olivier Houchard39cc0202019-04-09 19:12:51 +02001005 si->flags &= ~SI_FL_ERR;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001006 DBG_TRACE_STATE("retry a new connection", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001007 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001008
1009 end:
1010 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreaud66ed882019-06-05 18:02:04 +02001011}
1012
1013/* This function is called with (si->state == SI_ST_RDY) meaning that a
1014 * connection was attempted, that the file descriptor is already allocated,
1015 * and that it has succeeded. We must still check for errors and aborts.
1016 * Possible output states are SI_ST_EST (established), SI_ST_CER (error),
1017 * and SI_ST_DIS (abort). This only works with connection-based streams.
1018 * Timeouts and errors are *not* cleared.
1019 */
1020static void sess_update_st_rdy_tcp(struct stream *s)
1021{
1022 struct stream_interface *si = &s->si[1];
1023 struct channel *req = &s->req;
1024 struct channel *rep = &s->res;
1025 struct conn_stream *srv_cs = objt_cs(si->end);
1026 struct connection *conn = srv_cs ? srv_cs->conn : objt_conn(si->end);
1027
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001028 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreaud66ed882019-06-05 18:02:04 +02001029 /* We know the connection at least succeeded, though it could have
1030 * since met an error for any other reason. At least it didn't time out
1031 * eventhough the timeout might have been reported right after success.
1032 * We need to take care of various situations here :
1033 * - everything might be OK. We have to switch to established.
1034 * - an I/O error might have been reported after a successful transfer,
1035 * which is not retryable and needs to be logged correctly, and needs
1036 * established as well
1037 * - SI_ST_CON implies !CF_WROTE_DATA but not conversely as we could
1038 * have validated a connection with incoming data (e.g. TCP with a
1039 * banner protocol), or just a successful connect() probe.
1040 * - the client might have requested a connection abort, this needs to
1041 * be checked before we decide to retry anything.
1042 */
1043
1044 /* it's still possible to handle client aborts or connection retries
1045 * before any data were sent.
1046 */
1047 if (!(req->flags & CF_WROTE_DATA)) {
1048 /* client abort ? */
1049 if ((rep->flags & CF_SHUTW) ||
1050 ((req->flags & CF_SHUTW_NOW) &&
1051 (channel_is_empty(req) || (s->be->options & PR_O_ABRT_CLOSE)))) {
1052 /* give up */
1053 si->flags |= SI_FL_NOLINGER;
1054 si_shutw(si);
1055 si->err_type |= SI_ET_CONN_ABRT;
1056 if (s->srv_error)
1057 s->srv_error(s, si);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001058 DBG_TRACE_STATE("client abort during connection attempt", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
1059 goto end;
Willy Tarreaud66ed882019-06-05 18:02:04 +02001060 }
1061
1062 /* retryable error ? */
1063 if (si->flags & SI_FL_ERR) {
1064 if (!(s->flags & SF_SRV_REUSED) && conn) {
1065 conn_stop_tracking(conn);
1066 conn_full_close(conn);
1067 }
1068
1069 if (!si->err_type)
1070 si->err_type = SI_ET_CONN_ERR;
1071 si->state = SI_ST_CER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001072 DBG_TRACE_STATE("connection failed, retry", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
1073 goto end;
Willy Tarreaud66ed882019-06-05 18:02:04 +02001074 }
1075 }
1076
1077 /* data were sent and/or we had no error, sess_establish() will
1078 * now take over.
1079 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001080 DBG_TRACE_STATE("connection established", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreaud66ed882019-06-05 18:02:04 +02001081 si->err_type = SI_ET_NONE;
1082 si->state = SI_ST_EST;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001083
1084 end:
1085 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001086}
1087
1088/*
1089 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +02001090 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001091 * SI_ST_INI or SI_ST_RDY) to SI_ST_EST, but only when a ->proto is defined.
Olivier Houchardaacc4052019-05-21 17:43:50 +02001092 * Note that it will switch the interface to SI_ST_DIS if we already have
1093 * the CF_SHUTR flag, it means we were able to forward the request, and
1094 * receive the response, before process_stream() had the opportunity to
1095 * make the switch from SI_ST_CON to SI_ST_EST. When that happens, we want
1096 * to go through sess_establish() anyway, to make sure the analysers run.
Willy Tarreaud66ed882019-06-05 18:02:04 +02001097 * Timeouts are cleared. Error are reported on the channel so that analysers
1098 * can handle them.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001099 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001100static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001101{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001102 struct stream_interface *si = &s->si[1];
Willy Tarreaud66ed882019-06-05 18:02:04 +02001103 struct conn_stream *srv_cs = objt_cs(si->end);
1104 struct connection *conn = srv_cs ? srv_cs->conn : objt_conn(si->end);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001105 struct channel *req = &s->req;
1106 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001107
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001108 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreaud66ed882019-06-05 18:02:04 +02001109 /* First, centralize the timers information, and clear any irrelevant
1110 * timeout.
1111 */
Willy Tarreau0e37f1c2013-12-31 23:06:46 +01001112 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud66ed882019-06-05 18:02:04 +02001113 si->exp = TICK_ETERNITY;
1114 si->flags &= ~SI_FL_EXP;
1115
1116 /* errors faced after sending data need to be reported */
1117 if (si->flags & SI_FL_ERR && req->flags & CF_WROTE_DATA) {
1118 /* Don't add CF_WRITE_ERROR if we're here because
1119 * early data were rejected by the server, or
1120 * http_wait_for_response() will never be called
1121 * to send a 425.
1122 */
1123 if (conn && conn->err_code != CO_ER_SSL_EARLY_FAILED)
1124 req->flags |= CF_WRITE_ERROR;
1125 rep->flags |= CF_READ_ERROR;
1126 si->err_type = SI_ET_DATA_ERR;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001127 DBG_TRACE_STATE("read/write error", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
Willy Tarreaud66ed882019-06-05 18:02:04 +02001128 }
1129
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001130 if (objt_server(s->target))
1131 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001132
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001133 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001134 /* if the user wants to log as soon as possible, without counting
1135 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001136 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau66425e32018-07-25 06:55:12 +02001137 /* note: no pend_pos here, session is established */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001138 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001139 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001140 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001141 }
1142 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +01001143 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001144 }
1145
Willy Tarreau0007d0a2018-12-11 18:01:38 +01001146 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Christopher Faulet309c6412015-12-02 09:57:32 +01001147
Willy Tarreau0007d0a2018-12-11 18:01:38 +01001148 /* Be sure to filter response headers if the backend is an HTTP proxy
1149 * and if there are filters attached to the stream. */
1150 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
1151 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
Christopher Faulet309c6412015-12-02 09:57:32 +01001152
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001153 si_rx_endp_more(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001154 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001155 if (objt_cs(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +02001156 /* real connections have timeouts */
1157 req->wto = s->be->timeout.server;
1158 rep->rto = s->be->timeout.server;
Olivier Houchard47e9a1a2018-11-07 17:55:19 +01001159 /* The connection is now established, try to read data from the
1160 * underlying layer, and subscribe to recv events. We use a
1161 * delayed recv here to give a chance to the data to flow back
1162 * by the time we process other tasks.
1163 */
1164 si_chk_rcv(si);
Willy Tarreaud04e8582010-05-31 12:31:35 +02001165 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001166 req->wex = TICK_ETERNITY;
Olivier Houchard78595262019-07-26 14:54:34 +02001167 /* If we managed to get the whole response, and we don't have anything
1168 * left to send, or can't, switch to SI_ST_DIS now. */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001169 if (rep->flags & (CF_SHUTR | CF_SHUTW)) {
Olivier Houchardaacc4052019-05-21 17:43:50 +02001170 si->state = SI_ST_DIS;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001171 DBG_TRACE_STATE("response channel shutdwn for read/write", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
1172 }
1173
1174 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001175}
1176
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001177/* Check if the connection request is in such a state that it can be aborted. */
1178static int check_req_may_abort(struct channel *req, struct stream *s)
1179{
1180 return ((req->flags & (CF_READ_ERROR)) ||
Willy Tarreaud651ba12018-01-12 10:42:12 +01001181 ((req->flags & (CF_SHUTW_NOW|CF_SHUTW)) && /* empty and client aborted */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001182 (channel_is_empty(req) || (s->be->options & PR_O_ABRT_CLOSE))));
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001183}
1184
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001185/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
1186 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01001187 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
1188 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
1189 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001190 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001191static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001192{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001193 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001194 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +01001195 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +01001196
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001197 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001198
1199 if (si->state == SI_ST_ASS) {
1200 /* Server assigned to connection request, we have to try to connect now */
1201 int conn_err;
1202
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001203 /* Before we try to initiate the connection, see if the
1204 * request may be aborted instead.
1205 */
1206 if (check_req_may_abort(req, s)) {
1207 si->err_type |= SI_ET_CONN_ABRT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001208 DBG_TRACE_STATE("connection aborted", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001209 goto abort_connection;
1210 }
1211
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001212 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001213 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001214
Willy Tarreaue7dff022015-04-03 01:14:29 +02001215 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01001216 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +01001217 if (srv)
1218 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001219 if (srv)
1220 srv_set_sess_last(srv);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001221 DBG_TRACE_STATE("connection attempt", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1222 goto end;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001223 }
1224
1225 /* We have received a synchronous error. We might have to
1226 * abort, retry immediately or redispatch.
1227 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001228 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001229 if (!si->err_type) {
1230 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001231 }
1232
Willy Tarreau827aee92011-03-10 16:55:02 +01001233 if (srv)
1234 srv_inc_sess_ctr(srv);
1235 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001236 srv_set_sess_last(srv);
1237 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001238 _HA_ATOMIC_ADD(&srv->counters.failed_conns, 1);
1239 _HA_ATOMIC_ADD(&s->be->be_counters.failed_conns, 1);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001240
Willy Tarreau87b09662015-04-03 00:22:06 +02001241 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +01001242 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001243 if (may_dequeue_tasks(srv, s->be))
1244 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001245
1246 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001247 si_shutr(si);
1248 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001249 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001250
1251 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1252
Willy Tarreau66425e32018-07-25 06:55:12 +02001253 /* we may need to know the position in the queue for logging */
1254 pendconn_cond_unlink(s->pend_pos);
1255
Willy Tarreau87b09662015-04-03 00:22:06 +02001256 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001257 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001258 if (s->srv_error)
1259 s->srv_error(s, si);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001260 DBG_TRACE_STATE("internal error during connection", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
1261 goto end;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001262 }
1263
1264 /* We are facing a retryable error, but we don't want to run a
1265 * turn-around now, as the problem is likely a source port
1266 * allocation problem, so we want to retry now.
1267 */
1268 si->state = SI_ST_CER;
1269 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001270 sess_update_st_cer(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001271
1272 DBG_TRACE_STATE("connection error, retry", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001273 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001274 }
1275 else if (si->state == SI_ST_QUE) {
1276 /* connection request was queued, check for any update */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +01001277 if (!pendconn_dequeue(s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001278 /* The connection is not in the queue anymore. Either
1279 * we have a server connection slot available and we
1280 * go directly to the assigned state, or we need to
1281 * load-balance first and go to the INI state.
1282 */
1283 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001284 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001285 si->state = SI_ST_REQ;
1286 else {
1287 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1288 si->state = SI_ST_ASS;
1289 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001290 DBG_TRACE_STATE("dequeue connection request", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1291 goto end;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001292 }
1293
1294 /* Connection request still in queue... */
1295 if (si->flags & SI_FL_EXP) {
1296 /* ... and timeout expired */
1297 si->exp = TICK_ETERNITY;
Willy Tarreaud66ed882019-06-05 18:02:04 +02001298 si->flags &= ~SI_FL_EXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001299 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau66425e32018-07-25 06:55:12 +02001300
1301 /* we may need to know the position in the queue for logging */
1302 pendconn_cond_unlink(s->pend_pos);
1303
Willy Tarreau827aee92011-03-10 16:55:02 +01001304 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001305 _HA_ATOMIC_ADD(&srv->counters.failed_conns, 1);
1306 _HA_ATOMIC_ADD(&s->be->be_counters.failed_conns, 1);
Willy Tarreau73b013b2012-05-21 16:31:45 +02001307 si_shutr(si);
1308 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001309 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001310 if (!si->err_type)
1311 si->err_type = SI_ET_QUEUE_TO;
1312 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001313 if (s->srv_error)
1314 s->srv_error(s, si);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001315 DBG_TRACE_STATE("connection request still queued", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1316 goto end;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001317 }
1318
1319 /* Connection remains in queue, check if we have to abort it */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001320 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001321 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau66425e32018-07-25 06:55:12 +02001322
1323 /* we may need to know the position in the queue for logging */
1324 pendconn_cond_unlink(s->pend_pos);
1325
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001326 si->err_type |= SI_ET_QUEUE_ABRT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001327 DBG_TRACE_STATE("abort queued connection request", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001328 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001329 }
1330
1331 /* Nothing changed */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001332 }
1333 else if (si->state == SI_ST_TAR) {
1334 /* Connection request might be aborted */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001335 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001336 si->err_type |= SI_ET_CONN_ABRT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001337 DBG_TRACE_STATE("connection aborted", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001338 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001339 }
1340
1341 if (!(si->flags & SI_FL_EXP))
1342 return; /* still in turn-around */
1343
Olivier Houchard86dcad62019-04-09 19:17:41 +02001344 si->flags &= ~SI_FL_EXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001345 si->exp = TICK_ETERNITY;
1346
Willy Tarreau87b09662015-04-03 00:22:06 +02001347 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001348 * marked "assigned".
1349 * FIXME: Should we force a redispatch attempt when the server is down ?
1350 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001351 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001352 si->state = SI_ST_ASS;
1353 else
1354 si->state = SI_ST_REQ;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001355
1356 DBG_TRACE_STATE("retry connection now", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001357 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001358
1359 end:
1360 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001361 return;
1362
1363abort_connection:
1364 /* give up */
1365 si->exp = TICK_ETERNITY;
Willy Tarreaud66ed882019-06-05 18:02:04 +02001366 si->flags &= ~SI_FL_EXP;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001367 si_shutr(si);
1368 si_shutw(si);
1369 si->state = SI_ST_CLO;
1370 if (s->srv_error)
1371 s->srv_error(s, si);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001372 DBG_TRACE_DEVEL("leaving on error", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001373 return;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001374}
1375
Willy Tarreau87b09662015-04-03 00:22:06 +02001376/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +09001377 * also counts a failed request if the server state has not reached the request
1378 * stage.
1379 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001380static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +09001381{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001382 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001383 if (s->si[1].state == SI_ST_INI) {
1384 /* anything before REQ in fact */
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001385 _HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.failed_req, 1);
Willy Tarreau2c1068c2015-09-23 12:21:21 +02001386 if (strm_li(s) && strm_li(s)->counters)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01001387 _HA_ATOMIC_ADD(&strm_li(s)->counters->failed_req, 1);
Simon Hormandec5be42011-06-08 09:19:07 +09001388
Willy Tarreaue7dff022015-04-03 01:14:29 +02001389 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +09001390 }
1391 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001392 s->flags |= SF_FINST_Q;
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001393 else if (si_state_in(s->si[1].state, SI_SB_REQ|SI_SB_TAR|SI_SB_ASS|SI_SB_CON|SI_SB_CER|SI_SB_RDY))
Willy Tarreaue7dff022015-04-03 01:14:29 +02001394 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +09001395 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001396 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +09001397 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02001398 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +09001399 }
1400}
1401
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001402/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001403 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
1404 * a real connection to a server, indicating that a server has been assigned,
1405 * or SI_ST_EST for a successful connection to an applet. It may also return
1406 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001407 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001408static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001409{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001410 struct stream_interface *si = &s->si[1];
1411
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001412 if (si->state != SI_ST_REQ)
1413 return;
1414
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001415 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1416
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001417 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
1418 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001419 struct appctx *appctx = objt_appctx(si->end);
1420
1421 if (!appctx || appctx->applet != __objt_applet(s->target))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001422 appctx = si_register_handler(si, objt_applet(s->target));
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001423
1424 if (!appctx) {
1425 /* No more memory, let's immediately abort. Force the
1426 * error code to ignore the ERR_LOCAL which is not a
1427 * real error.
1428 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001429 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001430
1431 si_shutr(si);
1432 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001433 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001434 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001435 si->state = SI_ST_CLO;
1436 if (s->srv_error)
1437 s->srv_error(s, si);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001438 DBG_TRACE_STATE("failed to register applet", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
1439 goto end;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001440 }
1441
Willy Tarreauee8269e2017-11-23 17:34:29 +01001442 if (tv_iszero(&s->logs.tv_request))
1443 s->logs.tv_request = now;
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001444 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001445 si->state = SI_ST_EST;
1446 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001447 be_set_sess_last(s->be);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001448
1449 DBG_TRACE_STATE("applet registered", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001450 /* let sess_establish() finish the job */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001451 goto end;
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001452 }
1453
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001454 /* Try to assign a server */
1455 if (srv_redispatch_connect(s) != 0) {
1456 /* We did not get a server. Either we queued the
1457 * connection request, or we encountered an error.
1458 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001459 if (si->state == SI_ST_QUE) {
1460 DBG_TRACE_STATE("connection request queued", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1461 goto end;
1462 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001463
1464 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001465 si_shutr(si);
1466 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001467 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001468 if (!si->err_type)
1469 si->err_type = SI_ET_CONN_OTHER;
1470 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001471 if (s->srv_error)
1472 s->srv_error(s, si);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001473 DBG_TRACE_STATE("connection request failed", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
1474 goto end;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001475 }
1476
1477 /* The server is assigned */
1478 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1479 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001480 be_set_sess_last(s->be);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001481 DBG_TRACE_STATE("connection request assigned to a server", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1482
1483 end:
1484 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001485}
1486
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001487/* This function parses the use-service action ruleset. It executes
1488 * the associated ACL and set an applet as a stream or txn final node.
1489 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1490 * consistent state. It returns ACT_RET_STOP in succes case because
1491 * use-service must be a terminal action. Returns ACT_RET_YIELD
1492 * if the initialisation function require more data.
1493 */
1494enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1495 struct session *sess, struct stream *s, int flags)
1496
1497{
1498 struct appctx *appctx;
1499
1500 /* Initialises the applet if it is required. */
1501 if (flags & ACT_FLAG_FIRST) {
1502 /* Register applet. this function schedules the applet. */
1503 s->target = &rule->applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001504 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target))))
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001505 return ACT_RET_ERR;
1506
1507 /* Initialise the context. */
1508 appctx = si_appctx(&s->si[1]);
1509 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1510 appctx->rule = rule;
1511 }
1512 else
1513 appctx = si_appctx(&s->si[1]);
1514
1515 /* Stops the applet sheduling, in case of the init function miss
1516 * some data.
1517 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001518 si_stop_get(&s->si[1]);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001519
1520 /* Call initialisation. */
1521 if (rule->applet.init)
1522 switch (rule->applet.init(appctx, px, s)) {
1523 case 0: return ACT_RET_ERR;
1524 case 1: break;
1525 default: return ACT_RET_YIELD;
1526 }
1527
Christopher Faulet2571bc62019-03-01 11:44:26 +01001528 if (rule->from != ACT_F_HTTP_REQ) {
1529 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1530 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
1531
1532 /* The flag SF_ASSIGNED prevent from server assignment. */
1533 s->flags |= SF_ASSIGNED;
1534 }
1535
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001536 /* Now we can schedule the applet. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001537 si_cant_get(&s->si[1]);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001538 appctx_wakeup(appctx);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001539 return ACT_RET_STOP;
1540}
1541
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001542/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001543 * if appropriate. The default_backend rule is also considered, then the
1544 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001545 * It returns 1 if the processing can continue on next analysers, or zero if it
1546 * either needs more data or wants to immediately abort the request.
1547 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001548static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001549{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001550 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001551 struct session *sess = s->sess;
1552 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001553
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001554 req->analysers &= ~an_bit;
1555 req->analyse_exp = TICK_ETERNITY;
1556
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001557 DBG_TRACE_ENTER(STRM_EV_STRM_ANA, s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001558
1559 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001560 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001561 struct switching_rule *rule;
1562
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001563 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001564 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001565
Willy Tarreauf51658d2014-04-23 01:21:56 +02001566 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001567 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001568 ret = acl_pass(ret);
1569 if (rule->cond->pol == ACL_COND_UNLESS)
1570 ret = !ret;
1571 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001572
1573 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001574 /* If the backend name is dynamic, try to resolve the name.
1575 * If we can't resolve the name, or if any error occurs, break
1576 * the loop and fallback to the default backend.
1577 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001578 struct proxy *backend = NULL;
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001579
1580 if (rule->dynamic) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001581 struct buffer *tmp;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001582
1583 tmp = alloc_trash_chunk();
1584 if (!tmp)
1585 goto sw_failed;
1586
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001587 if (build_logline(s, tmp->area, tmp->size, &rule->be.expr))
1588 backend = proxy_be_by_name(tmp->area);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001589
1590 free_trash_chunk(tmp);
1591 tmp = NULL;
1592
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001593 if (!backend)
1594 break;
1595 }
1596 else
1597 backend = rule->be.backend;
1598
Willy Tarreau87b09662015-04-03 00:22:06 +02001599 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001600 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001601 break;
1602 }
1603 }
1604
1605 /* To ensure correct connection accounting on the backend, we
1606 * have to assign one if it was not set (eg: a listen). This
1607 * measure also takes care of correctly setting the default
1608 * backend if any.
1609 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001610 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001611 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001612 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001613 }
1614
Willy Tarreaufb356202010-08-03 14:02:05 +02001615 /* 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 +02001616 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001617 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1618 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001619 s->req.analysers &= ~AN_REQ_FLT_START_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001620 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001621
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001622 /* 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 +02001623 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001624 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001625 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001626 int ret = 1;
1627
1628 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001629 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 +01001630 ret = acl_pass(ret);
1631 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1632 ret = !ret;
1633 }
1634
1635 if (ret) {
1636 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001637 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001638 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001639 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001640 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001641 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001642 break;
1643 }
1644 }
1645
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001646 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001647 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001648
1649 sw_failed:
1650 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001651 channel_abort(&s->req);
1652 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001653
Willy Tarreaue7dff022015-04-03 01:14:29 +02001654 if (!(s->flags & SF_ERR_MASK))
1655 s->flags |= SF_ERR_RESOURCE;
1656 if (!(s->flags & SF_FINST_MASK))
1657 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001658
Willy Tarreaueee5b512015-04-03 23:46:31 +02001659 if (s->txn)
1660 s->txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001661 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001662 s->req.analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001663 DBG_TRACE_DEVEL("leaving on error", STRM_EV_STRM_ANA|STRM_EV_STRM_ERR, s);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001664 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001665}
1666
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001667/* This stream analyser works on a request. It applies all use-server rules on
1668 * it then returns 1. The data must already be present in the buffer otherwise
1669 * they won't match. It always returns 1.
1670 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001671static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001672{
1673 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001674 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001675 struct server_rule *rule;
1676
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001677 DBG_TRACE_ENTER(STRM_EV_STRM_ANA, s);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001678
Willy Tarreaue7dff022015-04-03 01:14:29 +02001679 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001680 list_for_each_entry(rule, &px->server_rules, list) {
1681 int ret;
1682
Willy Tarreau192252e2015-04-04 01:47:55 +02001683 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001684 ret = acl_pass(ret);
1685 if (rule->cond->pol == ACL_COND_UNLESS)
1686 ret = !ret;
1687
1688 if (ret) {
1689 struct server *srv = rule->srv.ptr;
1690
Emeric Brun52a91d32017-08-31 14:41:55 +02001691 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001692 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001693 (s->flags & SF_FORCE_PRST)) {
1694 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001695 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001696 break;
1697 }
1698 /* if the server is not UP, let's go on with next rules
1699 * just in case another one is suited.
1700 */
1701 }
1702 }
1703 }
1704
1705 req->analysers &= ~an_bit;
1706 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001707 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001708 return 1;
1709}
1710
Frédéric Lécaille03cdf552019-05-20 10:08:27 +02001711static inline void sticking_rule_find_target(struct stream *s,
1712 struct stktable *t, struct stksess *ts)
1713{
1714 struct proxy *px = s->be;
1715 struct eb32_node *node;
1716 struct dict_entry *de;
1717 void *ptr;
1718 struct server *srv;
1719
1720 /* Look for the server name previously stored in <t> stick-table */
1721 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
1722 ptr = __stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_NAME);
1723 de = stktable_data_cast(ptr, server_name);
1724 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
1725
1726 if (de) {
1727 struct ebpt_node *name;
1728
1729 name = ebis_lookup(&px->conf.used_server_name, de->value.key);
1730 if (name) {
1731 srv = container_of(name, struct server, conf.name);
1732 goto found;
1733 }
1734 }
1735
1736 /* Look for the server ID */
1737 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
1738 ptr = __stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
1739 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
1740 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
1741
1742 if (!node)
1743 return;
1744
1745 srv = container_of(node, struct server, conf.id);
1746 found:
1747 if ((srv->cur_state != SRV_ST_STOPPED) ||
1748 (px->options & PR_O_PERSIST) || (s->flags & SF_FORCE_PRST)) {
1749 s->flags |= SF_DIRECT | SF_ASSIGNED;
1750 s->target = &srv->obj_type;
1751 }
1752}
1753
Emeric Brun1d33b292010-01-04 15:47:17 +01001754/* This stream analyser works on a request. It applies all sticking rules on
1755 * it then returns 1. The data must already be present in the buffer otherwise
1756 * they won't match. It always returns 1.
1757 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001758static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001759{
1760 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001761 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001762 struct sticking_rule *rule;
1763
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001764 DBG_TRACE_ENTER(STRM_EV_STRM_ANA, s);
Emeric Brun1d33b292010-01-04 15:47:17 +01001765
1766 list_for_each_entry(rule, &px->sticking_rules, list) {
1767 int ret = 1 ;
1768 int i;
1769
Willy Tarreau9667a802013-12-09 12:52:13 +01001770 /* Only the first stick store-request of each table is applied
1771 * and other ones are ignored. The purpose is to allow complex
1772 * configurations which look for multiple entries by decreasing
1773 * order of precision and to stop at the first which matches.
1774 * An example could be a store of the IP address from an HTTP
1775 * header first, then from the source if not found.
1776 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001777 for (i = 0; i < s->store_count; i++) {
1778 if (rule->table.t == s->store[i].table)
1779 break;
1780 }
1781
1782 if (i != s->store_count)
1783 continue;
1784
1785 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001786 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001787 ret = acl_pass(ret);
1788 if (rule->cond->pol == ACL_COND_UNLESS)
1789 ret = !ret;
1790 }
1791
1792 if (ret) {
1793 struct stktable_key *key;
1794
Willy Tarreau192252e2015-04-04 01:47:55 +02001795 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 +01001796 if (!key)
1797 continue;
1798
1799 if (rule->flags & STK_IS_MATCH) {
1800 struct stksess *ts;
1801
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001802 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Frédéric Lécaille03cdf552019-05-20 10:08:27 +02001803 if (!(s->flags & SF_ASSIGNED))
1804 sticking_rule_find_target(s, rule->table.t, ts);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001805 stktable_touch_local(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001806 }
1807 }
1808 if (rule->flags & STK_IS_STORE) {
1809 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1810 struct stksess *ts;
1811
1812 ts = stksess_new(rule->table.t, key);
1813 if (ts) {
1814 s->store[s->store_count].table = rule->table.t;
1815 s->store[s->store_count++].ts = ts;
1816 }
1817 }
1818 }
1819 }
1820 }
1821
1822 req->analysers &= ~an_bit;
1823 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001824 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
Emeric Brun1d33b292010-01-04 15:47:17 +01001825 return 1;
1826}
1827
1828/* This stream analyser works on a response. It applies all store rules on it
1829 * then returns 1. The data must already be present in the buffer otherwise
1830 * they won't match. It always returns 1.
1831 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001832static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001833{
1834 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001835 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001836 struct sticking_rule *rule;
1837 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001838 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001839
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001840 DBG_TRACE_ENTER(STRM_EV_STRM_ANA, s);
Emeric Brun1d33b292010-01-04 15:47:17 +01001841
1842 list_for_each_entry(rule, &px->storersp_rules, list) {
1843 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001844
Willy Tarreau9667a802013-12-09 12:52:13 +01001845 /* Only the first stick store-response of each table is applied
1846 * and other ones are ignored. The purpose is to allow complex
1847 * configurations which look for multiple entries by decreasing
1848 * order of precision and to stop at the first which matches.
1849 * An example could be a store of a set-cookie value, with a
1850 * fallback to a parameter found in a 302 redirect.
1851 *
1852 * The store-response rules are not allowed to override the
1853 * store-request rules for the same table, but they may coexist.
1854 * Thus we can have up to one store-request entry and one store-
1855 * response entry for the same table at any time.
1856 */
1857 for (i = nbreq; i < s->store_count; i++) {
1858 if (rule->table.t == s->store[i].table)
1859 break;
1860 }
1861
1862 /* skip existing entries for this table */
1863 if (i < s->store_count)
1864 continue;
1865
Emeric Brun1d33b292010-01-04 15:47:17 +01001866 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001867 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001868 ret = acl_pass(ret);
1869 if (rule->cond->pol == ACL_COND_UNLESS)
1870 ret = !ret;
1871 }
1872
1873 if (ret) {
1874 struct stktable_key *key;
1875
Willy Tarreau192252e2015-04-04 01:47:55 +02001876 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 +01001877 if (!key)
1878 continue;
1879
Willy Tarreau37e340c2013-12-06 23:05:21 +01001880 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001881 struct stksess *ts;
1882
1883 ts = stksess_new(rule->table.t, key);
1884 if (ts) {
1885 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001886 s->store[s->store_count++].ts = ts;
1887 }
1888 }
1889 }
1890 }
1891
1892 /* process store request and store response */
1893 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001894 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001895 void *ptr;
Frédéric Lécaille03cdf552019-05-20 10:08:27 +02001896 struct dict_entry *de;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001897
Willy Tarreauc93cd162014-05-13 15:54:22 +02001898 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001899 stksess_free(s->store[i].table, s->store[i].ts);
1900 s->store[i].ts = NULL;
1901 continue;
1902 }
1903
Emeric Brun819fc6f2017-06-13 19:37:32 +02001904 ts = stktable_set_entry(s->store[i].table, s->store[i].ts);
1905 if (ts != s->store[i].ts) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001906 /* the entry already existed, we can free ours */
Emeric Brun1d33b292010-01-04 15:47:17 +01001907 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001908 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001909 s->store[i].ts = NULL;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001910
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001911 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreaub8d42d02018-09-20 11:11:15 +02001912 ptr = __stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1913 stktable_data_cast(ptr, server_id) = __objt_server(s->target)->puid;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001914 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille03cdf552019-05-20 10:08:27 +02001915
1916 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1917 de = dict_insert(&server_name_dict, __objt_server(s->target)->id);
1918 if (de) {
1919 ptr = __stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_NAME);
1920 stktable_data_cast(ptr, server_name) = de;
1921 }
1922 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1923
Emeric Brun819fc6f2017-06-13 19:37:32 +02001924 stktable_touch_local(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001925 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001926 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001927
1928 rep->analysers &= ~an_bit;
1929 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001930
1931 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
Emeric Brun1d33b292010-01-04 15:47:17 +01001932 return 1;
1933}
1934
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001935/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001936 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001937 */
1938#define UPDATE_ANALYSERS(real, list, back, flag) { \
1939 list = (((list) & ~(flag)) | ~(back)) & (real); \
1940 back = real; \
1941 if (!(list)) \
1942 break; \
1943 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1944 continue; \
1945}
1946
Christopher Fauleta9215b72016-05-11 17:06:28 +02001947/* These 2 following macros call an analayzer for the specified channel if the
1948 * right flag is set. The first one is used for "filterable" analyzers. If a
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001949 * stream has some registered filters, pre and post analyaze callbacks are
Christopher Faulet0184ea72017-01-05 14:06:34 +01001950 * called. The second are used for other analyzers (AN_REQ/RES_FLT_* and
Christopher Fauleta9215b72016-05-11 17:06:28 +02001951 * AN_REQ/RES_HTTP_XFER_BODY) */
1952#define FLT_ANALYZE(strm, chn, fun, list, back, flag, ...) \
1953 { \
1954 if ((list) & (flag)) { \
1955 if (HAS_FILTERS(strm)) { \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001956 if (!flt_pre_analyze((strm), (chn), (flag))) \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001957 break; \
1958 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1959 break; \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001960 if (!flt_post_analyze((strm), (chn), (flag))) \
1961 break; \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001962 } \
1963 else { \
1964 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1965 break; \
1966 } \
1967 UPDATE_ANALYSERS((chn)->analysers, (list), \
1968 (back), (flag)); \
1969 } \
1970 }
1971
1972#define ANALYZE(strm, chn, fun, list, back, flag, ...) \
1973 { \
1974 if ((list) & (flag)) { \
1975 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1976 break; \
1977 UPDATE_ANALYSERS((chn)->analysers, (list), \
1978 (back), (flag)); \
1979 } \
1980 }
1981
Willy Tarreau87b09662015-04-03 00:22:06 +02001982/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001983 * then puts it back to the wait queue in a clean state, or cleans up its
1984 * resources if it must be deleted. Returns in <next> the date the task wants
1985 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1986 * nothing too many times, the request and response buffers flags are monitored
1987 * and each function is called only if at least another function has changed at
1988 * least one flag it is interested in.
1989 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001990struct task *process_stream(struct task *t, void *context, unsigned short state)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001991{
Willy Tarreau827aee92011-03-10 16:55:02 +01001992 struct server *srv;
Olivier Houchard9f6af332018-05-25 14:04:04 +02001993 struct stream *s = context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001994 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001995 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001996 unsigned int rq_prod_last, rq_cons_last;
1997 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001998 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001999 struct channel *req, *res;
2000 struct stream_interface *si_f, *si_b;
Willy Tarreau3d07a162019-04-25 19:15:20 +02002001 unsigned int rate;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002002
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002003 DBG_TRACE_ENTER(STRM_EV_STRM_PROC, s);
2004
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01002005 activity[tid].stream++;
2006
Willy Tarreau8f128b42014-11-28 15:07:47 +01002007 req = &s->req;
2008 res = &s->res;
2009
2010 si_f = &s->si[0];
2011 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002012
Willy Tarreau0f8d3ab2018-10-25 10:42:39 +02002013 /* First, attempt to receive pending data from I/O layers */
Willy Tarreauade64782018-11-17 19:51:07 +01002014 si_sync_recv(si_f);
2015 si_sync_recv(si_b);
Willy Tarreau0f8d3ab2018-10-25 10:42:39 +02002016
Willy Tarreau3d07a162019-04-25 19:15:20 +02002017 rate = update_freq_ctr(&s->call_rate, 1);
2018 if (rate >= 100000 && s->call_rate.prev_ctr) { // make sure to wait at least a full second
2019 stream_dump_and_crash(&s->obj_type, read_freq_ctr(&s->call_rate));
2020 }
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002021
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002022 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002023 if (s->txn)
2024 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002025
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002026 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002027 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
2028 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02002029
2030 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002031 rqf_last = req->flags & ~CF_MASK_ANALYSER;
2032 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02002033
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002034 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002035 si_f->flags |= SI_FL_DONT_WAKE;
2036 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002037
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002038 /* update pending events */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002039 s->pending_events |= (state & TASK_WOKEN_ANY);
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002040
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002041 /* 1a: Check for low level timeouts if needed. We just set a flag on
2042 * stream interfaces when their timeouts have expired.
2043 */
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002044 if (unlikely(s->pending_events & TASK_WOKEN_TIMER)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01002045 si_check_timeouts(si_f);
2046 si_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02002047
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002048 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02002049 * for future reads or writes. Note: this will also concern upper layers
2050 * but we do not touch any other flag. We must be careful and correctly
2051 * detect state changes when calling them.
2052 */
2053
Willy Tarreau8f128b42014-11-28 15:07:47 +01002054 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02002055
Willy Tarreau8f128b42014-11-28 15:07:47 +01002056 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
2057 si_b->flags |= SI_FL_NOLINGER;
2058 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01002059 }
2060
Willy Tarreau8f128b42014-11-28 15:07:47 +01002061 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
2062 if (si_f->flags & SI_FL_NOHALF)
2063 si_f->flags |= SI_FL_NOLINGER;
2064 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002065 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02002066
Willy Tarreau8f128b42014-11-28 15:07:47 +01002067 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002068
Willy Tarreau8f128b42014-11-28 15:07:47 +01002069 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
2070 si_f->flags |= SI_FL_NOLINGER;
2071 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01002072 }
2073
Willy Tarreau8f128b42014-11-28 15:07:47 +01002074 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
2075 if (si_b->flags & SI_FL_NOHALF)
2076 si_b->flags |= SI_FL_NOLINGER;
2077 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002078 }
Willy Tarreau798f4322012-11-08 14:49:17 +01002079
Christopher Fauleta00d8172016-11-10 14:58:05 +01002080 if (HAS_FILTERS(s))
2081 flt_stream_check_timeouts(s);
2082
Willy Tarreau798f4322012-11-08 14:49:17 +01002083 /* Once in a while we're woken up because the task expires. But
2084 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02002085 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01002086 * timeout needs to be refreshed.
2087 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002088 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01002089 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
Willy Tarreauede3d882018-10-24 17:17:56 +02002090 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002091 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002092 ((s->pending_events & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER)) {
Willy Tarreau5fb04712016-05-04 10:18:37 +02002093 si_f->flags &= ~SI_FL_DONT_WAKE;
2094 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau798f4322012-11-08 14:49:17 +01002095 goto update_exp_and_leave;
Willy Tarreau5fb04712016-05-04 10:18:37 +02002096 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02002097 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002098
Willy Tarreau829bd472019-06-06 09:17:23 +02002099 resync_stream_interface:
Willy Tarreau10fc09e2014-11-25 19:46:36 +01002100 /* below we may emit error messages so we have to ensure that we have
2101 * our buffers properly allocated.
2102 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002103 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01002104 /* No buffer available, we've been subscribed to the list of
2105 * buffer waiters, let's wait for our turn.
2106 */
Willy Tarreau5fb04712016-05-04 10:18:37 +02002107 si_f->flags &= ~SI_FL_DONT_WAKE;
2108 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau10fc09e2014-11-25 19:46:36 +01002109 goto update_exp_and_leave;
2110 }
2111
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002112 /* 1b: check for low-level errors reported at the stream interface.
2113 * First we check if it's a retryable error (in which case we don't
2114 * want to tell the buffer). Otherwise we report the error one level
2115 * upper by setting flags into the buffers. Note that the side towards
2116 * the client cannot have connect (hence retryable) errors. Also, the
2117 * connection setup code must be able to deal with any type of abort.
2118 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002119 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002120 if (unlikely(si_f->flags & SI_FL_ERR)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02002121 if (si_state_in(si_f->state, SI_SB_EST|SI_SB_DIS)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002122 si_shutr(si_f);
2123 si_shutw(si_f);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01002124 si_report_error(si_f);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002125 if (!(req->analysers) && !(res->analysers)) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002126 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2127 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002128 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002129 _HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002130 if (!(s->flags & SF_ERR_MASK))
2131 s->flags |= SF_ERR_CLICL;
2132 if (!(s->flags & SF_FINST_MASK))
2133 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01002134 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002135 }
2136 }
2137
Willy Tarreau8f128b42014-11-28 15:07:47 +01002138 if (unlikely(si_b->flags & SI_FL_ERR)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02002139 if (si_state_in(si_b->state, SI_SB_EST|SI_SB_DIS)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002140 si_shutr(si_b);
2141 si_shutw(si_b);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01002142 si_report_error(si_b);
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002143 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002144 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002145 _HA_ATOMIC_ADD(&srv->counters.failed_resp, 1);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002146 if (!(req->analysers) && !(res->analysers)) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002147 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2148 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002149 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002150 _HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002151 if (!(s->flags & SF_ERR_MASK))
2152 s->flags |= SF_ERR_SRVCL;
2153 if (!(s->flags & SF_FINST_MASK))
2154 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01002155 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002156 }
2157 /* note: maybe we should process connection errors here ? */
2158 }
2159
Willy Tarreau4f283fa2019-06-05 14:34:03 +02002160 if (si_state_in(si_b->state, SI_SB_CON|SI_SB_RDY)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002161 /* we were trying to establish a connection on the server side,
2162 * maybe it succeeded, maybe it failed, maybe we timed out, ...
2163 */
Willy Tarreaud66ed882019-06-05 18:02:04 +02002164 if (si_b->state == SI_ST_RDY)
2165 sess_update_st_rdy_tcp(s);
2166 else if (si_b->state == SI_ST_CON)
2167 sess_update_st_con_tcp(s);
2168
2169 if (si_b->state == SI_ST_CER)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002170 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002171 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002172 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002173
2174 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
2175 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
2176 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
2177 */
2178 }
2179
Willy Tarreau8f128b42014-11-28 15:07:47 +01002180 rq_prod_last = si_f->state;
2181 rq_cons_last = si_b->state;
2182 rp_cons_last = si_f->state;
2183 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002184
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002185 /* Check for connection closure */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002186 DBG_TRACE_POINT(STRM_EV_STRM_PROC, s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002187
2188 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002189 if (unlikely(si_f->state == SI_ST_DIS))
2190 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002191
2192 /* When a server-side connection is released, we have to count it and
2193 * check for pending connections on this server.
2194 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002195 if (unlikely(si_b->state == SI_ST_DIS)) {
2196 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002197 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01002198 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002199 if (s->flags & SF_CURR_SESS) {
2200 s->flags &= ~SF_CURR_SESS;
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002201 _HA_ATOMIC_SUB(&srv->cur_sess, 1);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002202 }
2203 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01002204 if (may_dequeue_tasks(srv, s->be))
2205 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002206 }
2207 }
2208
2209 /*
2210 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
2211 * at this point.
2212 */
2213
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002214 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002215 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002216 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
2217 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
Willy Tarreau33982cb2017-11-20 15:37:13 +01002218 (req->analysers && (req->flags & CF_SHUTW)) ||
Willy Tarreau8f128b42014-11-28 15:07:47 +01002219 si_f->state != rq_prod_last ||
2220 si_b->state != rq_cons_last ||
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002221 s->pending_events & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002222 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002223
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02002224 if (si_state_in(si_f->state, SI_SB_EST|SI_SB_DIS|SI_SB_CLO)) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01002225 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002226 unsigned int ana_list;
2227 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002228
Willy Tarreau90deb182010-01-07 00:20:41 +01002229 /* it's up to the analysers to stop new connections,
2230 * disable reading or closing. Note: if an analyser
2231 * disables any of these bits, it is responsible for
2232 * enabling them again when it disables itself, so
2233 * that other analysers are called in similar conditions.
2234 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002235 channel_auto_read(req);
2236 channel_auto_connect(req);
2237 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002238
2239 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01002240 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01002241 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002242 * the list when not needed. Any analyser may return 0
2243 * to break out of the loop, either because of missing
2244 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02002245 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002246 * analyser, and we may loop again if other analysers
2247 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002248 *
2249 * We build a list of analysers to run. We evaluate all
2250 * of these analysers in the order of the lower bit to
2251 * the higher bit. This ordering is very important.
2252 * An analyser will often add/remove other analysers,
2253 * including itself. Any changes to itself have no effect
2254 * on the loop. If it removes any other analysers, we
2255 * want those analysers not to be called anymore during
2256 * this loop. If it adds an analyser that is located
2257 * after itself, we want it to be scheduled for being
2258 * processed during the loop. If it adds an analyser
2259 * which is located before it, we want it to switch to
2260 * it immediately, even if it has already been called
2261 * once but removed since.
2262 *
2263 * In order to achieve this, we compare the analyser
2264 * list after the call with a copy of it before the
2265 * call. The work list is fed with analyser bits that
2266 * appeared during the call. Then we compare previous
2267 * work list with the new one, and check the bits that
2268 * appeared. If the lowest of these bits is lower than
2269 * the current bit, it means we have enabled a previous
2270 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01002271 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002272
Willy Tarreau8f128b42014-11-28 15:07:47 +01002273 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01002274 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002275 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002276 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_REQ_FLT_START_FE);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002277 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_FE);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002278 FLT_ANALYZE(s, req, http_wait_for_request, ana_list, ana_back, AN_REQ_WAIT_HTTP);
2279 FLT_ANALYZE(s, req, http_wait_for_request_body, ana_list, ana_back, AN_REQ_HTTP_BODY);
2280 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE, sess->fe);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002281 FLT_ANALYZE(s, req, process_switching_rules, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002282 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_REQ_FLT_START_BE);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002283 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_BE);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002284 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE, s->be);
2285 FLT_ANALYZE(s, req, http_process_tarpit, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002286 FLT_ANALYZE(s, req, process_server_rules, ana_list, ana_back, AN_REQ_SRV_RULES);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002287 FLT_ANALYZE(s, req, http_process_request, ana_list, ana_back, AN_REQ_HTTP_INNER);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002288 FLT_ANALYZE(s, req, tcp_persist_rdp_cookie, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
2289 FLT_ANALYZE(s, req, process_sticking_rules, ana_list, ana_back, AN_REQ_STICKING_RULES);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002290 ANALYZE (s, req, flt_analyze_http_headers, ana_list, ana_back, AN_REQ_FLT_HTTP_HDRS);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002291 ANALYZE (s, req, http_request_forward_body, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002292 ANALYZE (s, req, pcli_wait_for_request, ana_list, ana_back, AN_REQ_WAIT_CLI);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002293 ANALYZE (s, req, flt_xfer_data, ana_list, ana_back, AN_REQ_FLT_XFER_DATA);
2294 ANALYZE (s, req, flt_end_analyze, ana_list, ana_back, AN_REQ_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01002295 break;
2296 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002297 }
Willy Tarreau84455332009-03-15 22:34:05 +01002298
Willy Tarreau8f128b42014-11-28 15:07:47 +01002299 rq_prod_last = si_f->state;
2300 rq_cons_last = si_b->state;
2301 req->flags &= ~CF_WAKE_ONCE;
2302 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002303
Willy Tarreau1ec9bb52019-06-06 14:45:26 +02002304 if ((req->flags ^ flags) & (CF_SHUTR|CF_SHUTW))
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002305 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002306 }
2307
Willy Tarreau576507f2010-01-07 00:09:04 +01002308 /* we'll monitor the request analysers while parsing the response,
2309 * because some response analysers may indirectly enable new request
2310 * analysers (eg: HTTP keep-alive).
2311 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002312 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01002313
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002314 resync_response:
2315 /* Analyse response */
2316
Willy Tarreau8f128b42014-11-28 15:07:47 +01002317 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
2318 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
Willy Tarreau33982cb2017-11-20 15:37:13 +01002319 (res->analysers && (res->flags & CF_SHUTW)) ||
Willy Tarreau8f128b42014-11-28 15:07:47 +01002320 si_f->state != rp_cons_last ||
2321 si_b->state != rp_prod_last ||
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002322 s->pending_events & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002323 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002324
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02002325 if (si_state_in(si_b->state, SI_SB_EST|SI_SB_DIS|SI_SB_CLO)) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01002326 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002327 unsigned int ana_list;
2328 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002329
Willy Tarreau90deb182010-01-07 00:20:41 +01002330 /* it's up to the analysers to stop disable reading or
2331 * closing. Note: if an analyser disables any of these
2332 * bits, it is responsible for enabling them again when
2333 * it disables itself, so that other analysers are called
2334 * in similar conditions.
2335 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002336 channel_auto_read(res);
2337 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002338
2339 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01002340 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002341 * to MSB. The analysers must remove themselves from
2342 * the list when not needed. Any analyser may return 0
2343 * to break out of the loop, either because of missing
2344 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02002345 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002346 * analyser, and we may loop again if other analysers
2347 * are added in the middle.
2348 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002349
Willy Tarreau8f128b42014-11-28 15:07:47 +01002350 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01002351 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002352 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002353 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_RES_FLT_START_FE);
2354 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_RES_FLT_START_BE);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002355 FLT_ANALYZE(s, res, tcp_inspect_response, ana_list, ana_back, AN_RES_INSPECT);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002356 FLT_ANALYZE(s, res, http_wait_for_response, ana_list, ana_back, AN_RES_WAIT_HTTP);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002357 FLT_ANALYZE(s, res, process_store_rules, ana_list, ana_back, AN_RES_STORE_RULES);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002358 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 +01002359 ANALYZE (s, res, flt_analyze_http_headers, ana_list, ana_back, AN_RES_FLT_HTTP_HDRS);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002360 ANALYZE (s, res, http_response_forward_body, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002361 ANALYZE (s, res, pcli_wait_for_response, ana_list, ana_back, AN_RES_WAIT_CLI);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002362 ANALYZE (s, res, flt_xfer_data, ana_list, ana_back, AN_RES_FLT_XFER_DATA);
2363 ANALYZE (s, res, flt_end_analyze, ana_list, ana_back, AN_RES_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01002364 break;
2365 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002366 }
2367
Willy Tarreau8f128b42014-11-28 15:07:47 +01002368 rp_cons_last = si_f->state;
2369 rp_prod_last = si_b->state;
Christopher Fauletcdaea892017-07-06 15:49:30 +02002370 res->flags &= ~CF_WAKE_ONCE;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002371 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002372
Willy Tarreau1ec9bb52019-06-06 14:45:26 +02002373 if ((res->flags ^ flags) & (CF_SHUTR|CF_SHUTW))
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002374 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002375 }
2376
Willy Tarreau576507f2010-01-07 00:09:04 +01002377 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002378 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01002379 goto resync_request;
2380
Willy Tarreau8f128b42014-11-28 15:07:47 +01002381 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01002382 goto resync_request;
2383
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002384 /* FIXME: here we should call protocol handlers which rely on
2385 * both buffers.
2386 */
2387
2388
2389 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02002390 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01002391 * we're just in a data phase here since it means we have not
2392 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002393 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002394 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002395 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002396 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002397 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002398 req->analysers = 0;
2399 if (req->flags & CF_READ_ERROR) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002400 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2401 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002402 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002403 _HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002404 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002405 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002406 else if (req->flags & CF_READ_TIMEOUT) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002407 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2408 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002409 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002410 _HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002411 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002412 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002413 else if (req->flags & CF_WRITE_ERROR) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002414 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2415 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002416 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002417 _HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002418 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002419 }
2420 else {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002421 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2422 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002423 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002424 _HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002425 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002426 }
Willy Tarreau84455332009-03-15 22:34:05 +01002427 sess_set_term_flags(s);
Christopher Faulet5e1a9d72019-04-23 17:34:22 +02002428
2429 /* Abort the request if a client error occurred while
2430 * the backend stream-interface is in the SI_ST_INI
2431 * state. It is switched into the SI_ST_CLO state and
2432 * the request channel is erased. */
2433 if (si_b->state == SI_ST_INI) {
2434 si_b->state = SI_ST_CLO;
2435 channel_abort(req);
2436 if (IS_HTX_STRM(s))
2437 channel_htx_erase(req, htxbuf(&req->buf));
2438 else
2439 channel_erase(req);
2440 }
Willy Tarreau84455332009-03-15 22:34:05 +01002441 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002442 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002443 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002444 res->analysers = 0;
2445 if (res->flags & CF_READ_ERROR) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002446 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2447 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002448 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002449 _HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002450 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002451 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002452 else if (res->flags & CF_READ_TIMEOUT) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002453 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2454 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002455 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002456 _HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002457 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002458 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002459 else if (res->flags & CF_WRITE_ERROR) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002460 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2461 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002462 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002463 _HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002464 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002465 }
2466 else {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002467 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2468 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002469 if (srv)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002470 _HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002471 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002472 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002473 sess_set_term_flags(s);
2474 }
Willy Tarreau84455332009-03-15 22:34:05 +01002475 }
2476
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002477 /*
2478 * Here we take care of forwarding unhandled data. This also includes
2479 * connection establishments and shutdown requests.
2480 */
2481
2482
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002483 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002484 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002485 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002486 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002487 */
Christopher Faulet35fe6992017-08-29 16:06:38 +02002488 if (unlikely((!req->analysers || (req->analysers == AN_REQ_FLT_END && !(req->flags & CF_FLT_ANALYZE))) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002489 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02002490 (si_state_in(si_f->state, SI_SB_EST|SI_SB_DIS|SI_SB_CLO)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002491 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002492 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002493 * attached to it. If any data are left in, we'll permit them to
2494 * move.
2495 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002496 channel_auto_read(req);
2497 channel_auto_connect(req);
2498 channel_auto_close(req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002499
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002500 if (IS_HTX_STRM(s)) {
2501 struct htx *htx = htxbuf(&req->buf);
2502
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002503 /* We'll let data flow between the producer (if still connected)
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002504 * to the consumer.
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002505 */
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002506 co_set_data(req, htx->data);
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002507 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002508 channel_htx_forward_forever(req, htx);
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002509 }
2510 else {
2511 /* We'll let data flow between the producer (if still connected)
2512 * to the consumer (which might possibly not be connected yet).
2513 */
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002514 c_adv(req, ci_data(req));
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002515 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2516 channel_forward_forever(req);
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002517 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002518 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002519
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002520 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002521 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2522 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002523 (global.tune.options & GTUNE_USE_SPLICE) &&
Christopher Faulet276c1e02019-11-05 16:49:23 +01002524 (objt_cs(si_f->end) && __objt_cs(si_f->end)->conn->xprt && __objt_cs(si_f->end)->conn->xprt->rcv_pipe &&
2525 __objt_cs(si_f->end)->conn->mux && __objt_cs(si_f->end)->conn->mux->rcv_pipe) &&
2526 (objt_cs(si_b->end) && __objt_cs(si_b->end)->conn->xprt && __objt_cs(si_b->end)->conn->xprt->snd_pipe &&
2527 __objt_cs(si_b->end)->conn->mux && __objt_cs(si_b->end)->conn->mux->snd_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002528 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002529 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2530 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002531 (req->flags & CF_STREAMER_FAST)))) {
2532 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002533 }
2534
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002535 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002536 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002537
Willy Tarreau520d95e2009-09-19 21:04:57 +02002538 /* it's possible that an upper layer has requested a connection setup or abort.
2539 * There are 2 situations where we decide to establish a new connection :
2540 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002541 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002542 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002543 if (si_b->state == SI_ST_INI) {
Christopher Faulet5e1a9d72019-04-23 17:34:22 +02002544 if (!(req->flags & CF_SHUTW)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002545 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002546 /* If we have an appctx, there is no connect method, so we
2547 * immediately switch to the connected state, otherwise we
2548 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002549 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002550 si_b->state = SI_ST_REQ; /* new connection requested */
2551 si_b->conn_retries = s->be->conn_retries;
Olivier Houchard602bf7d2019-05-10 13:59:15 +02002552 if ((s->be->retry_type &~ PR_RE_CONN_FAILED) &&
2553 !(si_b->flags & SI_FL_D_L7_RETRY))
Olivier Houcharda254a372019-04-05 15:30:12 +02002554 si_b->flags |= SI_FL_L7_RETRY;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002555 }
Willy Tarreau73201222009-08-16 18:27:24 +02002556 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002557 else {
Willy Tarreau0542c8b2017-11-24 15:01:10 +01002558 si_release_endpoint(si_b);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002559 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2560 channel_shutw_now(req); /* fix buffer flags upon abort */
2561 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002562 }
Willy Tarreau92795622009-03-06 12:51:23 +01002563 }
2564
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002565
2566 /* we may have a pending connection request, or a connection waiting
2567 * for completion.
2568 */
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02002569 if (si_state_in(si_b->state, SI_SB_REQ|SI_SB_QUE|SI_SB_TAR|SI_SB_ASS)) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002570 /* prune the request variables and swap to the response variables. */
2571 if (s->vars_reqres.scope != SCOPE_RES) {
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01002572 if (!LIST_ISEMPTY(&s->vars_reqres.head)) {
2573 vars_prune(&s->vars_reqres, s->sess, s);
2574 vars_init(&s->vars_reqres, SCOPE_RES);
2575 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002576 }
2577
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002578 do {
2579 /* nb: step 1 might switch from QUE to ASS, but we first want
2580 * to give a chance to step 2 to perform a redirect if needed.
2581 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002582 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002583 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002584 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002585 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002586
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002587 /* applets directly go to the ESTABLISHED state. Similarly,
2588 * servers experience the same fate when their connection
2589 * is reused.
2590 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002591 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002592 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002593
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002594 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002595 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002596 http_perform_server_redirect(s, si_b);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002597 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002598 }
2599
Willy Tarreau829bd472019-06-06 09:17:23 +02002600 /* Let's see if we can send the pending request now */
2601 si_sync_send(si_b);
2602
2603 /*
2604 * Now forward all shutdown requests between both sides of the request buffer
2605 */
2606
2607 /* first, let's check if the request buffer needs to shutdown(write), which may
2608 * happen either because the input is closed or because we want to force a close
2609 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2610 * the other side's timeout as well.
2611 */
2612 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
2613 (CF_AUTO_CLOSE|CF_SHUTR))) {
2614 channel_shutw_now(req);
2615 }
2616
2617 /* shutdown(write) pending */
2618 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2619 channel_is_empty(req))) {
2620 if (req->flags & CF_READ_ERROR)
2621 si_b->flags |= SI_FL_NOLINGER;
2622 si_shutw(si_b);
2623 }
2624
2625 /* shutdown(write) done on server side, we must stop the client too */
2626 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2627 !req->analysers))
2628 channel_shutr_now(req);
2629
2630 /* shutdown(read) pending */
2631 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2632 if (si_f->flags & SI_FL_NOHALF)
2633 si_f->flags |= SI_FL_NOLINGER;
2634 si_shutr(si_f);
2635 }
2636
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002637 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau829bd472019-06-06 09:17:23 +02002638 if (si_f->state == SI_ST_DIS ||
2639 si_state_in(si_b->state, SI_SB_RDY|SI_SB_DIS) ||
2640 (si_f->flags & SI_FL_ERR && si_f->state != SI_ST_CLO) ||
2641 (si_b->flags & SI_FL_ERR && si_b->state != SI_ST_CLO))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002642 goto resync_stream_interface;
2643
Willy Tarreau815a9b22010-07-27 17:15:12 +02002644 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau1ec9bb52019-06-06 14:45:26 +02002645 if ((req->flags ^ rqf_last) & (CF_SHUTR|CF_SHUTW))
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002646 goto resync_request;
2647
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002648 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002649
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002650 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002651 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002652 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002653 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002654 */
Christopher Faulet35fe6992017-08-29 16:06:38 +02002655 if (unlikely((!res->analysers || (res->analysers == AN_RES_FLT_END && !(res->flags & CF_FLT_ANALYZE))) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002656 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02002657 si_state_in(si_b->state, SI_SB_EST|SI_SB_DIS|SI_SB_CLO) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002658 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002659 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002660 * attached to it. If any data are left in, we'll permit them to
2661 * move.
2662 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002663 channel_auto_read(res);
2664 channel_auto_close(res);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002665
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002666 if (IS_HTX_STRM(s)) {
2667 struct htx *htx = htxbuf(&res->buf);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002668
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002669 /* We'll let data flow between the producer (if still connected)
2670 * to the consumer.
2671 */
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002672 co_set_data(res, htx->data);
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002673 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002674 channel_htx_forward_forever(res, htx);
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002675 }
2676 else {
2677 /* We'll let data flow between the producer (if still connected)
2678 * to the consumer.
2679 */
Christopher Faulet2dba1a52018-12-18 21:57:24 +01002680 c_adv(res, ci_data(res));
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002681 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2682 channel_forward_forever(res);
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002683 }
Willy Tarreau42529c32015-07-09 18:38:57 +02002684
Willy Tarreauce887fd2012-05-12 12:50:00 +02002685 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002686 * tunnel timeout set, use it now. Note that we must respect
2687 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002688 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002689 if (!req->analysers && s->be->timeout.tunnel) {
2690 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002691 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002692
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002693 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2694 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002695 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2696 res->rto = s->be->timeout.serverfin;
2697 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2698 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002699 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2700 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002701
Willy Tarreau8f128b42014-11-28 15:07:47 +01002702 req->rex = tick_add(now_ms, req->rto);
2703 req->wex = tick_add(now_ms, req->wto);
2704 res->rex = tick_add(now_ms, res->rto);
2705 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002706 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002707 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002708
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002709 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002710 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2711 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002712 (global.tune.options & GTUNE_USE_SPLICE) &&
Christopher Faulet276c1e02019-11-05 16:49:23 +01002713 (objt_cs(si_f->end) && __objt_cs(si_f->end)->conn->xprt && __objt_cs(si_f->end)->conn->xprt->snd_pipe &&
2714 __objt_cs(si_f->end)->conn->mux && __objt_cs(si_f->end)->conn->mux->snd_pipe) &&
2715 (objt_cs(si_b->end) && __objt_cs(si_b->end)->conn->xprt && __objt_cs(si_b->end)->conn->xprt->rcv_pipe &&
2716 __objt_cs(si_b->end)->conn->mux && __objt_cs(si_b->end)->conn->mux->rcv_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002717 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002718 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2719 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002720 (res->flags & CF_STREAMER_FAST)))) {
2721 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002722 }
2723
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002724 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002725 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002726
Willy Tarreau829bd472019-06-06 09:17:23 +02002727 /* Let's see if we can send the pending response now */
2728 si_sync_send(si_f);
2729
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002730 /*
2731 * Now forward all shutdown requests between both sides of the buffer
2732 */
2733
2734 /*
2735 * FIXME: this is probably where we should produce error responses.
2736 */
2737
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002738 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002739 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002740 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002741 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002742 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002743
2744 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002745 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2746 channel_is_empty(res))) {
2747 si_shutw(si_f);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002748 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002749
2750 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002751 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2752 !res->analysers)
2753 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002754
2755 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002756 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2757 if (si_b->flags & SI_FL_NOHALF)
2758 si_b->flags |= SI_FL_NOLINGER;
2759 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002760 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002761
Willy Tarreau829bd472019-06-06 09:17:23 +02002762 if (si_f->state == SI_ST_DIS ||
2763 si_state_in(si_b->state, SI_SB_RDY|SI_SB_DIS) ||
2764 (si_f->flags & SI_FL_ERR && si_f->state != SI_ST_CLO) ||
2765 (si_b->flags & SI_FL_ERR && si_b->state != SI_ST_CLO))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002766 goto resync_stream_interface;
2767
Willy Tarreau3c5c0662019-06-06 14:32:49 +02002768 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002769 goto resync_request;
2770
Willy Tarreau8f128b42014-11-28 15:07:47 +01002771 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002772 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002773
Willy Tarreau829bd472019-06-06 09:17:23 +02002774 if (((req->flags ^ rqf_last) | (res->flags ^ rpf_last)) & CF_MASK_ANALYSER)
2775 goto resync_request;
2776
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002777 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002778 si_f->flags &= ~SI_FL_DONT_WAKE;
2779 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002780
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002781 /* This is needed only when debugging is enabled, to indicate
2782 * client-side or server-side close. Please note that in the unlikely
2783 * event where both sides would close at once, the sequence is reported
2784 * on the server side first.
2785 */
2786 if (unlikely((global.mode & MODE_DEBUG) &&
2787 (!(global.mode & MODE_QUIET) ||
2788 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002789 if (si_b->state == SI_ST_CLO &&
2790 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002791 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002792 s->uniq_id, s->be->id,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002793 objt_cs(si_f->end) ? (unsigned short)objt_cs(si_f->end)->conn->handle.fd : -1,
2794 objt_cs(si_b->end) ? (unsigned short)objt_cs(si_b->end)->conn->handle.fd : -1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002795 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002796 }
2797
Willy Tarreau8f128b42014-11-28 15:07:47 +01002798 if (si_f->state == SI_ST_CLO &&
2799 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002800 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002801 s->uniq_id, s->be->id,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002802 objt_cs(si_f->end) ? (unsigned short)objt_cs(si_f->end)->conn->handle.fd : -1,
2803 objt_cs(si_b->end) ? (unsigned short)objt_cs(si_b->end)->conn->handle.fd : -1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002804 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002805 }
2806 }
2807
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02002808 if (likely((si_f->state != SI_ST_CLO) || !si_state_in(si_b->state, SI_SB_INI|SI_SB_CLO))) {
Olivier Houchard4c18f942019-07-31 18:05:26 +02002809 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED) && !(s->flags & SF_IGNORE))
Willy Tarreau87b09662015-04-03 00:22:06 +02002810 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002811
Willy Tarreaud14844a2018-11-08 18:15:29 +01002812 si_update_both(si_f, si_b);
Olivier Houchard53216e72018-10-10 15:46:36 +02002813
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002814 /* Trick: if a request is being waiting for the server to respond,
2815 * and if we know the server can timeout, we don't want the timeout
2816 * to expire on the client side first, but we're still interested
2817 * in passing data from the client to the server (eg: POST). Thus,
2818 * we can cancel the client's request timeout if the server's
2819 * request timeout is set and the server has not yet sent a response.
2820 */
2821
Willy Tarreau8f128b42014-11-28 15:07:47 +01002822 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2823 (tick_isset(req->wex) || tick_isset(res->rex))) {
2824 req->flags |= CF_READ_NOEXP;
2825 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002826 }
2827
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002828 /* Reset pending events now */
2829 s->pending_events = 0;
2830
Willy Tarreau798f4322012-11-08 14:49:17 +01002831 update_exp_and_leave:
Willy Tarreau5fb04712016-05-04 10:18:37 +02002832 /* Note: please ensure that if you branch here you disable SI_FL_DONT_WAKE */
Christopher Fauleta00d8172016-11-10 14:58:05 +01002833 t->expire = tick_first((tick_is_expired(t->expire, now_ms) ? 0 : t->expire),
2834 tick_first(tick_first(req->rex, req->wex),
2835 tick_first(res->rex, res->wex)));
Willy Tarreaudef0d222016-11-08 22:03:00 +01002836 if (!req->analysers)
2837 req->analyse_exp = TICK_ETERNITY;
2838
2839 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED) &&
2840 (!tick_isset(req->analyse_exp) || tick_is_expired(req->analyse_exp, now_ms)))
2841 req->analyse_exp = tick_add(now_ms, 5000);
2842
2843 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002844
Willy Tarreau9a398be2017-11-10 17:14:23 +01002845 t->expire = tick_first(t->expire, res->analyse_exp);
2846
Willy Tarreau8f128b42014-11-28 15:07:47 +01002847 if (si_f->exp)
2848 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002849
Willy Tarreau8f128b42014-11-28 15:07:47 +01002850 if (si_b->exp)
2851 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002852
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002853 s->pending_events &= ~(TASK_WOKEN_TIMER | TASK_WOKEN_RES);
Willy Tarreau87b09662015-04-03 00:22:06 +02002854 stream_release_buffers(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002855
2856 DBG_TRACE_DEVEL("queuing", STRM_EV_STRM_PROC, s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002857 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002858 }
2859
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002860 DBG_TRACE_DEVEL("releasing", STRM_EV_STRM_PROC, s);
2861
Willy Tarreaue7dff022015-04-03 01:14:29 +02002862 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002863 _HA_ATOMIC_SUB(&s->be->beconn, 1);
Willy Tarreau6f5e4b92017-09-15 09:07:56 +02002864
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002865 if (unlikely((global.mode & MODE_DEBUG) &&
2866 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002867 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002868 s->uniq_id, s->be->id,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002869 objt_cs(si_f->end) ? (unsigned short)objt_cs(si_f->end)->conn->handle.fd : -1,
2870 objt_cs(si_b->end) ? (unsigned short)objt_cs(si_b->end)->conn->handle.fd : -1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002871 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002872 }
2873
2874 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Olivier Houchard4c18f942019-07-31 18:05:26 +02002875 if (!(s->flags & SF_IGNORE))
2876 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002877
Willy Tarreaueee5b512015-04-03 23:46:31 +02002878 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002879 int n;
2880
Willy Tarreaueee5b512015-04-03 23:46:31 +02002881 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002882 if (n < 1 || n > 5)
2883 n = 0;
2884
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002885 if (sess->fe->mode == PR_MODE_HTTP) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002886 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002887 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002888 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002889 (s->be->mode == PR_MODE_HTTP)) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002890 _HA_ATOMIC_ADD(&s->be->be_counters.p.http.rsp[n], 1);
2891 _HA_ATOMIC_ADD(&s->be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002892 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002893 }
2894
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002895 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002896 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002897 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002898 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreau66425e32018-07-25 06:55:12 +02002899 /* we may need to know the position in the queue */
2900 pendconn_free(s);
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002901 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002902 }
2903
Willy Tarreau87b09662015-04-03 00:22:06 +02002904 /* update time stats for this stream */
2905 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002906
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002907 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002908 stream_free(s);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002909 task_destroy(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002910 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002911}
2912
Willy Tarreau87b09662015-04-03 00:22:06 +02002913/* Update the stream's backend and server time stats */
2914void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002915{
2916 int t_request;
2917 int t_queue;
2918 int t_connect;
2919 int t_data;
2920 int t_close;
2921 struct server *srv;
2922
2923 t_request = 0;
2924 t_queue = s->logs.t_queue;
2925 t_connect = s->logs.t_connect;
2926 t_close = s->logs.t_close;
2927 t_data = s->logs.t_data;
2928
2929 if (s->be->mode != PR_MODE_HTTP)
2930 t_data = t_connect;
2931
2932 if (t_connect < 0 || t_data < 0)
2933 return;
2934
2935 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2936 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2937
2938 t_data -= t_connect;
2939 t_connect -= t_queue;
2940 t_queue -= t_request;
2941
2942 srv = objt_server(s->target);
2943 if (srv) {
2944 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2945 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2946 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2947 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2948 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002949 HA_SPIN_LOCK(PROXY_LOCK, &s->be->lock);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002950 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2951 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2952 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2953 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002954 HA_SPIN_UNLOCK(PROXY_LOCK, &s->be->lock);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002955}
2956
Willy Tarreau7c669d72008-06-20 15:04:11 +02002957/*
2958 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002959 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002960 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002961 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002962 * server.
2963 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002964void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002965{
2966 if (sess->srv_conn == newsrv)
2967 return;
2968
2969 if (sess->srv_conn) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002970 _HA_ATOMIC_SUB(&sess->srv_conn->served, 1);
2971 _HA_ATOMIC_SUB(&sess->srv_conn->proxy->served, 1);
2972 __ha_barrier_atomic_store();
Willy Tarreau7c669d72008-06-20 15:04:11 +02002973 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2974 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002975 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002976 }
2977
2978 if (newsrv) {
Olivier Houcharddc6111e2019-03-08 18:54:51 +01002979 _HA_ATOMIC_ADD(&newsrv->served, 1);
2980 _HA_ATOMIC_ADD(&newsrv->proxy->served, 1);
2981 __ha_barrier_atomic_store();
Willy Tarreau7c669d72008-06-20 15:04:11 +02002982 if (newsrv->proxy->lbprm.server_take_conn)
2983 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002984 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002985 }
2986}
2987
Willy Tarreau84455332009-03-15 22:34:05 +01002988/* Handle server-side errors for default protocols. It is called whenever a a
2989 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002990 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002991 * them. It's installed as ->srv_error for the server-side stream_interface.
2992 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002993void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002994{
2995 int err_type = si->err_type;
2996 int err = 0, fin = 0;
2997
2998 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002999 err = SF_ERR_CLICL;
3000 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01003001 }
3002 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003003 err = SF_ERR_CLICL;
3004 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01003005 }
3006 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003007 err = SF_ERR_SRVTO;
3008 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01003009 }
3010 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003011 err = SF_ERR_SRVCL;
3012 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01003013 }
3014 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003015 err = SF_ERR_SRVTO;
3016 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01003017 }
3018 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003019 err = SF_ERR_SRVCL;
3020 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01003021 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02003022 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003023 err = SF_ERR_RESOURCE;
3024 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02003025 }
Willy Tarreau84455332009-03-15 22:34:05 +01003026 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003027 err = SF_ERR_INTERNAL;
3028 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01003029 }
3030
Willy Tarreaue7dff022015-04-03 01:14:29 +02003031 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01003032 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02003033 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01003034 s->flags |= fin;
3035}
Willy Tarreau7c669d72008-06-20 15:04:11 +02003036
Willy Tarreaue7dff022015-04-03 01:14:29 +02003037/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02003038void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02003039{
Willy Tarreau87b09662015-04-03 00:22:06 +02003040 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02003041 return;
3042
Willy Tarreau87b09662015-04-03 00:22:06 +02003043 channel_shutw_now(&stream->req);
3044 channel_shutr_now(&stream->res);
3045 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02003046 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02003047 stream->flags |= why;
3048 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02003049}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003050
Willy Tarreau5484d582019-05-22 09:33:03 +02003051/* Appends a dump of the state of stream <s> into buffer <buf> which must have
3052 * preliminary be prepared by its caller, with each line prepended by prefix
3053 * <pfx>, and each line terminated by character <eol>.
Willy Tarreau71c07ac2019-04-25 19:08:48 +02003054 */
Willy Tarreau5484d582019-05-22 09:33:03 +02003055void stream_dump(struct buffer *buf, const struct stream *s, const char *pfx, char eol)
Willy Tarreau71c07ac2019-04-25 19:08:48 +02003056{
3057 const struct conn_stream *csf, *csb;
3058 const struct connection *cof, *cob;
3059 const struct appctx *acf, *acb;
3060 const struct server *srv;
Willy Tarreau71c07ac2019-04-25 19:08:48 +02003061 const char *src = "unknown";
3062 const char *dst = "unknown";
3063 char pn[INET6_ADDRSTRLEN];
Willy Tarreau71c07ac2019-04-25 19:08:48 +02003064 const struct channel *req, *res;
3065 const struct stream_interface *si_f, *si_b;
Willy Tarreau71c07ac2019-04-25 19:08:48 +02003066
Willy Tarreau71c07ac2019-04-25 19:08:48 +02003067 if (!s) {
Willy Tarreau5484d582019-05-22 09:33:03 +02003068 chunk_appendf(buf, "%sstrm=%p%c", pfx, s, eol);
3069 return;
Willy Tarreau71c07ac2019-04-25 19:08:48 +02003070 }
3071
Willy Tarreau5484d582019-05-22 09:33:03 +02003072 if (s->obj_type != OBJ_TYPE_STREAM) {
3073 chunk_appendf(buf, "%sstrm=%p [invalid type=%d(%s)]%c",
3074 pfx, s, s->obj_type, obj_type_name(&s->obj_type), eol);
3075 return;
3076 }
3077
Willy Tarreau71c07ac2019-04-25 19:08:48 +02003078 si_f = &s->si[0];
3079 si_b = &s->si[1];
3080 req = &s->req;
3081 res = &s->res;
3082
3083 csf = objt_cs(si_f->end);
3084 cof = cs_conn(csf);
3085 acf = objt_appctx(si_f->end);
Willy Tarreau71e34c12019-07-17 15:07:06 +02003086 if (cof && cof->src && addr_to_str(cof->src, pn, sizeof(pn)) >= 0)
Willy Tarreau71c07ac2019-04-25 19:08:48 +02003087 src = pn;
3088 else if (acf)
3089 src = acf->applet->name;
3090
3091 csb = objt_cs(si_b->end);
3092 cob = cs_conn(csb);
3093 acb = objt_appctx(si_b->end);
3094 srv = objt_server(s->target);
3095 if (srv)
3096 dst = srv->id;
3097 else if (acb)
3098 dst = acb->applet->name;
3099
Willy Tarreau5484d582019-05-22 09:33:03 +02003100 chunk_appendf(buf,
3101 "%sstrm=%p src=%s fe=%s be=%s dst=%s%c"
3102 "%srqf=%x rqa=%x rpf=%x rpa=%x sif=%s,%x sib=%s,%x%c"
3103 "%saf=%p,%u csf=%p,%x%c"
3104 "%sab=%p,%u csb=%p,%x%c"
3105 "%scof=%p,%x:%s(%p)/%s(%p)/%s(%d)%c"
3106 "%scob=%p,%x:%s(%p)/%s(%p)/%s(%d)%c"
3107 "",
3108 pfx, s, src, s->sess->fe->id, s->be->id, dst, eol,
3109 pfx, req->flags, req->analysers, res->flags, res->analysers,
3110 si_state_str(si_f->state), si_f->flags,
3111 si_state_str(si_b->state), si_b->flags, eol,
3112 pfx, acf, acf ? acf->st0 : 0, csf, csf ? csf->flags : 0, eol,
3113 pfx, acb, acb ? acb->st0 : 0, csb, csb ? csb->flags : 0, eol,
3114 pfx, cof, cof ? cof->flags : 0, conn_get_mux_name(cof), cof?cof->ctx:0, conn_get_xprt_name(cof),
3115 cof ? cof->xprt_ctx : 0, conn_get_ctrl_name(cof), cof ? cof->handle.fd : 0, eol,
3116 pfx, cob, cob ? cob->flags : 0, conn_get_mux_name(cob), cob?cob->ctx:0, conn_get_xprt_name(cob),
3117 cob ? cob->xprt_ctx : 0, conn_get_ctrl_name(cob), cob ? cob->handle.fd : 0, eol);
3118}
3119
3120/* dumps an error message for type <type> at ptr <ptr> related to stream <s>,
Willy Tarreaub106ce12019-05-22 08:57:01 +02003121 * having reached loop rate <rate>, then aborts hoping to retrieve a core.
Willy Tarreau5484d582019-05-22 09:33:03 +02003122 */
3123void stream_dump_and_crash(enum obj_type *obj, int rate)
3124{
3125 const struct stream *s;
Willy Tarreau5484d582019-05-22 09:33:03 +02003126 char *msg = NULL;
3127 const void *ptr;
3128
3129 ptr = s = objt_stream(obj);
3130 if (!s) {
3131 const struct appctx *appctx = objt_appctx(obj);
3132 if (!appctx)
3133 return;
3134 ptr = appctx;
3135 s = si_strm(appctx->owner);
3136 if (!s)
3137 return;
3138 }
3139
Willy Tarreau5484d582019-05-22 09:33:03 +02003140 chunk_reset(&trash);
3141 stream_dump(&trash, s, "", ' ');
Willy Tarreaub106ce12019-05-22 08:57:01 +02003142 memprintf(&msg,
3143 "A bogus %s [%p] is spinning at %d calls per second and refuses to die, "
3144 "aborting now! Please report this error to developers "
3145 "[%s]\n",
Willy Tarreau5484d582019-05-22 09:33:03 +02003146 obj_type_name(obj), ptr, rate, trash.area);
Willy Tarreau71c07ac2019-04-25 19:08:48 +02003147
3148 ha_alert("%s", msg);
3149 send_log(NULL, LOG_EMERG, "%s", msg);
3150 abort();
3151}
3152
Willy Tarreau8b22a712010-06-18 17:46:06 +02003153/************************************************************************/
3154/* All supported ACL keywords must be declared here. */
3155/************************************************************************/
3156
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003157/* 0=OK, <0=Alert, >0=Warning */
3158static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
3159 struct proxy *px, struct act_rule *rule,
3160 char **err)
3161{
3162 struct action_kw *kw;
3163
3164 /* Check if the service name exists. */
3165 if (*(args[*cur_arg]) == 0) {
3166 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01003167 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003168 }
3169
3170 /* lookup for keyword corresponding to a service. */
3171 kw = action_lookup(&service_keywords, args[*cur_arg]);
3172 if (!kw) {
3173 memprintf(err, "'%s' unknown service name.", args[1]);
3174 return ACT_RET_PRS_ERR;
3175 }
3176 (*cur_arg)++;
3177
3178 /* executes specific rule parser. */
3179 rule->kw = kw;
3180 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
3181 return ACT_RET_PRS_ERR;
3182
3183 /* Register processing function. */
3184 rule->action_ptr = process_use_service;
3185 rule->action = ACT_CUSTOM;
3186
3187 return ACT_RET_PRS_OK;
3188}
3189
3190void service_keywords_register(struct action_kw_list *kw_list)
3191{
3192 LIST_ADDQ(&service_keywords, &kw_list->list);
3193}
3194
Willy Tarreau679bba12019-03-19 08:08:10 +01003195/* Lists the known services on <out> */
3196void list_services(FILE *out)
3197{
3198 struct action_kw_list *kw_list;
3199 int found = 0;
3200 int i;
3201
3202 fprintf(out, "Available services :");
3203 list_for_each_entry(kw_list, &service_keywords, list) {
3204 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
3205 if (!found)
3206 fputc('\n', out);
3207 found = 1;
3208 fprintf(out, "\t%s\n", kw_list->kw[i].kw);
3209 }
3210 }
3211 if (!found)
3212 fprintf(out, " none\n");
3213}
William Lallemand4c5b4d52016-11-21 08:51:11 +01003214
3215/* This function dumps a complete stream state onto the stream interface's
3216 * read buffer. The stream has to be set in strm. It returns 0 if the output
3217 * buffer is full and it needs to be called again, otherwise non-zero. It is
3218 * designed to be called from stats_dump_strm_to_buffer() below.
3219 */
3220static int stats_dump_full_strm_to_buffer(struct stream_interface *si, struct stream *strm)
3221{
3222 struct appctx *appctx = __objt_appctx(si->end);
3223 struct tm tm;
3224 extern const char *monthname[12];
3225 char pn[INET6_ADDRSTRLEN];
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003226 struct conn_stream *cs;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003227 struct connection *conn;
3228 struct appctx *tmpctx;
3229
3230 chunk_reset(&trash);
3231
3232 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != strm->uniq_id) {
3233 /* stream changed, no need to go any further */
3234 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaue6e52362019-01-04 17:42:57 +01003235 if (ci_putchk(si_ic(si), &trash) == -1)
3236 goto full;
3237 goto done;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003238 }
3239
3240 switch (appctx->ctx.sess.section) {
3241 case 0: /* main status of the stream */
3242 appctx->ctx.sess.uid = strm->uniq_id;
3243 appctx->ctx.sess.section = 1;
3244 /* fall through */
3245
3246 case 1:
3247 get_localtime(strm->logs.accept_date.tv_sec, &tm);
3248 chunk_appendf(&trash,
3249 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
3250 strm,
3251 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3252 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(strm->logs.accept_date.tv_usec),
3253 strm->uniq_id,
3254 strm_li(strm) ? strm_li(strm)->proto->name : "?");
3255
3256 conn = objt_conn(strm_orig(strm));
Willy Tarreau71e34c12019-07-17 15:07:06 +02003257 switch (conn && conn_get_src(conn) ? addr_to_str(conn->src, pn, sizeof(pn)) : AF_UNSPEC) {
William Lallemand4c5b4d52016-11-21 08:51:11 +01003258 case AF_INET:
3259 case AF_INET6:
3260 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreau71e34c12019-07-17 15:07:06 +02003261 pn, get_host_port(conn->src));
William Lallemand4c5b4d52016-11-21 08:51:11 +01003262 break;
3263 case AF_UNIX:
3264 chunk_appendf(&trash, " source=unix:%d\n", strm_li(strm)->luid);
3265 break;
3266 default:
3267 /* no more information to print right now */
3268 chunk_appendf(&trash, "\n");
3269 break;
3270 }
3271
3272 chunk_appendf(&trash,
Willy Tarreauadf7a152019-01-07 10:10:07 +01003273 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p waiting=%d\n",
3274 strm->flags, strm->si[1].conn_retries, strm->srv_conn, strm->pend_pos,
3275 !LIST_ISEMPTY(&strm->buffer_wait.list));
William Lallemand4c5b4d52016-11-21 08:51:11 +01003276
3277 chunk_appendf(&trash,
3278 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
3279 strm_fe(strm)->id, strm_fe(strm)->uuid, strm_fe(strm)->mode ? "http" : "tcp",
3280 strm_li(strm) ? strm_li(strm)->name ? strm_li(strm)->name : "?" : "?",
3281 strm_li(strm) ? strm_li(strm)->luid : 0);
3282
Willy Tarreau71e34c12019-07-17 15:07:06 +02003283 switch (conn && conn_get_dst(conn) ? addr_to_str(conn->dst, pn, sizeof(pn)) : AF_UNSPEC) {
William Lallemand4c5b4d52016-11-21 08:51:11 +01003284 case AF_INET:
3285 case AF_INET6:
3286 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreau71e34c12019-07-17 15:07:06 +02003287 pn, get_host_port(conn->dst));
William Lallemand4c5b4d52016-11-21 08:51:11 +01003288 break;
3289 case AF_UNIX:
3290 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(strm)->luid);
3291 break;
3292 default:
3293 /* no more information to print right now */
3294 chunk_appendf(&trash, "\n");
3295 break;
3296 }
3297
3298 if (strm->be->cap & PR_CAP_BE)
3299 chunk_appendf(&trash,
3300 " backend=%s (id=%u mode=%s)",
3301 strm->be->id,
3302 strm->be->uuid, strm->be->mode ? "http" : "tcp");
3303 else
3304 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
3305
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003306 cs = objt_cs(strm->si[1].end);
3307 conn = cs_conn(cs);
3308
Willy Tarreau71e34c12019-07-17 15:07:06 +02003309 switch (conn && conn_get_src(conn) ? addr_to_str(conn->src, pn, sizeof(pn)) : AF_UNSPEC) {
William Lallemand4c5b4d52016-11-21 08:51:11 +01003310 case AF_INET:
3311 case AF_INET6:
3312 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreau71e34c12019-07-17 15:07:06 +02003313 pn, get_host_port(conn->src));
William Lallemand4c5b4d52016-11-21 08:51:11 +01003314 break;
3315 case AF_UNIX:
3316 chunk_appendf(&trash, " addr=unix\n");
3317 break;
3318 default:
3319 /* no more information to print right now */
3320 chunk_appendf(&trash, "\n");
3321 break;
3322 }
3323
3324 if (strm->be->cap & PR_CAP_BE)
3325 chunk_appendf(&trash,
3326 " server=%s (id=%u)",
3327 objt_server(strm->target) ? objt_server(strm->target)->id : "<none>",
3328 objt_server(strm->target) ? objt_server(strm->target)->puid : 0);
3329 else
3330 chunk_appendf(&trash, " server=<NONE> (id=-1)");
3331
Willy Tarreau71e34c12019-07-17 15:07:06 +02003332 switch (conn && conn_get_dst(conn) ? addr_to_str(conn->dst, pn, sizeof(pn)) : AF_UNSPEC) {
William Lallemand4c5b4d52016-11-21 08:51:11 +01003333 case AF_INET:
3334 case AF_INET6:
3335 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreau71e34c12019-07-17 15:07:06 +02003336 pn, get_host_port(conn->dst));
William Lallemand4c5b4d52016-11-21 08:51:11 +01003337 break;
3338 case AF_UNIX:
3339 chunk_appendf(&trash, " addr=unix\n");
3340 break;
3341 default:
3342 /* no more information to print right now */
3343 chunk_appendf(&trash, "\n");
3344 break;
3345 }
3346
3347 chunk_appendf(&trash,
Willy Tarreau2e9c1d22019-04-24 08:28:31 +02003348 " task=%p (state=0x%02x nice=%d calls=%u rate=%u exp=%s tmask=0x%lx%s",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003349 strm->task,
3350 strm->task->state,
Willy Tarreau2e9c1d22019-04-24 08:28:31 +02003351 strm->task->nice, strm->task->calls, read_freq_ctr(&strm->call_rate),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003352 strm->task->expire ?
3353 tick_is_expired(strm->task->expire, now_ms) ? "<PAST>" :
3354 human_time(TICKS_TO_MS(strm->task->expire - now_ms),
3355 TICKS_TO_MS(1000)) : "<NEVER>",
Christopher Fauletf0205062017-11-15 20:56:43 +01003356 strm->task->thread_mask,
William Lallemand4c5b4d52016-11-21 08:51:11 +01003357 task_in_rq(strm->task) ? ", running" : "");
3358
3359 chunk_appendf(&trash,
3360 " age=%s)\n",
3361 human_time(now.tv_sec - strm->logs.accept_date.tv_sec, 1));
3362
3363 if (strm->txn)
3364 chunk_appendf(&trash,
Christopher Fauletbcac7862019-07-17 10:46:50 +02003365 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s req.f=0x%02x rsp.f=0x%02x\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003366 strm->txn, strm->txn->flags, strm->txn->meth, strm->txn->status,
Willy Tarreau7778b592019-01-07 10:38:10 +01003367 h1_msg_state_str(strm->txn->req.msg_state), h1_msg_state_str(strm->txn->rsp.msg_state),
Christopher Fauletbcac7862019-07-17 10:46:50 +02003368 strm->txn->req.flags, strm->txn->rsp.flags);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003369
3370 chunk_appendf(&trash,
Christopher Faulet224a2d72019-01-04 15:06:13 +01003371 " 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 +01003372 &strm->si[0],
3373 si_state_str(strm->si[0].state),
3374 strm->si[0].flags,
3375 obj_type_name(strm->si[0].end),
3376 obj_base_ptr(strm->si[0].end),
3377 strm->si[0].exp ?
3378 tick_is_expired(strm->si[0].exp, now_ms) ? "<PAST>" :
3379 human_time(TICKS_TO_MS(strm->si[0].exp - now_ms),
3380 TICKS_TO_MS(1000)) : "<NEVER>",
Christopher Faulet224a2d72019-01-04 15:06:13 +01003381 strm->si[0].err_type, strm->si[0].wait_event.events);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003382
3383 chunk_appendf(&trash,
Christopher Faulet224a2d72019-01-04 15:06:13 +01003384 " 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 +01003385 &strm->si[1],
3386 si_state_str(strm->si[1].state),
3387 strm->si[1].flags,
3388 obj_type_name(strm->si[1].end),
3389 obj_base_ptr(strm->si[1].end),
3390 strm->si[1].exp ?
3391 tick_is_expired(strm->si[1].exp, now_ms) ? "<PAST>" :
3392 human_time(TICKS_TO_MS(strm->si[1].exp - now_ms),
3393 TICKS_TO_MS(1000)) : "<NEVER>",
Christopher Faulet224a2d72019-01-04 15:06:13 +01003394 strm->si[1].err_type, strm->si[1].wait_event.events);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003395
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003396 if ((cs = objt_cs(strm->si[0].end)) != NULL) {
3397 conn = cs->conn;
3398
William Lallemand4c5b4d52016-11-21 08:51:11 +01003399 chunk_appendf(&trash,
Willy Tarreau53a47662017-08-28 10:53:00 +02003400 " co0=%p ctrl=%s xprt=%s mux=%s data=%s target=%s:%p\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003401 conn,
3402 conn_get_ctrl_name(conn),
3403 conn_get_xprt_name(conn),
Willy Tarreau53a47662017-08-28 10:53:00 +02003404 conn_get_mux_name(conn),
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003405 cs_get_data_name(cs),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003406 obj_type_name(conn->target),
3407 obj_base_ptr(conn->target));
3408
3409 chunk_appendf(&trash,
Willy Tarreau76913d32019-08-30 14:33:11 +02003410 " flags=0x%08x fd=%d fd.state=%02x updt=%d fd.tmask=0x%lx\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003411 conn->flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003412 conn->handle.fd,
3413 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].state : 0,
Willy Tarreauebc78d72018-01-20 23:53:50 +01003414 conn->handle.fd >= 0 ? !!(fdtab[conn->handle.fd].update_mask & tid_bit) : 0,
Christopher Fauletf0205062017-11-15 20:56:43 +01003415 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].thread_mask: 0);
Willy Tarreaufb3b1b02018-12-18 14:28:24 +01003416
3417 chunk_appendf(&trash, " cs=%p csf=0x%08x ctx=%p\n", cs, cs->flags, cs->ctx);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003418 }
3419 else if ((tmpctx = objt_appctx(strm->si[0].end)) != NULL) {
3420 chunk_appendf(&trash,
Willy Tarreau22d63a22019-04-24 08:41:29 +02003421 " 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 +01003422 tmpctx,
3423 tmpctx->st0,
3424 tmpctx->st1,
3425 tmpctx->st2,
Christopher Fauletf0205062017-11-15 20:56:43 +01003426 tmpctx->applet->name,
Willy Tarreau34b1fac2018-05-31 14:40:19 +02003427 tmpctx->thread_mask,
Willy Tarreau22d63a22019-04-24 08:41:29 +02003428 tmpctx->t->nice, tmpctx->t->calls, read_freq_ctr(&tmpctx->call_rate),
Willy Tarreau9efd7452018-05-31 14:48:54 +02003429 (unsigned long long)tmpctx->t->cpu_time, (unsigned long long)tmpctx->t->lat_time);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003430 }
3431
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003432 if ((cs = objt_cs(strm->si[1].end)) != NULL) {
3433 conn = cs->conn;
3434
William Lallemand4c5b4d52016-11-21 08:51:11 +01003435 chunk_appendf(&trash,
Willy Tarreau53a47662017-08-28 10:53:00 +02003436 " co1=%p ctrl=%s xprt=%s mux=%s data=%s target=%s:%p\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003437 conn,
3438 conn_get_ctrl_name(conn),
3439 conn_get_xprt_name(conn),
Willy Tarreau53a47662017-08-28 10:53:00 +02003440 conn_get_mux_name(conn),
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003441 cs_get_data_name(cs),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003442 obj_type_name(conn->target),
3443 obj_base_ptr(conn->target));
3444
3445 chunk_appendf(&trash,
Willy Tarreau76913d32019-08-30 14:33:11 +02003446 " flags=0x%08x fd=%d fd.state=%02x updt=%d fd.tmask=0x%lx\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003447 conn->flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003448 conn->handle.fd,
3449 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].state : 0,
Willy Tarreauebc78d72018-01-20 23:53:50 +01003450 conn->handle.fd >= 0 ? !!(fdtab[conn->handle.fd].update_mask & tid_bit) : 0,
Christopher Fauletf0205062017-11-15 20:56:43 +01003451 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].thread_mask: 0);
Willy Tarreaufb3b1b02018-12-18 14:28:24 +01003452
3453 chunk_appendf(&trash, " cs=%p csf=0x%08x ctx=%p\n", cs, cs->flags, cs->ctx);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003454 }
3455 else if ((tmpctx = objt_appctx(strm->si[1].end)) != NULL) {
3456 chunk_appendf(&trash,
Willy Tarreau22d63a22019-04-24 08:41:29 +02003457 " 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 +01003458 tmpctx,
3459 tmpctx->st0,
3460 tmpctx->st1,
3461 tmpctx->st2,
Christopher Fauletf0205062017-11-15 20:56:43 +01003462 tmpctx->applet->name,
Willy Tarreau34b1fac2018-05-31 14:40:19 +02003463 tmpctx->thread_mask,
Willy Tarreau22d63a22019-04-24 08:41:29 +02003464 tmpctx->t->nice, tmpctx->t->calls, read_freq_ctr(&tmpctx->call_rate),
Willy Tarreau9efd7452018-05-31 14:48:54 +02003465 (unsigned long long)tmpctx->t->cpu_time, (unsigned long long)tmpctx->t->lat_time);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003466 }
3467
3468 chunk_appendf(&trash,
3469 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
3470 " an_exp=%s",
3471 &strm->req,
3472 strm->req.flags, strm->req.analysers,
3473 strm->req.pipe ? strm->req.pipe->data : 0,
3474 strm->req.to_forward, strm->req.total,
3475 strm->req.analyse_exp ?
3476 human_time(TICKS_TO_MS(strm->req.analyse_exp - now_ms),
3477 TICKS_TO_MS(1000)) : "<NEVER>");
3478
3479 chunk_appendf(&trash,
3480 " rex=%s",
3481 strm->req.rex ?
3482 human_time(TICKS_TO_MS(strm->req.rex - now_ms),
3483 TICKS_TO_MS(1000)) : "<NEVER>");
3484
3485 chunk_appendf(&trash,
3486 " wex=%s\n"
Christopher Fauletbcac7862019-07-17 10:46:50 +02003487 " buf=%p data=%p o=%u p=%u i=%u size=%u\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003488 strm->req.wex ?
3489 human_time(TICKS_TO_MS(strm->req.wex - now_ms),
3490 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003491 &strm->req.buf,
3492 b_orig(&strm->req.buf), (unsigned int)co_data(&strm->req),
Christopher Fauletbcac7862019-07-17 10:46:50 +02003493 (unsigned int)ci_head_ofs(&strm->req), (unsigned int)ci_data(&strm->req),
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003494 (unsigned int)strm->req.buf.size);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003495
Christopher Fauletb9af8812019-01-04 14:30:44 +01003496 if (IS_HTX_STRM(strm)) {
3497 struct htx *htx = htxbuf(&strm->req.buf);
3498
3499 chunk_appendf(&trash,
Willy Tarreaub84e67f2019-01-07 10:01:34 +01003500 " htx=%p flags=0x%x size=%u data=%u used=%u wrap=%s extra=%llu\n",
Christopher Faulet192c6a22019-06-11 16:32:24 +02003501 htx, htx->flags, htx->size, htx->data, htx_nbblks(htx),
Christopher Faulet28f29c72019-04-30 17:55:45 +02003502 (htx->tail >= htx->head) ? "NO" : "YES",
Christopher Fauletb9af8812019-01-04 14:30:44 +01003503 (unsigned long long)htx->extra);
3504 }
3505
William Lallemand4c5b4d52016-11-21 08:51:11 +01003506 chunk_appendf(&trash,
3507 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
3508 " an_exp=%s",
3509 &strm->res,
3510 strm->res.flags, strm->res.analysers,
3511 strm->res.pipe ? strm->res.pipe->data : 0,
3512 strm->res.to_forward, strm->res.total,
3513 strm->res.analyse_exp ?
3514 human_time(TICKS_TO_MS(strm->res.analyse_exp - now_ms),
3515 TICKS_TO_MS(1000)) : "<NEVER>");
3516
3517 chunk_appendf(&trash,
3518 " rex=%s",
3519 strm->res.rex ?
3520 human_time(TICKS_TO_MS(strm->res.rex - now_ms),
3521 TICKS_TO_MS(1000)) : "<NEVER>");
3522
3523 chunk_appendf(&trash,
3524 " wex=%s\n"
Christopher Fauletbcac7862019-07-17 10:46:50 +02003525 " buf=%p data=%p o=%u p=%u i=%u size=%u\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003526 strm->res.wex ?
3527 human_time(TICKS_TO_MS(strm->res.wex - now_ms),
3528 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003529 &strm->res.buf,
3530 b_orig(&strm->res.buf), (unsigned int)co_data(&strm->res),
Christopher Fauletbcac7862019-07-17 10:46:50 +02003531 (unsigned int)ci_head_ofs(&strm->res), (unsigned int)ci_data(&strm->res),
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003532 (unsigned int)strm->res.buf.size);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003533
Christopher Fauletb9af8812019-01-04 14:30:44 +01003534 if (IS_HTX_STRM(strm)) {
3535 struct htx *htx = htxbuf(&strm->res.buf);
3536
3537 chunk_appendf(&trash,
3538 " htx=%p flags=0x%x size=%u data=%u used=%u wrap=%s extra=%llu\n",
Christopher Faulet192c6a22019-06-11 16:32:24 +02003539 htx, htx->flags, htx->size, htx->data, htx_nbblks(htx),
Christopher Faulet28f29c72019-04-30 17:55:45 +02003540 (htx->tail >= htx->head) ? "NO" : "YES",
Christopher Fauletb9af8812019-01-04 14:30:44 +01003541 (unsigned long long)htx->extra);
3542 }
3543
Willy Tarreaue6e52362019-01-04 17:42:57 +01003544 if (ci_putchk(si_ic(si), &trash) == -1)
3545 goto full;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003546
3547 /* use other states to dump the contents */
3548 }
3549 /* end of dump */
Willy Tarreaue6e52362019-01-04 17:42:57 +01003550 done:
William Lallemand4c5b4d52016-11-21 08:51:11 +01003551 appctx->ctx.sess.uid = 0;
3552 appctx->ctx.sess.section = 0;
3553 return 1;
Willy Tarreaue6e52362019-01-04 17:42:57 +01003554 full:
3555 return 0;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003556}
3557
3558
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003559static int cli_parse_show_sess(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand4c5b4d52016-11-21 08:51:11 +01003560{
William Lallemand4c5b4d52016-11-21 08:51:11 +01003561 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3562 return 1;
3563
3564 if (*args[2] && strcmp(args[2], "all") == 0)
3565 appctx->ctx.sess.target = (void *)-1;
3566 else if (*args[2])
3567 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
3568 else
3569 appctx->ctx.sess.target = NULL;
3570 appctx->ctx.sess.section = 0; /* start with stream status */
3571 appctx->ctx.sess.pos = 0;
3572
3573 return 0;
3574}
3575
3576/* This function dumps all streams' states onto the stream interface's
3577 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreaue6e52362019-01-04 17:42:57 +01003578 * to be called again, otherwise non-zero. It proceeds in an isolated
3579 * thread so there is no thread safety issue here.
William Lallemand4c5b4d52016-11-21 08:51:11 +01003580 */
3581static int cli_io_handler_dump_sess(struct appctx *appctx)
3582{
3583 struct stream_interface *si = appctx->owner;
3584 struct connection *conn;
3585
Willy Tarreaue6e52362019-01-04 17:42:57 +01003586 thread_isolate();
3587
William Lallemand4c5b4d52016-11-21 08:51:11 +01003588 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
3589 /* If we're forced to shut down, we might have to remove our
3590 * reference to the last stream being dumped.
3591 */
3592 if (appctx->st2 == STAT_ST_LIST) {
3593 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
3594 LIST_DEL(&appctx->ctx.sess.bref.users);
3595 LIST_INIT(&appctx->ctx.sess.bref.users);
3596 }
3597 }
Willy Tarreaue6e52362019-01-04 17:42:57 +01003598 goto done;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003599 }
3600
3601 chunk_reset(&trash);
3602
3603 switch (appctx->st2) {
3604 case STAT_ST_INIT:
3605 /* the function had not been called yet, let's prepare the
3606 * buffer for a response. We initialize the current stream
3607 * pointer to the first in the global list. When a target
3608 * stream is being destroyed, it is responsible for updating
3609 * this pointer. We know we have reached the end when this
3610 * pointer points back to the head of the streams list.
3611 */
3612 LIST_INIT(&appctx->ctx.sess.bref.users);
3613 appctx->ctx.sess.bref.ref = streams.n;
3614 appctx->st2 = STAT_ST_LIST;
3615 /* fall through */
3616
3617 case STAT_ST_LIST:
3618 /* first, let's detach the back-ref from a possible previous stream */
3619 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
3620 LIST_DEL(&appctx->ctx.sess.bref.users);
3621 LIST_INIT(&appctx->ctx.sess.bref.users);
3622 }
3623
3624 /* and start from where we stopped */
3625 while (appctx->ctx.sess.bref.ref != &streams) {
3626 char pn[INET6_ADDRSTRLEN];
3627 struct stream *curr_strm;
3628
3629 curr_strm = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
3630
3631 if (appctx->ctx.sess.target) {
3632 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_strm)
3633 goto next_sess;
3634
3635 LIST_ADDQ(&curr_strm->back_refs, &appctx->ctx.sess.bref.users);
3636 /* call the proper dump() function and return if we're missing space */
Willy Tarreaue6e52362019-01-04 17:42:57 +01003637 if (!stats_dump_full_strm_to_buffer(si, curr_strm))
3638 goto full;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003639
3640 /* stream dump complete */
3641 LIST_DEL(&appctx->ctx.sess.bref.users);
3642 LIST_INIT(&appctx->ctx.sess.bref.users);
3643 if (appctx->ctx.sess.target != (void *)-1) {
3644 appctx->ctx.sess.target = NULL;
3645 break;
3646 }
3647 else
3648 goto next_sess;
3649 }
3650
3651 chunk_appendf(&trash,
3652 "%p: proto=%s",
3653 curr_strm,
3654 strm_li(curr_strm) ? strm_li(curr_strm)->proto->name : "?");
3655
3656 conn = objt_conn(strm_orig(curr_strm));
Willy Tarreau71e34c12019-07-17 15:07:06 +02003657 switch (conn && conn_get_src(conn) ? addr_to_str(conn->src, pn, sizeof(pn)) : AF_UNSPEC) {
William Lallemand4c5b4d52016-11-21 08:51:11 +01003658 case AF_INET:
3659 case AF_INET6:
3660 chunk_appendf(&trash,
3661 " src=%s:%d fe=%s be=%s srv=%s",
3662 pn,
Willy Tarreau71e34c12019-07-17 15:07:06 +02003663 get_host_port(conn->src),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003664 strm_fe(curr_strm)->id,
3665 (curr_strm->be->cap & PR_CAP_BE) ? curr_strm->be->id : "<NONE>",
3666 objt_server(curr_strm->target) ? objt_server(curr_strm->target)->id : "<none>"
3667 );
3668 break;
3669 case AF_UNIX:
3670 chunk_appendf(&trash,
3671 " src=unix:%d fe=%s be=%s srv=%s",
3672 strm_li(curr_strm)->luid,
3673 strm_fe(curr_strm)->id,
3674 (curr_strm->be->cap & PR_CAP_BE) ? curr_strm->be->id : "<NONE>",
3675 objt_server(curr_strm->target) ? objt_server(curr_strm->target)->id : "<none>"
3676 );
3677 break;
3678 }
3679
3680 chunk_appendf(&trash,
Willy Tarreau2e9c1d22019-04-24 08:28:31 +02003681 " ts=%02x age=%s calls=%u rate=%u cpu=%llu lat=%llu",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003682 curr_strm->task->state,
3683 human_time(now.tv_sec - curr_strm->logs.tv_accept.tv_sec, 1),
Willy Tarreau2e9c1d22019-04-24 08:28:31 +02003684 curr_strm->task->calls, read_freq_ctr(&curr_strm->call_rate),
Willy Tarreau9efd7452018-05-31 14:48:54 +02003685 (unsigned long long)curr_strm->task->cpu_time, (unsigned long long)curr_strm->task->lat_time);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003686
3687 chunk_appendf(&trash,
Willy Tarreau506a29a2018-07-18 10:07:58 +02003688 " rq[f=%06xh,i=%u,an=%02xh,rx=%s",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003689 curr_strm->req.flags,
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003690 (unsigned int)ci_data(&curr_strm->req),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003691 curr_strm->req.analysers,
3692 curr_strm->req.rex ?
3693 human_time(TICKS_TO_MS(curr_strm->req.rex - now_ms),
3694 TICKS_TO_MS(1000)) : "");
3695
3696 chunk_appendf(&trash,
3697 ",wx=%s",
3698 curr_strm->req.wex ?
3699 human_time(TICKS_TO_MS(curr_strm->req.wex - now_ms),
3700 TICKS_TO_MS(1000)) : "");
3701
3702 chunk_appendf(&trash,
3703 ",ax=%s]",
3704 curr_strm->req.analyse_exp ?
3705 human_time(TICKS_TO_MS(curr_strm->req.analyse_exp - now_ms),
3706 TICKS_TO_MS(1000)) : "");
3707
3708 chunk_appendf(&trash,
Willy Tarreau506a29a2018-07-18 10:07:58 +02003709 " rp[f=%06xh,i=%u,an=%02xh,rx=%s",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003710 curr_strm->res.flags,
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003711 (unsigned int)ci_data(&curr_strm->res),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003712 curr_strm->res.analysers,
3713 curr_strm->res.rex ?
3714 human_time(TICKS_TO_MS(curr_strm->res.rex - now_ms),
3715 TICKS_TO_MS(1000)) : "");
3716
3717 chunk_appendf(&trash,
3718 ",wx=%s",
3719 curr_strm->res.wex ?
3720 human_time(TICKS_TO_MS(curr_strm->res.wex - now_ms),
3721 TICKS_TO_MS(1000)) : "");
3722
3723 chunk_appendf(&trash,
3724 ",ax=%s]",
3725 curr_strm->res.analyse_exp ?
3726 human_time(TICKS_TO_MS(curr_strm->res.analyse_exp - now_ms),
3727 TICKS_TO_MS(1000)) : "");
3728
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003729 conn = cs_conn(objt_cs(curr_strm->si[0].end));
William Lallemand4c5b4d52016-11-21 08:51:11 +01003730 chunk_appendf(&trash,
3731 " s0=[%d,%1xh,fd=%d,ex=%s]",
3732 curr_strm->si[0].state,
3733 curr_strm->si[0].flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003734 conn ? conn->handle.fd : -1,
William Lallemand4c5b4d52016-11-21 08:51:11 +01003735 curr_strm->si[0].exp ?
3736 human_time(TICKS_TO_MS(curr_strm->si[0].exp - now_ms),
3737 TICKS_TO_MS(1000)) : "");
3738
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003739 conn = cs_conn(objt_cs(curr_strm->si[1].end));
William Lallemand4c5b4d52016-11-21 08:51:11 +01003740 chunk_appendf(&trash,
3741 " s1=[%d,%1xh,fd=%d,ex=%s]",
3742 curr_strm->si[1].state,
3743 curr_strm->si[1].flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003744 conn ? conn->handle.fd : -1,
William Lallemand4c5b4d52016-11-21 08:51:11 +01003745 curr_strm->si[1].exp ?
3746 human_time(TICKS_TO_MS(curr_strm->si[1].exp - now_ms),
3747 TICKS_TO_MS(1000)) : "");
3748
3749 chunk_appendf(&trash,
3750 " exp=%s",
3751 curr_strm->task->expire ?
3752 human_time(TICKS_TO_MS(curr_strm->task->expire - now_ms),
3753 TICKS_TO_MS(1000)) : "");
3754 if (task_in_rq(curr_strm->task))
3755 chunk_appendf(&trash, " run(nice=%d)", curr_strm->task->nice);
3756
3757 chunk_appendf(&trash, "\n");
3758
Willy Tarreau06d80a92017-10-19 14:32:15 +02003759 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand4c5b4d52016-11-21 08:51:11 +01003760 /* let's try again later from this stream. We add ourselves into
3761 * this stream's users so that it can remove us upon termination.
3762 */
William Lallemand4c5b4d52016-11-21 08:51:11 +01003763 LIST_ADDQ(&curr_strm->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreaue6e52362019-01-04 17:42:57 +01003764 goto full;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003765 }
3766
3767 next_sess:
3768 appctx->ctx.sess.bref.ref = curr_strm->list.n;
3769 }
3770
3771 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
3772 /* specified stream not found */
3773 if (appctx->ctx.sess.section > 0)
3774 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3775 else
3776 chunk_appendf(&trash, "Session not found.\n");
3777
Willy Tarreaue6e52362019-01-04 17:42:57 +01003778 if (ci_putchk(si_ic(si), &trash) == -1)
3779 goto full;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003780
3781 appctx->ctx.sess.target = NULL;
3782 appctx->ctx.sess.uid = 0;
Willy Tarreaue6e52362019-01-04 17:42:57 +01003783 goto done;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003784 }
William Lallemand4c5b4d52016-11-21 08:51:11 +01003785 /* fall through */
3786
3787 default:
3788 appctx->st2 = STAT_ST_FIN;
Willy Tarreaue6e52362019-01-04 17:42:57 +01003789 goto done;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003790 }
Willy Tarreaue6e52362019-01-04 17:42:57 +01003791 done:
3792 thread_release();
3793 return 1;
3794 full:
3795 thread_release();
3796 si_rx_room_blk(si);
3797 return 0;
William Lallemand4c5b4d52016-11-21 08:51:11 +01003798}
3799
3800static void cli_release_show_sess(struct appctx *appctx)
3801{
3802 if (appctx->st2 == STAT_ST_LIST) {
Willy Tarreau7ce3f092017-11-10 16:24:41 +01003803 HA_SPIN_LOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003804 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
3805 LIST_DEL(&appctx->ctx.sess.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003806 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003807 }
3808}
3809
Willy Tarreau61b65212016-11-24 11:09:25 +01003810/* Parses the "shutdown session" directive, it always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003811static int cli_parse_shutdown_session(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau61b65212016-11-24 11:09:25 +01003812{
3813 struct stream *strm, *ptr;
3814
3815 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3816 return 1;
3817
Willy Tarreau9d008692019-08-09 11:21:01 +02003818 if (!*args[2])
3819 return cli_err(appctx, "Session pointer expected (use 'show sess').\n");
Willy Tarreau61b65212016-11-24 11:09:25 +01003820
3821 ptr = (void *)strtoul(args[2], NULL, 0);
3822
3823 /* first, look for the requested stream in the stream table */
3824 list_for_each_entry(strm, &streams, list) {
3825 if (strm == ptr)
3826 break;
3827 }
3828
3829 /* do we have the stream ? */
Willy Tarreau9d008692019-08-09 11:21:01 +02003830 if (strm != ptr)
3831 return cli_err(appctx, "No such session (use 'show sess').\n");
Willy Tarreau61b65212016-11-24 11:09:25 +01003832
3833 stream_shutdown(strm, SF_ERR_KILLED);
3834 return 1;
3835}
3836
Willy Tarreau4e46b622016-11-23 16:50:48 +01003837/* Parses the "shutdown session server" directive, it always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003838static int cli_parse_shutdown_sessions_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau4e46b622016-11-23 16:50:48 +01003839{
3840 struct server *sv;
3841 struct stream *strm, *strm_bck;
3842
3843 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3844 return 1;
3845
3846 sv = cli_find_server(appctx, args[3]);
3847 if (!sv)
3848 return 1;
3849
3850 /* kill all the stream that are on this server */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003851 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau4e46b622016-11-23 16:50:48 +01003852 list_for_each_entry_safe(strm, strm_bck, &sv->actconns, by_srv)
3853 if (strm->srv_conn == sv)
3854 stream_shutdown(strm, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003855 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau4e46b622016-11-23 16:50:48 +01003856 return 1;
3857}
3858
William Lallemand4c5b4d52016-11-21 08:51:11 +01003859/* register cli keywords */
3860static struct cli_kw_list cli_kws = {{ },{
3861 { { "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 +01003862 { { "shutdown", "session", NULL }, "shutdown session : kill a specific session", cli_parse_shutdown_session, NULL, NULL },
Willy Tarreau4e46b622016-11-23 16:50:48 +01003863 { { "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 +01003864 {{},}
3865}};
3866
Willy Tarreau0108d902018-11-25 19:14:37 +01003867INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3868
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003869/* main configuration keyword registration. */
3870static struct action_kw_list stream_tcp_keywords = { ILH, {
3871 { "use-service", stream_parse_use_service },
3872 { /* END */ }
3873}};
3874
Willy Tarreau0108d902018-11-25 19:14:37 +01003875INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &stream_tcp_keywords);
3876
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003877static struct action_kw_list stream_http_keywords = { ILH, {
3878 { "use-service", stream_parse_use_service },
3879 { /* END */ }
3880}};
3881
Willy Tarreau0108d902018-11-25 19:14:37 +01003882INITCALL1(STG_REGISTER, http_req_keywords_register, &stream_http_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003883
Willy Tarreaubaaee002006-06-26 02:48:02 +02003884/*
3885 * Local variables:
3886 * c-indent-level: 8
3887 * c-basic-offset: 8
3888 * End:
3889 */