Willy TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2 | * include/proto/stream.h |
| 3 | * This file defines everything related to streams. |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 4 | * |
| 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 TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 22 | #ifndef _PROTO_STREAM_H |
| 23 | #define _PROTO_STREAM_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 25 | #include <common/config.h> |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 26 | #include <common/memory.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 27 | #include <types/action.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 28 | #include <types/stream.h> |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 29 | #include <proto/fd.h> |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 30 | #include <proto/freq_ctr.h> |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 31 | #include <proto/obj_type.h> |
| 32 | #include <proto/queue.h> |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 33 | #include <proto/stick_table.h> |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 34 | #include <proto/task.h> |
Willy TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 35 | |
Christopher Faulet | c8b246f | 2019-05-14 22:05:28 +0200 | [diff] [blame] | 36 | #define IS_HTX_STRM(strm) ((strm)->flags & SF_HTX) |
| 37 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 38 | extern struct pool_head *pool_head_stream; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 39 | extern struct list streams; |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | bc174aa | 2012-11-19 16:10:32 +0100 | [diff] [blame] | 41 | extern struct data_cb sess_conn_cb; |
| 42 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 43 | struct stream *stream_new(struct session *sess, enum obj_type *origin); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 44 | int stream_create_from_cs(struct conn_stream *cs); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 46 | /* 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] | 47 | void stream_shutdown(struct stream *stream, int why); |
Willy Tarreau | 5484d58 | 2019-05-22 09:33:03 +0200 | [diff] [blame] | 48 | 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] | 49 | void stream_dump_and_crash(enum obj_type *obj, int rate); |
Simon Horman | dec5be4 | 2011-06-08 09:19:07 +0900 | [diff] [blame] | 50 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 51 | void stream_process_counters(struct stream *s); |
| 52 | void sess_change_server(struct stream *sess, struct server *newsrv); |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 53 | struct task *process_stream(struct task *t, void *context, unsigned short state); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 54 | void default_srv_error(struct stream *s, struct stream_interface *si); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 55 | int parse_track_counters(char **args, int *arg, |
| 56 | int section_type, struct proxy *curpx, |
| 57 | struct track_ctr_prm *prm, |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 58 | struct proxy *defpx, char **err); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 59 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 60 | /* Update the stream's backend and server time stats */ |
| 61 | void stream_update_time_stats(struct stream *s); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 62 | void stream_release_buffers(struct stream *s); |
Willy Tarreau | b882dd8 | 2018-11-06 15:50:21 +0100 | [diff] [blame] | 63 | int stream_buf_available(void *arg); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 64 | |
Willy Tarreau | b1ec8c4 | 2015-04-03 13:53:24 +0200 | [diff] [blame] | 65 | /* returns the session this stream belongs to */ |
| 66 | static inline struct session *strm_sess(const struct stream *strm) |
| 67 | { |
| 68 | return strm->sess; |
| 69 | } |
| 70 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 71 | /* returns the frontend this stream was initiated from */ |
| 72 | static 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 */ |
| 78 | static 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 */ |
| 84 | static inline enum obj_type *strm_orig(const struct stream *strm) |
| 85 | { |
| 86 | return strm->sess->origin; |
| 87 | } |
| 88 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 89 | /* 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] | 90 | * 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] | 91 | * ensuring that the pointer is valid first. We must be extremely careful not |
| 92 | * to touch the entries we inherited from the session. |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 93 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 94 | static inline void stream_store_counters(struct stream *s) |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 95 | { |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 96 | void *ptr; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 97 | int i; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 98 | struct stksess *ts; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 99 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 100 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 101 | ts = stkctr_entry(&s->stkctr[i]); |
| 102 | if (!ts) |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 103 | continue; |
Willy Tarreau | a68f762 | 2015-09-21 17:48:24 +0200 | [diff] [blame] | 104 | |
| 105 | if (stkctr_entry(&s->sess->stkctr[i])) |
| 106 | continue; |
| 107 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 108 | ptr = stktable_data_ptr(s->stkctr[i].table, ts, STKTABLE_DT_CONN_CUR); |
| 109 | if (ptr) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 110 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 111 | |
Tim Duesterhus | 8b87c01 | 2019-01-04 00:11:59 +0100 | [diff] [blame] | 112 | if (stktable_data_cast(ptr, conn_cur) > 0) |
| 113 | stktable_data_cast(ptr, conn_cur)--; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 114 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 115 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 116 | |
| 117 | /* If data was modified, we need to touch to re-schedule sync */ |
| 118 | stktable_touch_local(s->stkctr[i].table, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 119 | } |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 120 | stkctr_set_entry(&s->stkctr[i], NULL); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 121 | stksess_kill_if_expired(s->stkctr[i].table, ts, 1); |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 122 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 125 | /* 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] | 126 | * 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] | 127 | * 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 Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 129 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 130 | static inline void stream_stop_content_counters(struct stream *s) |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 131 | { |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 132 | struct stksess *ts; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 133 | void *ptr; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 134 | int i; |
Willy Tarreau | e348793 | 2010-06-18 21:03:20 +0200 | [diff] [blame] | 135 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 136 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 137 | ts = stkctr_entry(&s->stkctr[i]); |
| 138 | if (!ts) |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 139 | continue; |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 140 | |
Willy Tarreau | a68f762 | 2015-09-21 17:48:24 +0200 | [diff] [blame] | 141 | if (stkctr_entry(&s->sess->stkctr[i])) |
| 142 | continue; |
| 143 | |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 144 | if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_CONTENT)) |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 145 | continue; |
| 146 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 147 | ptr = stktable_data_ptr(s->stkctr[i].table, ts, STKTABLE_DT_CONN_CUR); |
| 148 | if (ptr) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 149 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 150 | |
Tim Duesterhus | 8b87c01 | 2019-01-04 00:11:59 +0100 | [diff] [blame] | 151 | if (stktable_data_cast(ptr, conn_cur) > 0) |
| 152 | stktable_data_cast(ptr, conn_cur)--; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 153 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 154 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 155 | |
| 156 | /* If data was modified, we need to touch to re-schedule sync */ |
| 157 | stktable_touch_local(s->stkctr[i].table, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 158 | } |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 159 | stkctr_set_entry(&s->stkctr[i], NULL); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 160 | stksess_kill_if_expired(s->stkctr[i].table, ts, 1); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 161 | } |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 162 | } |
Willy Tarreau | e348793 | 2010-06-18 21:03:20 +0200 | [diff] [blame] | 163 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 164 | /* 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 Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 168 | static inline void stream_start_counters(struct stktable *t, struct stksess *ts) |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 169 | { |
| 170 | void *ptr; |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 171 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 172 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 173 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 174 | 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 Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 188 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 189 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 190 | |
| 191 | /* If data was modified, we need to touch to re-schedule sync */ |
| 192 | stktable_touch_local(t, ts, 0); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 195 | /* 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] | 196 | * responsible for ensuring that <t> and <ts> are valid pointers. Some controls |
| 197 | * are performed to ensure the state can still change. |
| 198 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 199 | 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] | 200 | { |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 201 | /* Why this test ???? */ |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 202 | if (stkctr_entry(ctr)) |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 203 | return; |
| 204 | |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 205 | ctr->table = t; |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 206 | stkctr_set_entry(ctr, ts); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 207 | stream_start_counters(t, ts); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 210 | /* Increase the number of cumulated HTTP requests in the tracked counters */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 211 | static inline void stream_inc_http_req_ctr(struct stream *s) |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 212 | { |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 213 | struct stksess *ts; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 214 | void *ptr; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 215 | int i; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 216 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 217 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Willy Tarreau | 8b7f868 | 2015-04-04 16:29:12 +0200 | [diff] [blame] | 218 | struct stkctr *stkctr = &s->stkctr[i]; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 219 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 220 | ts = stkctr_entry(stkctr); |
| 221 | if (!ts) { |
Willy Tarreau | 8b7f868 | 2015-04-04 16:29:12 +0200 | [diff] [blame] | 222 | stkctr = &s->sess->stkctr[i]; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 223 | ts = stkctr_entry(stkctr); |
| 224 | if (!ts) |
Willy Tarreau | 8b7f868 | 2015-04-04 16:29:12 +0200 | [diff] [blame] | 225 | continue; |
| 226 | } |
| 227 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 228 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 229 | |
| 230 | ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_CNT); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 231 | if (ptr) |
| 232 | stktable_data_cast(ptr, http_req_cnt)++; |
| 233 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 234 | ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_RATE); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 235 | if (ptr) |
| 236 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
Willy Tarreau | 8b7f868 | 2015-04-04 16:29:12 +0200 | [diff] [blame] | 237 | stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 238 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 239 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 240 | |
| 241 | /* If data was modified, we need to touch to re-schedule sync */ |
| 242 | stktable_touch_local(stkctr->table, ts, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Willy Tarreau | 8b7f868 | 2015-04-04 16:29:12 +0200 | [diff] [blame] | 246 | /* 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 Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 249 | static inline void stream_inc_be_http_req_ctr(struct stream *s) |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 250 | { |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 251 | struct stksess *ts; |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 252 | void *ptr; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 253 | int i; |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 254 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 255 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Willy Tarreau | 8b7f868 | 2015-04-04 16:29:12 +0200 | [diff] [blame] | 256 | struct stkctr *stkctr = &s->stkctr[i]; |
| 257 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 258 | ts = stkctr_entry(stkctr); |
| 259 | if (!ts) |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 260 | continue; |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 261 | |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 262 | if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_BACKEND)) |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 263 | continue; |
| 264 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 265 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 266 | |
| 267 | ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_CNT); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 268 | if (ptr) |
| 269 | stktable_data_cast(ptr, http_req_cnt)++; |
| 270 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 271 | ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_RATE); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 272 | if (ptr) |
| 273 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
Willy Tarreau | 8b7f868 | 2015-04-04 16:29:12 +0200 | [diff] [blame] | 274 | stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 275 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 276 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 277 | |
| 278 | /* If data was modified, we need to touch to re-schedule sync */ |
| 279 | stktable_touch_local(stkctr->table, ts, 0); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 283 | /* 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 Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 289 | static inline void stream_inc_http_err_ctr(struct stream *s) |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 290 | { |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 291 | struct stksess *ts; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 292 | void *ptr; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 293 | int i; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 294 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 295 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Willy Tarreau | 8b7f868 | 2015-04-04 16:29:12 +0200 | [diff] [blame] | 296 | struct stkctr *stkctr = &s->stkctr[i]; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 297 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 298 | ts = stkctr_entry(stkctr); |
| 299 | if (!ts) { |
Willy Tarreau | 8b7f868 | 2015-04-04 16:29:12 +0200 | [diff] [blame] | 300 | stkctr = &s->sess->stkctr[i]; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 301 | ts = stkctr_entry(stkctr); |
| 302 | if (!ts) |
Willy Tarreau | 8b7f868 | 2015-04-04 16:29:12 +0200 | [diff] [blame] | 303 | continue; |
| 304 | } |
| 305 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 306 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 307 | |
| 308 | ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_ERR_CNT); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 309 | if (ptr) |
| 310 | stktable_data_cast(ptr, http_err_cnt)++; |
| 311 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 312 | ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_ERR_RATE); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 313 | if (ptr) |
| 314 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
Willy Tarreau | 8b7f868 | 2015-04-04 16:29:12 +0200 | [diff] [blame] | 315 | stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 316 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 317 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 318 | |
| 319 | /* If data was modified, we need to touch to re-schedule sync */ |
| 320 | stktable_touch_local(stkctr->table, ts, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 324 | static inline void __stream_add_srv_conn(struct stream *sess, struct server *srv) |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 325 | { |
| 326 | sess->srv_conn = srv; |
| 327 | LIST_ADD(&srv->actconns, &sess->by_srv); |
Willy Tarreau | 103e566 | 2017-11-26 18:48:14 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 330 | static inline void stream_add_srv_conn(struct stream *sess, struct server *srv) |
Willy Tarreau | 103e566 | 2017-11-26 18:48:14 +0100 | [diff] [blame] | 331 | { |
| 332 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
| 333 | __stream_add_srv_conn(sess, srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 334 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 335 | } |
| 336 | |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 337 | static inline void stream_del_srv_conn(struct stream *sess) |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 338 | { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 339 | struct server *srv = sess->srv_conn; |
| 340 | |
| 341 | if (!srv) |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 342 | return; |
| 343 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 344 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 345 | sess->srv_conn = NULL; |
| 346 | LIST_DEL(&sess->by_srv); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 347 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 348 | } |
| 349 | |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 350 | static inline void stream_init_srv_conn(struct stream *sess) |
Willy Tarreau | 9bd0d74 | 2011-07-20 00:17:39 +0200 | [diff] [blame] | 351 | { |
| 352 | sess->srv_conn = NULL; |
| 353 | LIST_INIT(&sess->by_srv); |
| 354 | } |
| 355 | |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 356 | static 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 FOURNIER | 5a363e7 | 2015-09-27 19:29:33 +0200 | [diff] [blame] | 398 | void service_keywords_register(struct action_kw_list *kw_list); |
Willy Tarreau | 679bba1 | 2019-03-19 08:08:10 +0100 | [diff] [blame] | 399 | void list_services(FILE *out); |
Thierry FOURNIER | 5a363e7 | 2015-09-27 19:29:33 +0200 | [diff] [blame] | 400 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 401 | #endif /* _PROTO_STREAM_H */ |
Willy TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 402 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 403 | /* |
| 404 | * Local variables: |
| 405 | * c-indent-level: 8 |
| 406 | * c-basic-offset: 8 |
| 407 | * End: |
| 408 | */ |