blob: 69386b8382d49a11124d39f0241c8ff118315f89 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 include/proto/backend.h
3 Functions prototypes for the backend.
4
Willy Tarreauefb453c2008-10-26 20:49:47 +01005 Copyright (C) 2000-2008 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);
Willy Tarreaubaaee002006-06-26 02:48:02 +020036int srv_redispatch_connect(struct session *t);
Willy Tarreaua0cbda62007-11-01 21:39:54 +010037int backend_parse_balance(const char **args, char *err,
38 int errlen, struct proxy *curproxy);
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
Willy Tarreaubaaee002006-06-26 02:48:02 +020040void recalc_server_map(struct proxy *px);
Krzysztof Oledzki85130942007-10-22 16:21:10 +020041int be_downtime(struct proxy *px);
Willy Tarreau5dc2fa62007-11-19 19:10:18 +010042void init_server_map(struct proxy *p);
Willy Tarreaub625a082007-11-26 01:15:43 +010043void fwrr_init_server_groups(struct proxy *p);
Willy Tarreau51406232008-03-10 22:04:20 +010044void fwlc_init_server_tree(struct proxy *p);
Willy Tarreauc5d9c802009-10-01 09:17:05 +020045void recount_servers(struct proxy *px);
46void update_backend_weight(struct proxy *px);
47
48/* This function returns non-zero if a server with the given weight and state
49 * is usable for LB, otherwise zero.
50 */
51static inline int srv_is_usable(int state, int weight)
52{
53 if (!weight)
54 return 0;
55 if (state & SRV_GOINGDOWN)
56 return 0;
57 if (!(state & SRV_RUNNING))
58 return 0;
59 return 1;
60}
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
62/*
63 * This function tries to find a running server with free connection slots for
64 * the proxy <px> following the round-robin method.
Willy Tarreau20697042007-11-15 23:26:18 +010065 * 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 +020066 * to point to the next server. If no valid server is found, NULL is returned.
67 */
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010068static inline struct server *get_server_rr_with_conns(struct proxy *px, struct server *srvtoavoid)
Willy Tarreaubaaee002006-06-26 02:48:02 +020069{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010070 int newidx, avoididx;
71 struct server *srv, *avoided;
Willy Tarreaubaaee002006-06-26 02:48:02 +020072
Willy Tarreau20697042007-11-15 23:26:18 +010073 if (px->lbprm.tot_weight == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +020074 return NULL;
75
Willy Tarreau20697042007-11-15 23:26:18 +010076 if (px->lbprm.map.state & PR_MAP_RECALC)
77 recalc_server_map(px);
78
79 if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight)
80 px->lbprm.map.rr_idx = 0;
81 newidx = px->lbprm.map.rr_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +020082
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010083 avoided = NULL;
Willy Tarreauf863ac12008-03-04 06:38:57 +010084 avoididx = 0; /* shut a gcc warning */
Willy Tarreaubaaee002006-06-26 02:48:02 +020085 do {
Willy Tarreau20697042007-11-15 23:26:18 +010086 srv = px->lbprm.map.srv[newidx++];
Willy Tarreaubaaee002006-06-26 02:48:02 +020087 if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv)) {
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010088 /* make sure it is not the server we are try to exclude... */
89 if (srv != srvtoavoid) {
90 px->lbprm.map.rr_idx = newidx;
91 return srv;
92 }
93
94 avoided = srv; /* ...but remember that is was selected yet avoided */
95 avoididx = newidx;
Willy Tarreaubaaee002006-06-26 02:48:02 +020096 }
Willy Tarreau20697042007-11-15 23:26:18 +010097 if (newidx == px->lbprm.tot_weight)
Willy Tarreaubaaee002006-06-26 02:48:02 +020098 newidx = 0;
Willy Tarreau20697042007-11-15 23:26:18 +010099 } while (newidx != px->lbprm.map.rr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100101 if (avoided)
102 px->lbprm.map.rr_idx = avoididx;
103
104 /* return NULL or srvtoavoid if found */
105 return avoided;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106}
107
108
109/*
110 * This function tries to find a running server for the proxy <px> following
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111 * the source hash method. Depending on the number of active/backup servers,
112 * it will either look for active servers, or for backup servers.
113 * If any server is found, it will be returned. If no valid server is found,
114 * NULL is returned.
115 */
Willy Tarreau5af3a692007-07-24 23:32:33 +0200116static inline struct server *get_server_sh(struct proxy *px,
Willy Tarreaub17916e2006-10-15 15:17:57 +0200117 const char *addr, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118{
119 unsigned int h, l;
120
Willy Tarreau20697042007-11-15 23:26:18 +0100121 if (px->lbprm.tot_weight == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 return NULL;
123
Willy Tarreau20697042007-11-15 23:26:18 +0100124 if (px->lbprm.map.state & PR_MAP_RECALC)
125 recalc_server_map(px);
126
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127 l = h = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +0100128
129 /* note: we won't hash if there's only one server left */
130 if (px->lbprm.tot_used > 1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131 while ((l + sizeof (int)) <= len) {
132 h ^= ntohl(*(unsigned int *)(&addr[l]));
133 l += sizeof (int);
134 }
Willy Tarreau20697042007-11-15 23:26:18 +0100135 h %= px->lbprm.tot_weight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136 }
Willy Tarreau20697042007-11-15 23:26:18 +0100137 return px->lbprm.map.srv[h];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200138}
139
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200140/*
141 * This function tries to find a running server for the proxy <px> following
142 * the URI hash method. In order to optimize cache hits, the hash computation
143 * ends at the question mark. Depending on the number of active/backup servers,
144 * it will either look for active servers, or for backup servers.
145 * If any server is found, it will be returned. If no valid server is found,
146 * NULL is returned.
147 *
148 * This code was contributed by Guillaume Dallaire, who also selected this hash
149 * algorithm out of a tens because it gave him the best results.
150 *
151 */
152static inline struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
153{
154 unsigned long hash = 0;
155 int c;
Marek Majkowski9c30fc12008-04-27 23:25:55 +0200156 int slashes = 0;
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200157
Willy Tarreau20697042007-11-15 23:26:18 +0100158 if (px->lbprm.tot_weight == 0)
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200159 return NULL;
160
Willy Tarreau20697042007-11-15 23:26:18 +0100161 if (px->lbprm.map.state & PR_MAP_RECALC)
162 recalc_server_map(px);
163
Marek Majkowski9c30fc12008-04-27 23:25:55 +0200164 if (px->uri_len_limit)
165 uri_len = MIN(uri_len, px->uri_len_limit);
166
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200167 while (uri_len--) {
168 c = *uri++;
Marek Majkowski9c30fc12008-04-27 23:25:55 +0200169 if (c == '/') {
170 slashes++;
171 if (slashes == px->uri_dirs_depth1) /* depth+1 */
172 break;
173 }
174 else if (c == '?')
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200175 break;
Marek Majkowski9c30fc12008-04-27 23:25:55 +0200176
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200177 hash = c + (hash << 6) + (hash << 16) - hash;
178 }
179
Willy Tarreau20697042007-11-15 23:26:18 +0100180 return px->lbprm.map.srv[hash % px->lbprm.tot_weight];
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200181}
182
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183
184#endif /* _PROTO_BACKEND_H */
185
186/*
187 * Local variables:
188 * c-indent-level: 8
189 * c-basic-offset: 8
190 * End:
191 */