2015-04-03 07:53:24 -04:00
|
|
|
/*
|
2020-06-04 12:58:52 -04:00
|
|
|
* include/haproxy/session.h
|
|
|
|
|
* This file contains functions used to manage sessions.
|
2015-04-03 07:53:24 -04:00
|
|
|
*
|
2020-06-04 12:58:52 -04:00
|
|
|
* Copyright (C) 2000-2020 Willy Tarreau - w@1wt.eu
|
2015-04-03 07:53:24 -04:00
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
|
* exclusively.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-04 12:58:52 -04:00
|
|
|
#ifndef _HAPROXY_SESSION_H
|
|
|
|
|
#define _HAPROXY_SESSION_H
|
2015-04-03 07:53:24 -04:00
|
|
|
|
2020-05-27 06:58:42 -04:00
|
|
|
#include <haproxy/api.h>
|
2020-06-04 11:05:57 -04:00
|
|
|
#include <haproxy/global-t.h>
|
2020-06-04 12:58:52 -04:00
|
|
|
#include <haproxy/obj_type-t.h>
|
|
|
|
|
#include <haproxy/pool.h>
|
2020-06-04 17:20:13 -04:00
|
|
|
#include <haproxy/server.h>
|
2020-06-04 12:58:52 -04:00
|
|
|
#include <haproxy/session-t.h>
|
2020-06-04 12:46:44 -04:00
|
|
|
#include <haproxy/stick_table.h>
|
2015-04-04 10:31:16 -04:00
|
|
|
|
2017-11-24 11:34:44 -05:00
|
|
|
extern struct pool_head *pool_head_session;
|
2018-12-27 11:20:54 -05:00
|
|
|
extern struct pool_head *pool_head_sess_srv_list;
|
|
|
|
|
|
2015-04-04 18:38:48 -04:00
|
|
|
struct session *session_new(struct proxy *fe, struct listener *li, enum obj_type *origin);
|
2015-04-04 09:54:03 -04:00
|
|
|
void session_free(struct session *sess);
|
2020-10-14 11:37:17 -04:00
|
|
|
int session_accept_fd(struct connection *cli_conn);
|
2020-01-22 12:08:48 -05:00
|
|
|
int conn_complete_session(struct connection *conn);
|
2015-04-03 07:53:24 -04:00
|
|
|
|
2015-04-04 10:31:16 -04:00
|
|
|
/* Remove the refcount from the session to the tracked counters, and clear the
|
|
|
|
|
* pointer to ensure this is only performed once. The caller is responsible for
|
|
|
|
|
* ensuring that the pointer is valid first.
|
|
|
|
|
*/
|
|
|
|
|
static inline void session_store_counters(struct session *sess)
|
|
|
|
|
{
|
|
|
|
|
void *ptr;
|
|
|
|
|
int i;
|
2017-06-13 13:37:32 -04:00
|
|
|
struct stksess *ts;
|
2015-04-04 10:31:16 -04:00
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_SESS_STKCTR; i++) {
|
|
|
|
|
struct stkctr *stkctr = &sess->stkctr[i];
|
|
|
|
|
|
2017-06-13 13:37:32 -04:00
|
|
|
ts = stkctr_entry(stkctr);
|
|
|
|
|
if (!ts)
|
2015-04-04 10:31:16 -04:00
|
|
|
continue;
|
|
|
|
|
|
2017-06-13 13:37:32 -04:00
|
|
|
ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_CONN_CUR);
|
|
|
|
|
if (ptr) {
|
2017-11-07 04:42:54 -05:00
|
|
|
HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
|
2017-06-13 13:37:32 -04:00
|
|
|
|
2019-01-03 18:11:59 -05:00
|
|
|
if (stktable_data_cast(ptr, conn_cur) > 0)
|
|
|
|
|
stktable_data_cast(ptr, conn_cur)--;
|
2017-06-13 13:37:32 -04:00
|
|
|
|
2017-11-07 04:42:54 -05:00
|
|
|
HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
|
2017-11-29 10:15:07 -05:00
|
|
|
|
|
|
|
|
/* If data was modified, we need to touch to re-schedule sync */
|
|
|
|
|
stktable_touch_local(stkctr->table, ts, 0);
|
2017-06-13 13:37:32 -04:00
|
|
|
}
|
|
|
|
|
|
2015-04-04 10:31:16 -04:00
|
|
|
stkctr_set_entry(stkctr, NULL);
|
2017-06-13 13:37:32 -04:00
|
|
|
stksess_kill_if_expired(stkctr->table, ts, 1);
|
2015-04-04 10:31:16 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-27 11:20:54 -05:00
|
|
|
/* Remove the connection from the session list, and destroy the srv_list if it's now empty */
|
|
|
|
|
static inline void session_unown_conn(struct session *sess, struct connection *conn)
|
2018-11-30 11:24:55 -05:00
|
|
|
{
|
2018-12-27 11:20:54 -05:00
|
|
|
struct sess_srv_list *srv_list = NULL;
|
2020-07-02 09:56:23 -04:00
|
|
|
|
|
|
|
|
if (conn->flags & CO_FL_SESS_IDLE)
|
|
|
|
|
sess->idle_conns--;
|
2020-10-14 12:17:05 -04:00
|
|
|
LIST_DEL_INIT(&conn->session_list);
|
2018-12-27 11:20:54 -05:00
|
|
|
list_for_each_entry(srv_list, &sess->srv_list, srv_list) {
|
|
|
|
|
if (srv_list->target == conn->target) {
|
|
|
|
|
if (LIST_ISEMPTY(&srv_list->conn_list)) {
|
|
|
|
|
LIST_DEL(&srv_list->srv_list);
|
|
|
|
|
pool_free(pool_head_sess_srv_list, srv_list);
|
|
|
|
|
}
|
2018-11-30 11:24:55 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-27 11:20:54 -05:00
|
|
|
}
|
|
|
|
|
|
MEDIUM: connection: Add private connections synchronously in session server list
When a connection is marked as private, it is now added in the session server
list. We don't wait a stream is detached from the mux to do so. When the
connection is created, this happens after the mux creation. Otherwise, it is
performed when the connection is marked as private.
To allow that, when a connection is created, the session is systematically set
as the connectin owner. Thus, a backend connection has always a owner during its
creation. And a private connection has always a owner until its death.
Note that outside the detach() callback, if the call to session_add_conn()
failed, the error is ignored. In this situation, we retry to add the connection
into the session server list in the detach() callback. If this fails at this
step, the multiplexer is destroyed and the connection is closed.
2020-07-01 10:10:06 -04:00
|
|
|
/* Add the connection <conn> to the server list of the session <sess>. This
|
|
|
|
|
* function is called only if the connection is private. Nothing is performed if
|
|
|
|
|
* the connection is already in the session sever list or if the session does
|
|
|
|
|
* not own the connection.
|
|
|
|
|
*/
|
2018-12-27 11:20:54 -05:00
|
|
|
static inline int session_add_conn(struct session *sess, struct connection *conn, void *target)
|
|
|
|
|
{
|
|
|
|
|
struct sess_srv_list *srv_list = NULL;
|
|
|
|
|
int found = 0;
|
2018-11-30 11:24:55 -05:00
|
|
|
|
MEDIUM: connection: Add private connections synchronously in session server list
When a connection is marked as private, it is now added in the session server
list. We don't wait a stream is detached from the mux to do so. When the
connection is created, this happens after the mux creation. Otherwise, it is
performed when the connection is marked as private.
To allow that, when a connection is created, the session is systematically set
as the connectin owner. Thus, a backend connection has always a owner during its
creation. And a private connection has always a owner until its death.
Note that outside the detach() callback, if the call to session_add_conn()
failed, the error is ignored. In this situation, we retry to add the connection
into the session server list in the detach() callback. If this fails at this
step, the multiplexer is destroyed and the connection is closed.
2020-07-01 10:10:06 -04:00
|
|
|
/* Already attach to the session or not the connection owner */
|
|
|
|
|
if (!LIST_ISEMPTY(&conn->session_list) || conn->owner != sess)
|
|
|
|
|
return 1;
|
|
|
|
|
|
2018-12-27 11:20:54 -05:00
|
|
|
list_for_each_entry(srv_list, &sess->srv_list, srv_list) {
|
|
|
|
|
if (srv_list->target == target) {
|
|
|
|
|
found = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!found) {
|
|
|
|
|
/* The session has no connection for the server, create a new entry */
|
|
|
|
|
srv_list = pool_alloc(pool_head_sess_srv_list);
|
|
|
|
|
if (!srv_list)
|
|
|
|
|
return 0;
|
|
|
|
|
srv_list->target = target;
|
|
|
|
|
LIST_INIT(&srv_list->conn_list);
|
|
|
|
|
LIST_ADDQ(&sess->srv_list, &srv_list->srv_list);
|
2018-11-30 11:24:55 -05:00
|
|
|
}
|
2018-12-27 11:20:54 -05:00
|
|
|
LIST_ADDQ(&srv_list->conn_list, &conn->session_list);
|
|
|
|
|
return 1;
|
2018-11-30 11:24:55 -05:00
|
|
|
}
|
|
|
|
|
|
MEDIUM: connection: Add private connections synchronously in session server list
When a connection is marked as private, it is now added in the session server
list. We don't wait a stream is detached from the mux to do so. When the
connection is created, this happens after the mux creation. Otherwise, it is
performed when the connection is marked as private.
To allow that, when a connection is created, the session is systematically set
as the connectin owner. Thus, a backend connection has always a owner during its
creation. And a private connection has always a owner until its death.
Note that outside the detach() callback, if the call to session_add_conn()
failed, the error is ignored. In this situation, we retry to add the connection
into the session server list in the detach() callback. If this fails at this
step, the multiplexer is destroyed and the connection is closed.
2020-07-01 10:10:06 -04:00
|
|
|
/* Returns 0 if the session can keep the idle conn, -1 if it was destroyed. The
|
|
|
|
|
* connection must be private.
|
|
|
|
|
*/
|
2018-12-14 13:27:06 -05:00
|
|
|
static inline int session_check_idle_conn(struct session *sess, struct connection *conn)
|
|
|
|
|
{
|
MEDIUM: connection: Add private connections synchronously in session server list
When a connection is marked as private, it is now added in the session server
list. We don't wait a stream is detached from the mux to do so. When the
connection is created, this happens after the mux creation. Otherwise, it is
performed when the connection is marked as private.
To allow that, when a connection is created, the session is systematically set
as the connectin owner. Thus, a backend connection has always a owner during its
creation. And a private connection has always a owner until its death.
Note that outside the detach() callback, if the call to session_add_conn()
failed, the error is ignored. In this situation, we retry to add the connection
into the session server list in the detach() callback. If this fails at this
step, the multiplexer is destroyed and the connection is closed.
2020-07-01 10:10:06 -04:00
|
|
|
/* Another session owns this connection */
|
|
|
|
|
if (conn->owner != sess)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (sess->idle_conns >= sess->fe->max_out_conns) {
|
2018-12-27 11:20:54 -05:00
|
|
|
session_unown_conn(sess, conn);
|
2018-12-14 13:27:06 -05:00
|
|
|
conn->owner = NULL;
|
2019-05-02 06:04:15 -04:00
|
|
|
conn->flags &= ~CO_FL_SESS_IDLE;
|
2020-04-27 09:53:41 -04:00
|
|
|
conn->mux->destroy(conn->ctx);
|
2020-01-20 07:56:01 -05:00
|
|
|
return -1;
|
2018-12-28 12:50:57 -05:00
|
|
|
} else {
|
|
|
|
|
conn->flags |= CO_FL_SESS_IDLE;
|
|
|
|
|
sess->idle_conns++;
|
2018-12-14 13:27:06 -05:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2015-04-04 10:31:16 -04:00
|
|
|
|
2020-07-01 10:36:51 -04:00
|
|
|
/* Look for an available connection matching the target <target> in the server
|
|
|
|
|
* list of the session <sess>. It returns a connection if found. Otherwise it
|
|
|
|
|
* returns NULL.
|
|
|
|
|
*/
|
|
|
|
|
static inline struct connection *session_get_conn(struct session *sess, void *target)
|
|
|
|
|
{
|
|
|
|
|
struct connection *srv_conn = NULL;
|
|
|
|
|
struct sess_srv_list *srv_list;
|
|
|
|
|
|
|
|
|
|
list_for_each_entry(srv_list, &sess->srv_list, srv_list) {
|
|
|
|
|
if (srv_list->target == target) {
|
|
|
|
|
list_for_each_entry(srv_conn, &srv_list->conn_list, session_list) {
|
|
|
|
|
if (srv_conn->mux && (srv_conn->mux->avail_streams(srv_conn) > 0)) {
|
|
|
|
|
if (srv_conn->flags & CO_FL_SESS_IDLE) {
|
|
|
|
|
srv_conn->flags &= ~CO_FL_SESS_IDLE;
|
|
|
|
|
sess->idle_conns--;
|
|
|
|
|
}
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
srv_conn = NULL; /* No available connection found */
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
return srv_conn;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 12:58:52 -04:00
|
|
|
#endif /* _HAPROXY_SESSION_H */
|
2015-04-03 07:53:24 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Local variables:
|
|
|
|
|
* c-indent-level: 8
|
|
|
|
|
* c-basic-offset: 8
|
|
|
|
|
* End:
|
|
|
|
|
*/
|