blob: 8fa8c834fd11f72c1af95c001df60eedd9acf602 [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 Tarreau5dc2fa62007-11-19 19:10:18 +010045void init_server_map(struct proxy *p);
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
47/*
48 * This function tries to find a running server with free connection slots for
49 * the proxy <px> following the round-robin method.
Willy Tarreau20697042007-11-15 23:26:18 +010050 * 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 +020051 * to point to the next server. If no valid server is found, NULL is returned.
52 */
53static inline struct server *get_server_rr_with_conns(struct proxy *px)
54{
55 int newidx;
56 struct server *srv;
57
Willy Tarreau20697042007-11-15 23:26:18 +010058 if (px->lbprm.tot_weight == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +020059 return NULL;
60
Willy Tarreau20697042007-11-15 23:26:18 +010061 if (px->lbprm.map.state & PR_MAP_RECALC)
62 recalc_server_map(px);
63
64 if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight)
65 px->lbprm.map.rr_idx = 0;
66 newidx = px->lbprm.map.rr_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +020067
68 do {
Willy Tarreau20697042007-11-15 23:26:18 +010069 srv = px->lbprm.map.srv[newidx++];
Willy Tarreaubaaee002006-06-26 02:48:02 +020070 if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv)) {
Willy Tarreau20697042007-11-15 23:26:18 +010071 px->lbprm.map.rr_idx = newidx;
Willy Tarreaubaaee002006-06-26 02:48:02 +020072 return srv;
73 }
Willy Tarreau20697042007-11-15 23:26:18 +010074 if (newidx == px->lbprm.tot_weight)
Willy Tarreaubaaee002006-06-26 02:48:02 +020075 newidx = 0;
Willy Tarreau20697042007-11-15 23:26:18 +010076 } while (newidx != px->lbprm.map.rr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
78 return NULL;
79}
80
81
82/*
83 * This function tries to find a running server for the proxy <px> following
84 * the round-robin method.
Willy Tarreau20697042007-11-15 23:26:18 +010085 * 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 +020086 * to point to the next server. If no valid server is found, NULL is returned.
87 */
88static inline struct server *get_server_rr(struct proxy *px)
89{
Willy Tarreau20697042007-11-15 23:26:18 +010090 if (px->lbprm.tot_weight == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 return NULL;
92
Willy Tarreau20697042007-11-15 23:26:18 +010093 if (px->lbprm.map.state & PR_MAP_RECALC)
94 recalc_server_map(px);
95
96 if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight)
97 px->lbprm.map.rr_idx = 0;
98 return px->lbprm.map.srv[px->lbprm.map.rr_idx++];
Willy Tarreaubaaee002006-06-26 02:48:02 +020099}
100
101
102/*
103 * This function tries to find a running server for the proxy <px> following
104 * the source hash method. Depending on the number of active/backup servers,
105 * it will either look for active servers, or for backup servers.
106 * If any server is found, it will be returned. If no valid server is found,
107 * NULL is returned.
108 */
Willy Tarreau5af3a692007-07-24 23:32:33 +0200109static inline struct server *get_server_sh(struct proxy *px,
Willy Tarreaub17916e2006-10-15 15:17:57 +0200110 const char *addr, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111{
112 unsigned int h, l;
113
Willy Tarreau20697042007-11-15 23:26:18 +0100114 if (px->lbprm.tot_weight == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 return NULL;
116
Willy Tarreau20697042007-11-15 23:26:18 +0100117 if (px->lbprm.map.state & PR_MAP_RECALC)
118 recalc_server_map(px);
119
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120 l = h = 0;
121 if (px->srv_act > 1 || (px->srv_act == 0 && px->srv_bck > 1)) {
122 while ((l + sizeof (int)) <= len) {
123 h ^= ntohl(*(unsigned int *)(&addr[l]));
124 l += sizeof (int);
125 }
Willy Tarreau20697042007-11-15 23:26:18 +0100126 h %= px->lbprm.tot_weight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127 }
Willy Tarreau20697042007-11-15 23:26:18 +0100128 return px->lbprm.map.srv[h];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129}
130
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200131/*
132 * This function tries to find a running server for the proxy <px> following
133 * the URI hash method. In order to optimize cache hits, the hash computation
134 * ends at the question mark. Depending on the number of active/backup servers,
135 * it will either look for active servers, or for backup servers.
136 * If any server is found, it will be returned. If no valid server is found,
137 * NULL is returned.
138 *
139 * This code was contributed by Guillaume Dallaire, who also selected this hash
140 * algorithm out of a tens because it gave him the best results.
141 *
142 */
143static inline struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
144{
145 unsigned long hash = 0;
146 int c;
147
Willy Tarreau20697042007-11-15 23:26:18 +0100148 if (px->lbprm.tot_weight == 0)
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200149 return NULL;
150
Willy Tarreau20697042007-11-15 23:26:18 +0100151 if (px->lbprm.map.state & PR_MAP_RECALC)
152 recalc_server_map(px);
153
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200154 while (uri_len--) {
155 c = *uri++;
156 if (c == '?')
157 break;
158 hash = c + (hash << 6) + (hash << 16) - hash;
159 }
160
Willy Tarreau20697042007-11-15 23:26:18 +0100161 return px->lbprm.map.srv[hash % px->lbprm.tot_weight];
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200162}
163
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164
165#endif /* _PROTO_BACKEND_H */
166
167/*
168 * Local variables:
169 * c-indent-level: 8
170 * c-basic-offset: 8
171 * End:
172 */