blob: a5b95a8deadf87c5fe76064c1c6b4eac944ccc00 [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);
Olivier Houchard477902b2020-01-22 18:08:48 +010043int conn_complete_session(struct connection *conn);
Willy Tarreaub1ec8c42015-04-03 13:53:24 +020044
Willy Tarreaubb2ef122015-04-04 16:31:16 +020045/* Remove the refcount from the session to the tracked counters, and clear the
46 * pointer to ensure this is only performed once. The caller is responsible for
47 * ensuring that the pointer is valid first.
48 */
49static inline void session_store_counters(struct session *sess)
50{
51 void *ptr;
52 int i;
Emeric Brun819fc6f2017-06-13 19:37:32 +020053 struct stksess *ts;
Willy Tarreaubb2ef122015-04-04 16:31:16 +020054
55 for (i = 0; i < MAX_SESS_STKCTR; i++) {
56 struct stkctr *stkctr = &sess->stkctr[i];
57
Emeric Brun819fc6f2017-06-13 19:37:32 +020058 ts = stkctr_entry(stkctr);
59 if (!ts)
Willy Tarreaubb2ef122015-04-04 16:31:16 +020060 continue;
61
Emeric Brun819fc6f2017-06-13 19:37:32 +020062 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_CONN_CUR);
63 if (ptr) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +010064 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +020065
Tim Duesterhus8b87c012019-01-04 00:11:59 +010066 if (stktable_data_cast(ptr, conn_cur) > 0)
67 stktable_data_cast(ptr, conn_cur)--;
Emeric Brun819fc6f2017-06-13 19:37:32 +020068
Christopher Faulet2a944ee2017-11-07 10:42:54 +010069 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +010070
71 /* If data was modified, we need to touch to re-schedule sync */
72 stktable_touch_local(stkctr->table, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +020073 }
74
Willy Tarreaubb2ef122015-04-04 16:31:16 +020075 stkctr_set_entry(stkctr, NULL);
Emeric Brun819fc6f2017-06-13 19:37:32 +020076 stksess_kill_if_expired(stkctr->table, ts, 1);
Willy Tarreaubb2ef122015-04-04 16:31:16 +020077 }
78}
79
Olivier Houchard351411f2018-12-27 17:20:54 +010080/* Remove the connection from the session list, and destroy the srv_list if it's now empty */
81static inline void session_unown_conn(struct session *sess, struct connection *conn)
Olivier Houchard00cf70f2018-11-30 17:24:55 +010082{
Olivier Houchard351411f2018-12-27 17:20:54 +010083 struct sess_srv_list *srv_list = NULL;
84 LIST_DEL(&conn->session_list);
85 LIST_INIT(&conn->session_list);
86 list_for_each_entry(srv_list, &sess->srv_list, srv_list) {
87 if (srv_list->target == conn->target) {
88 if (LIST_ISEMPTY(&srv_list->conn_list)) {
89 LIST_DEL(&srv_list->srv_list);
90 pool_free(pool_head_sess_srv_list, srv_list);
91 }
Olivier Houchard00cf70f2018-11-30 17:24:55 +010092 break;
93 }
Olivier Houchard00cf70f2018-11-30 17:24:55 +010094 }
Olivier Houchard351411f2018-12-27 17:20:54 +010095}
96
97static inline int session_add_conn(struct session *sess, struct connection *conn, void *target)
98{
99 struct sess_srv_list *srv_list = NULL;
100 int found = 0;
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100101
Olivier Houchard351411f2018-12-27 17:20:54 +0100102 list_for_each_entry(srv_list, &sess->srv_list, srv_list) {
103 if (srv_list->target == target) {
104 found = 1;
105 break;
106 }
107 }
108 if (!found) {
109 /* The session has no connection for the server, create a new entry */
110 srv_list = pool_alloc(pool_head_sess_srv_list);
111 if (!srv_list)
112 return 0;
113 srv_list->target = target;
114 LIST_INIT(&srv_list->conn_list);
115 LIST_ADDQ(&sess->srv_list, &srv_list->srv_list);
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100116 }
Olivier Houchard351411f2018-12-27 17:20:54 +0100117 LIST_ADDQ(&srv_list->conn_list, &conn->session_list);
118 return 1;
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100119}
120
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100121/* 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 */
122static inline int session_check_idle_conn(struct session *sess, struct connection *conn)
123{
Olivier Houchard2444aa52020-01-20 13:56:01 +0100124 if (!(conn->flags & CO_FL_PRIVATE) ||
125 sess->idle_conns >= sess->fe->max_out_conns) {
Olivier Houchard351411f2018-12-27 17:20:54 +0100126 session_unown_conn(sess, conn);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100127 conn->owner = NULL;
Olivier Houcharda48237f2019-05-02 12:04:15 +0200128 conn->flags &= ~CO_FL_SESS_IDLE;
Christopher Fauletbfb0f722020-04-27 15:53:41 +0200129 conn->mux->destroy(conn->ctx);
Olivier Houchard2444aa52020-01-20 13:56:01 +0100130 return -1;
Olivier Houcharda2dbeb22018-12-28 18:50:57 +0100131 } else {
132 conn->flags |= CO_FL_SESS_IDLE;
133 sess->idle_conns++;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100134 }
135 return 0;
136}
Willy Tarreaubb2ef122015-04-04 16:31:16 +0200137
Willy Tarreaub1ec8c42015-04-03 13:53:24 +0200138#endif /* _PROTO_SESSION_H */
139
140/*
141 * Local variables:
142 * c-indent-level: 8
143 * c-basic-offset: 8
144 * End:
145 */