1 /*
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  */
21 
22 #ifndef _PROTO_BACKEND_H
23 #define _PROTO_BACKEND_H
24 
25 #include <common/config.h>
26 #include <common/time.h>
27 
28 #include <types/backend.h>
29 #include <types/proxy.h>
30 #include <types/server.h>
31 #include <types/stream.h>
32 
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);
38 const char *backend_lb_algo_str(int algo);
39 int backend_parse_balance(const char **args, char **err, struct proxy *curproxy);
40 int tcp_persist_rdp_cookie(struct stream *s, struct channel *req, int an_bit);
41 
42 int be_downtime(struct proxy *px);
43 void recount_servers(struct proxy *px);
44 void update_backend_weight(struct proxy *px);
45 struct server *get_server_sh(struct proxy *px, const char *addr, int len);
46 struct server *get_server_uh(struct proxy *px, char *uri, int uri_len);
47 int be_lastsession(const struct proxy *be);
48 
49 /* set the time of last session on the backend */
be_set_sess_last(struct proxy * be)50 static void inline be_set_sess_last(struct proxy *be)
51 {
52 	be->be_counters.last_sess = now.tv_sec;
53 }
54 
55 /* This function returns non-zero if the designated server is usable for LB
56  * according to its current weight and current state. Otherwise it returns 0.
57  */
srv_is_usable(const struct server * srv)58 static inline int srv_is_usable(const struct server *srv)
59 {
60 	enum srv_state state = srv->state;
61 
62 	if (!srv->eweight)
63 		return 0;
64 	if (srv->admin & SRV_ADMF_MAINT)
65 		return 0;
66 	if (srv->admin & SRV_ADMF_DRAIN)
67 		return 0;
68 	switch (state) {
69 	case SRV_ST_STARTING:
70 	case SRV_ST_RUNNING:
71 		return 1;
72 	case SRV_ST_STOPPING:
73 	case SRV_ST_STOPPED:
74 		return 0;
75 	}
76 	return 0;
77 }
78 
79 /* This function returns non-zero if the designated server was usable for LB
80  * according to its current weight and previous state. Otherwise it returns 0.
81  */
srv_was_usable(const struct server * srv)82 static inline int srv_was_usable(const struct server *srv)
83 {
84 	enum srv_state state = srv->prev_state;
85 
86 	if (!srv->prev_eweight)
87 		return 0;
88 	if (srv->prev_admin & SRV_ADMF_MAINT)
89 		return 0;
90 	if (srv->prev_admin & SRV_ADMF_DRAIN)
91 		return 0;
92 	switch (state) {
93 	case SRV_ST_STARTING:
94 	case SRV_ST_RUNNING:
95 		return 1;
96 	case SRV_ST_STOPPING:
97 	case SRV_ST_STOPPED:
98 		return 0;
99 	}
100 	return 0;
101 }
102 
103 /* This function commits the current server state and weight onto the previous
104  * ones in order to detect future changes.
105  */
srv_lb_commit_status(struct server * srv)106 static inline void srv_lb_commit_status(struct server *srv)
107 {
108 	srv->prev_state = srv->state;
109 	srv->prev_admin = srv->admin;
110 	srv->prev_eweight = srv->eweight;
111 }
112 
113 /* This function returns true when a server has experienced a change since last
114  * commit on its state or weight, otherwise zero.
115  */
srv_lb_status_changed(const struct server * srv)116 static inline int srv_lb_status_changed(const struct server *srv)
117 {
118 	return (srv->state != srv->prev_state ||
119 		srv->admin != srv->prev_admin ||
120 		srv->eweight != srv->prev_eweight);
121 }
122 
123 /* sends a log message when a backend goes down, and also sets last
124  * change date.
125  */
126 void set_backend_down(struct proxy *be);
127 
128 #endif /* _PROTO_BACKEND_H */
129 
130 /*
131  * Local variables:
132  *  c-indent-level: 8
133  *  c-basic-offset: 8
134  * End:
135  */
136