blob: d54e9454b55ae7086a112e036022497c832c9f65 [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
Willy Tarreaubb2ef122015-04-04 16:31:16 +020065 stktable_data_cast(ptr, conn_cur)--;
Emeric Brun819fc6f2017-06-13 19:37:32 +020066
Christopher Faulet2a944ee2017-11-07 10:42:54 +010067 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +010068
69 /* If data was modified, we need to touch to re-schedule sync */
70 stktable_touch_local(stkctr->table, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +020071 }
72
Willy Tarreaubb2ef122015-04-04 16:31:16 +020073 stkctr_set_entry(stkctr, NULL);
Emeric Brun819fc6f2017-06-13 19:37:32 +020074 stksess_kill_if_expired(stkctr->table, ts, 1);
Willy Tarreaubb2ef122015-04-04 16:31:16 +020075 }
76}
77
Olivier Houchard351411f2018-12-27 17:20:54 +010078/* Remove the connection from the session list, and destroy the srv_list if it's now empty */
79static inline void session_unown_conn(struct session *sess, struct connection *conn)
Olivier Houchard00cf70f2018-11-30 17:24:55 +010080{
Olivier Houchard351411f2018-12-27 17:20:54 +010081 struct sess_srv_list *srv_list = NULL;
82 LIST_DEL(&conn->session_list);
83 LIST_INIT(&conn->session_list);
84 list_for_each_entry(srv_list, &sess->srv_list, srv_list) {
85 if (srv_list->target == conn->target) {
86 if (LIST_ISEMPTY(&srv_list->conn_list)) {
87 LIST_DEL(&srv_list->srv_list);
88 pool_free(pool_head_sess_srv_list, srv_list);
89 }
Olivier Houchard00cf70f2018-11-30 17:24:55 +010090 break;
91 }
Olivier Houchard00cf70f2018-11-30 17:24:55 +010092 }
Olivier Houchard351411f2018-12-27 17:20:54 +010093}
94
95static inline int session_add_conn(struct session *sess, struct connection *conn, void *target)
96{
97 struct sess_srv_list *srv_list = NULL;
98 int found = 0;
Olivier Houchard00cf70f2018-11-30 17:24:55 +010099
Olivier Houchard351411f2018-12-27 17:20:54 +0100100 list_for_each_entry(srv_list, &sess->srv_list, srv_list) {
101 if (srv_list->target == target) {
102 found = 1;
103 break;
104 }
105 }
106 if (!found) {
107 /* The session has no connection for the server, create a new entry */
108 srv_list = pool_alloc(pool_head_sess_srv_list);
109 if (!srv_list)
110 return 0;
111 srv_list->target = target;
112 LIST_INIT(&srv_list->conn_list);
113 LIST_ADDQ(&sess->srv_list, &srv_list->srv_list);
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100114 }
Olivier Houchard351411f2018-12-27 17:20:54 +0100115 LIST_ADDQ(&srv_list->conn_list, &conn->session_list);
116 return 1;
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100117}
118
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100119/* 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 */
120static inline int session_check_idle_conn(struct session *sess, struct connection *conn)
121{
Olivier Houcharda2dbeb22018-12-28 18:50:57 +0100122 if (sess->idle_conns > sess->fe->max_out_conns) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100123 /* We can't keep the connection, let's try to add it to the server idle list */
Olivier Houchard351411f2018-12-27 17:20:54 +0100124 session_unown_conn(sess, conn);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100125 conn->owner = NULL;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100126 if (!srv_add_to_idle_list(objt_server(conn->target), conn)) {
127 /* The server doesn't want it, let's kill the connection right away */
128 conn->mux->destroy(conn);
129 return -1;
130 }
131 return 1;
Olivier Houcharda2dbeb22018-12-28 18:50:57 +0100132 } else {
133 conn->flags |= CO_FL_SESS_IDLE;
134 sess->idle_conns++;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100135 }
136 return 0;
137}
Willy Tarreaubb2ef122015-04-04 16:31:16 +0200138
Willy Tarreaub1ec8c42015-04-03 13:53:24 +0200139#endif /* _PROTO_SESSION_H */
140
141/*
142 * Local variables:
143 * c-indent-level: 8
144 * c-basic-offset: 8
145 * End:
146 */