blob: 80c56ef8889f97c342abfe90b941f8a5955a4423 [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 Tarreau4c7e4b72020-05-27 12:58:42 +020025#include <haproxy/api.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020026#include <haproxy/pool.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 Tarreau66347942020-06-01 12:18:08 +020030#include <haproxy/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;
Tim Duesterhus127a74d2020-02-28 15:13:33 +010056extern struct pool_head *pool_head_uniqueid;
Willy Tarreau87b09662015-04-03 00:22:06 +020057extern struct list streams;
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020058
Willy Tarreaubc174aa2012-11-19 16:10:32 +010059extern struct data_cb sess_conn_cb;
60
Willy Tarreau87787ac2017-08-28 16:22:54 +020061struct stream *stream_new(struct session *sess, enum obj_type *origin);
Olivier Houchard9aaf7782017-09-13 18:30:23 +020062int stream_create_from_cs(struct conn_stream *cs);
Willy Tarreaubaaee002006-06-26 02:48:02 +020063
Willy Tarreaue7dff022015-04-03 01:14:29 +020064/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +020065void stream_shutdown(struct stream *stream, int why);
Willy Tarreau5484d582019-05-22 09:33:03 +020066void stream_dump(struct buffer *buf, const struct stream *s, const char *pfx, char eol);
Willy Tarreau71c07ac2019-04-25 19:08:48 +020067void stream_dump_and_crash(enum obj_type *obj, int rate);
Simon Hormandec5be42011-06-08 09:19:07 +090068
Tim Duesterhusa17e6622020-03-05 20:19:02 +010069struct ist stream_generate_unique_id(struct stream *strm, struct list *format);
Tim Duesterhus127a74d2020-02-28 15:13:33 +010070
Willy Tarreau87b09662015-04-03 00:22:06 +020071void stream_process_counters(struct stream *s);
72void sess_change_server(struct stream *sess, struct server *newsrv);
Olivier Houchard9f6af332018-05-25 14:04:04 +020073struct task *process_stream(struct task *t, void *context, unsigned short state);
Willy Tarreau87b09662015-04-03 00:22:06 +020074void default_srv_error(struct stream *s, struct stream_interface *si);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020075int parse_track_counters(char **args, int *arg,
76 int section_type, struct proxy *curpx,
77 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +020078 struct proxy *defpx, char **err);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020079
Willy Tarreau87b09662015-04-03 00:22:06 +020080/* Update the stream's backend and server time stats */
81void stream_update_time_stats(struct stream *s);
Willy Tarreau87b09662015-04-03 00:22:06 +020082void stream_release_buffers(struct stream *s);
Willy Tarreaub882dd82018-11-06 15:50:21 +010083int stream_buf_available(void *arg);
Willy Tarreau4bfc5802014-06-17 12:19:18 +020084
Willy Tarreaub1ec8c42015-04-03 13:53:24 +020085/* returns the session this stream belongs to */
86static inline struct session *strm_sess(const struct stream *strm)
87{
88 return strm->sess;
89}
90
Willy Tarreaud0d8da92015-04-04 02:10:38 +020091/* returns the frontend this stream was initiated from */
92static inline struct proxy *strm_fe(const struct stream *strm)
93{
94 return strm->sess->fe;
95}
96
97/* returns the listener this stream was initiated from */
98static inline struct listener *strm_li(const struct stream *strm)
99{
100 return strm->sess->listener;
101}
102
103/* returns a pointer to the origin of the session which created this stream */
104static inline enum obj_type *strm_orig(const struct stream *strm)
105{
106 return strm->sess->origin;
107}
108
Willy Tarreau87b09662015-04-03 00:22:06 +0200109/* Remove the refcount from the stream to the tracked counters, and clear the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200110 * pointer to ensure this is only performed once. The caller is responsible for
Willy Tarreaua68f7622015-09-21 17:48:24 +0200111 * ensuring that the pointer is valid first. We must be extremely careful not
112 * to touch the entries we inherited from the session.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200113 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200114static inline void stream_store_counters(struct stream *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200115{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200116 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100117 int i;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200118 struct stksess *ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200119
Willy Tarreaub4c84932013-07-23 19:15:30 +0200120 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200121 ts = stkctr_entry(&s->stkctr[i]);
122 if (!ts)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100123 continue;
Willy Tarreaua68f7622015-09-21 17:48:24 +0200124
125 if (stkctr_entry(&s->sess->stkctr[i]))
126 continue;
127
Emeric Brun819fc6f2017-06-13 19:37:32 +0200128 ptr = stktable_data_ptr(s->stkctr[i].table, ts, STKTABLE_DT_CONN_CUR);
129 if (ptr) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100130 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200131
Tim Duesterhus8b87c012019-01-04 00:11:59 +0100132 if (stktable_data_cast(ptr, conn_cur) > 0)
133 stktable_data_cast(ptr, conn_cur)--;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200134
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100135 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100136
137 /* If data was modified, we need to touch to re-schedule sync */
138 stktable_touch_local(s->stkctr[i].table, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200139 }
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100140 stkctr_set_entry(&s->stkctr[i], NULL);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200141 stksess_kill_if_expired(s->stkctr[i].table, ts, 1);
Willy Tarreau38285c12010-06-18 16:35:43 +0200142 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200143}
144
Willy Tarreau87b09662015-04-03 00:22:06 +0200145/* Remove the refcount from the stream counters tracked at the content level if
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200146 * any, and clear the pointer to ensure this is only performed once. The caller
Willy Tarreaua68f7622015-09-21 17:48:24 +0200147 * is responsible for ensuring that the pointer is valid first. We must be
148 * extremely careful not to touch the entries we inherited from the session.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200149 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200150static inline void stream_stop_content_counters(struct stream *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200151{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200152 struct stksess *ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200153 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100154 int i;
Willy Tarreaue3487932010-06-18 21:03:20 +0200155
Willy Tarreaub4c84932013-07-23 19:15:30 +0200156 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200157 ts = stkctr_entry(&s->stkctr[i]);
158 if (!ts)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100159 continue;
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200160
Willy Tarreaua68f7622015-09-21 17:48:24 +0200161 if (stkctr_entry(&s->sess->stkctr[i]))
162 continue;
163
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100164 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_CONTENT))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100165 continue;
166
Emeric Brun819fc6f2017-06-13 19:37:32 +0200167 ptr = stktable_data_ptr(s->stkctr[i].table, ts, STKTABLE_DT_CONN_CUR);
168 if (ptr) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100169 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200170
Tim Duesterhus8b87c012019-01-04 00:11:59 +0100171 if (stktable_data_cast(ptr, conn_cur) > 0)
172 stktable_data_cast(ptr, conn_cur)--;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200173
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100174 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100175
176 /* If data was modified, we need to touch to re-schedule sync */
177 stktable_touch_local(s->stkctr[i].table, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200178 }
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100179 stkctr_set_entry(&s->stkctr[i], NULL);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200180 stksess_kill_if_expired(s->stkctr[i].table, ts, 1);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200181 }
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200182}
Willy Tarreaue3487932010-06-18 21:03:20 +0200183
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200184/* Increase total and concurrent connection count for stick entry <ts> of table
185 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
186 * pointers, and for calling this only once per connection.
187 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200188static inline void stream_start_counters(struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200189{
190 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200191
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100192 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200193
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200194 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
195 if (ptr)
196 stktable_data_cast(ptr, conn_cur)++;
197
198 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
199 if (ptr)
200 stktable_data_cast(ptr, conn_cnt)++;
201
202 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
203 if (ptr)
204 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
205 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
206 if (tick_isset(t->expire))
207 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
Emeric Brun819fc6f2017-06-13 19:37:32 +0200208
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100209 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100210
211 /* If data was modified, we need to touch to re-schedule sync */
212 stktable_touch_local(t, ts, 0);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200213}
214
Willy Tarreau87b09662015-04-03 00:22:06 +0200215/* Enable tracking of stream counters as <stkctr> on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200216 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
217 * are performed to ensure the state can still change.
218 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200219static inline void stream_track_stkctr(struct stkctr *ctr, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200220{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200221 /* Why this test ???? */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100222 if (stkctr_entry(ctr))
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200223 return;
224
Willy Tarreau20d46a52012-12-09 15:55:40 +0100225 ctr->table = t;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100226 stkctr_set_entry(ctr, ts);
Willy Tarreau87b09662015-04-03 00:22:06 +0200227 stream_start_counters(t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200228}
229
Willy Tarreauda7ff642010-06-23 11:44:09 +0200230/* Increase the number of cumulated HTTP requests in the tracked counters */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200231static inline void stream_inc_http_req_ctr(struct stream *s)
Willy Tarreauda7ff642010-06-23 11:44:09 +0200232{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200233 struct stksess *ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200234 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100235 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200236
Willy Tarreaub4c84932013-07-23 19:15:30 +0200237 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200238 struct stkctr *stkctr = &s->stkctr[i];
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200239
Emeric Brun819fc6f2017-06-13 19:37:32 +0200240 ts = stkctr_entry(stkctr);
241 if (!ts) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200242 stkctr = &s->sess->stkctr[i];
Emeric Brun819fc6f2017-06-13 19:37:32 +0200243 ts = stkctr_entry(stkctr);
244 if (!ts)
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200245 continue;
246 }
247
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100248 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200249
250 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200251 if (ptr)
252 stktable_data_cast(ptr, http_req_cnt)++;
253
Emeric Brun819fc6f2017-06-13 19:37:32 +0200254 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200255 if (ptr)
256 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200257 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200258
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100259 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100260
261 /* If data was modified, we need to touch to re-schedule sync */
262 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200263 }
264}
265
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200266/* Increase the number of cumulated HTTP requests in the backend's tracked
267 * counters. We don't look up the session since it cannot happen in the bakcend.
268 */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200269static inline void stream_inc_be_http_req_ctr(struct stream *s)
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100270{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200271 struct stksess *ts;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100272 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100273 int i;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100274
Willy Tarreaub4c84932013-07-23 19:15:30 +0200275 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200276 struct stkctr *stkctr = &s->stkctr[i];
277
Emeric Brun819fc6f2017-06-13 19:37:32 +0200278 ts = stkctr_entry(stkctr);
279 if (!ts)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100280 continue;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100281
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100282 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_BACKEND))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100283 continue;
284
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100285 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200286
287 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100288 if (ptr)
289 stktable_data_cast(ptr, http_req_cnt)++;
290
Emeric Brun819fc6f2017-06-13 19:37:32 +0200291 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100292 if (ptr)
293 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200294 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200295
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100296 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100297
298 /* If data was modified, we need to touch to re-schedule sync */
299 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100300 }
301}
302
Willy Tarreauda7ff642010-06-23 11:44:09 +0200303/* Increase the number of cumulated failed HTTP requests in the tracked
304 * counters. Only 4xx requests should be counted here so that we can
305 * distinguish between errors caused by client behaviour and other ones.
306 * Note that even 404 are interesting because they're generally caused by
307 * vulnerability scans.
308 */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200309static inline void stream_inc_http_err_ctr(struct stream *s)
Willy Tarreauda7ff642010-06-23 11:44:09 +0200310{
Emeric Brun819fc6f2017-06-13 19:37:32 +0200311 struct stksess *ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200312 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100313 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200314
Willy Tarreaub4c84932013-07-23 19:15:30 +0200315 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200316 struct stkctr *stkctr = &s->stkctr[i];
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200317
Emeric Brun819fc6f2017-06-13 19:37:32 +0200318 ts = stkctr_entry(stkctr);
319 if (!ts) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200320 stkctr = &s->sess->stkctr[i];
Emeric Brun819fc6f2017-06-13 19:37:32 +0200321 ts = stkctr_entry(stkctr);
322 if (!ts)
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200323 continue;
324 }
325
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100326 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200327
328 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200329 if (ptr)
330 stktable_data_cast(ptr, http_err_cnt)++;
331
Emeric Brun819fc6f2017-06-13 19:37:32 +0200332 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200333 if (ptr)
334 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200335 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200336
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100337 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +0100338
339 /* If data was modified, we need to touch to re-schedule sync */
340 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200341 }
342}
343
Willy Tarreau0e492e22019-04-15 21:25:03 +0200344static inline void __stream_add_srv_conn(struct stream *sess, struct server *srv)
Simon Hormanaf514952011-06-21 14:34:57 +0900345{
346 sess->srv_conn = srv;
347 LIST_ADD(&srv->actconns, &sess->by_srv);
Willy Tarreau103e5662017-11-26 18:48:14 +0100348}
349
Willy Tarreau0e492e22019-04-15 21:25:03 +0200350static inline void stream_add_srv_conn(struct stream *sess, struct server *srv)
Willy Tarreau103e5662017-11-26 18:48:14 +0100351{
352 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
353 __stream_add_srv_conn(sess, srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100354 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Simon Hormanaf514952011-06-21 14:34:57 +0900355}
356
Willy Tarreau0e492e22019-04-15 21:25:03 +0200357static inline void stream_del_srv_conn(struct stream *sess)
Simon Hormanaf514952011-06-21 14:34:57 +0900358{
Christopher Faulet29f77e82017-06-08 14:04:45 +0200359 struct server *srv = sess->srv_conn;
360
361 if (!srv)
Simon Hormanaf514952011-06-21 14:34:57 +0900362 return;
363
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100364 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Simon Hormanaf514952011-06-21 14:34:57 +0900365 sess->srv_conn = NULL;
366 LIST_DEL(&sess->by_srv);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100367 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Simon Hormanaf514952011-06-21 14:34:57 +0900368}
369
Willy Tarreau0e492e22019-04-15 21:25:03 +0200370static inline void stream_init_srv_conn(struct stream *sess)
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200371{
372 sess->srv_conn = NULL;
373 LIST_INIT(&sess->by_srv);
374}
375
Olivier Houchard4bd58672019-07-12 16:16:59 +0200376static inline void stream_choose_redispatch(struct stream *s)
377{
378 struct stream_interface *si = &s->si[1];
379
380 /* If the "redispatch" option is set on the backend, we are allowed to
381 * retry on another server. By default this redispatch occurs on the
382 * last retry, but if configured we allow redispatches to occur on
383 * configurable intervals, e.g. on every retry. In order to achieve this,
384 * we must mark the stream unassigned, and eventually clear the DIRECT
385 * bit to ignore any persistence cookie. We won't count a retry nor a
386 * redispatch yet, because this will depend on what server is selected.
387 * If the connection is not persistent, the balancing algorithm is not
388 * determinist (round robin) and there is more than one active server,
389 * we accept to perform an immediate redispatch without waiting since
390 * we don't care about this particular server.
391 */
392 if (objt_server(s->target) &&
393 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
394 ((__objt_server(s->target)->cur_state < SRV_ST_RUNNING) ||
395 (((s->be->redispatch_after > 0) &&
396 ((s->be->conn_retries - si->conn_retries) %
397 s->be->redispatch_after == 0)) ||
398 ((s->be->redispatch_after < 0) &&
399 ((s->be->conn_retries - si->conn_retries) %
400 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
401 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
402 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
403 sess_change_server(s, NULL);
404 if (may_dequeue_tasks(objt_server(s->target), s->be))
405 process_srv_queue(objt_server(s->target));
406
407 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
408 si->state = SI_ST_REQ;
409 } else {
410 if (objt_server(s->target))
411 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
412 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
413 si->state = SI_ST_ASS;
414 }
415
416}
417
Thierry FOURNIER5a363e72015-09-27 19:29:33 +0200418void service_keywords_register(struct action_kw_list *kw_list);
Willy Tarreau679bba12019-03-19 08:08:10 +0100419void list_services(FILE *out);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +0200420
Willy Tarreau87b09662015-04-03 00:22:06 +0200421#endif /* _PROTO_STREAM_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200422
Willy Tarreaubaaee002006-06-26 02:48:02 +0200423/*
424 * Local variables:
425 * c-indent-level: 8
426 * c-basic-offset: 8
427 * End:
428 */