blob: 0e2d75f526ac394d777731f929437c233de8d7f4 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau49801602020-06-04 22:50:02 +02002 * include/haproxy/backend.h
Willy Tarreau62c3be22012-01-20 13:12:32 +01003 * 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 Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau49801602020-06-04 22:50:02 +020022#ifndef _HAPROXY_BACKEND_H
23#define _HAPROXY_BACKEND_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020025#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/backend-t.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020027#include <haproxy/proxy-t.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020028#include <haproxy/server-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020029#include <haproxy/stream-t.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020030#include <haproxy/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreau87b09662015-04-03 00:22:06 +020032int assign_server(struct stream *s);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +020033int assign_server_address(struct stream *s);
Willy Tarreau87b09662015-04-03 00:22:06 +020034int assign_server_and_queue(struct stream *s);
35int connect_server(struct stream *s);
36int srv_redispatch_connect(struct stream *t);
Willy Tarreau3a9312a2020-01-09 18:43:15 +010037void back_try_conn_req(struct stream *s);
38void back_handle_st_req(struct stream *s);
39void back_handle_st_con(struct stream *s);
40void back_handle_st_rdy(struct stream *s);
41void back_handle_st_cer(struct stream *s);
42
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +010043const char *backend_lb_algo_str(int algo);
Willy Tarreaua93c74b2012-05-08 18:14:39 +020044int backend_parse_balance(const char **args, char **err, struct proxy *curproxy);
Willy Tarreau87b09662015-04-03 00:22:06 +020045int tcp_persist_rdp_cookie(struct stream *s, struct channel *req, int an_bit);
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Krzysztof Oledzki85130942007-10-22 16:21:10 +020047int be_downtime(struct proxy *px);
Willy Tarreauc5d9c802009-10-01 09:17:05 +020048void recount_servers(struct proxy *px);
49void update_backend_weight(struct proxy *px);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050050int be_lastsession(const struct proxy *be);
Natalie Chena93f0a72021-09-10 13:13:19 +080051struct server *get_server_uh(struct proxy *px, char *uri, int uri_len, const struct server *avoid);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050052
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +010053/* Returns number of usable servers in backend */
54static inline int be_usable_srv(struct proxy *be)
55{
Willy Tarreauc3914d42020-09-24 08:39:22 +020056 if (be->disabled)
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +010057 return 0;
58 else if (be->srv_act)
59 return be->srv_act;
60 else if (be->lbprm.fbck)
61 return 1;
62 else
63 return be->srv_bck;
64}
65
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050066/* set the time of last session on the backend */
Willy Tarreau0e492e22019-04-15 21:25:03 +020067static inline void be_set_sess_last(struct proxy *be)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050068{
69 be->be_counters.last_sess = now.tv_sec;
70}
Willy Tarreauc5d9c802009-10-01 09:17:05 +020071
Emeric Brun52a91d32017-08-31 14:41:55 +020072/* This function returns non-zero if the designated server will be
73 * usable for LB according to pending weight and state.
74 * Otherwise it returns 0.
Willy Tarreauc5d9c802009-10-01 09:17:05 +020075 */
Emeric Brun52a91d32017-08-31 14:41:55 +020076static inline int srv_willbe_usable(const struct server *srv)
Willy Tarreau87eb1d62014-05-13 18:51:40 +020077{
Emeric Brun52a91d32017-08-31 14:41:55 +020078 enum srv_state state = srv->next_state;
Willy Tarreau87eb1d62014-05-13 18:51:40 +020079
Emeric Brun52a91d32017-08-31 14:41:55 +020080 if (!srv->next_eweight)
Willy Tarreau87eb1d62014-05-13 18:51:40 +020081 return 0;
Emeric Brun52a91d32017-08-31 14:41:55 +020082 if (srv->next_admin & SRV_ADMF_MAINT)
Willy Tarreau20125212014-05-13 19:44:56 +020083 return 0;
Emeric Brun52a91d32017-08-31 14:41:55 +020084 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +020085 return 0;
Willy Tarreau892337c2014-05-13 23:41:20 +020086 switch (state) {
87 case SRV_ST_STARTING:
88 case SRV_ST_RUNNING:
89 return 1;
90 case SRV_ST_STOPPING:
91 case SRV_ST_STOPPED:
Willy Tarreau87eb1d62014-05-13 18:51:40 +020092 return 0;
Willy Tarreau892337c2014-05-13 23:41:20 +020093 }
94 return 0;
Willy Tarreau87eb1d62014-05-13 18:51:40 +020095}
96
97/* This function returns non-zero if the designated server was usable for LB
Emeric Brun52a91d32017-08-31 14:41:55 +020098 * according to its current weight and state. Otherwise it returns 0.
Willy Tarreau87eb1d62014-05-13 18:51:40 +020099 */
Emeric Brun52a91d32017-08-31 14:41:55 +0200100static inline int srv_currently_usable(const struct server *srv)
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200101{
Emeric Brun52a91d32017-08-31 14:41:55 +0200102 enum srv_state state = srv->cur_state;
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200103
Emeric Brun52a91d32017-08-31 14:41:55 +0200104 if (!srv->cur_eweight)
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200105 return 0;
Emeric Brun52a91d32017-08-31 14:41:55 +0200106 if (srv->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau20125212014-05-13 19:44:56 +0200107 return 0;
Emeric Brun52a91d32017-08-31 14:41:55 +0200108 if (srv->cur_admin & SRV_ADMF_DRAIN)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200109 return 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200110 switch (state) {
111 case SRV_ST_STARTING:
112 case SRV_ST_RUNNING:
113 return 1;
114 case SRV_ST_STOPPING:
115 case SRV_ST_STOPPED:
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200116 return 0;
Willy Tarreau892337c2014-05-13 23:41:20 +0200117 }
118 return 0;
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200119}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120
Emeric Brun52a91d32017-08-31 14:41:55 +0200121/* This function commits the next server state and weight onto the current
Willy Tarreau9df86f92019-04-16 11:21:14 +0200122 * ones in order to detect future changes. The server's lock is expected to
123 * be held when calling this function.
Willy Tarreauc5150da2014-05-13 19:27:31 +0200124 */
125static inline void srv_lb_commit_status(struct server *srv)
126{
Emeric Brun52a91d32017-08-31 14:41:55 +0200127 srv->cur_state = srv->next_state;
128 srv->cur_admin = srv->next_admin;
129 srv->cur_eweight = srv->next_eweight;
Willy Tarreauc5150da2014-05-13 19:27:31 +0200130}
131
132/* This function returns true when a server has experienced a change since last
133 * commit on its state or weight, otherwise zero.
134 */
135static inline int srv_lb_status_changed(const struct server *srv)
136{
Emeric Brun52a91d32017-08-31 14:41:55 +0200137 return (srv->next_state != srv->cur_state ||
138 srv->next_admin != srv->cur_admin ||
139 srv->next_eweight != srv->cur_eweight);
Willy Tarreauc5150da2014-05-13 19:27:31 +0200140}
141
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200142/* sends a log message when a backend goes down, and also sets last
143 * change date.
144 */
145void set_backend_down(struct proxy *be);
146
Willy Tarreau49801602020-06-04 22:50:02 +0200147#endif /* _HAPROXY_BACKEND_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200148
149/*
150 * Local variables:
151 * c-indent-level: 8
152 * c-basic-offset: 8
153 * End:
154 */