blob: f8c0887b94232f8506b5b7228baaca9db932ba0d [file] [log] [blame]
Willy TARREAU3dc06442006-06-15 21:48:13 +02001/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002 * include/proto/stream.h
3 * This file defines everything related to streams.
Willy Tarreau81f9aa32010-06-01 17:45:26 +02004 *
5 * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy TARREAU3dc06442006-06-15 21:48:13 +020021
Willy Tarreau87b09662015-04-03 00:22:06 +020022#ifndef _PROTO_STREAM_H
23#define _PROTO_STREAM_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020026#include <common/memory.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020027#include <types/action.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020028#include <types/stream.h>
Willy Tarreaua24adf02014-11-27 01:11:56 +010029#include <proto/fd.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020030#include <proto/freq_ctr.h>
Olivier Houchard4bd58672019-07-12 16:16:59 +020031#include <proto/obj_type.h>
32#include <proto/queue.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020033#include <proto/stick_table.h>
Willy Tarreaua24adf02014-11-27 01:11:56 +010034#include <proto/task.h>
Christopher Fauleteea8fc72019-11-05 16:18:10 +010035#include <proto/trace.h>
36
37extern struct trace_source trace_strm;
38
39/* Details about these events are defined in <src/stream.c> */
40#define STRM_EV_STRM_NEW (1ULL << 0)
41#define STRM_EV_STRM_FREE (1ULL << 1)
42#define STRM_EV_STRM_ERR (1ULL << 2)
43#define STRM_EV_STRM_ANA (1ULL << 3)
44#define STRM_EV_STRM_PROC (1ULL << 4)
45#define STRM_EV_SI_ST (1ULL << 5)
46#define STRM_EV_HTTP_ANA (1ULL << 6)
47#define STRM_EV_HTTP_ERR (1ULL << 7)
48#define STRM_EV_TCP_ANA (1ULL << 8)
49#define STRM_EV_TCP_ERR (1ULL << 9)
50#define STRM_EV_FLT_ANA (1ULL << 10)
51#define STRM_EV_FLT_ERR (1ULL << 11)
Willy TARREAU3dc06442006-06-15 21:48:13 +020052
Christopher Fauletc8b246f2019-05-14 22:05:28 +020053#define IS_HTX_STRM(strm) ((strm)->flags & SF_HTX)
54
Willy Tarreaubafbe012017-11-24 17:34:44 +010055extern struct pool_head *pool_head_stream;
Willy Tarreau87b09662015-04-03 00:22:06 +020056extern struct list streams;
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020057
Willy Tarreaubc174aa2012-11-19 16:10:32 +010058extern struct data_cb sess_conn_cb;
59
Willy Tarreau87787ac2017-08-28 16:22:54 +020060struct stream *stream_new(struct session *sess, enum obj_type *origin);
Olivier Houchard9aaf7782017-09-13 18:30:23 +020061int stream_create_from_cs(struct conn_stream *cs);
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Willy Tarreaue7dff022015-04-03 01:14:29 +020063/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +020064void stream_shutdown(struct stream *stream, int why);
Willy Tarreau5484d582019-05-22 09:33:03 +020065void stream_dump(struct buffer *buf, const struct stream *s, const char *pfx, char eol);
Willy Tarreau71c07ac2019-04-25 19:08:48 +020066void stream_dump_and_crash(enum obj_type *obj, int rate);
Simon Hormandec5be42011-06-08 09:19:07 +090067
Willy Tarreau87b09662015-04-03 00:22:06 +020068void stream_process_counters(struct stream *s);
69void sess_change_server(struct stream *sess, struct server *newsrv);
Olivier Houchard9f6af332018-05-25 14:04:04 +020070struct task *process_stream(struct task *t, void *context, unsigned short state);
Willy Tarreau87b09662015-04-03 00:22:06 +020071void default_srv_error(struct stream *s, struct stream_interface *si);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020072int parse_track_counters(char **args, int *arg,
73 int section_type, struct proxy *curpx,
74 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +020075 struct proxy *defpx, char **err);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020076
Willy Tarreau87b09662015-04-03 00:22:06 +020077/* Update the stream's backend and server time stats */
78void stream_update_time_stats(struct stream *s);
Willy Tarreau87b09662015-04-03 00:22:06 +020079void stream_release_buffers(struct stream *s);
Willy Tarreaub882dd82018-11-06 15:50:21 +010080int stream_buf_available(void *arg);
Willy Tarreau4bfc5802014-06-17 12:19:18 +020081
Willy Tarreaub1ec8c42015-04-03 13:53:24 +020082/* returns the session this stream belongs to */
83static inline struct session *strm_sess(const struct stream *strm)
84{
85 return strm->sess;
86}
87
Willy Tarreaud0d8da92015-04-04 02:10:38 +020088/* returns the frontend this stream was initiated from */
89static inline struct proxy *strm_fe(const struct stream *strm)
90{
91 return strm->sess->fe;
92}
93
94/* returns the listener this stream was initiated from */
95static inline struct listener *strm_li(const struct stream *strm)
96{
97 return strm->sess->listener;
98}
99
100/* returns a pointer to the origin of the session which created this stream */
101static inline enum obj_type *strm_orig(const struct stream *strm)
102{
103 return strm->sess->origin;
104}
105
Willy Tarreau87b09662015-04-03 00:22:06 +0200106/* Remove the refcount from the stream to the tracked counters, and clear the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200107 * pointer to ensure this is only performed once. The caller is responsible for
Willy Tarreaua68f7622015-09-21 17:48:24 +0200108 * ensuring that the pointer is valid first. We must be extremely careful not
109 * to touch the entries we inherited from the session.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200110 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200111static inline void stream_store_counters(struct stream *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200112{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200113 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100114 int i;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200115 struct stksess *ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200116
Willy Tarreaub4c84932013-07-23 19:15:30 +0200117 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200118 ts = stkctr_entry(&s->stkctr[i]);
119 if (!ts)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100120 continue;
Willy Tarreaua68f7622015-09-21 17:48:24 +0200121
122 if (stkctr_entry(&s->sess->stkctr[i]))
123 continue;
124
Emeric Brun819fc6f2017-06-13 19:37:32 +0200125 ptr = stktable_data_ptr(s->stkctr[i].table, ts, STKTABLE_DT_CONN_CUR);
126 if (ptr) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100127 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200128
Tim Duesterhus8b87c012019-01-04 00:11:59 +0100129 if (stktable_data_cast(ptr, conn_cur) > 0)
130 stktable_data_cast(ptr, conn_cur)--;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200131
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100132 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100133
134 /* If data was modified, we need to touch to re-schedule sync */
135 stktable_touch_local(s->stkctr[i].table, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200136 }
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100137 stkctr_set_entry(&s->stkctr[i], NULL);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200138 stksess_kill_if_expired(s->stkctr[i].table, ts, 1);
Willy Tarreau38285c12010-06-18 16:35:43 +0200139 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200140}
141
Willy Tarreau87b09662015-04-03 00:22:06 +0200142/* Remove the refcount from the stream counters tracked at the content level if
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200143 * any, and clear the pointer to ensure this is only performed once. The caller
Willy Tarreaua68f7622015-09-21 17:48:24 +0200144 * is responsible for ensuring that the pointer is valid first. We must be
145 * extremely careful not to touch the entries we inherited from the session.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200146 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200147static inline void stream_stop_content_counters(struct stream *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200148{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200149 struct stksess *ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200150 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100151 int i;
Willy Tarreaue3487932010-06-18 21:03:20 +0200152
Willy Tarreaub4c84932013-07-23 19:15:30 +0200153 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200154 ts = stkctr_entry(&s->stkctr[i]);
155 if (!ts)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100156 continue;
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200157
Willy Tarreaua68f7622015-09-21 17:48:24 +0200158 if (stkctr_entry(&s->sess->stkctr[i]))
159 continue;
160
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100161 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_CONTENT))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100162 continue;
163
Emeric Brun819fc6f2017-06-13 19:37:32 +0200164 ptr = stktable_data_ptr(s->stkctr[i].table, ts, STKTABLE_DT_CONN_CUR);
165 if (ptr) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100166 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200167
Tim Duesterhus8b87c012019-01-04 00:11:59 +0100168 if (stktable_data_cast(ptr, conn_cur) > 0)
169 stktable_data_cast(ptr, conn_cur)--;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200170
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100171 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100172
173 /* If data was modified, we need to touch to re-schedule sync */
174 stktable_touch_local(s->stkctr[i].table, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200175 }
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100176 stkctr_set_entry(&s->stkctr[i], NULL);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200177 stksess_kill_if_expired(s->stkctr[i].table, ts, 1);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200178 }
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200179}
Willy Tarreaue3487932010-06-18 21:03:20 +0200180
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200181/* Increase total and concurrent connection count for stick entry <ts> of table
182 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
183 * pointers, and for calling this only once per connection.
184 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200185static inline void stream_start_counters(struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200186{
187 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200188
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100189 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200190
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200191 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
192 if (ptr)
193 stktable_data_cast(ptr, conn_cur)++;
194
195 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
196 if (ptr)
197 stktable_data_cast(ptr, conn_cnt)++;
198
199 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
200 if (ptr)
201 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
202 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
203 if (tick_isset(t->expire))
204 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
Emeric Brun819fc6f2017-06-13 19:37:32 +0200205
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100206 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100207
208 /* If data was modified, we need to touch to re-schedule sync */
209 stktable_touch_local(t, ts, 0);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200210}
211
Willy Tarreau87b09662015-04-03 00:22:06 +0200212/* Enable tracking of stream counters as <stkctr> on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200213 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
214 * are performed to ensure the state can still change.
215 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200216static inline void stream_track_stkctr(struct stkctr *ctr, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200217{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200218 /* Why this test ???? */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100219 if (stkctr_entry(ctr))
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200220 return;
221
Willy Tarreau20d46a52012-12-09 15:55:40 +0100222 ctr->table = t;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100223 stkctr_set_entry(ctr, ts);
Willy Tarreau87b09662015-04-03 00:22:06 +0200224 stream_start_counters(t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200225}
226
Willy Tarreauda7ff642010-06-23 11:44:09 +0200227/* Increase the number of cumulated HTTP requests in the tracked counters */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200228static inline void stream_inc_http_req_ctr(struct stream *s)
Willy Tarreauda7ff642010-06-23 11:44:09 +0200229{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200230 struct stksess *ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200231 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100232 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200233
Willy Tarreaub4c84932013-07-23 19:15:30 +0200234 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200235 struct stkctr *stkctr = &s->stkctr[i];
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200236
Emeric Brun819fc6f2017-06-13 19:37:32 +0200237 ts = stkctr_entry(stkctr);
238 if (!ts) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200239 stkctr = &s->sess->stkctr[i];
Emeric Brun819fc6f2017-06-13 19:37:32 +0200240 ts = stkctr_entry(stkctr);
241 if (!ts)
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200242 continue;
243 }
244
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100245 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200246
247 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200248 if (ptr)
249 stktable_data_cast(ptr, http_req_cnt)++;
250
Emeric Brun819fc6f2017-06-13 19:37:32 +0200251 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200252 if (ptr)
253 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200254 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200255
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100256 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100257
258 /* If data was modified, we need to touch to re-schedule sync */
259 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200260 }
261}
262
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200263/* Increase the number of cumulated HTTP requests in the backend's tracked
264 * counters. We don't look up the session since it cannot happen in the bakcend.
265 */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200266static inline void stream_inc_be_http_req_ctr(struct stream *s)
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100267{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200268 struct stksess *ts;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100269 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100270 int i;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100271
Willy Tarreaub4c84932013-07-23 19:15:30 +0200272 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200273 struct stkctr *stkctr = &s->stkctr[i];
274
Emeric Brun819fc6f2017-06-13 19:37:32 +0200275 ts = stkctr_entry(stkctr);
276 if (!ts)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100277 continue;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100278
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100279 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_BACKEND))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100280 continue;
281
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100282 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200283
284 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100285 if (ptr)
286 stktable_data_cast(ptr, http_req_cnt)++;
287
Emeric Brun819fc6f2017-06-13 19:37:32 +0200288 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100289 if (ptr)
290 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200291 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200292
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100293 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100294
295 /* If data was modified, we need to touch to re-schedule sync */
296 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100297 }
298}
299
Willy Tarreauda7ff642010-06-23 11:44:09 +0200300/* Increase the number of cumulated failed HTTP requests in the tracked
301 * counters. Only 4xx requests should be counted here so that we can
302 * distinguish between errors caused by client behaviour and other ones.
303 * Note that even 404 are interesting because they're generally caused by
304 * vulnerability scans.
305 */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200306static inline void stream_inc_http_err_ctr(struct stream *s)
Willy Tarreauda7ff642010-06-23 11:44:09 +0200307{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200308 struct stksess *ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200309 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100310 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200311
Willy Tarreaub4c84932013-07-23 19:15:30 +0200312 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200313 struct stkctr *stkctr = &s->stkctr[i];
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200314
Emeric Brun819fc6f2017-06-13 19:37:32 +0200315 ts = stkctr_entry(stkctr);
316 if (!ts) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200317 stkctr = &s->sess->stkctr[i];
Emeric Brun819fc6f2017-06-13 19:37:32 +0200318 ts = stkctr_entry(stkctr);
319 if (!ts)
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200320 continue;
321 }
322
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100323 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200324
325 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200326 if (ptr)
327 stktable_data_cast(ptr, http_err_cnt)++;
328
Emeric Brun819fc6f2017-06-13 19:37:32 +0200329 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200330 if (ptr)
331 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200332 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200333
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100334 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100335
336 /* If data was modified, we need to touch to re-schedule sync */
337 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200338 }
339}
340
Willy Tarreau0e492e22019-04-15 21:25:03 +0200341static inline void __stream_add_srv_conn(struct stream *sess, struct server *srv)
Simon Hormanaf514952011-06-21 14:34:57 +0900342{
343 sess->srv_conn = srv;
344 LIST_ADD(&srv->actconns, &sess->by_srv);
Willy Tarreau103e5662017-11-26 18:48:14 +0100345}
346
Willy Tarreau0e492e22019-04-15 21:25:03 +0200347static inline void stream_add_srv_conn(struct stream *sess, struct server *srv)
Willy Tarreau103e5662017-11-26 18:48:14 +0100348{
349 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
350 __stream_add_srv_conn(sess, srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100351 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Simon Hormanaf514952011-06-21 14:34:57 +0900352}
353
Willy Tarreau0e492e22019-04-15 21:25:03 +0200354static inline void stream_del_srv_conn(struct stream *sess)
Simon Hormanaf514952011-06-21 14:34:57 +0900355{
Christopher Faulet29f77e82017-06-08 14:04:45 +0200356 struct server *srv = sess->srv_conn;
357
358 if (!srv)
Simon Hormanaf514952011-06-21 14:34:57 +0900359 return;
360
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100361 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Simon Hormanaf514952011-06-21 14:34:57 +0900362 sess->srv_conn = NULL;
363 LIST_DEL(&sess->by_srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100364 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Simon Hormanaf514952011-06-21 14:34:57 +0900365}
366
Willy Tarreau0e492e22019-04-15 21:25:03 +0200367static inline void stream_init_srv_conn(struct stream *sess)
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200368{
369 sess->srv_conn = NULL;
370 LIST_INIT(&sess->by_srv);
371}
372
Olivier Houchard4bd58672019-07-12 16:16:59 +0200373static inline void stream_choose_redispatch(struct stream *s)
374{
375 struct stream_interface *si = &s->si[1];
376
377 /* If the "redispatch" option is set on the backend, we are allowed to
378 * retry on another server. By default this redispatch occurs on the
379 * last retry, but if configured we allow redispatches to occur on
380 * configurable intervals, e.g. on every retry. In order to achieve this,
381 * we must mark the stream unassigned, and eventually clear the DIRECT
382 * bit to ignore any persistence cookie. We won't count a retry nor a
383 * redispatch yet, because this will depend on what server is selected.
384 * If the connection is not persistent, the balancing algorithm is not
385 * determinist (round robin) and there is more than one active server,
386 * we accept to perform an immediate redispatch without waiting since
387 * we don't care about this particular server.
388 */
389 if (objt_server(s->target) &&
390 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
391 ((__objt_server(s->target)->cur_state < SRV_ST_RUNNING) ||
392 (((s->be->redispatch_after > 0) &&
393 ((s->be->conn_retries - si->conn_retries) %
394 s->be->redispatch_after == 0)) ||
395 ((s->be->redispatch_after < 0) &&
396 ((s->be->conn_retries - si->conn_retries) %
397 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
398 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
399 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
400 sess_change_server(s, NULL);
401 if (may_dequeue_tasks(objt_server(s->target), s->be))
402 process_srv_queue(objt_server(s->target));
403
404 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
405 si->state = SI_ST_REQ;
406 } else {
407 if (objt_server(s->target))
408 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
409 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
410 si->state = SI_ST_ASS;
411 }
412
413}
414
Thierry FOURNIER5a363e72015-09-27 19:29:33 +0200415void service_keywords_register(struct action_kw_list *kw_list);
Willy Tarreau679bba12019-03-19 08:08:10 +0100416void list_services(FILE *out);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +0200417
Willy Tarreau87b09662015-04-03 00:22:06 +0200418#endif /* _PROTO_STREAM_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200419
Willy Tarreaubaaee002006-06-26 02:48:02 +0200420/*
421 * Local variables:
422 * c-indent-level: 8
423 * c-basic-offset: 8
424 * End:
425 */