blob: aa27654f39d7611826d13cdffecaca9b71316bfa [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
Willy Tarreaubaaee002006-06-26 02:48:02 +020042void recalc_server_map(struct proxy *px);
Krzysztof Oledzki85130942007-10-22 16:21:10 +020043int be_downtime(struct proxy *px);
Willy Tarreau5dc2fa62007-11-19 19:10:18 +010044void init_server_map(struct proxy *p);
Willy Tarreaub625a082007-11-26 01:15:43 +010045void fwrr_init_server_groups(struct proxy *p);
Willy Tarreau51406232008-03-10 22:04:20 +010046void fwlc_init_server_tree(struct proxy *p);
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
48/*
49 * This function tries to find a running server with free connection slots for
50 * the proxy <px> following the round-robin method.
Willy Tarreau20697042007-11-15 23:26:18 +010051 * 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 +020052 * to point to the next server. If no valid server is found, NULL is returned.
53 */
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010054static inline struct server *get_server_rr_with_conns(struct proxy *px, struct server *srvtoavoid)
Willy Tarreaubaaee002006-06-26 02:48:02 +020055{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010056 int newidx, avoididx;
57 struct server *srv, *avoided;
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau20697042007-11-15 23:26:18 +010059 if (px->lbprm.tot_weight == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +020060 return NULL;
61
Willy Tarreau20697042007-11-15 23:26:18 +010062 if (px->lbprm.map.state & PR_MAP_RECALC)
63 recalc_server_map(px);
64
65 if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight)
66 px->lbprm.map.rr_idx = 0;
67 newidx = px->lbprm.map.rr_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010069 avoided = NULL;
Willy Tarreauf863ac12008-03-04 06:38:57 +010070 avoididx = 0; /* shut a gcc warning */
Willy Tarreaubaaee002006-06-26 02:48:02 +020071 do {
Willy Tarreau20697042007-11-15 23:26:18 +010072 srv = px->lbprm.map.srv[newidx++];
Willy Tarreaubaaee002006-06-26 02:48:02 +020073 if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv)) {
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010074 /* make sure it is not the server we are try to exclude... */
75 if (srv != srvtoavoid) {
76 px->lbprm.map.rr_idx = newidx;
77 return srv;
78 }
79
80 avoided = srv; /* ...but remember that is was selected yet avoided */
81 avoididx = newidx;
Willy Tarreaubaaee002006-06-26 02:48:02 +020082 }
Willy Tarreau20697042007-11-15 23:26:18 +010083 if (newidx == px->lbprm.tot_weight)
Willy Tarreaubaaee002006-06-26 02:48:02 +020084 newidx = 0;
Willy Tarreau20697042007-11-15 23:26:18 +010085 } while (newidx != px->lbprm.map.rr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010087 if (avoided)
88 px->lbprm.map.rr_idx = avoididx;
89
90 /* return NULL or srvtoavoid if found */
91 return avoided;
Willy Tarreaubaaee002006-06-26 02:48:02 +020092}
93
94
95/*
96 * This function tries to find a running server for the proxy <px> following
97 * the round-robin method.
Willy Tarreau20697042007-11-15 23:26:18 +010098 * 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 +020099 * to point to the next server. If no valid server is found, NULL is returned.
100 */
101static inline struct server *get_server_rr(struct proxy *px)
102{
Willy Tarreau20697042007-11-15 23:26:18 +0100103 if (px->lbprm.tot_weight == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104 return NULL;
105
Willy Tarreau20697042007-11-15 23:26:18 +0100106 if (px->lbprm.map.state & PR_MAP_RECALC)
107 recalc_server_map(px);
108
109 if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight)
110 px->lbprm.map.rr_idx = 0;
111 return px->lbprm.map.srv[px->lbprm.map.rr_idx++];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112}
113
114
115/*
116 * This function tries to find a running server for the proxy <px> following
117 * the source hash method. Depending on the number of active/backup servers,
118 * it will either look for active servers, or for backup servers.
119 * If any server is found, it will be returned. If no valid server is found,
120 * NULL is returned.
121 */
Willy Tarreau5af3a692007-07-24 23:32:33 +0200122static inline struct server *get_server_sh(struct proxy *px,
Willy Tarreaub17916e2006-10-15 15:17:57 +0200123 const char *addr, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124{
125 unsigned int h, l;
126
Willy Tarreau20697042007-11-15 23:26:18 +0100127 if (px->lbprm.tot_weight == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128 return NULL;
129
Willy Tarreau20697042007-11-15 23:26:18 +0100130 if (px->lbprm.map.state & PR_MAP_RECALC)
131 recalc_server_map(px);
132
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133 l = h = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +0100134
135 /* note: we won't hash if there's only one server left */
136 if (px->lbprm.tot_used > 1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200137 while ((l + sizeof (int)) <= len) {
138 h ^= ntohl(*(unsigned int *)(&addr[l]));
139 l += sizeof (int);
140 }
Willy Tarreau20697042007-11-15 23:26:18 +0100141 h %= px->lbprm.tot_weight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200142 }
Willy Tarreau20697042007-11-15 23:26:18 +0100143 return px->lbprm.map.srv[h];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200144}
145
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200146/*
147 * This function tries to find a running server for the proxy <px> following
148 * the URI hash method. In order to optimize cache hits, the hash computation
149 * ends at the question mark. Depending on the number of active/backup servers,
150 * it will either look for active servers, or for backup servers.
151 * If any server is found, it will be returned. If no valid server is found,
152 * NULL is returned.
153 *
154 * This code was contributed by Guillaume Dallaire, who also selected this hash
155 * algorithm out of a tens because it gave him the best results.
156 *
157 */
158static inline struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
159{
160 unsigned long hash = 0;
161 int c;
Marek Majkowski9c30fc12008-04-27 23:25:55 +0200162 int slashes = 0;
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200163
Willy Tarreau20697042007-11-15 23:26:18 +0100164 if (px->lbprm.tot_weight == 0)
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200165 return NULL;
166
Willy Tarreau20697042007-11-15 23:26:18 +0100167 if (px->lbprm.map.state & PR_MAP_RECALC)
168 recalc_server_map(px);
169
Marek Majkowski9c30fc12008-04-27 23:25:55 +0200170 if (px->uri_len_limit)
171 uri_len = MIN(uri_len, px->uri_len_limit);
172
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200173 while (uri_len--) {
174 c = *uri++;
Marek Majkowski9c30fc12008-04-27 23:25:55 +0200175 if (c == '/') {
176 slashes++;
177 if (slashes == px->uri_dirs_depth1) /* depth+1 */
178 break;
179 }
180 else if (c == '?')
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200181 break;
Marek Majkowski9c30fc12008-04-27 23:25:55 +0200182
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200183 hash = c + (hash << 6) + (hash << 16) - hash;
184 }
185
Willy Tarreau20697042007-11-15 23:26:18 +0100186 return px->lbprm.map.srv[hash % px->lbprm.tot_weight];
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200187}
188
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189
190#endif /* _PROTO_BACKEND_H */
191
192/*
193 * Local variables:
194 * c-indent-level: 8
195 * c-basic-offset: 8
196 * End:
197 */