Willy Tarreau | b1ec8c4 | 2015-04-03 13:53:24 +0200 | [diff] [blame] | 1 | /* |
| 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 Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 33 | #include <proto/obj_type.h> |
Willy Tarreau | bb2ef12 | 2015-04-04 16:31:16 +0200 | [diff] [blame] | 34 | #include <proto/stick_table.h> |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 35 | #include <proto/server.h> |
Willy Tarreau | bb2ef12 | 2015-04-04 16:31:16 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 37 | extern struct pool_head *pool_head_session; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 38 | extern struct pool_head *pool_head_sess_srv_list; |
| 39 | |
Willy Tarreau | c38f71c | 2015-04-05 00:38:48 +0200 | [diff] [blame] | 40 | struct session *session_new(struct proxy *fe, struct listener *li, enum obj_type *origin); |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 41 | void session_free(struct session *sess); |
Willy Tarreau | 9903f0e | 2015-04-04 18:50:31 +0200 | [diff] [blame] | 42 | int session_accept_fd(struct listener *l, int cfd, struct sockaddr_storage *addr); |
Willy Tarreau | b1ec8c4 | 2015-04-03 13:53:24 +0200 | [diff] [blame] | 43 | |
Willy Tarreau | bb2ef12 | 2015-04-04 16:31:16 +0200 | [diff] [blame] | 44 | /* 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 | */ |
| 48 | static inline void session_store_counters(struct session *sess) |
| 49 | { |
| 50 | void *ptr; |
| 51 | int i; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 52 | struct stksess *ts; |
Willy Tarreau | bb2ef12 | 2015-04-04 16:31:16 +0200 | [diff] [blame] | 53 | |
| 54 | for (i = 0; i < MAX_SESS_STKCTR; i++) { |
| 55 | struct stkctr *stkctr = &sess->stkctr[i]; |
| 56 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 57 | ts = stkctr_entry(stkctr); |
| 58 | if (!ts) |
Willy Tarreau | bb2ef12 | 2015-04-04 16:31:16 +0200 | [diff] [blame] | 59 | continue; |
| 60 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 61 | ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_CONN_CUR); |
| 62 | if (ptr) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 63 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 64 | |
Tim Duesterhus | 8b87c01 | 2019-01-04 00:11:59 +0100 | [diff] [blame] | 65 | if (stktable_data_cast(ptr, conn_cur) > 0) |
| 66 | stktable_data_cast(ptr, conn_cur)--; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 67 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 68 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 69 | |
| 70 | /* If data was modified, we need to touch to re-schedule sync */ |
| 71 | stktable_touch_local(stkctr->table, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Willy Tarreau | bb2ef12 | 2015-04-04 16:31:16 +0200 | [diff] [blame] | 74 | stkctr_set_entry(stkctr, NULL); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 75 | stksess_kill_if_expired(stkctr->table, ts, 1); |
Willy Tarreau | bb2ef12 | 2015-04-04 16:31:16 +0200 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 79 | /* Remove the connection from the session list, and destroy the srv_list if it's now empty */ |
| 80 | static inline void session_unown_conn(struct session *sess, struct connection *conn) |
Olivier Houchard | 00cf70f | 2018-11-30 17:24:55 +0100 | [diff] [blame] | 81 | { |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 82 | 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 Houchard | 00cf70f | 2018-11-30 17:24:55 +0100 | [diff] [blame] | 91 | break; |
| 92 | } |
Olivier Houchard | 00cf70f | 2018-11-30 17:24:55 +0100 | [diff] [blame] | 93 | } |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static 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 Houchard | 00cf70f | 2018-11-30 17:24:55 +0100 | [diff] [blame] | 100 | |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 101 | 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 Houchard | 00cf70f | 2018-11-30 17:24:55 +0100 | [diff] [blame] | 115 | } |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 116 | LIST_ADDQ(&srv_list->conn_list, &conn->session_list); |
| 117 | return 1; |
Olivier Houchard | 00cf70f | 2018-11-30 17:24:55 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 120 | /* 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 */ |
| 121 | static inline int session_check_idle_conn(struct session *sess, struct connection *conn) |
| 122 | { |
Olivier Houchard | cee0389 | 2019-07-04 14:26:41 +0200 | [diff] [blame] | 123 | if (sess->idle_conns >= sess->fe->max_out_conns) { |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 124 | /* We can't keep the connection, let's try to add it to the server idle list */ |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 125 | session_unown_conn(sess, conn); |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 126 | conn->owner = NULL; |
Olivier Houchard | a48237f | 2019-05-02 12:04:15 +0200 | [diff] [blame] | 127 | conn->flags &= ~CO_FL_SESS_IDLE; |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 128 | 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 Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 130 | conn->mux->destroy(conn->ctx); |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 131 | return -1; |
Olivier Houchard | a48237f | 2019-05-02 12:04:15 +0200 | [diff] [blame] | 132 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 133 | return 1; |
Olivier Houchard | a2dbeb2 | 2018-12-28 18:50:57 +0100 | [diff] [blame] | 134 | } else { |
| 135 | conn->flags |= CO_FL_SESS_IDLE; |
| 136 | sess->idle_conns++; |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 137 | } |
| 138 | return 0; |
| 139 | } |
Willy Tarreau | bb2ef12 | 2015-04-04 16:31:16 +0200 | [diff] [blame] | 140 | |
Willy Tarreau | b1ec8c4 | 2015-04-03 13:53:24 +0200 | [diff] [blame] | 141 | #endif /* _PROTO_SESSION_H */ |
| 142 | |
| 143 | /* |
| 144 | * Local variables: |
| 145 | * c-indent-level: 8 |
| 146 | * c-basic-offset: 8 |
| 147 | * End: |
| 148 | */ |