blob: 049fc931c61822b24f9002decaf01e7c139873e0 [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 Tarreau91c43d72010-06-20 11:19:22 +020028#include <proto/freq_ctr.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020029#include <proto/stick_table.h>
Willy TARREAU3dc06442006-06-15 21:48:13 +020030
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020031extern struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010032extern struct list sessions;
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020033
Willy Tarreau81f9aa32010-06-01 17:45:26 +020034int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020036/* perform minimal intializations, report 0 in case of error, 1 if OK. */
37int init_session();
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
Willy Tarreaua2a64e92011-09-07 23:01:56 +020039/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
40void session_shutdown(struct session *session, int why);
Simon Hormandec5be42011-06-08 09:19:07 +090041
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +010042void session_process_counters(struct session *s);
Willy Tarreau7c669d72008-06-20 15:04:11 +020043void sess_change_server(struct session *sess, struct server *newsrv);
Willy Tarreau26c25062009-03-08 09:38:41 +010044struct task *process_session(struct task *t);
Willy Tarreau84455332009-03-15 22:34:05 +010045void default_srv_error(struct session *s, struct stream_interface *si);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020046int parse_track_counters(char **args, int *arg,
47 int section_type, struct proxy *curpx,
48 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +020049 struct proxy *defpx, char **err);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020050
51/* Remove the refcount from the session to the tracked counters, and clear the
52 * pointer to ensure this is only performed once. The caller is responsible for
53 * ensuring that the pointer is valid first.
54 */
55static inline void session_store_counters(struct session *s)
56{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020057 void *ptr;
58
Willy Tarreau56123282010-08-06 19:06:56 +020059 if (s->stkctr2_entry) {
60 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_CONN_CUR);
Willy Tarreauf059a0f2010-08-03 16:29:52 +020061 if (ptr)
62 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreau56123282010-08-06 19:06:56 +020063 s->stkctr2_entry->ref_cnt--;
64 stksess_kill_if_expired(s->stkctr2_table, s->stkctr2_entry);
65 s->stkctr2_entry = NULL;
Willy Tarreauf059a0f2010-08-03 16:29:52 +020066 }
67
Willy Tarreau56123282010-08-06 19:06:56 +020068 if (s->stkctr1_entry) {
69 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_CONN_CUR);
Willy Tarreau38285c12010-06-18 16:35:43 +020070 if (ptr)
71 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreau56123282010-08-06 19:06:56 +020072 s->stkctr1_entry->ref_cnt--;
73 stksess_kill_if_expired(s->stkctr1_table, s->stkctr1_entry);
74 s->stkctr1_entry = NULL;
Willy Tarreau38285c12010-06-18 16:35:43 +020075 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020076}
77
Willy Tarreauf059a0f2010-08-03 16:29:52 +020078/* Remove the refcount from the session counters tracked only by the backend if
79 * any, and clear the pointer to ensure this is only performed once. The caller
80 * is responsible for ensuring that the pointer is valid first.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020081 */
Willy Tarreauf059a0f2010-08-03 16:29:52 +020082static inline void session_stop_backend_counters(struct session *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020083{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020084 void *ptr;
Willy Tarreaue3487932010-06-18 21:03:20 +020085
Willy Tarreau0a4838c2010-08-06 20:11:05 +020086 if (!(s->flags & (SN_BE_TRACK_SC1|SN_BE_TRACK_SC2)))
Willy Tarreauf059a0f2010-08-03 16:29:52 +020087 return;
Willy Tarreaue3487932010-06-18 21:03:20 +020088
Willy Tarreau0a4838c2010-08-06 20:11:05 +020089 if ((s->flags & SN_BE_TRACK_SC1) && s->stkctr1_entry) {
90 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_CONN_CUR);
91 if (ptr)
92 stktable_data_cast(ptr, conn_cur)--;
93 s->stkctr1_entry->ref_cnt--;
94 stksess_kill_if_expired(s->stkctr1_table, s->stkctr1_entry);
95 s->stkctr1_entry = NULL;
96 }
97
98 if ((s->flags & SN_BE_TRACK_SC2) && s->stkctr2_entry) {
99 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_CONN_CUR);
100 if (ptr)
101 stktable_data_cast(ptr, conn_cur)--;
102 s->stkctr2_entry->ref_cnt--;
103 stksess_kill_if_expired(s->stkctr2_table, s->stkctr2_entry);
104 s->stkctr2_entry = NULL;
105 }
106 s->flags &= ~(SN_BE_TRACK_SC1|SN_BE_TRACK_SC2);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200107}
Willy Tarreaue3487932010-06-18 21:03:20 +0200108
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200109/* Increase total and concurrent connection count for stick entry <ts> of table
110 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
111 * pointers, and for calling this only once per connection.
112 */
113static inline void session_start_counters(struct stktable *t, struct stksess *ts)
114{
115 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200116
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200117 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
118 if (ptr)
119 stktable_data_cast(ptr, conn_cur)++;
120
121 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
122 if (ptr)
123 stktable_data_cast(ptr, conn_cnt)++;
124
125 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
126 if (ptr)
127 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
128 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
129 if (tick_isset(t->expire))
130 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
131}
132
Willy Tarreau56123282010-08-06 19:06:56 +0200133/* Enable tracking of session counters as stkctr1 on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200134 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
135 * are performed to ensure the state can still change.
136 */
Willy Tarreau56123282010-08-06 19:06:56 +0200137static inline void session_track_stkctr1(struct session *s, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200138{
Willy Tarreau56123282010-08-06 19:06:56 +0200139 if (s->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200140 return;
141
142 ts->ref_cnt++;
Willy Tarreau56123282010-08-06 19:06:56 +0200143 s->stkctr1_table = t;
144 s->stkctr1_entry = ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200145 session_start_counters(t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200146}
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100147
Willy Tarreau56123282010-08-06 19:06:56 +0200148/* Enable tracking of session counters as stkctr1 on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200149 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
150 * are performed to ensure the state can still change.
151 */
Willy Tarreau56123282010-08-06 19:06:56 +0200152static inline void session_track_stkctr2(struct session *s, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200153{
Willy Tarreau56123282010-08-06 19:06:56 +0200154 if (s->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200155 return;
156
157 ts->ref_cnt++;
Willy Tarreau56123282010-08-06 19:06:56 +0200158 s->stkctr2_table = t;
159 s->stkctr2_entry = ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200160 session_start_counters(t, ts);
161}
162
Willy Tarreauf8533202008-08-16 14:55:08 +0200163static void inline trace_term(struct session *s, unsigned int code)
164{
165 s->term_trace <<= TT_BIT_SHIFT;
166 s->term_trace |= code;
167}
168
Willy Tarreauda7ff642010-06-23 11:44:09 +0200169/* Increase the number of cumulated HTTP requests in the tracked counters */
170static void inline session_inc_http_req_ctr(struct session *s)
171{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200172 void *ptr;
173
Willy Tarreau56123282010-08-06 19:06:56 +0200174 if (s->stkctr2_entry) {
175 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200176 if (ptr)
177 stktable_data_cast(ptr, http_req_cnt)++;
178
Willy Tarreau56123282010-08-06 19:06:56 +0200179 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200180 if (ptr)
181 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200182 s->stkctr2_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200183 }
Willy Tarreauda7ff642010-06-23 11:44:09 +0200184
Willy Tarreau56123282010-08-06 19:06:56 +0200185 if (s->stkctr1_entry) {
186 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200187 if (ptr)
188 stktable_data_cast(ptr, http_req_cnt)++;
189
Willy Tarreau56123282010-08-06 19:06:56 +0200190 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200191 if (ptr)
192 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200193 s->stkctr1_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200194 }
195}
196
197/* Increase the number of cumulated failed HTTP requests in the tracked
198 * counters. Only 4xx requests should be counted here so that we can
199 * distinguish between errors caused by client behaviour and other ones.
200 * Note that even 404 are interesting because they're generally caused by
201 * vulnerability scans.
202 */
203static void inline session_inc_http_err_ctr(struct session *s)
204{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200205 void *ptr;
206
Willy Tarreau56123282010-08-06 19:06:56 +0200207 if (s->stkctr2_entry) {
208 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200209 if (ptr)
210 stktable_data_cast(ptr, http_err_cnt)++;
211
Willy Tarreau56123282010-08-06 19:06:56 +0200212 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200213 if (ptr)
214 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200215 s->stkctr2_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200216 }
Willy Tarreauda7ff642010-06-23 11:44:09 +0200217
Willy Tarreau56123282010-08-06 19:06:56 +0200218 if (s->stkctr1_entry) {
219 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200220 if (ptr)
221 stktable_data_cast(ptr, http_err_cnt)++;
222
Willy Tarreau56123282010-08-06 19:06:56 +0200223 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200224 if (ptr)
225 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200226 s->stkctr1_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200227 }
228}
229
Simon Hormanaf514952011-06-21 14:34:57 +0900230static void inline session_add_srv_conn(struct session *sess, struct server *srv)
231{
232 sess->srv_conn = srv;
233 LIST_ADD(&srv->actconns, &sess->by_srv);
234}
235
236static void inline session_del_srv_conn(struct session *sess)
237{
238 if (!sess->srv_conn)
239 return;
240
241 sess->srv_conn = NULL;
242 LIST_DEL(&sess->by_srv);
243}
244
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200245static void inline session_init_srv_conn(struct session *sess)
246{
247 sess->srv_conn = NULL;
248 LIST_INIT(&sess->by_srv);
249}
250
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251#endif /* _PROTO_SESSION_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200252
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253/*
254 * Local variables:
255 * c-indent-level: 8
256 * c-basic-offset: 8
257 * End:
258 */