blob: 2dc3d830e4cbfc6a3ad393c528e70ff8323b9d34 [file] [log] [blame]
Willy TARREAU3dc06442006-06-15 21:48:13 +02001/*
Willy Tarreau81f9aa32010-06-01 17:45:26 +02002 * include/proto/session.h
3 * This file defines everything related to sessions.
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 TARREAU3dc06442006-06-15 21:48:13 +020021
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#ifndef _PROTO_SESSION_H
23#define _PROTO_SESSION_H
24
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 Tarreaubaaee002006-06-26 02:48:02 +020027#include <types/session.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 Tarreauc6ca1a02007-05-13 19:43:47 +020033extern struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010034extern struct list sessions;
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 Tarreau81f9aa32010-06-01 17:45:26 +020039int session_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. */
42int init_session();
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreaua2a64e92011-09-07 23:01:56 +020044/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
45void session_shutdown(struct session *session, int why);
Simon Hormandec5be42011-06-08 09:19:07 +090046
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +010047void session_process_counters(struct session *s);
Willy Tarreau7c669d72008-06-20 15:04:11 +020048void sess_change_server(struct session *sess, struct server *newsrv);
Willy Tarreau26c25062009-03-08 09:38:41 +010049struct task *process_session(struct task *t);
Willy Tarreau84455332009-03-15 22:34:05 +010050void default_srv_error(struct session *s, struct stream_interface *si);
Willy Tarreaue12704b2014-07-15 19:06:18 +020051struct stkctr *smp_fetch_sc_stkctr(struct session *l4, 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 Tarreau4bfc5802014-06-17 12:19:18 +020057/* Update the session's backend and server time stats */
58void session_update_time_stats(struct session *s);
Willy Tarreaua24adf02014-11-27 01:11:56 +010059void __session_offer_buffers(int rqlimit);
60static inline void session_offer_buffers();
61int session_alloc_work_buffer(struct session *s);
Willy Tarreau656859d2014-11-25 19:46:36 +010062void session_release_buffers(struct session *s);
Willy Tarreau78955f42014-12-28 13:09:02 +010063int session_alloc_recv_buffer(struct channel *chn);
Willy Tarreau4bfc5802014-06-17 12:19:18 +020064
Willy Tarreaucc08d2c2014-01-28 23:18:23 +010065/* sets the stick counter's entry pointer */
66static inline void stkctr_set_entry(struct stkctr *stkctr, struct stksess *entry)
67{
68 stkctr->entry = caddr_from_ptr(entry, 0);
69}
70
71/* returns the entry pointer from a stick counter */
72static inline struct stksess *stkctr_entry(struct stkctr *stkctr)
73{
74 return caddr_to_ptr(stkctr->entry);
75}
76
77/* returns the two flags from a stick counter */
78static inline unsigned int stkctr_flags(struct stkctr *stkctr)
79{
80 return caddr_to_data(stkctr->entry);
81}
82
83/* sets up to two flags at a time on a composite address */
84static inline void stkctr_set_flags(struct stkctr *stkctr, unsigned int flags)
85{
86 stkctr->entry = caddr_set_flags(stkctr->entry, flags);
87}
88
89/* returns the two flags from a stick counter */
90static inline void stkctr_clr_flags(struct stkctr *stkctr, unsigned int flags)
91{
92 stkctr->entry = caddr_clr_flags(stkctr->entry, flags);
93}
94
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020095/* Remove the refcount from the session to the tracked counters, and clear the
96 * pointer to ensure this is only performed once. The caller is responsible for
97 * ensuring that the pointer is valid first.
98 */
99static inline void session_store_counters(struct session *s)
100{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200101 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100102 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200103
Willy Tarreaub4c84932013-07-23 19:15:30 +0200104 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100105 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100106 continue;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100107 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_CONN_CUR);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200108 if (ptr)
109 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100110 stkctr_entry(&s->stkctr[i])->ref_cnt--;
111 stksess_kill_if_expired(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]));
112 stkctr_set_entry(&s->stkctr[i], NULL);
Willy Tarreau38285c12010-06-18 16:35:43 +0200113 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200114}
115
Willy Tarreauf3338342014-01-28 21:40:28 +0100116/* Remove the refcount from the session counters tracked at the content level if
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200117 * any, and clear the pointer to ensure this is only performed once. The caller
118 * is responsible for ensuring that the pointer is valid first.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200119 */
Willy Tarreauf3338342014-01-28 21:40:28 +0100120static inline void session_stop_content_counters(struct session *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200121{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200122 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100123 int i;
Willy Tarreaue3487932010-06-18 21:03:20 +0200124
Willy Tarreaub4c84932013-07-23 19:15:30 +0200125 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100126 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100127 continue;
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200128
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100129 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_CONTENT))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100130 continue;
131
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100132 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_CONN_CUR);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200133 if (ptr)
134 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100135 stkctr_entry(&s->stkctr[i])->ref_cnt--;
136 stksess_kill_if_expired(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]));
137 stkctr_set_entry(&s->stkctr[i], NULL);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200138 }
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200139}
Willy Tarreaue3487932010-06-18 21:03:20 +0200140
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200141/* Increase total and concurrent connection count for stick entry <ts> of table
142 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
143 * pointers, and for calling this only once per connection.
144 */
145static inline void session_start_counters(struct stktable *t, struct stksess *ts)
146{
147 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200148
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200149 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
150 if (ptr)
151 stktable_data_cast(ptr, conn_cur)++;
152
153 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
154 if (ptr)
155 stktable_data_cast(ptr, conn_cnt)++;
156
157 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
158 if (ptr)
159 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
160 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
161 if (tick_isset(t->expire))
162 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
163}
164
Willy Tarreau20d46a52012-12-09 15:55:40 +0100165/* Enable tracking of session counters as <stkctr> on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200166 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
167 * are performed to ensure the state can still change.
168 */
Willy Tarreau20d46a52012-12-09 15:55:40 +0100169static inline void session_track_stkctr(struct stkctr *ctr, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200170{
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100171 if (stkctr_entry(ctr))
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200172 return;
173
174 ts->ref_cnt++;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100175 ctr->table = t;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100176 stkctr_set_entry(ctr, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200177 session_start_counters(t, ts);
178}
179
Willy Tarreauda7ff642010-06-23 11:44:09 +0200180/* Increase the number of cumulated HTTP requests in the tracked counters */
181static void inline session_inc_http_req_ctr(struct session *s)
182{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200183 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100184 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200185
Willy Tarreaub4c84932013-07-23 19:15:30 +0200186 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100187 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100188 continue;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200189
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100190 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 +0200191 if (ptr)
192 stktable_data_cast(ptr, http_req_cnt)++;
193
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100194 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 +0200195 if (ptr)
196 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100197 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200198 }
199}
200
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100201/* Increase the number of cumulated HTTP requests in the backend's tracked counters */
202static void inline session_inc_be_http_req_ctr(struct session *s)
203{
204 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100205 int i;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100206
Willy Tarreaub4c84932013-07-23 19:15:30 +0200207 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100208 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100209 continue;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100210
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100211 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_BACKEND))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100212 continue;
213
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100214 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 +0100215 if (ptr)
216 stktable_data_cast(ptr, http_req_cnt)++;
217
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100218 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 +0100219 if (ptr)
220 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100221 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100222 }
223}
224
Willy Tarreauda7ff642010-06-23 11:44:09 +0200225/* Increase the number of cumulated failed HTTP requests in the tracked
226 * counters. Only 4xx requests should be counted here so that we can
227 * distinguish between errors caused by client behaviour and other ones.
228 * Note that even 404 are interesting because they're generally caused by
229 * vulnerability scans.
230 */
231static void inline session_inc_http_err_ctr(struct session *s)
232{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200233 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100234 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200235
Willy Tarreaub4c84932013-07-23 19:15:30 +0200236 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100237 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100238 continue;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200239
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100240 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 +0200241 if (ptr)
242 stktable_data_cast(ptr, http_err_cnt)++;
243
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100244 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 +0200245 if (ptr)
246 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100247 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200248 }
249}
250
Simon Hormanaf514952011-06-21 14:34:57 +0900251static void inline session_add_srv_conn(struct session *sess, struct server *srv)
252{
253 sess->srv_conn = srv;
254 LIST_ADD(&srv->actconns, &sess->by_srv);
255}
256
257static void inline session_del_srv_conn(struct session *sess)
258{
259 if (!sess->srv_conn)
260 return;
261
262 sess->srv_conn = NULL;
263 LIST_DEL(&sess->by_srv);
264}
265
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200266static void inline session_init_srv_conn(struct session *sess)
267{
268 sess->srv_conn = NULL;
269 LIST_INIT(&sess->by_srv);
270}
271
Willy Tarreaua24adf02014-11-27 01:11:56 +0100272static inline void session_offer_buffers()
273{
274 int avail;
275
276 if (LIST_ISEMPTY(&buffer_wq))
277 return;
278
279 /* all sessions will need 1 buffer, so we can stop waking up sessions
280 * once we have enough of them to eat all the buffers. Note that we
281 * don't really know if they are sessions or just other tasks, but
282 * that's a rough estimate. Similarly, for each cached event we'll need
283 * 1 buffer. If no buffer is currently used, always wake up the number
284 * of tasks we can offer a buffer based on what is allocated, and in
285 * any case at least one task per two reserved buffers.
286 */
287 avail = pool2_buffer->allocated - pool2_buffer->used - global.tune.reserved_bufs / 2;
288
289 if (avail > (int)run_queue)
290 __session_offer_buffers(avail);
291}
292
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293#endif /* _PROTO_SESSION_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200294
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295/*
296 * Local variables:
297 * c-indent-level: 8
298 * c-basic-offset: 8
299 * End:
300 */