blob: a52e982c9c32a2a980a227631dd53ed7b5d00d37 [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
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020025#include <haproxy/api.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020026#include <haproxy/pool.h>
Willy Tarreaub1ec8c42015-04-03 13:53:24 +020027
28#include <types/global.h>
29#include <types/session.h>
30
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +010031#include <proto/obj_type.h>
Willy Tarreaubb2ef122015-04-04 16:31:16 +020032#include <proto/stick_table.h>
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +010033#include <proto/server.h>
Willy Tarreaubb2ef122015-04-04 16:31:16 +020034
Willy Tarreaubafbe012017-11-24 17:34:44 +010035extern struct pool_head *pool_head_session;
Olivier Houchard351411f2018-12-27 17:20:54 +010036extern struct pool_head *pool_head_sess_srv_list;
37
Willy Tarreauc38f71c2015-04-05 00:38:48 +020038struct session *session_new(struct proxy *fe, struct listener *li, enum obj_type *origin);
Willy Tarreau11c36242015-04-04 15:54:03 +020039void session_free(struct session *sess);
Willy Tarreau9903f0e2015-04-04 18:50:31 +020040int session_accept_fd(struct listener *l, int cfd, struct sockaddr_storage *addr);
Olivier Houchard477902b2020-01-22 18:08:48 +010041int conn_complete_session(struct connection *conn);
Willy Tarreaub1ec8c42015-04-03 13:53:24 +020042
Willy Tarreaubb2ef122015-04-04 16:31:16 +020043/* Remove the refcount from the session to the tracked counters, and clear the
44 * pointer to ensure this is only performed once. The caller is responsible for
45 * ensuring that the pointer is valid first.
46 */
47static inline void session_store_counters(struct session *sess)
48{
49 void *ptr;
50 int i;
Emeric Brun819fc6f2017-06-13 19:37:32 +020051 struct stksess *ts;
Willy Tarreaubb2ef122015-04-04 16:31:16 +020052
53 for (i = 0; i < MAX_SESS_STKCTR; i++) {
54 struct stkctr *stkctr = &sess->stkctr[i];
55
Emeric Brun819fc6f2017-06-13 19:37:32 +020056 ts = stkctr_entry(stkctr);
57 if (!ts)
Willy Tarreaubb2ef122015-04-04 16:31:16 +020058 continue;
59
Emeric Brun819fc6f2017-06-13 19:37:32 +020060 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_CONN_CUR);
61 if (ptr) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +010062 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +020063
Tim Duesterhus8b87c012019-01-04 00:11:59 +010064 if (stktable_data_cast(ptr, conn_cur) > 0)
65 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 Houchard2444aa52020-01-20 13:56:01 +0100122 if (!(conn->flags & CO_FL_PRIVATE) ||
123 sess->idle_conns >= sess->fe->max_out_conns) {
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 Houcharda48237f2019-05-02 12:04:15 +0200126 conn->flags &= ~CO_FL_SESS_IDLE;
Christopher Fauletbfb0f722020-04-27 15:53:41 +0200127 conn->mux->destroy(conn->ctx);
Olivier Houchard2444aa52020-01-20 13:56:01 +0100128 return -1;
Olivier Houcharda2dbeb22018-12-28 18:50:57 +0100129 } else {
130 conn->flags |= CO_FL_SESS_IDLE;
131 sess->idle_conns++;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +0100132 }
133 return 0;
134}
Willy Tarreaubb2ef122015-04-04 16:31:16 +0200135
Willy Tarreaub1ec8c42015-04-03 13:53:24 +0200136#endif /* _PROTO_SESSION_H */
137
138/*
139 * Local variables:
140 * c-indent-level: 8
141 * c-basic-offset: 8
142 * End:
143 */