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 int be_lastsession(const struct proxy *be);
46
47 /* Returns number of usable servers in backend */
be_usable_srv(struct proxy * be)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
60 /* set the time of last session on the backend */
be_set_sess_last(struct proxy * be)61 static inline void be_set_sess_last(struct proxy *be)
62 {
63 be->be_counters.last_sess = now.tv_sec;
64 }
65
66 /* This function returns non-zero if the designated server will be
67 * usable for LB according to pending weight and state.
68 * Otherwise it returns 0.
69 */
srv_willbe_usable(const struct server * srv)70 static inline int srv_willbe_usable(const struct server *srv)
71 {
72 enum srv_state state = srv->next_state;
73
74 if (!srv->next_eweight)
75 return 0;
76 if (srv->next_admin & SRV_ADMF_MAINT)
77 return 0;
78 if (srv->next_admin & SRV_ADMF_DRAIN)
79 return 0;
80 switch (state) {
81 case SRV_ST_STARTING:
82 case SRV_ST_RUNNING:
83 return 1;
84 case SRV_ST_STOPPING:
85 case SRV_ST_STOPPED:
86 return 0;
87 }
88 return 0;
89 }
90
91 /* This function returns non-zero if the designated server was usable for LB
92 * according to its current weight and state. Otherwise it returns 0.
93 */
srv_currently_usable(const struct server * srv)94 static inline int srv_currently_usable(const struct server *srv)
95 {
96 enum srv_state state = srv->cur_state;
97
98 if (!srv->cur_eweight)
99 return 0;
100 if (srv->cur_admin & SRV_ADMF_MAINT)
101 return 0;
102 if (srv->cur_admin & SRV_ADMF_DRAIN)
103 return 0;
104 switch (state) {
105 case SRV_ST_STARTING:
106 case SRV_ST_RUNNING:
107 return 1;
108 case SRV_ST_STOPPING:
109 case SRV_ST_STOPPED:
110 return 0;
111 }
112 return 0;
113 }
114
115 /* This function commits the next server state and weight onto the current
116 * ones in order to detect future changes. The server's lock is expected to
117 * be held when calling this function.
118 */
srv_lb_commit_status(struct server * srv)119 static inline void srv_lb_commit_status(struct server *srv)
120 {
121 srv->cur_state = srv->next_state;
122 srv->cur_admin = srv->next_admin;
123 srv->cur_eweight = srv->next_eweight;
124 }
125
126 /* This function returns true when a server has experienced a change since last
127 * commit on its state or weight, otherwise zero.
128 */
srv_lb_status_changed(const struct server * srv)129 static inline int srv_lb_status_changed(const struct server *srv)
130 {
131 return (srv->next_state != srv->cur_state ||
132 srv->next_admin != srv->cur_admin ||
133 srv->next_eweight != srv->cur_eweight);
134 }
135
136 /* sends a log message when a backend goes down, and also sets last
137 * change date.
138 */
139 void set_backend_down(struct proxy *be);
140
141 #endif /* _PROTO_BACKEND_H */
142
143 /*
144 * Local variables:
145 * c-indent-level: 8
146 * c-basic-offset: 8
147 * End:
148 */
149