blob: a80f97dc132b3981bece0d229a788855395ed924 [file] [log] [blame]
Willy Tarreaub1ec8c42015-04-03 13:53:24 +02001/*
2 * include/proto/session.h
3 * This file defines everything related to sessions.
4 *
5 * Copyright (C) 2000-2015 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 */
21
22#ifndef _PROTO_SESSION_H
23#define _PROTO_SESSION_H
24
25#include <common/config.h>
26#include <common/buffer.h>
27#include <common/debug.h>
28#include <common/memory.h>
29
30#include <types/global.h>
31#include <types/session.h>
32
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +010033#include <proto/obj_type.h>
Willy Tarreaubb2ef122015-04-04 16:31:16 +020034#include <proto/stick_table.h>
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +010035#include <proto/server.h>
Willy Tarreaubb2ef122015-04-04 16:31:16 +020036
Willy Tarreaubafbe012017-11-24 17:34:44 +010037extern struct pool_head *pool_head_session;
Olivier Houchard351411f2018-12-27 17:20:54 +010038extern struct pool_head *pool_head_sess_srv_list;
39
Willy Tarreauc38f71c2015-04-05 00:38:48 +020040struct session *session_new(struct proxy *fe, struct listener *li, enum obj_type *origin);
Willy Tarreau11c36242015-04-04 15:54:03 +020041void session_free(struct session *sess);
Willy Tarreau9903f0e2015-04-04 18:50:31 +020042int session_accept_fd(struct listener *l, int cfd, struct sockaddr_storage *addr);
Willy Tarreaub1ec8c42015-04-03 13:53:24 +020043
Willy Tarreaubb2ef122015-04-04 16:31:16 +020044/* Remove the refcount from the session to the tracked counters, and clear the
45 * pointer to ensure this is only performed once. The caller is responsible for
46 * ensuring that the pointer is valid first.
47 */
48static inline void session_store_counters(struct session *sess)
49{
50 void *ptr;
51 int i;
Emeric Brun819fc6f2017-06-13 19:37:32 +020052 struct stksess *ts;
Willy Tarreaubb2ef122015-04-04 16:31:16 +020053
54 for (i = 0; i < MAX_SESS_STKCTR; i++) {
55 struct stkctr *stkctr = &sess->stkctr[i];
56
Emeric Brun819fc6f2017-06-13 19:37:32 +020057 ts = stkctr_entry(stkctr);
58 if (!ts)
Willy Tarreaubb2ef122015-04-04 16:31:16 +020059 continue;
60
Emeric Brun819fc6f2017-06-13 19:37:32 +020061 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_CONN_CUR);
62 if (ptr) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +010063 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +020064
Tim Duesterhus8b87c012019-01-04 00:11:59 +010065 if (stktable_data_cast(ptr, conn_cur) > 0)
66 stktable_data_cast(ptr, conn_cur)--;
Emeric Brun819fc6f2017-06-13 19:37:32 +020067
Christopher Faulet2a944ee2017-11-07 10:42:54 +010068 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +010069
70 /* If data was modified, we need to touch to re-schedule sync */
71 stktable_touch_local(stkctr->table, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +020072 }
73
Willy Tarreaubb2ef122015-04-04 16:31:16 +020074 stkctr_set_entry(stkctr, NULL);
Emeric Brun819fc6f2017-06-13 19:37:32 +020075 stksess_kill_if_expired(stkctr->table, ts, 1);
Willy Tarreaubb2ef122015-04-04 16:31:16 +020076 }
77}
78
Olivier Houchard351411f2018-12-27 17:20:54 +010079/* Remove the connection from the session list, and destroy the srv_list if it's now empty */
80static inline void session_unown_conn(struct session *sess, struct connection *conn)
Olivier Houchard00cf70f2018-11-30 17:24:55 +010081{
Olivier Houchard351411f2018-12-27 17:20:54 +010082 struct sess_srv_list *srv_list = NULL;
83 LIST_DEL(&conn->session_list);
84 LIST_INIT(&conn->session_list);
85 list_for_each_entry(srv_list, &sess->srv_list, srv_list) {
86 if (srv_list->target == conn->target) {
87 if (LIST_ISEMPTY(&srv_list->conn_list)) {
88 LIST_DEL(&srv_list->srv_list);
89 pool_free(pool_head_sess_srv_list, srv_list);
90 }
Olivier Houchard00cf70f2018-11-30 17:24:55 +010091 break;
92 }
Olivier Houchard00cf70f2018-11-30 17:24:55 +010093 }
Olivier Houchard351411f2018-12-27 17:20:54 +010094}
95
96static inline int session_add_conn(struct session *sess, struct connection *conn, void *target)
97{
98 struct sess_srv_list *srv_list = NULL;
99 int found = 0;
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100100
Olivier Houchard351411f2018-12-27 17:20:54 +0100101 list_for_each_entry(srv_list, &sess->srv_list, srv_list) {
102 if (srv_list->target == target) {
103 found = 1;
104 break;
105 }
106 }
107 if (!found) {
108 /* The session has no connection for the server, create a new entry */
109 srv_list = pool_alloc(pool_head_sess_srv_list);
110 if (!srv_list)
111 return 0;
112 srv_list->target = target;
113 LIST_INIT(&srv_list->conn_list);
114 LIST_ADDQ(&sess->srv_list, &srv_list->srv_list);
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100115 }
Olivier Houchard351411f2018-12-27 17:20:54 +0100116 LIST_ADDQ(&srv_list->conn_list, &conn->session_list);
117 return 1;
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100118}
119
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100120/* Returns 0 if the session can keep the idle conn, -1 if it was destroyed, or 1 if it was added to the server list */
121static inline int session_check_idle_conn(struct session *sess, struct connection *conn)
122{
Olivier Houchardb2eb3262019-07-04 14:26:41 +0200123 if (sess->idle_conns >= sess->fe->max_out_conns) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100124 /* We can't keep the connection, let's try to add it to the server idle list */
Olivier Houchard351411f2018-12-27 17:20:54 +0100125 session_unown_conn(sess, conn);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100126 conn->owner = NULL;
Olivier Houcharda48237f2019-05-02 12:04:15 +0200127 conn->flags &= ~CO_FL_SESS_IDLE;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100128 if (!srv_add_to_idle_list(objt_server(conn->target), conn)) {
129 /* The server doesn't want it, let's kill the connection right away */
Christopher Faulet73c12072019-04-08 11:23:22 +0200130 conn->mux->destroy(conn->ctx);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100131 return -1;
Olivier Houcharda48237f2019-05-02 12:04:15 +0200132 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100133 return 1;
Olivier Houcharda2dbeb22018-12-28 18:50:57 +0100134 } else {
135 conn->flags |= CO_FL_SESS_IDLE;
136 sess->idle_conns++;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100137 }
138 return 0;
139}
Willy Tarreaubb2ef122015-04-04 16:31:16 +0200140
Willy Tarreaub1ec8c42015-04-03 13:53:24 +0200141#endif /* _PROTO_SESSION_H */
142
143/*
144 * Local variables:
145 * c-indent-level: 8
146 * c-basic-offset: 8
147 * End:
148 */