Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 62c3be2 | 2012-01-20 13:12:32 +0100 | [diff] [blame] | 2 | * include/proto/backend.h |
| 3 | * Functions prototypes for the backend. |
| 4 | * |
| 5 | * Copyright (C) 2000-2012 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 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _PROTO_BACKEND_H |
| 23 | #define _PROTO_BACKEND_H |
| 24 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 25 | #include <common/config.h> |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 26 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | |
| 28 | #include <types/backend.h> |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 29 | #include <types/proxy.h> |
| 30 | #include <types/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 31 | #include <types/stream.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 33 | int assign_server(struct stream *s); |
| 34 | int assign_server_address(struct stream *s); |
| 35 | int assign_server_and_queue(struct stream *s); |
| 36 | int connect_server(struct stream *s); |
| 37 | int srv_redispatch_connect(struct stream *t); |
Krzysztof Piotr Oledzki | 15514c2 | 2010-01-04 16:03:09 +0100 | [diff] [blame] | 38 | const char *backend_lb_algo_str(int algo); |
Willy Tarreau | a93c74b | 2012-05-08 18:14:39 +0200 | [diff] [blame] | 39 | int backend_parse_balance(const char **args, char **err, struct proxy *curproxy); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 40 | int tcp_persist_rdp_cookie(struct stream *s, struct channel *req, int an_bit); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 42 | int be_downtime(struct proxy *px); |
Willy Tarreau | c5d9c80 | 2009-10-01 09:17:05 +0200 | [diff] [blame] | 43 | void recount_servers(struct proxy *px); |
| 44 | void update_backend_weight(struct proxy *px); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 45 | int be_lastsession(const struct proxy *be); |
| 46 | |
Nenad Merdanovic | b7e7c47 | 2017-03-12 21:56:55 +0100 | [diff] [blame] | 47 | /* Returns number of usable servers in backend */ |
| 48 | static inline int be_usable_srv(struct proxy *be) |
| 49 | { |
| 50 | if (be->state == PR_STSTOPPED) |
| 51 | return 0; |
| 52 | else if (be->srv_act) |
| 53 | return be->srv_act; |
| 54 | else if (be->lbprm.fbck) |
| 55 | return 1; |
| 56 | else |
| 57 | return be->srv_bck; |
| 58 | } |
| 59 | |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 60 | /* set the time of last session on the backend */ |
| 61 | static void inline be_set_sess_last(struct proxy *be) |
| 62 | { |
| 63 | be->be_counters.last_sess = now.tv_sec; |
| 64 | } |
Willy Tarreau | c5d9c80 | 2009-10-01 09:17:05 +0200 | [diff] [blame] | 65 | |
Willy Tarreau | 87eb1d6 | 2014-05-13 18:51:40 +0200 | [diff] [blame] | 66 | /* This function returns non-zero if the designated server is usable for LB |
| 67 | * according to its current weight and current state. Otherwise it returns 0. |
Willy Tarreau | c5d9c80 | 2009-10-01 09:17:05 +0200 | [diff] [blame] | 68 | */ |
Willy Tarreau | 87eb1d6 | 2014-05-13 18:51:40 +0200 | [diff] [blame] | 69 | static inline int srv_is_usable(const struct server *srv) |
| 70 | { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 71 | enum srv_state state = srv->state; |
Willy Tarreau | 87eb1d6 | 2014-05-13 18:51:40 +0200 | [diff] [blame] | 72 | |
| 73 | if (!srv->eweight) |
| 74 | return 0; |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame] | 75 | if (srv->admin & SRV_ADMF_MAINT) |
| 76 | return 0; |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 77 | if (srv->admin & SRV_ADMF_DRAIN) |
| 78 | return 0; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 79 | switch (state) { |
| 80 | case SRV_ST_STARTING: |
| 81 | case SRV_ST_RUNNING: |
| 82 | return 1; |
| 83 | case SRV_ST_STOPPING: |
| 84 | case SRV_ST_STOPPED: |
Willy Tarreau | 87eb1d6 | 2014-05-13 18:51:40 +0200 | [diff] [blame] | 85 | return 0; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 86 | } |
| 87 | return 0; |
Willy Tarreau | 87eb1d6 | 2014-05-13 18:51:40 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* This function returns non-zero if the designated server was usable for LB |
| 91 | * according to its current weight and previous state. Otherwise it returns 0. |
| 92 | */ |
| 93 | static inline int srv_was_usable(const struct server *srv) |
Willy Tarreau | c5d9c80 | 2009-10-01 09:17:05 +0200 | [diff] [blame] | 94 | { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 95 | enum srv_state state = srv->prev_state; |
Willy Tarreau | 87eb1d6 | 2014-05-13 18:51:40 +0200 | [diff] [blame] | 96 | |
| 97 | if (!srv->prev_eweight) |
Willy Tarreau | c5d9c80 | 2009-10-01 09:17:05 +0200 | [diff] [blame] | 98 | return 0; |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame] | 99 | if (srv->prev_admin & SRV_ADMF_MAINT) |
| 100 | return 0; |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 101 | if (srv->prev_admin & SRV_ADMF_DRAIN) |
| 102 | return 0; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 103 | switch (state) { |
| 104 | case SRV_ST_STARTING: |
| 105 | case SRV_ST_RUNNING: |
| 106 | return 1; |
| 107 | case SRV_ST_STOPPING: |
| 108 | case SRV_ST_STOPPED: |
Willy Tarreau | c5d9c80 | 2009-10-01 09:17:05 +0200 | [diff] [blame] | 109 | return 0; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 110 | } |
| 111 | return 0; |
Willy Tarreau | c5d9c80 | 2009-10-01 09:17:05 +0200 | [diff] [blame] | 112 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 114 | /* This function commits the current server state and weight onto the previous |
| 115 | * ones in order to detect future changes. |
| 116 | */ |
| 117 | static inline void srv_lb_commit_status(struct server *srv) |
| 118 | { |
| 119 | srv->prev_state = srv->state; |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame] | 120 | srv->prev_admin = srv->admin; |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 121 | srv->prev_eweight = srv->eweight; |
| 122 | } |
| 123 | |
| 124 | /* This function returns true when a server has experienced a change since last |
| 125 | * commit on its state or weight, otherwise zero. |
| 126 | */ |
| 127 | static inline int srv_lb_status_changed(const struct server *srv) |
| 128 | { |
| 129 | return (srv->state != srv->prev_state || |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame] | 130 | srv->admin != srv->prev_admin || |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 131 | srv->eweight != srv->prev_eweight); |
| 132 | } |
| 133 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 134 | /* sends a log message when a backend goes down, and also sets last |
| 135 | * change date. |
| 136 | */ |
| 137 | void set_backend_down(struct proxy *be); |
| 138 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 139 | #endif /* _PROTO_BACKEND_H */ |
| 140 | |
| 141 | /* |
| 142 | * Local variables: |
| 143 | * c-indent-level: 8 |
| 144 | * c-basic-offset: 8 |
| 145 | * End: |
| 146 | */ |