Willy TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 2 | * include/haproxy/stream.h |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3 | * This file defines everything related to streams. |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 4 | * |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 5 | * Copyright (C) 2000-2020 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 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 TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 22 | #ifndef _HAPROXY_STREAM_H |
| 23 | #define _HAPROXY_STREAM_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 25 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 26 | #include <haproxy/api.h> |
Willy Tarreau | 0f6ffd6 | 2020-06-03 19:33:00 +0200 | [diff] [blame] | 27 | #include <haproxy/fd.h> |
Willy Tarreau | 6634794 | 2020-06-01 12:18:08 +0200 | [diff] [blame] | 28 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 29 | #include <haproxy/obj_type.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/pool-t.h> |
Willy Tarreau | a55c454 | 2020-06-04 22:59:39 +0200 | [diff] [blame] | 31 | #include <haproxy/queue.h> |
Willy Tarreau | 872f2ea | 2020-06-04 18:46:44 +0200 | [diff] [blame] | 32 | #include <haproxy/stick_table.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 33 | #include <haproxy/stream-t.h> |
| 34 | #include <haproxy/task-t.h> |
| 35 | #include <haproxy/trace-t.h> |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 36 | |
| 37 | extern 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 TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 52 | |
Christopher Faulet | c8b246f | 2019-05-14 22:05:28 +0200 | [diff] [blame] | 53 | #define IS_HTX_STRM(strm) ((strm)->flags & SF_HTX) |
| 54 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 55 | extern struct pool_head *pool_head_stream; |
Tim Duesterhus | 127a74d | 2020-02-28 15:13:33 +0100 | [diff] [blame] | 56 | extern struct pool_head *pool_head_uniqueid; |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 57 | |
Willy Tarreau | bc174aa | 2012-11-19 16:10:32 +0100 | [diff] [blame] | 58 | extern struct data_cb sess_conn_cb; |
| 59 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 60 | struct stream *stream_new(struct session *sess, enum obj_type *origin, struct buffer *input); |
| 61 | int stream_create_from_cs(struct conn_stream *cs, struct buffer *input); |
Christopher Faulet | 4ef84c9 | 2021-01-21 17:36:12 +0100 | [diff] [blame] | 62 | int stream_upgrade_from_cs(struct conn_stream *cs, struct buffer *input); |
Christopher Faulet | ae863c6 | 2021-03-15 12:03:44 +0100 | [diff] [blame] | 63 | int stream_set_http_mode(struct stream *s, const struct mux_proto_list *mux_proto); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 65 | /* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 66 | void stream_shutdown(struct stream *stream, int why); |
Willy Tarreau | 5484d58 | 2019-05-22 09:33:03 +0200 | [diff] [blame] | 67 | void stream_dump(struct buffer *buf, const struct stream *s, const char *pfx, char eol); |
Willy Tarreau | 71c07ac | 2019-04-25 19:08:48 +0200 | [diff] [blame] | 68 | void stream_dump_and_crash(enum obj_type *obj, int rate); |
Simon Horman | dec5be4 | 2011-06-08 09:19:07 +0900 | [diff] [blame] | 69 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 70 | struct ist stream_generate_unique_id(struct stream *strm, struct list *format); |
Tim Duesterhus | 127a74d | 2020-02-28 15:13:33 +0100 | [diff] [blame] | 71 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 72 | void stream_process_counters(struct stream *s); |
Willy Tarreau | e89fae3 | 2021-03-09 15:43:32 +0100 | [diff] [blame] | 73 | void sess_change_server(struct stream *strm, struct server *newsrv); |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 74 | struct task *process_stream(struct task *t, void *context, unsigned int state); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 75 | void default_srv_error(struct stream *s, struct stream_interface *si); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 76 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 77 | /* Update the stream's backend and server time stats */ |
| 78 | void stream_update_time_stats(struct stream *s); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 79 | void stream_release_buffers(struct stream *s); |
Willy Tarreau | b882dd8 | 2018-11-06 15:50:21 +0100 | [diff] [blame] | 80 | int stream_buf_available(void *arg); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 81 | |
Willy Tarreau | b1ec8c4 | 2015-04-03 13:53:24 +0200 | [diff] [blame] | 82 | /* returns the session this stream belongs to */ |
| 83 | static inline struct session *strm_sess(const struct stream *strm) |
| 84 | { |
| 85 | return strm->sess; |
| 86 | } |
| 87 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 88 | /* returns the frontend this stream was initiated from */ |
| 89 | static 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 */ |
| 95 | static 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 */ |
| 101 | static inline enum obj_type *strm_orig(const struct stream *strm) |
| 102 | { |
| 103 | return strm->sess->origin; |
| 104 | } |
| 105 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 106 | /* Remove the refcount from the stream to the tracked counters, and clear the |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 107 | * pointer to ensure this is only performed once. The caller is responsible for |
Willy Tarreau | a68f762 | 2015-09-21 17:48:24 +0200 | [diff] [blame] | 108 | * ensuring that the pointer is valid first. We must be extremely careful not |
| 109 | * to touch the entries we inherited from the session. |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 110 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 111 | static inline void stream_store_counters(struct stream *s) |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 112 | { |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 113 | void *ptr; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 114 | int i; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 115 | struct stksess *ts; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 117 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 118 | ts = stkctr_entry(&s->stkctr[i]); |
| 119 | if (!ts) |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 120 | continue; |
Willy Tarreau | a68f762 | 2015-09-21 17:48:24 +0200 | [diff] [blame] | 121 | |
| 122 | if (stkctr_entry(&s->sess->stkctr[i])) |
| 123 | continue; |
| 124 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 125 | ptr = stktable_data_ptr(s->stkctr[i].table, ts, STKTABLE_DT_CONN_CUR); |
| 126 | if (ptr) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 127 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 128 | |
Tim Duesterhus | 8b87c01 | 2019-01-04 00:11:59 +0100 | [diff] [blame] | 129 | if (stktable_data_cast(ptr, conn_cur) > 0) |
| 130 | stktable_data_cast(ptr, conn_cur)--; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 131 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 132 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 133 | |
| 134 | /* If data was modified, we need to touch to re-schedule sync */ |
| 135 | stktable_touch_local(s->stkctr[i].table, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 136 | } |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 137 | stkctr_set_entry(&s->stkctr[i], NULL); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 138 | stksess_kill_if_expired(s->stkctr[i].table, ts, 1); |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 139 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 142 | /* Remove the refcount from the stream counters tracked at the content level if |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 143 | * any, and clear the pointer to ensure this is only performed once. The caller |
Willy Tarreau | a68f762 | 2015-09-21 17:48:24 +0200 | [diff] [blame] | 144 | * 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 Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 146 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 147 | static inline void stream_stop_content_counters(struct stream *s) |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 148 | { |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 149 | struct stksess *ts; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 150 | void *ptr; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 151 | int i; |
Willy Tarreau | e348793 | 2010-06-18 21:03:20 +0200 | [diff] [blame] | 152 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 153 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 154 | ts = stkctr_entry(&s->stkctr[i]); |
| 155 | if (!ts) |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 156 | continue; |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 157 | |
Willy Tarreau | a68f762 | 2015-09-21 17:48:24 +0200 | [diff] [blame] | 158 | if (stkctr_entry(&s->sess->stkctr[i])) |
| 159 | continue; |
| 160 | |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 161 | if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_CONTENT)) |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 162 | continue; |
| 163 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 164 | ptr = stktable_data_ptr(s->stkctr[i].table, ts, STKTABLE_DT_CONN_CUR); |
| 165 | if (ptr) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 166 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 167 | |
Tim Duesterhus | 8b87c01 | 2019-01-04 00:11:59 +0100 | [diff] [blame] | 168 | if (stktable_data_cast(ptr, conn_cur) > 0) |
| 169 | stktable_data_cast(ptr, conn_cur)--; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 170 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 171 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 172 | |
| 173 | /* If data was modified, we need to touch to re-schedule sync */ |
| 174 | stktable_touch_local(s->stkctr[i].table, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 175 | } |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 176 | stkctr_set_entry(&s->stkctr[i], NULL); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 177 | stksess_kill_if_expired(s->stkctr[i].table, ts, 1); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 178 | } |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 179 | } |
Willy Tarreau | e348793 | 2010-06-18 21:03:20 +0200 | [diff] [blame] | 180 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 181 | /* 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 Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 185 | static inline void stream_start_counters(struct stktable *t, struct stksess *ts) |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 186 | { |
| 187 | void *ptr; |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 188 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 189 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 190 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 191 | 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 Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 205 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 206 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 207 | |
| 208 | /* If data was modified, we need to touch to re-schedule sync */ |
| 209 | stktable_touch_local(t, ts, 0); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 212 | /* Enable tracking of stream counters as <stkctr> on stksess <ts>. The caller is |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 213 | * responsible for ensuring that <t> and <ts> are valid pointers. Some controls |
| 214 | * are performed to ensure the state can still change. |
| 215 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 216 | static inline void stream_track_stkctr(struct stkctr *ctr, struct stktable *t, struct stksess *ts) |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 217 | { |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 218 | /* Why this test ???? */ |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 219 | if (stkctr_entry(ctr)) |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 220 | return; |
| 221 | |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 222 | ctr->table = t; |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 223 | stkctr_set_entry(ctr, ts); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 224 | stream_start_counters(t, ts); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 227 | /* Increase the number of cumulated HTTP requests in the tracked counters */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 228 | static inline void stream_inc_http_req_ctr(struct stream *s) |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 229 | { |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 230 | int i; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 231 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 232 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Christopher Faulet | 8460063 | 2020-10-06 13:52:40 +0200 | [diff] [blame] | 233 | if (!stkctr_inc_http_req_ctr(&s->stkctr[i])) |
| 234 | stkctr_inc_http_req_ctr(&s->sess->stkctr[i]); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Willy Tarreau | 8b7f868 | 2015-04-04 16:29:12 +0200 | [diff] [blame] | 238 | /* Increase the number of cumulated HTTP requests in the backend's tracked |
| 239 | * counters. We don't look up the session since it cannot happen in the bakcend. |
| 240 | */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 241 | static inline void stream_inc_be_http_req_ctr(struct stream *s) |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 242 | { |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 243 | int i; |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 244 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 245 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Christopher Faulet | 8460063 | 2020-10-06 13:52:40 +0200 | [diff] [blame] | 246 | if (!stkctr_entry(&s->stkctr[i]) || !(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_BACKEND)) |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 247 | continue; |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 248 | |
Christopher Faulet | 8460063 | 2020-10-06 13:52:40 +0200 | [diff] [blame] | 249 | stkctr_inc_http_req_ctr(&s->stkctr[i]); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 253 | /* Increase the number of cumulated failed HTTP requests in the tracked |
| 254 | * counters. Only 4xx requests should be counted here so that we can |
| 255 | * distinguish between errors caused by client behaviour and other ones. |
| 256 | * Note that even 404 are interesting because they're generally caused by |
| 257 | * vulnerability scans. |
| 258 | */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 259 | static inline void stream_inc_http_err_ctr(struct stream *s) |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 260 | { |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 261 | int i; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 262 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 263 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Christopher Faulet | 8460063 | 2020-10-06 13:52:40 +0200 | [diff] [blame] | 264 | if (!stkctr_inc_http_err_ctr(&s->stkctr[i])) |
| 265 | stkctr_inc_http_err_ctr(&s->sess->stkctr[i]); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 269 | /* Increase the number of cumulated failed HTTP responses in the tracked |
| 270 | * counters. Only some 5xx responses should be counted here so that we can |
| 271 | * distinguish between server failures and errors triggered by the client |
| 272 | * (i.e. 501 and 505 may be triggered and must be ignored). |
| 273 | */ |
| 274 | static inline void stream_inc_http_fail_ctr(struct stream *s) |
| 275 | { |
| 276 | int i; |
| 277 | |
| 278 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
| 279 | if (!stkctr_inc_http_fail_ctr(&s->stkctr[i])) |
| 280 | stkctr_inc_http_fail_ctr(&s->sess->stkctr[i]); |
| 281 | } |
| 282 | } |
| 283 | |
Willy Tarreau | e89fae3 | 2021-03-09 15:43:32 +0100 | [diff] [blame] | 284 | static inline void stream_add_srv_conn(struct stream *strm, struct server *srv) |
Willy Tarreau | 103e566 | 2017-11-26 18:48:14 +0100 | [diff] [blame] | 285 | { |
Ubuntu | 6fa9225 | 2021-03-01 06:21:47 +0000 | [diff] [blame] | 286 | /* note: this inserts in reverse order but we do not care, it's only |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 287 | * used for massive kills (i.e. almost never). MT_LIST_INSERT() is a bit |
| 288 | * faster than MT_LIST_APPEND under contention due to a faster recovery |
| 289 | * from a conflict with an adjacent MT_LIST_DELETE, and using it improves |
Ubuntu | 6fa9225 | 2021-03-01 06:21:47 +0000 | [diff] [blame] | 290 | * the performance by about 3% on 32-cores. |
| 291 | */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 292 | MT_LIST_INSERT(&srv->per_thr[tid].streams, &strm->by_srv); |
Willy Tarreau | e89fae3 | 2021-03-09 15:43:32 +0100 | [diff] [blame] | 293 | HA_ATOMIC_STORE(&strm->srv_conn, srv); |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 294 | } |
| 295 | |
Willy Tarreau | e89fae3 | 2021-03-09 15:43:32 +0100 | [diff] [blame] | 296 | static inline void stream_del_srv_conn(struct stream *strm) |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 297 | { |
Willy Tarreau | e89fae3 | 2021-03-09 15:43:32 +0100 | [diff] [blame] | 298 | struct server *srv = strm->srv_conn; |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 299 | |
| 300 | if (!srv) |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 301 | return; |
| 302 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 303 | MT_LIST_DELETE(&strm->by_srv); |
Willy Tarreau | e89fae3 | 2021-03-09 15:43:32 +0100 | [diff] [blame] | 304 | HA_ATOMIC_STORE(&strm->srv_conn, NULL); |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 305 | } |
| 306 | |
Willy Tarreau | e89fae3 | 2021-03-09 15:43:32 +0100 | [diff] [blame] | 307 | static inline void stream_init_srv_conn(struct stream *strm) |
Willy Tarreau | 9bd0d74 | 2011-07-20 00:17:39 +0200 | [diff] [blame] | 308 | { |
Willy Tarreau | e89fae3 | 2021-03-09 15:43:32 +0100 | [diff] [blame] | 309 | strm->srv_conn = NULL; |
| 310 | MT_LIST_INIT(&strm->by_srv); |
Willy Tarreau | 9bd0d74 | 2011-07-20 00:17:39 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 313 | static inline void stream_choose_redispatch(struct stream *s) |
| 314 | { |
| 315 | struct stream_interface *si = &s->si[1]; |
| 316 | |
| 317 | /* If the "redispatch" option is set on the backend, we are allowed to |
| 318 | * retry on another server. By default this redispatch occurs on the |
| 319 | * last retry, but if configured we allow redispatches to occur on |
| 320 | * configurable intervals, e.g. on every retry. In order to achieve this, |
| 321 | * we must mark the stream unassigned, and eventually clear the DIRECT |
| 322 | * bit to ignore any persistence cookie. We won't count a retry nor a |
| 323 | * redispatch yet, because this will depend on what server is selected. |
| 324 | * If the connection is not persistent, the balancing algorithm is not |
| 325 | * determinist (round robin) and there is more than one active server, |
| 326 | * we accept to perform an immediate redispatch without waiting since |
| 327 | * we don't care about this particular server. |
| 328 | */ |
| 329 | if (objt_server(s->target) && |
| 330 | (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) && |
| 331 | ((__objt_server(s->target)->cur_state < SRV_ST_RUNNING) || |
| 332 | (((s->be->redispatch_after > 0) && |
| 333 | ((s->be->conn_retries - si->conn_retries) % |
| 334 | s->be->redispatch_after == 0)) || |
| 335 | ((s->be->redispatch_after < 0) && |
| 336 | ((s->be->conn_retries - si->conn_retries) % |
| 337 | (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) || |
| 338 | (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 && |
| 339 | ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) { |
| 340 | sess_change_server(s, NULL); |
| 341 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
Amaury Denoyelle | caaafd0 | 2021-06-18 11:11:36 +0200 | [diff] [blame^] | 342 | process_srv_queue(objt_server(s->target), 0); |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 343 | |
| 344 | s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
| 345 | si->state = SI_ST_REQ; |
| 346 | } else { |
| 347 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 348 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries); |
| 349 | _HA_ATOMIC_INC(&s->be->be_counters.retries); |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 350 | si->state = SI_ST_ASS; |
| 351 | } |
| 352 | |
| 353 | } |
| 354 | |
Amaury Denoyelle | b715078 | 2020-12-10 13:43:51 +0100 | [diff] [blame] | 355 | int stream_set_timeout(struct stream *s, enum act_timeout_name name, int timeout); |
| 356 | |
Thierry FOURNIER | 5a363e7 | 2015-09-27 19:29:33 +0200 | [diff] [blame] | 357 | void service_keywords_register(struct action_kw_list *kw_list); |
Thierry Fournier | 87e5399 | 2020-11-28 19:32:14 +0100 | [diff] [blame] | 358 | struct action_kw *service_find(const char *kw); |
Willy Tarreau | 679bba1 | 2019-03-19 08:08:10 +0100 | [diff] [blame] | 359 | void list_services(FILE *out); |
Thierry FOURNIER | 5a363e7 | 2015-09-27 19:29:33 +0200 | [diff] [blame] | 360 | |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 361 | #endif /* _HAPROXY_STREAM_H */ |
Willy TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 362 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 363 | /* |
| 364 | * Local variables: |
| 365 | * c-indent-level: 8 |
| 366 | * c-basic-offset: 8 |
| 367 | * End: |
| 368 | */ |