blob: 1cd506a04b1ac3a3b1d169417b2c8d3e1d6ff697 [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 Tarreau87b09662015-04-03 00:22:06 +020027#include <types/stream.h>
Willy Tarreaua24adf02014-11-27 01:11:56 +010028#include <proto/fd.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020029#include <proto/freq_ctr.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020030#include <proto/stick_table.h>
Willy Tarreaua24adf02014-11-27 01:11:56 +010031#include <proto/task.h>
Willy TARREAU3dc06442006-06-15 21:48:13 +020032
Willy Tarreau87b09662015-04-03 00:22:06 +020033extern struct pool_head *pool2_stream;
34extern struct list streams;
Willy Tarreaubf883e02014-11-25 21:10:35 +010035extern struct list buffer_wq;
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020036
Willy Tarreaubc174aa2012-11-19 16:10:32 +010037extern struct data_cb sess_conn_cb;
38
Willy Tarreau87b09662015-04-03 00:22:06 +020039int stream_accept(struct listener *l, int cfd, struct sockaddr_storage *addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020041/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +020042int init_stream();
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreaue7dff022015-04-03 01:14:29 +020044/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +020045void stream_shutdown(struct stream *stream, int why);
Simon Hormandec5be42011-06-08 09:19:07 +090046
Willy Tarreau87b09662015-04-03 00:22:06 +020047void stream_process_counters(struct stream *s);
48void sess_change_server(struct stream *sess, struct server *newsrv);
49struct task *process_stream(struct task *t);
50void default_srv_error(struct stream *s, struct stream_interface *si);
Willy Tarreau192252e2015-04-04 01:47:55 +020051struct stkctr *smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020052int parse_track_counters(char **args, int *arg,
53 int section_type, struct proxy *curpx,
54 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +020055 struct proxy *defpx, char **err);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020056
Willy Tarreau87b09662015-04-03 00:22:06 +020057/* Update the stream's backend and server time stats */
58void stream_update_time_stats(struct stream *s);
59void __stream_offer_buffers(int rqlimit);
60static inline void stream_offer_buffers();
61int stream_alloc_work_buffer(struct stream *s);
62void stream_release_buffers(struct stream *s);
63int stream_alloc_recv_buffer(struct channel *chn);
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 Tarreaucc08d2c2014-01-28 23:18:23 +010071/* sets the stick counter's entry pointer */
72static inline void stkctr_set_entry(struct stkctr *stkctr, struct stksess *entry)
73{
74 stkctr->entry = caddr_from_ptr(entry, 0);
75}
76
77/* returns the entry pointer from a stick counter */
78static inline struct stksess *stkctr_entry(struct stkctr *stkctr)
79{
80 return caddr_to_ptr(stkctr->entry);
81}
82
83/* returns the two flags from a stick counter */
84static inline unsigned int stkctr_flags(struct stkctr *stkctr)
85{
86 return caddr_to_data(stkctr->entry);
87}
88
89/* sets up to two flags at a time on a composite address */
90static inline void stkctr_set_flags(struct stkctr *stkctr, unsigned int flags)
91{
92 stkctr->entry = caddr_set_flags(stkctr->entry, flags);
93}
94
95/* returns the two flags from a stick counter */
96static inline void stkctr_clr_flags(struct stkctr *stkctr, unsigned int flags)
97{
98 stkctr->entry = caddr_clr_flags(stkctr->entry, flags);
99}
100
Willy Tarreau87b09662015-04-03 00:22:06 +0200101/* Remove the refcount from the stream to the tracked counters, and clear the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200102 * pointer to ensure this is only performed once. The caller is responsible for
103 * ensuring that the pointer is valid first.
104 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200105static inline void stream_store_counters(struct stream *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200106{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200107 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100108 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200109
Willy Tarreaub4c84932013-07-23 19:15:30 +0200110 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100111 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100112 continue;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100113 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_CONN_CUR);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200114 if (ptr)
115 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100116 stkctr_entry(&s->stkctr[i])->ref_cnt--;
117 stksess_kill_if_expired(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]));
118 stkctr_set_entry(&s->stkctr[i], NULL);
Willy Tarreau38285c12010-06-18 16:35:43 +0200119 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200120}
121
Willy Tarreau87b09662015-04-03 00:22:06 +0200122/* Remove the refcount from the stream counters tracked at the content level if
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200123 * any, and clear the pointer to ensure this is only performed once. The caller
124 * is responsible for ensuring that the pointer is valid first.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200125 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200126static inline void stream_stop_content_counters(struct stream *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200127{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200128 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100129 int i;
Willy Tarreaue3487932010-06-18 21:03:20 +0200130
Willy Tarreaub4c84932013-07-23 19:15:30 +0200131 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100132 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100133 continue;
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200134
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100135 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_CONTENT))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100136 continue;
137
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100138 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_CONN_CUR);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200139 if (ptr)
140 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100141 stkctr_entry(&s->stkctr[i])->ref_cnt--;
142 stksess_kill_if_expired(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]));
143 stkctr_set_entry(&s->stkctr[i], NULL);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200144 }
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200145}
Willy Tarreaue3487932010-06-18 21:03:20 +0200146
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200147/* Increase total and concurrent connection count for stick entry <ts> of table
148 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
149 * pointers, and for calling this only once per connection.
150 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200151static inline void stream_start_counters(struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200152{
153 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200154
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200155 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
156 if (ptr)
157 stktable_data_cast(ptr, conn_cur)++;
158
159 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
160 if (ptr)
161 stktable_data_cast(ptr, conn_cnt)++;
162
163 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
164 if (ptr)
165 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
166 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
167 if (tick_isset(t->expire))
168 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
169}
170
Willy Tarreau87b09662015-04-03 00:22:06 +0200171/* Enable tracking of stream counters as <stkctr> on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200172 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
173 * are performed to ensure the state can still change.
174 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200175static inline void stream_track_stkctr(struct stkctr *ctr, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200176{
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100177 if (stkctr_entry(ctr))
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200178 return;
179
180 ts->ref_cnt++;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100181 ctr->table = t;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100182 stkctr_set_entry(ctr, ts);
Willy Tarreau87b09662015-04-03 00:22:06 +0200183 stream_start_counters(t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200184}
185
Willy Tarreauda7ff642010-06-23 11:44:09 +0200186/* Increase the number of cumulated HTTP requests in the tracked counters */
Willy Tarreau87b09662015-04-03 00:22:06 +0200187static void inline stream_inc_http_req_ctr(struct stream *s)
Willy Tarreauda7ff642010-06-23 11:44:09 +0200188{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200189 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100190 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200191
Willy Tarreaub4c84932013-07-23 19:15:30 +0200192 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100193 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100194 continue;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200195
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100196 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200197 if (ptr)
198 stktable_data_cast(ptr, http_req_cnt)++;
199
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100200 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200201 if (ptr)
202 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100203 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200204 }
205}
206
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100207/* Increase the number of cumulated HTTP requests in the backend's tracked counters */
Willy Tarreau87b09662015-04-03 00:22:06 +0200208static void inline stream_inc_be_http_req_ctr(struct stream *s)
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100209{
210 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100211 int i;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100212
Willy Tarreaub4c84932013-07-23 19:15:30 +0200213 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100214 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100215 continue;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100216
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100217 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_BACKEND))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100218 continue;
219
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100220 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100221 if (ptr)
222 stktable_data_cast(ptr, http_req_cnt)++;
223
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100224 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100225 if (ptr)
226 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100227 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100228 }
229}
230
Willy Tarreauda7ff642010-06-23 11:44:09 +0200231/* Increase the number of cumulated failed HTTP requests in the tracked
232 * counters. Only 4xx requests should be counted here so that we can
233 * distinguish between errors caused by client behaviour and other ones.
234 * Note that even 404 are interesting because they're generally caused by
235 * vulnerability scans.
236 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200237static void inline stream_inc_http_err_ctr(struct stream *s)
Willy Tarreauda7ff642010-06-23 11:44:09 +0200238{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200239 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100240 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200241
Willy Tarreaub4c84932013-07-23 19:15:30 +0200242 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100243 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100244 continue;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200245
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100246 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200247 if (ptr)
248 stktable_data_cast(ptr, http_err_cnt)++;
249
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100250 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200251 if (ptr)
252 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100253 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200254 }
255}
256
Willy Tarreau87b09662015-04-03 00:22:06 +0200257static void inline stream_add_srv_conn(struct stream *sess, struct server *srv)
Simon Hormanaf514952011-06-21 14:34:57 +0900258{
259 sess->srv_conn = srv;
260 LIST_ADD(&srv->actconns, &sess->by_srv);
261}
262
Willy Tarreau87b09662015-04-03 00:22:06 +0200263static void inline stream_del_srv_conn(struct stream *sess)
Simon Hormanaf514952011-06-21 14:34:57 +0900264{
265 if (!sess->srv_conn)
266 return;
267
268 sess->srv_conn = NULL;
269 LIST_DEL(&sess->by_srv);
270}
271
Willy Tarreau87b09662015-04-03 00:22:06 +0200272static void inline stream_init_srv_conn(struct stream *sess)
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200273{
274 sess->srv_conn = NULL;
275 LIST_INIT(&sess->by_srv);
276}
277
Willy Tarreau87b09662015-04-03 00:22:06 +0200278static inline void stream_offer_buffers()
Willy Tarreaua24adf02014-11-27 01:11:56 +0100279{
280 int avail;
281
282 if (LIST_ISEMPTY(&buffer_wq))
283 return;
284
Willy Tarreau87b09662015-04-03 00:22:06 +0200285 /* all streams will need 1 buffer, so we can stop waking up streams
Willy Tarreaua24adf02014-11-27 01:11:56 +0100286 * once we have enough of them to eat all the buffers. Note that we
Willy Tarreau87b09662015-04-03 00:22:06 +0200287 * don't really know if they are streams or just other tasks, but
Willy Tarreaua24adf02014-11-27 01:11:56 +0100288 * that's a rough estimate. Similarly, for each cached event we'll need
289 * 1 buffer. If no buffer is currently used, always wake up the number
290 * of tasks we can offer a buffer based on what is allocated, and in
291 * any case at least one task per two reserved buffers.
292 */
293 avail = pool2_buffer->allocated - pool2_buffer->used - global.tune.reserved_bufs / 2;
294
295 if (avail > (int)run_queue)
Willy Tarreau87b09662015-04-03 00:22:06 +0200296 __stream_offer_buffers(avail);
Willy Tarreaua24adf02014-11-27 01:11:56 +0100297}
298
Willy Tarreau87b09662015-04-03 00:22:06 +0200299#endif /* _PROTO_STREAM_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200300
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301/*
302 * Local variables:
303 * c-indent-level: 8
304 * c-basic-offset: 8
305 * End:
306 */