blob: 7414edeb6fd5a14938af0d6aebbe7abb66c72e5c [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 include/proto/backend.h
3 Functions prototypes for the backend.
4
Willy Tarreau2fcb5002007-05-08 13:35:26 +02005 Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
Willy Tarreaubaaee002006-06-26 02:48:02 +02006
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
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
27#include <types/backend.h>
28#include <types/session.h>
29
30#include <proto/queue.h>
31
32int assign_server(struct session *s);
33int assign_server_address(struct session *s);
34int assign_server_and_queue(struct session *s);
35int connect_server(struct session *s);
36int srv_count_retry_down(struct session *t, int conn_err);
37int srv_retryable_connect(struct session *t);
38int srv_redispatch_connect(struct session *t);
Willy Tarreaua0cbda62007-11-01 21:39:54 +010039int backend_parse_balance(const char **args, char *err,
40 int errlen, struct proxy *curproxy);
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42void recount_servers(struct proxy *px);
43void recalc_server_map(struct proxy *px);
Krzysztof Oledzki85130942007-10-22 16:21:10 +020044int be_downtime(struct proxy *px);
Willy Tarreaubaaee002006-06-26 02:48:02 +020045
46/*
47 * This function tries to find a running server with free connection slots for
48 * the proxy <px> following the round-robin method.
Willy Tarreau20697042007-11-15 23:26:18 +010049 * If any server is found, it will be returned and px->lbprm.map.rr_idx will be updated
Willy Tarreaubaaee002006-06-26 02:48:02 +020050 * to point to the next server. If no valid server is found, NULL is returned.
51 */
52static inline struct server *get_server_rr_with_conns(struct proxy *px)
53{
54 int newidx;
55 struct server *srv;
56
Willy Tarreau20697042007-11-15 23:26:18 +010057 if (px->lbprm.tot_weight == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 return NULL;
59
Willy Tarreau20697042007-11-15 23:26:18 +010060 if (px->lbprm.map.state & PR_MAP_RECALC)
61 recalc_server_map(px);
62
63 if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight)
64 px->lbprm.map.rr_idx = 0;
65 newidx = px->lbprm.map.rr_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +020066
67 do {
Willy Tarreau20697042007-11-15 23:26:18 +010068 srv = px->lbprm.map.srv[newidx++];
Willy Tarreaubaaee002006-06-26 02:48:02 +020069 if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv)) {
Willy Tarreau20697042007-11-15 23:26:18 +010070 px->lbprm.map.rr_idx = newidx;
Willy Tarreaubaaee002006-06-26 02:48:02 +020071 return srv;
72 }
Willy Tarreau20697042007-11-15 23:26:18 +010073 if (newidx == px->lbprm.tot_weight)
Willy Tarreaubaaee002006-06-26 02:48:02 +020074 newidx = 0;
Willy Tarreau20697042007-11-15 23:26:18 +010075 } while (newidx != px->lbprm.map.rr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +020076
77 return NULL;
78}
79
80
81/*
82 * This function tries to find a running server for the proxy <px> following
83 * the round-robin method.
Willy Tarreau20697042007-11-15 23:26:18 +010084 * If any server is found, it will be returned and px->lbprm.map.rr_idx will be updated
Willy Tarreaubaaee002006-06-26 02:48:02 +020085 * to point to the next server. If no valid server is found, NULL is returned.
86 */
87static inline struct server *get_server_rr(struct proxy *px)
88{
Willy Tarreau20697042007-11-15 23:26:18 +010089 if (px->lbprm.tot_weight == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +020090 return NULL;
91
Willy Tarreau20697042007-11-15 23:26:18 +010092 if (px->lbprm.map.state & PR_MAP_RECALC)
93 recalc_server_map(px);
94
95 if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight)
96 px->lbprm.map.rr_idx = 0;
97 return px->lbprm.map.srv[px->lbprm.map.rr_idx++];
Willy Tarreaubaaee002006-06-26 02:48:02 +020098}
99
100
101/*
102 * This function tries to find a running server for the proxy <px> following
103 * the source hash method. Depending on the number of active/backup servers,
104 * it will either look for active servers, or for backup servers.
105 * If any server is found, it will be returned. If no valid server is found,
106 * NULL is returned.
107 */
Willy Tarreau5af3a692007-07-24 23:32:33 +0200108static inline struct server *get_server_sh(struct proxy *px,
Willy Tarreaub17916e2006-10-15 15:17:57 +0200109 const char *addr, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110{
111 unsigned int h, l;
112
Willy Tarreau20697042007-11-15 23:26:18 +0100113 if (px->lbprm.tot_weight == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 return NULL;
115
Willy Tarreau20697042007-11-15 23:26:18 +0100116 if (px->lbprm.map.state & PR_MAP_RECALC)
117 recalc_server_map(px);
118
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119 l = h = 0;
120 if (px->srv_act > 1 || (px->srv_act == 0 && px->srv_bck > 1)) {
121 while ((l + sizeof (int)) <= len) {
122 h ^= ntohl(*(unsigned int *)(&addr[l]));
123 l += sizeof (int);
124 }
Willy Tarreau20697042007-11-15 23:26:18 +0100125 h %= px->lbprm.tot_weight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126 }
Willy Tarreau20697042007-11-15 23:26:18 +0100127 return px->lbprm.map.srv[h];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128}
129
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200130/*
131 * This function tries to find a running server for the proxy <px> following
132 * the URI hash method. In order to optimize cache hits, the hash computation
133 * ends at the question mark. Depending on the number of active/backup servers,
134 * it will either look for active servers, or for backup servers.
135 * If any server is found, it will be returned. If no valid server is found,
136 * NULL is returned.
137 *
138 * This code was contributed by Guillaume Dallaire, who also selected this hash
139 * algorithm out of a tens because it gave him the best results.
140 *
141 */
142static inline struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
143{
144 unsigned long hash = 0;
145 int c;
146
Willy Tarreau20697042007-11-15 23:26:18 +0100147 if (px->lbprm.tot_weight == 0)
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200148 return NULL;
149
Willy Tarreau20697042007-11-15 23:26:18 +0100150 if (px->lbprm.map.state & PR_MAP_RECALC)
151 recalc_server_map(px);
152
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200153 while (uri_len--) {
154 c = *uri++;
155 if (c == '?')
156 break;
157 hash = c + (hash << 6) + (hash << 16) - hash;
158 }
159
Willy Tarreau20697042007-11-15 23:26:18 +0100160 return px->lbprm.map.srv[hash % px->lbprm.tot_weight];
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200161}
162
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163
164#endif /* _PROTO_BACKEND_H */
165
166/*
167 * Local variables:
168 * c-indent-level: 8
169 * c-basic-offset: 8
170 * End:
171 */