Willy TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 2 | * 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 TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | #ifndef _PROTO_SESSION_H |
| 23 | #define _PROTO_SESSION_H |
| 24 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 25 | #include <common/config.h> |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 26 | #include <common/memory.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | #include <types/session.h> |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 28 | #include <proto/freq_ctr.h> |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 29 | #include <proto/stick_table.h> |
Willy TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 30 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 31 | extern struct pool_head *pool2_session; |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 32 | extern struct list sessions; |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 33 | |
Willy Tarreau | bc174aa | 2012-11-19 16:10:32 +0100 | [diff] [blame] | 34 | extern struct data_cb sess_conn_cb; |
| 35 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 36 | int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 38 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 39 | int init_session(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | a2a64e9 | 2011-09-07 23:01:56 +0200 | [diff] [blame] | 41 | /* kill a session and set the termination flags to <why> (one of SN_ERR_*) */ |
| 42 | void session_shutdown(struct session *session, int why); |
Simon Horman | dec5be4 | 2011-06-08 09:19:07 +0900 | [diff] [blame] | 43 | |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 44 | void session_process_counters(struct session *s); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 45 | void sess_change_server(struct session *sess, struct server *newsrv); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 46 | struct task *process_session(struct task *t); |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 47 | void default_srv_error(struct session *s, struct stream_interface *si); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 48 | int parse_track_counters(char **args, int *arg, |
| 49 | int section_type, struct proxy *curpx, |
| 50 | struct track_ctr_prm *prm, |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 51 | struct proxy *defpx, char **err); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 52 | |
Willy Tarreau | 787add2 | 2013-11-23 23:37:04 +0100 | [diff] [blame] | 53 | /* returns the session from a void *owner */ |
| 54 | static inline struct session *session_from_task(struct task *t) |
| 55 | { |
| 56 | return (struct session *)t->context; |
| 57 | } |
| 58 | |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 59 | /* Remove the refcount from the session to the tracked counters, and clear the |
| 60 | * pointer to ensure this is only performed once. The caller is responsible for |
| 61 | * ensuring that the pointer is valid first. |
| 62 | */ |
| 63 | static inline void session_store_counters(struct session *s) |
| 64 | { |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 65 | void *ptr; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 66 | int i; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 67 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 68 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 69 | if (!s->stkctr[i].entry) |
| 70 | continue; |
| 71 | ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_CONN_CUR); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 72 | if (ptr) |
| 73 | stktable_data_cast(ptr, conn_cur)--; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 74 | s->stkctr[i].entry->ref_cnt--; |
| 75 | stksess_kill_if_expired(s->stkctr[i].table, s->stkctr[i].entry); |
| 76 | s->stkctr[i].entry = NULL; |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 77 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 80 | /* Remove the refcount from the session counters tracked only by the backend if |
| 81 | * any, and clear the pointer to ensure this is only performed once. The caller |
| 82 | * is responsible for ensuring that the pointer is valid first. |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 83 | */ |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 84 | static inline void session_stop_backend_counters(struct session *s) |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 85 | { |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 86 | void *ptr; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 87 | int i; |
Willy Tarreau | e348793 | 2010-06-18 21:03:20 +0200 | [diff] [blame] | 88 | |
Willy Tarreau | d5ca9ab | 2013-05-28 17:40:25 +0200 | [diff] [blame] | 89 | if (likely(!(s->flags & SN_BE_TRACK_ANY))) |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 90 | return; |
Willy Tarreau | e348793 | 2010-06-18 21:03:20 +0200 | [diff] [blame] | 91 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 92 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 93 | if (!s->stkctr[i].entry) |
| 94 | continue; |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 95 | |
Willy Tarreau | be4a3ef | 2013-06-17 15:04:07 +0200 | [diff] [blame] | 96 | if (!(s->flags & (SN_BE_TRACK_SC0 << i))) |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 97 | continue; |
| 98 | |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 99 | ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_CONN_CUR); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 100 | if (ptr) |
| 101 | stktable_data_cast(ptr, conn_cur)--; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 102 | s->stkctr[i].entry->ref_cnt--; |
| 103 | stksess_kill_if_expired(s->stkctr[i].table, s->stkctr[i].entry); |
| 104 | s->stkctr[i].entry = NULL; |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 105 | } |
Willy Tarreau | d5ca9ab | 2013-05-28 17:40:25 +0200 | [diff] [blame] | 106 | s->flags &= ~SN_BE_TRACK_ANY; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 107 | } |
Willy Tarreau | e348793 | 2010-06-18 21:03:20 +0200 | [diff] [blame] | 108 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 109 | /* 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 | */ |
| 113 | static inline void session_start_counters(struct stktable *t, struct stksess *ts) |
| 114 | { |
| 115 | void *ptr; |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 117 | 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 Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 133 | /* Enable tracking of session counters as <stkctr> on stksess <ts>. The caller is |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 134 | * responsible for ensuring that <t> and <ts> are valid pointers. Some controls |
| 135 | * are performed to ensure the state can still change. |
| 136 | */ |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 137 | static inline void session_track_stkctr(struct stkctr *ctr, struct stktable *t, struct stksess *ts) |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 138 | { |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 139 | if (ctr->entry) |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 140 | return; |
| 141 | |
| 142 | ts->ref_cnt++; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 143 | ctr->table = t; |
| 144 | ctr->entry = ts; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 145 | session_start_counters(t, ts); |
| 146 | } |
| 147 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 148 | /* Increase the number of cumulated HTTP requests in the tracked counters */ |
| 149 | static void inline session_inc_http_req_ctr(struct session *s) |
| 150 | { |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 151 | void *ptr; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 152 | int i; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 153 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 154 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 155 | if (!s->stkctr[i].entry) |
| 156 | continue; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 157 | |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 158 | ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_REQ_CNT); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 159 | if (ptr) |
| 160 | stktable_data_cast(ptr, http_req_cnt)++; |
| 161 | |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 162 | ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_REQ_RATE); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 163 | if (ptr) |
| 164 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 165 | s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 169 | /* Increase the number of cumulated HTTP requests in the backend's tracked counters */ |
| 170 | static void inline session_inc_be_http_req_ctr(struct session *s) |
| 171 | { |
| 172 | void *ptr; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 173 | int i; |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 174 | |
Willy Tarreau | d5ca9ab | 2013-05-28 17:40:25 +0200 | [diff] [blame] | 175 | if (likely(!(s->flags & SN_BE_TRACK_ANY))) |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 176 | return; |
| 177 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 178 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 179 | if (!s->stkctr[i].entry) |
| 180 | continue; |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 181 | |
Willy Tarreau | be4a3ef | 2013-06-17 15:04:07 +0200 | [diff] [blame] | 182 | if (!(s->flags & (SN_BE_TRACK_SC0 << i))) |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 183 | continue; |
| 184 | |
| 185 | ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_REQ_CNT); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 186 | if (ptr) |
| 187 | stktable_data_cast(ptr, http_req_cnt)++; |
| 188 | |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 189 | ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_REQ_RATE); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 190 | if (ptr) |
| 191 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 192 | s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 196 | /* Increase the number of cumulated failed HTTP requests in the tracked |
| 197 | * counters. Only 4xx requests should be counted here so that we can |
| 198 | * distinguish between errors caused by client behaviour and other ones. |
| 199 | * Note that even 404 are interesting because they're generally caused by |
| 200 | * vulnerability scans. |
| 201 | */ |
| 202 | static void inline session_inc_http_err_ctr(struct session *s) |
| 203 | { |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 204 | void *ptr; |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 205 | int i; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 206 | |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 207 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 208 | if (!s->stkctr[i].entry) |
| 209 | continue; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 210 | |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 211 | ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_ERR_CNT); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 212 | if (ptr) |
| 213 | stktable_data_cast(ptr, http_err_cnt)++; |
| 214 | |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 215 | ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_ERR_RATE); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 216 | if (ptr) |
| 217 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 218 | s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 222 | static void inline session_add_srv_conn(struct session *sess, struct server *srv) |
| 223 | { |
| 224 | sess->srv_conn = srv; |
| 225 | LIST_ADD(&srv->actconns, &sess->by_srv); |
| 226 | } |
| 227 | |
| 228 | static void inline session_del_srv_conn(struct session *sess) |
| 229 | { |
| 230 | if (!sess->srv_conn) |
| 231 | return; |
| 232 | |
| 233 | sess->srv_conn = NULL; |
| 234 | LIST_DEL(&sess->by_srv); |
| 235 | } |
| 236 | |
Willy Tarreau | 9bd0d74 | 2011-07-20 00:17:39 +0200 | [diff] [blame] | 237 | static void inline session_init_srv_conn(struct session *sess) |
| 238 | { |
| 239 | sess->srv_conn = NULL; |
| 240 | LIST_INIT(&sess->by_srv); |
| 241 | } |
| 242 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 243 | #endif /* _PROTO_SESSION_H */ |
Willy TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 244 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 245 | /* |
| 246 | * Local variables: |
| 247 | * c-indent-level: 8 |
| 248 | * c-basic-offset: 8 |
| 249 | * End: |
| 250 | */ |