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