blob: c835bf0778fea17c0e9c772b53887909093cefa6 [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 Tarreau9ba2dcc2010-06-14 21:04:55 +020048int parse_track_counters(char **args, int *arg,
49 int section_type, struct proxy *curpx,
50 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +020051 struct proxy *defpx, char **err);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020052
Willy Tarreau4bfc5802014-06-17 12:19:18 +020053/* Update the session's backend and server time stats */
54void session_update_time_stats(struct session *s);
55
Willy Tarreau787add22013-11-23 23:37:04 +010056/* returns the session from a void *owner */
57static inline struct session *session_from_task(struct task *t)
58{
59 return (struct session *)t->context;
60}
61
Willy Tarreaucc08d2c2014-01-28 23:18:23 +010062/* sets the stick counter's entry pointer */
63static inline void stkctr_set_entry(struct stkctr *stkctr, struct stksess *entry)
64{
65 stkctr->entry = caddr_from_ptr(entry, 0);
66}
67
68/* returns the entry pointer from a stick counter */
69static inline struct stksess *stkctr_entry(struct stkctr *stkctr)
70{
71 return caddr_to_ptr(stkctr->entry);
72}
73
74/* returns the two flags from a stick counter */
75static inline unsigned int stkctr_flags(struct stkctr *stkctr)
76{
77 return caddr_to_data(stkctr->entry);
78}
79
80/* sets up to two flags at a time on a composite address */
81static inline void stkctr_set_flags(struct stkctr *stkctr, unsigned int flags)
82{
83 stkctr->entry = caddr_set_flags(stkctr->entry, flags);
84}
85
86/* returns the two flags from a stick counter */
87static inline void stkctr_clr_flags(struct stkctr *stkctr, unsigned int flags)
88{
89 stkctr->entry = caddr_clr_flags(stkctr->entry, flags);
90}
91
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020092/* Remove the refcount from the session to the tracked counters, and clear the
93 * pointer to ensure this is only performed once. The caller is responsible for
94 * ensuring that the pointer is valid first.
95 */
96static inline void session_store_counters(struct session *s)
97{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020098 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +010099 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200100
Willy Tarreaub4c84932013-07-23 19:15:30 +0200101 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100102 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100103 continue;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100104 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_CONN_CUR);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200105 if (ptr)
106 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100107 stkctr_entry(&s->stkctr[i])->ref_cnt--;
108 stksess_kill_if_expired(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]));
109 stkctr_set_entry(&s->stkctr[i], NULL);
Willy Tarreau38285c12010-06-18 16:35:43 +0200110 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200111}
112
Willy Tarreauf3338342014-01-28 21:40:28 +0100113/* Remove the refcount from the session counters tracked at the content level if
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200114 * any, and clear the pointer to ensure this is only performed once. The caller
115 * is responsible for ensuring that the pointer is valid first.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200116 */
Willy Tarreauf3338342014-01-28 21:40:28 +0100117static inline void session_stop_content_counters(struct session *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200118{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200119 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100120 int i;
Willy Tarreaue3487932010-06-18 21:03:20 +0200121
Willy Tarreaub4c84932013-07-23 19:15:30 +0200122 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100123 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100124 continue;
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200125
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100126 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_CONTENT))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100127 continue;
128
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100129 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_CONN_CUR);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200130 if (ptr)
131 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100132 stkctr_entry(&s->stkctr[i])->ref_cnt--;
133 stksess_kill_if_expired(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]));
134 stkctr_set_entry(&s->stkctr[i], NULL);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200135 }
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200136}
Willy Tarreaue3487932010-06-18 21:03:20 +0200137
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200138/* Increase total and concurrent connection count for stick entry <ts> of table
139 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
140 * pointers, and for calling this only once per connection.
141 */
142static inline void session_start_counters(struct stktable *t, struct stksess *ts)
143{
144 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200145
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200146 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
147 if (ptr)
148 stktable_data_cast(ptr, conn_cur)++;
149
150 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
151 if (ptr)
152 stktable_data_cast(ptr, conn_cnt)++;
153
154 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
155 if (ptr)
156 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
157 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
158 if (tick_isset(t->expire))
159 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
160}
161
Willy Tarreau20d46a52012-12-09 15:55:40 +0100162/* Enable tracking of session counters as <stkctr> on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200163 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
164 * are performed to ensure the state can still change.
165 */
Willy Tarreau20d46a52012-12-09 15:55:40 +0100166static inline void session_track_stkctr(struct stkctr *ctr, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200167{
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100168 if (stkctr_entry(ctr))
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200169 return;
170
171 ts->ref_cnt++;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100172 ctr->table = t;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100173 stkctr_set_entry(ctr, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200174 session_start_counters(t, ts);
175}
176
Willy Tarreauda7ff642010-06-23 11:44:09 +0200177/* Increase the number of cumulated HTTP requests in the tracked counters */
178static void inline session_inc_http_req_ctr(struct session *s)
179{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200180 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100181 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200182
Willy Tarreaub4c84932013-07-23 19:15:30 +0200183 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100184 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100185 continue;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200186
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100187 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 +0200188 if (ptr)
189 stktable_data_cast(ptr, http_req_cnt)++;
190
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100191 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 +0200192 if (ptr)
193 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100194 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200195 }
196}
197
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100198/* Increase the number of cumulated HTTP requests in the backend's tracked counters */
199static void inline session_inc_be_http_req_ctr(struct session *s)
200{
201 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100202 int i;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100203
Willy Tarreaub4c84932013-07-23 19:15:30 +0200204 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100205 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100206 continue;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100207
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100208 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_BACKEND))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100209 continue;
210
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100211 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 +0100212 if (ptr)
213 stktable_data_cast(ptr, http_req_cnt)++;
214
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100215 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 +0100216 if (ptr)
217 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100218 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100219 }
220}
221
Willy Tarreauda7ff642010-06-23 11:44:09 +0200222/* Increase the number of cumulated failed HTTP requests in the tracked
223 * counters. Only 4xx requests should be counted here so that we can
224 * distinguish between errors caused by client behaviour and other ones.
225 * Note that even 404 are interesting because they're generally caused by
226 * vulnerability scans.
227 */
228static void inline session_inc_http_err_ctr(struct session *s)
229{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200230 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100231 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200232
Willy Tarreaub4c84932013-07-23 19:15:30 +0200233 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100234 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100235 continue;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200236
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100237 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 +0200238 if (ptr)
239 stktable_data_cast(ptr, http_err_cnt)++;
240
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100241 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 +0200242 if (ptr)
243 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100244 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200245 }
246}
247
Simon Hormanaf514952011-06-21 14:34:57 +0900248static void inline session_add_srv_conn(struct session *sess, struct server *srv)
249{
250 sess->srv_conn = srv;
251 LIST_ADD(&srv->actconns, &sess->by_srv);
252}
253
254static void inline session_del_srv_conn(struct session *sess)
255{
256 if (!sess->srv_conn)
257 return;
258
259 sess->srv_conn = NULL;
260 LIST_DEL(&sess->by_srv);
261}
262
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200263static void inline session_init_srv_conn(struct session *sess)
264{
265 sess->srv_conn = NULL;
266 LIST_INIT(&sess->by_srv);
267}
268
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269#endif /* _PROTO_SESSION_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200270
Willy Tarreaubaaee002006-06-26 02:48:02 +0200271/*
272 * Local variables:
273 * c-indent-level: 8
274 * c-basic-offset: 8
275 * End:
276 */