blob: e94e32bd4ddb9a8ca534a8f5d6d553cb60994e30 [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>
Willy TARREAU3dc06442006-06-15 21:48:13 +020035
Christopher Fauletc8b246f2019-05-14 22:05:28 +020036#define IS_HTX_STRM(strm) ((strm)->flags & SF_HTX)
37
Willy Tarreaubafbe012017-11-24 17:34:44 +010038extern struct pool_head *pool_head_stream;
Willy Tarreau87b09662015-04-03 00:22:06 +020039extern struct list streams;
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020040
Willy Tarreaubc174aa2012-11-19 16:10:32 +010041extern struct data_cb sess_conn_cb;
42
Willy Tarreau87787ac2017-08-28 16:22:54 +020043struct stream *stream_new(struct session *sess, enum obj_type *origin);
Olivier Houchard9aaf7782017-09-13 18:30:23 +020044int stream_create_from_cs(struct conn_stream *cs);
Willy Tarreaubaaee002006-06-26 02:48:02 +020045
Willy Tarreaue7dff022015-04-03 01:14:29 +020046/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +020047void stream_shutdown(struct stream *stream, int why);
Willy Tarreau5484d582019-05-22 09:33:03 +020048void stream_dump(struct buffer *buf, const struct stream *s, const char *pfx, char eol);
Willy Tarreau71c07ac2019-04-25 19:08:48 +020049void stream_dump_and_crash(enum obj_type *obj, int rate);
Simon Hormandec5be42011-06-08 09:19:07 +090050
Willy Tarreau87b09662015-04-03 00:22:06 +020051void stream_process_counters(struct stream *s);
52void sess_change_server(struct stream *sess, struct server *newsrv);
Olivier Houchard9f6af332018-05-25 14:04:04 +020053struct task *process_stream(struct task *t, void *context, unsigned short state);
Willy Tarreau87b09662015-04-03 00:22:06 +020054void default_srv_error(struct stream *s, struct stream_interface *si);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020055int parse_track_counters(char **args, int *arg,
56 int section_type, struct proxy *curpx,
57 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +020058 struct proxy *defpx, char **err);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020059
Willy Tarreau87b09662015-04-03 00:22:06 +020060/* Update the stream's backend and server time stats */
61void stream_update_time_stats(struct stream *s);
Willy Tarreau87b09662015-04-03 00:22:06 +020062void stream_release_buffers(struct stream *s);
Willy Tarreaub882dd82018-11-06 15:50:21 +010063int stream_buf_available(void *arg);
Willy Tarreau4bfc5802014-06-17 12:19:18 +020064
Willy Tarreaub1ec8c42015-04-03 13:53:24 +020065/* returns the session this stream belongs to */
66static inline struct session *strm_sess(const struct stream *strm)
67{
68 return strm->sess;
69}
70
Willy Tarreaud0d8da92015-04-04 02:10:38 +020071/* returns the frontend this stream was initiated from */
72static inline struct proxy *strm_fe(const struct stream *strm)
73{
74 return strm->sess->fe;
75}
76
77/* returns the listener this stream was initiated from */
78static inline struct listener *strm_li(const struct stream *strm)
79{
80 return strm->sess->listener;
81}
82
83/* returns a pointer to the origin of the session which created this stream */
84static inline enum obj_type *strm_orig(const struct stream *strm)
85{
86 return strm->sess->origin;
87}
88
Willy Tarreau87b09662015-04-03 00:22:06 +020089/* Remove the refcount from the stream to the tracked counters, and clear the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020090 * pointer to ensure this is only performed once. The caller is responsible for
Willy Tarreaua68f7622015-09-21 17:48:24 +020091 * ensuring that the pointer is valid first. We must be extremely careful not
92 * to touch the entries we inherited from the session.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020093 */
Willy Tarreau87b09662015-04-03 00:22:06 +020094static inline void stream_store_counters(struct stream *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020095{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020096 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +010097 int i;
Emeric Brun819fc6f2017-06-13 19:37:32 +020098 struct stksess *ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +020099
Willy Tarreaub4c84932013-07-23 19:15:30 +0200100 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200101 ts = stkctr_entry(&s->stkctr[i]);
102 if (!ts)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100103 continue;
Willy Tarreaua68f7622015-09-21 17:48:24 +0200104
105 if (stkctr_entry(&s->sess->stkctr[i]))
106 continue;
107
Emeric Brun819fc6f2017-06-13 19:37:32 +0200108 ptr = stktable_data_ptr(s->stkctr[i].table, ts, STKTABLE_DT_CONN_CUR);
109 if (ptr) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100110 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200111
Tim Duesterhus8b87c012019-01-04 00:11:59 +0100112 if (stktable_data_cast(ptr, conn_cur) > 0)
113 stktable_data_cast(ptr, conn_cur)--;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200114
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100115 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100116
117 /* If data was modified, we need to touch to re-schedule sync */
118 stktable_touch_local(s->stkctr[i].table, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200119 }
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100120 stkctr_set_entry(&s->stkctr[i], NULL);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200121 stksess_kill_if_expired(s->stkctr[i].table, ts, 1);
Willy Tarreau38285c12010-06-18 16:35:43 +0200122 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200123}
124
Willy Tarreau87b09662015-04-03 00:22:06 +0200125/* Remove the refcount from the stream counters tracked at the content level if
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200126 * any, and clear the pointer to ensure this is only performed once. The caller
Willy Tarreaua68f7622015-09-21 17:48:24 +0200127 * is responsible for ensuring that the pointer is valid first. We must be
128 * extremely careful not to touch the entries we inherited from the session.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200129 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200130static inline void stream_stop_content_counters(struct stream *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200131{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200132 struct stksess *ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200133 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100134 int i;
Willy Tarreaue3487932010-06-18 21:03:20 +0200135
Willy Tarreaub4c84932013-07-23 19:15:30 +0200136 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200137 ts = stkctr_entry(&s->stkctr[i]);
138 if (!ts)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100139 continue;
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200140
Willy Tarreaua68f7622015-09-21 17:48:24 +0200141 if (stkctr_entry(&s->sess->stkctr[i]))
142 continue;
143
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100144 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_CONTENT))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100145 continue;
146
Emeric Brun819fc6f2017-06-13 19:37:32 +0200147 ptr = stktable_data_ptr(s->stkctr[i].table, ts, STKTABLE_DT_CONN_CUR);
148 if (ptr) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100149 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200150
Tim Duesterhus8b87c012019-01-04 00:11:59 +0100151 if (stktable_data_cast(ptr, conn_cur) > 0)
152 stktable_data_cast(ptr, conn_cur)--;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200153
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100154 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100155
156 /* If data was modified, we need to touch to re-schedule sync */
157 stktable_touch_local(s->stkctr[i].table, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200158 }
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100159 stkctr_set_entry(&s->stkctr[i], NULL);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200160 stksess_kill_if_expired(s->stkctr[i].table, ts, 1);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200161 }
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200162}
Willy Tarreaue3487932010-06-18 21:03:20 +0200163
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200164/* Increase total and concurrent connection count for stick entry <ts> of table
165 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
166 * pointers, and for calling this only once per connection.
167 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200168static inline void stream_start_counters(struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200169{
170 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200171
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100172 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200173
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200174 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
175 if (ptr)
176 stktable_data_cast(ptr, conn_cur)++;
177
178 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
179 if (ptr)
180 stktable_data_cast(ptr, conn_cnt)++;
181
182 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
183 if (ptr)
184 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
185 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
186 if (tick_isset(t->expire))
187 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
Emeric Brun819fc6f2017-06-13 19:37:32 +0200188
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100189 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100190
191 /* If data was modified, we need to touch to re-schedule sync */
192 stktable_touch_local(t, ts, 0);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200193}
194
Willy Tarreau87b09662015-04-03 00:22:06 +0200195/* Enable tracking of stream counters as <stkctr> on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200196 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
197 * are performed to ensure the state can still change.
198 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200199static inline void stream_track_stkctr(struct stkctr *ctr, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200200{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200201 /* Why this test ???? */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100202 if (stkctr_entry(ctr))
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200203 return;
204
Willy Tarreau20d46a52012-12-09 15:55:40 +0100205 ctr->table = t;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100206 stkctr_set_entry(ctr, ts);
Willy Tarreau87b09662015-04-03 00:22:06 +0200207 stream_start_counters(t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200208}
209
Willy Tarreauda7ff642010-06-23 11:44:09 +0200210/* Increase the number of cumulated HTTP requests in the tracked counters */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200211static inline void stream_inc_http_req_ctr(struct stream *s)
Willy Tarreauda7ff642010-06-23 11:44:09 +0200212{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200213 struct stksess *ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200214 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100215 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200216
Willy Tarreaub4c84932013-07-23 19:15:30 +0200217 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200218 struct stkctr *stkctr = &s->stkctr[i];
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200219
Emeric Brun819fc6f2017-06-13 19:37:32 +0200220 ts = stkctr_entry(stkctr);
221 if (!ts) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200222 stkctr = &s->sess->stkctr[i];
Emeric Brun819fc6f2017-06-13 19:37:32 +0200223 ts = stkctr_entry(stkctr);
224 if (!ts)
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200225 continue;
226 }
227
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100228 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200229
230 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200231 if (ptr)
232 stktable_data_cast(ptr, http_req_cnt)++;
233
Emeric Brun819fc6f2017-06-13 19:37:32 +0200234 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200235 if (ptr)
236 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200237 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200238
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100239 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100240
241 /* If data was modified, we need to touch to re-schedule sync */
242 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200243 }
244}
245
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200246/* Increase the number of cumulated HTTP requests in the backend's tracked
247 * counters. We don't look up the session since it cannot happen in the bakcend.
248 */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200249static inline void stream_inc_be_http_req_ctr(struct stream *s)
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100250{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200251 struct stksess *ts;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100252 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100253 int i;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100254
Willy Tarreaub4c84932013-07-23 19:15:30 +0200255 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200256 struct stkctr *stkctr = &s->stkctr[i];
257
Emeric Brun819fc6f2017-06-13 19:37:32 +0200258 ts = stkctr_entry(stkctr);
259 if (!ts)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100260 continue;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100261
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100262 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_BACKEND))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100263 continue;
264
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100265 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200266
267 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100268 if (ptr)
269 stktable_data_cast(ptr, http_req_cnt)++;
270
Emeric Brun819fc6f2017-06-13 19:37:32 +0200271 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100272 if (ptr)
273 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200274 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200275
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100276 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100277
278 /* If data was modified, we need to touch to re-schedule sync */
279 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100280 }
281}
282
Willy Tarreauda7ff642010-06-23 11:44:09 +0200283/* Increase the number of cumulated failed HTTP requests in the tracked
284 * counters. Only 4xx requests should be counted here so that we can
285 * distinguish between errors caused by client behaviour and other ones.
286 * Note that even 404 are interesting because they're generally caused by
287 * vulnerability scans.
288 */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200289static inline void stream_inc_http_err_ctr(struct stream *s)
Willy Tarreauda7ff642010-06-23 11:44:09 +0200290{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200291 struct stksess *ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200292 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100293 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200294
Willy Tarreaub4c84932013-07-23 19:15:30 +0200295 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200296 struct stkctr *stkctr = &s->stkctr[i];
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200297
Emeric Brun819fc6f2017-06-13 19:37:32 +0200298 ts = stkctr_entry(stkctr);
299 if (!ts) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200300 stkctr = &s->sess->stkctr[i];
Emeric Brun819fc6f2017-06-13 19:37:32 +0200301 ts = stkctr_entry(stkctr);
302 if (!ts)
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200303 continue;
304 }
305
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100306 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200307
308 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200309 if (ptr)
310 stktable_data_cast(ptr, http_err_cnt)++;
311
Emeric Brun819fc6f2017-06-13 19:37:32 +0200312 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200313 if (ptr)
314 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200315 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200316
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100317 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100318
319 /* If data was modified, we need to touch to re-schedule sync */
320 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200321 }
322}
323
Willy Tarreau0e492e22019-04-15 21:25:03 +0200324static inline void __stream_add_srv_conn(struct stream *sess, struct server *srv)
Simon Hormanaf514952011-06-21 14:34:57 +0900325{
326 sess->srv_conn = srv;
327 LIST_ADD(&srv->actconns, &sess->by_srv);
Willy Tarreau103e5662017-11-26 18:48:14 +0100328}
329
Willy Tarreau0e492e22019-04-15 21:25:03 +0200330static inline void stream_add_srv_conn(struct stream *sess, struct server *srv)
Willy Tarreau103e5662017-11-26 18:48:14 +0100331{
332 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
333 __stream_add_srv_conn(sess, srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100334 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Simon Hormanaf514952011-06-21 14:34:57 +0900335}
336
Willy Tarreau0e492e22019-04-15 21:25:03 +0200337static inline void stream_del_srv_conn(struct stream *sess)
Simon Hormanaf514952011-06-21 14:34:57 +0900338{
Christopher Faulet29f77e82017-06-08 14:04:45 +0200339 struct server *srv = sess->srv_conn;
340
341 if (!srv)
Simon Hormanaf514952011-06-21 14:34:57 +0900342 return;
343
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100344 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Simon Hormanaf514952011-06-21 14:34:57 +0900345 sess->srv_conn = NULL;
346 LIST_DEL(&sess->by_srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100347 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Simon Hormanaf514952011-06-21 14:34:57 +0900348}
349
Willy Tarreau0e492e22019-04-15 21:25:03 +0200350static inline void stream_init_srv_conn(struct stream *sess)
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200351{
352 sess->srv_conn = NULL;
353 LIST_INIT(&sess->by_srv);
354}
355
Olivier Houchard4bd58672019-07-12 16:16:59 +0200356static inline void stream_choose_redispatch(struct stream *s)
357{
358 struct stream_interface *si = &s->si[1];
359
360 /* If the "redispatch" option is set on the backend, we are allowed to
361 * retry on another server. By default this redispatch occurs on the
362 * last retry, but if configured we allow redispatches to occur on
363 * configurable intervals, e.g. on every retry. In order to achieve this,
364 * we must mark the stream unassigned, and eventually clear the DIRECT
365 * bit to ignore any persistence cookie. We won't count a retry nor a
366 * redispatch yet, because this will depend on what server is selected.
367 * If the connection is not persistent, the balancing algorithm is not
368 * determinist (round robin) and there is more than one active server,
369 * we accept to perform an immediate redispatch without waiting since
370 * we don't care about this particular server.
371 */
372 if (objt_server(s->target) &&
373 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
374 ((__objt_server(s->target)->cur_state < SRV_ST_RUNNING) ||
375 (((s->be->redispatch_after > 0) &&
376 ((s->be->conn_retries - si->conn_retries) %
377 s->be->redispatch_after == 0)) ||
378 ((s->be->redispatch_after < 0) &&
379 ((s->be->conn_retries - si->conn_retries) %
380 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
381 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
382 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
383 sess_change_server(s, NULL);
384 if (may_dequeue_tasks(objt_server(s->target), s->be))
385 process_srv_queue(objt_server(s->target));
386
387 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
388 si->state = SI_ST_REQ;
389 } else {
390 if (objt_server(s->target))
391 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
392 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
393 si->state = SI_ST_ASS;
394 }
395
396}
397
Thierry FOURNIER5a363e72015-09-27 19:29:33 +0200398void service_keywords_register(struct action_kw_list *kw_list);
Willy Tarreau679bba12019-03-19 08:08:10 +0100399void list_services(FILE *out);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +0200400
Willy Tarreau87b09662015-04-03 00:22:06 +0200401#endif /* _PROTO_STREAM_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200402
Willy Tarreaubaaee002006-06-26 02:48:02 +0200403/*
404 * Local variables:
405 * c-indent-level: 8
406 * c-basic-offset: 8
407 * End:
408 */