blob: 2389a79934ac35ef9a08dbd4c37b766247d4f2bf [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 Tarreaubc174aa2012-11-19 16:10:32 +010034extern struct data_cb sess_conn_cb;
35
Willy Tarreau81f9aa32010-06-01 17:45:26 +020036int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020038/* perform minimal intializations, report 0 in case of error, 1 if OK. */
39int init_session();
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreaua2a64e92011-09-07 23:01:56 +020041/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
42void session_shutdown(struct session *session, int why);
Simon Hormandec5be42011-06-08 09:19:07 +090043
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +010044void session_process_counters(struct session *s);
Willy Tarreau7c669d72008-06-20 15:04:11 +020045void sess_change_server(struct session *sess, struct server *newsrv);
Willy Tarreau26c25062009-03-08 09:38:41 +010046struct task *process_session(struct task *t);
Willy Tarreau84455332009-03-15 22:34:05 +010047void default_srv_error(struct session *s, struct stream_interface *si);
Willy Tarreaue12704b2014-07-15 19:06:18 +020048struct stkctr *smp_fetch_sc_stkctr(struct session *l4, const struct arg *args, const char *kw);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020049int parse_track_counters(char **args, int *arg,
50 int section_type, struct proxy *curpx,
51 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +020052 struct proxy *defpx, char **err);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020053
Willy Tarreau4bfc5802014-06-17 12:19:18 +020054/* Update the session's backend and server time stats */
55void session_update_time_stats(struct session *s);
56
Willy Tarreau787add22013-11-23 23:37:04 +010057/* returns the session from a void *owner */
58static inline struct session *session_from_task(struct task *t)
59{
60 return (struct session *)t->context;
61}
62
Willy Tarreaucc08d2c2014-01-28 23:18:23 +010063/* sets the stick counter's entry pointer */
64static inline void stkctr_set_entry(struct stkctr *stkctr, struct stksess *entry)
65{
66 stkctr->entry = caddr_from_ptr(entry, 0);
67}
68
69/* returns the entry pointer from a stick counter */
70static inline struct stksess *stkctr_entry(struct stkctr *stkctr)
71{
72 return caddr_to_ptr(stkctr->entry);
73}
74
75/* returns the two flags from a stick counter */
76static inline unsigned int stkctr_flags(struct stkctr *stkctr)
77{
78 return caddr_to_data(stkctr->entry);
79}
80
81/* sets up to two flags at a time on a composite address */
82static inline void stkctr_set_flags(struct stkctr *stkctr, unsigned int flags)
83{
84 stkctr->entry = caddr_set_flags(stkctr->entry, flags);
85}
86
87/* returns the two flags from a stick counter */
88static inline void stkctr_clr_flags(struct stkctr *stkctr, unsigned int flags)
89{
90 stkctr->entry = caddr_clr_flags(stkctr->entry, flags);
91}
92
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020093/* Remove the refcount from the session to the tracked counters, and clear the
94 * pointer to ensure this is only performed once. The caller is responsible for
95 * ensuring that the pointer is valid first.
96 */
97static inline void session_store_counters(struct session *s)
98{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020099 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100100 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200101
Willy Tarreaub4c84932013-07-23 19:15:30 +0200102 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100103 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100104 continue;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100105 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_CONN_CUR);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200106 if (ptr)
107 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100108 stkctr_entry(&s->stkctr[i])->ref_cnt--;
109 stksess_kill_if_expired(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]));
110 stkctr_set_entry(&s->stkctr[i], NULL);
Willy Tarreau38285c12010-06-18 16:35:43 +0200111 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200112}
113
Willy Tarreauf3338342014-01-28 21:40:28 +0100114/* Remove the refcount from the session counters tracked at the content level if
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200115 * any, and clear the pointer to ensure this is only performed once. The caller
116 * is responsible for ensuring that the pointer is valid first.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200117 */
Willy Tarreauf3338342014-01-28 21:40:28 +0100118static inline void session_stop_content_counters(struct session *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200119{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200120 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100121 int i;
Willy Tarreaue3487932010-06-18 21:03:20 +0200122
Willy Tarreaub4c84932013-07-23 19:15:30 +0200123 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100124 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100125 continue;
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200126
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100127 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_CONTENT))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100128 continue;
129
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100130 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_CONN_CUR);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200131 if (ptr)
132 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100133 stkctr_entry(&s->stkctr[i])->ref_cnt--;
134 stksess_kill_if_expired(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]));
135 stkctr_set_entry(&s->stkctr[i], NULL);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200136 }
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200137}
Willy Tarreaue3487932010-06-18 21:03:20 +0200138
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200139/* Increase total and concurrent connection count for stick entry <ts> of table
140 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
141 * pointers, and for calling this only once per connection.
142 */
143static inline void session_start_counters(struct stktable *t, struct stksess *ts)
144{
145 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200146
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200147 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
148 if (ptr)
149 stktable_data_cast(ptr, conn_cur)++;
150
151 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
152 if (ptr)
153 stktable_data_cast(ptr, conn_cnt)++;
154
155 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
156 if (ptr)
157 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
158 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
159 if (tick_isset(t->expire))
160 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
161}
162
Willy Tarreau20d46a52012-12-09 15:55:40 +0100163/* Enable tracking of session counters as <stkctr> on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200164 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
165 * are performed to ensure the state can still change.
166 */
Willy Tarreau20d46a52012-12-09 15:55:40 +0100167static inline void session_track_stkctr(struct stkctr *ctr, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200168{
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100169 if (stkctr_entry(ctr))
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200170 return;
171
172 ts->ref_cnt++;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100173 ctr->table = t;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100174 stkctr_set_entry(ctr, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200175 session_start_counters(t, ts);
176}
177
Willy Tarreauda7ff642010-06-23 11:44:09 +0200178/* Increase the number of cumulated HTTP requests in the tracked counters */
179static void inline session_inc_http_req_ctr(struct session *s)
180{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200181 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100182 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200183
Willy Tarreaub4c84932013-07-23 19:15:30 +0200184 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100185 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100186 continue;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200187
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100188 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 +0200189 if (ptr)
190 stktable_data_cast(ptr, http_req_cnt)++;
191
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100192 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 +0200193 if (ptr)
194 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100195 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200196 }
197}
198
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100199/* Increase the number of cumulated HTTP requests in the backend's tracked counters */
200static void inline session_inc_be_http_req_ctr(struct session *s)
201{
202 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100203 int i;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100204
Willy Tarreaub4c84932013-07-23 19:15:30 +0200205 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100206 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100207 continue;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100208
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100209 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_BACKEND))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100210 continue;
211
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100212 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 +0100213 if (ptr)
214 stktable_data_cast(ptr, http_req_cnt)++;
215
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100216 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 +0100217 if (ptr)
218 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100219 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100220 }
221}
222
Willy Tarreauda7ff642010-06-23 11:44:09 +0200223/* Increase the number of cumulated failed HTTP requests in the tracked
224 * counters. Only 4xx requests should be counted here so that we can
225 * distinguish between errors caused by client behaviour and other ones.
226 * Note that even 404 are interesting because they're generally caused by
227 * vulnerability scans.
228 */
229static void inline session_inc_http_err_ctr(struct session *s)
230{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200231 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100232 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200233
Willy Tarreaub4c84932013-07-23 19:15:30 +0200234 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100235 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100236 continue;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200237
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100238 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 +0200239 if (ptr)
240 stktable_data_cast(ptr, http_err_cnt)++;
241
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100242 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 +0200243 if (ptr)
244 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100245 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200246 }
247}
248
Simon Hormanaf514952011-06-21 14:34:57 +0900249static void inline session_add_srv_conn(struct session *sess, struct server *srv)
250{
251 sess->srv_conn = srv;
252 LIST_ADD(&srv->actconns, &sess->by_srv);
253}
254
255static void inline session_del_srv_conn(struct session *sess)
256{
257 if (!sess->srv_conn)
258 return;
259
260 sess->srv_conn = NULL;
261 LIST_DEL(&sess->by_srv);
262}
263
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200264static void inline session_init_srv_conn(struct session *sess)
265{
266 sess->srv_conn = NULL;
267 LIST_INIT(&sess->by_srv);
268}
269
Willy Tarreaubaaee002006-06-26 02:48:02 +0200270#endif /* _PROTO_SESSION_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200271
Willy Tarreaubaaee002006-06-26 02:48:02 +0200272/*
273 * Local variables:
274 * c-indent-level: 8
275 * c-basic-offset: 8
276 * End:
277 */