blob: 34783f51e6899d9670980ee278ba06514daf1879 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreauf89c1872009-10-01 11:19:37 +02004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <syslog.h>
Willy Tarreauf19cf372006-11-14 15:40:51 +010018#include <string.h>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +020019#include <ctype.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040020#include <sys/types.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020023#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020024#include <common/debug.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020025#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010030#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <proto/backend.h>
Willy Tarreau14c8aac2007-05-08 19:46:30 +020032#include <proto/client.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020033#include <proto/lb_chash.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020034#include <proto/lb_fwlc.h>
35#include <proto/lb_fwrr.h>
36#include <proto/lb_map.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010038#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010040#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020041#include <proto/session.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/task.h>
43
Willy Tarreaubaaee002006-06-26 02:48:02 +020044/*
45 * This function recounts the number of usable active and backup servers for
46 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010047 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010048 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010049 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020050 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020051void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020052{
53 struct server *srv;
54
Willy Tarreau20697042007-11-15 23:26:18 +010055 px->srv_act = px->srv_bck = 0;
56 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010057 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010059 if (!srv_is_usable(srv->state, srv->eweight))
60 continue;
61
62 if (srv->state & SRV_BACKUP) {
63 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010064 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010065 px->lbprm.fbck = srv;
66 px->srv_bck++;
67 px->lbprm.tot_wbck += srv->eweight;
68 } else {
69 px->srv_act++;
70 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020071 }
72 }
Willy Tarreaub625a082007-11-26 01:15:43 +010073}
Willy Tarreau20697042007-11-15 23:26:18 +010074
Willy Tarreaub625a082007-11-26 01:15:43 +010075/* This function simply updates the backend's tot_weight and tot_used values
76 * after servers weights have been updated. It is designed to be used after
77 * recount_servers() or equivalent.
78 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020079void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010080{
Willy Tarreau20697042007-11-15 23:26:18 +010081 if (px->srv_act) {
82 px->lbprm.tot_weight = px->lbprm.tot_wact;
83 px->lbprm.tot_used = px->srv_act;
84 }
Willy Tarreaub625a082007-11-26 01:15:43 +010085 else if (px->lbprm.fbck) {
86 /* use only the first backup server */
87 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
88 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010089 }
90 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010091 px->lbprm.tot_weight = px->lbprm.tot_wbck;
92 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +010093 }
Willy Tarreaub625a082007-11-26 01:15:43 +010094}
95
Willy Tarreau39c9ba72009-10-01 21:11:15 +020096/*
97 * This function tries to find a running server for the proxy <px> following
98 * the source hash method. Depending on the number of active/backup servers,
99 * it will either look for active servers, or for backup servers.
100 * If any server is found, it will be returned. If no valid server is found,
101 * NULL is returned.
102 */
103struct server *get_server_sh(struct proxy *px, const char *addr, int len)
104{
105 unsigned int h, l;
106
107 if (px->lbprm.tot_weight == 0)
108 return NULL;
109
110 l = h = 0;
111
112 /* note: we won't hash if there's only one server left */
113 if (px->lbprm.tot_used == 1)
114 goto hash_done;
115
116 while ((l + sizeof (int)) <= len) {
117 h ^= ntohl(*(unsigned int *)(&addr[l]));
118 l += sizeof (int);
119 }
120 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200121 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
122 return chash_get_server_hash(px, h);
123 else
124 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200125}
126
127/*
128 * This function tries to find a running server for the proxy <px> following
129 * the URI hash method. In order to optimize cache hits, the hash computation
130 * ends at the question mark. Depending on the number of active/backup servers,
131 * it will either look for active servers, or for backup servers.
132 * If any server is found, it will be returned. If no valid server is found,
133 * NULL is returned.
134 *
135 * This code was contributed by Guillaume Dallaire, who also selected this hash
136 * algorithm out of a tens because it gave him the best results.
137 *
138 */
139struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
140{
141 unsigned long hash = 0;
142 int c;
143 int slashes = 0;
144
145 if (px->lbprm.tot_weight == 0)
146 return NULL;
147
148 /* note: we won't hash if there's only one server left */
149 if (px->lbprm.tot_used == 1)
150 goto hash_done;
151
152 if (px->uri_len_limit)
153 uri_len = MIN(uri_len, px->uri_len_limit);
154
155 while (uri_len--) {
156 c = *uri++;
157 if (c == '/') {
158 slashes++;
159 if (slashes == px->uri_dirs_depth1) /* depth+1 */
160 break;
161 }
162 else if (c == '?')
163 break;
164
165 hash = c + (hash << 6) + (hash << 16) - hash;
166 }
167 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200168 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
169 return chash_get_server_hash(px, hash);
170 else
171 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200172}
173
Willy Tarreau01732802007-11-01 22:48:15 +0100174/*
175 * This function tries to find a running server for the proxy <px> following
176 * the URL parameter hash method. It looks for a specific parameter in the
177 * URL and hashes it to compute the server ID. This is useful to optimize
178 * performance by avoiding bounces between servers in contexts where sessions
179 * are shared but cookies are not usable. If the parameter is not found, NULL
180 * is returned. If any server is found, it will be returned. If no valid server
181 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100182 */
183struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
184{
185 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200186 const char *p;
187 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100188 int plen;
189
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200190 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100191 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100192 return NULL;
193
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200194 if ((p = memchr(uri, '?', uri_len)) == NULL)
195 return NULL;
196
Willy Tarreau01732802007-11-01 22:48:15 +0100197 p++;
198
199 uri_len -= (p - uri);
200 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200201 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100202
203 while (uri_len > plen) {
204 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200205 if (params[plen] == '=') {
206 if (memcmp(params, px->url_param_name, plen) == 0) {
207 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100208 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200209 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100210 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200211 p += plen + 1;
212 uri_len -= plen + 1;
213
Willy Tarreau01732802007-11-01 22:48:15 +0100214 while (uri_len && *p != '&') {
215 hash = *p + (hash << 6) + (hash << 16) - hash;
216 uri_len--;
217 p++;
218 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200219 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
220 return chash_get_server_hash(px, hash);
221 else
222 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100223 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200224 }
225 /* skip to next parameter */
226 p = memchr(params, '&', uri_len);
227 if (!p)
228 return NULL;
229 p++;
230 uri_len -= (p - params);
231 params = p;
232 }
233 return NULL;
234}
235
236/*
237 * this does the same as the previous server_ph, but check the body contents
238 */
239struct server *get_server_ph_post(struct session *s)
240{
241 unsigned long hash = 0;
242 struct http_txn *txn = &s->txn;
243 struct buffer *req = s->req;
244 struct http_msg *msg = &txn->req;
245 struct proxy *px = s->be;
246 unsigned int plen = px->url_param_len;
Willy Tarreau157dd632009-12-06 19:18:09 +0100247 unsigned long len = msg->hdr_content_len;
248 const char *params = req->data + msg->sov;
249 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200250
Willy Tarreau7c96f672009-12-27 22:47:25 +0100251 if (len > req->l - (msg->sov - msg->som))
252 len = req->l - (msg->sov - msg->som);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200253
Willy Tarreau157dd632009-12-06 19:18:09 +0100254 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200255 return NULL;
256
Willy Tarreau157dd632009-12-06 19:18:09 +0100257 if (px->lbprm.tot_weight == 0)
258 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200259
260 while (len > plen) {
261 /* Look for the parameter name followed by an equal symbol */
262 if (params[plen] == '=') {
263 if (memcmp(params, px->url_param_name, plen) == 0) {
264 /* OK, we have the parameter here at <params>, and
265 * the value after the equal sign, at <p>
266 * skip the equal symbol
267 */
268 p += plen + 1;
269 len -= plen + 1;
270
271 while (len && *p != '&') {
272 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100273 /* if in a POST, body must be URI encoded or it's not a URI.
274 * Do not interprete any possible binary data as a parameter.
275 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200276 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
277 break;
278 return NULL; /* oh, no; this is not uri-encoded.
279 * This body does not contain parameters.
280 */
281 }
282 hash = *p + (hash << 6) + (hash << 16) - hash;
283 len--;
284 p++;
285 /* should we break if vlen exceeds limit? */
286 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200287 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
288 return chash_get_server_hash(px, hash);
289 else
290 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200291 }
292 }
Willy Tarreau01732802007-11-01 22:48:15 +0100293 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200294 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100295 if (!p)
296 return NULL;
297 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200298 len -= (p - params);
299 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100300 }
301 return NULL;
302}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200303
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200304
Willy Tarreaubaaee002006-06-26 02:48:02 +0200305/*
Benoitaffb4812009-03-25 13:02:10 +0100306 * This function tries to find a running server for the proxy <px> following
307 * the Header parameter hash method. It looks for a specific parameter in the
308 * URL and hashes it to compute the server ID. This is useful to optimize
309 * performance by avoiding bounces between servers in contexts where sessions
310 * are shared but cookies are not usable. If the parameter is not found, NULL
311 * is returned. If any server is found, it will be returned. If no valid server
312 * is found, NULL is returned.
313 */
314struct server *get_server_hh(struct session *s)
315{
316 unsigned long hash = 0;
317 struct http_txn *txn = &s->txn;
318 struct http_msg *msg = &txn->req;
319 struct proxy *px = s->be;
320 unsigned int plen = px->hh_len;
321 unsigned long len;
322 struct hdr_ctx ctx;
323 const char *p;
324
325 /* tot_weight appears to mean srv_count */
326 if (px->lbprm.tot_weight == 0)
327 return NULL;
328
Benoitaffb4812009-03-25 13:02:10 +0100329 ctx.idx = 0;
330
331 /* if the message is chunked, we skip the chunk size, but use the value as len */
332 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
333
334 /* if the header is not found or empty, let's fallback to round robin */
335 if (!ctx.idx || !ctx.vlen)
336 return NULL;
337
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200338 /* note: we won't hash if there's only one server left */
339 if (px->lbprm.tot_used == 1)
340 goto hash_done;
341
Benoitaffb4812009-03-25 13:02:10 +0100342 /* Found a the hh_name in the headers.
343 * we will compute the hash based on this value ctx.val.
344 */
345 len = ctx.vlen;
346 p = (char *)ctx.line + ctx.val;
347 if (!px->hh_match_domain) {
348 while (len) {
349 hash = *p + (hash << 6) + (hash << 16) - hash;
350 len--;
351 p++;
352 }
353 } else {
354 int dohash = 0;
355 p += len - 1;
356 /* special computation, use only main domain name, not tld/host
357 * going back from the end of string, start hashing at first
358 * dot stop at next.
359 * This is designed to work with the 'Host' header, and requires
360 * a special option to activate this.
361 */
362 while (len) {
363 if (*p == '.') {
364 if (!dohash)
365 dohash = 1;
366 else
367 break;
368 } else {
369 if (dohash)
370 hash = *p + (hash << 6) + (hash << 16) - hash;
371 }
372 len--;
373 p--;
374 }
375 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200376 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200377 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
378 return chash_get_server_hash(px, hash);
379 else
380 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100381}
382
Emeric Brun736aa232009-06-30 17:56:00 +0200383struct server *get_server_rch(struct session *s)
384{
385 unsigned long hash = 0;
386 struct proxy *px = s->be;
387 unsigned long len;
388 const char *p;
389 int ret;
390 struct acl_expr expr;
391 struct acl_test test;
392
393 /* tot_weight appears to mean srv_count */
394 if (px->lbprm.tot_weight == 0)
395 return NULL;
396
Emeric Brun736aa232009-06-30 17:56:00 +0200397 memset(&expr, 0, sizeof(expr));
398 memset(&test, 0, sizeof(test));
399
400 expr.arg.str = px->hh_name;
401 expr.arg_len = px->hh_len;
402
403 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
404 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
405 return NULL;
406
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200407 /* note: we won't hash if there's only one server left */
408 if (px->lbprm.tot_used == 1)
409 goto hash_done;
410
Emeric Brun736aa232009-06-30 17:56:00 +0200411 /* Found a the hh_name in the headers.
412 * we will compute the hash based on this value ctx.val.
413 */
414 len = test.len;
415 p = (char *)test.ptr;
416 while (len) {
417 hash = *p + (hash << 6) + (hash << 16) - hash;
418 len--;
419 p++;
420 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200421 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200422 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
423 return chash_get_server_hash(px, hash);
424 else
425 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200426}
Benoitaffb4812009-03-25 13:02:10 +0100427
428/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200429 * This function applies the load-balancing algorithm to the session, as
430 * defined by the backend it is assigned to. The session is then marked as
431 * 'assigned'.
432 *
433 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
434 * had a server previously assigned, it is rebalanced, trying to avoid the same
435 * server.
436 * The function tries to keep the original connection slot if it reconnects to
437 * the same server, otherwise it releases it and tries to offer it.
438 *
439 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440 *
441 * It may return :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200442 * SRV_STATUS_OK if everything is OK. Session assigned to ->srv
443 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
444 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445 * SRV_STATUS_INTERNAL for other unrecoverable errors.
446 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200447 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
448 * it does not need to be called anymore. This means that s->srv can be trusted
449 * in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450 *
451 */
452
453int assign_server(struct session *s)
454{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100455
Willy Tarreau7c669d72008-06-20 15:04:11 +0200456 struct server *conn_slot;
457 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100458
Willy Tarreaubaaee002006-06-26 02:48:02 +0200459#ifdef DEBUG_FULL
460 fprintf(stderr,"assign_server : s=%p\n",s);
461#endif
462
Willy Tarreau7c669d72008-06-20 15:04:11 +0200463 err = SRV_STATUS_INTERNAL;
464 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
465 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100466
Willy Tarreau7c669d72008-06-20 15:04:11 +0200467 s->prev_srv = s->prev_srv;
468 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469
Willy Tarreau7c669d72008-06-20 15:04:11 +0200470 /* We have to release any connection slot before applying any LB algo,
471 * otherwise we may erroneously end up with no available slot.
472 */
473 if (conn_slot)
474 sess_change_server(s, NULL);
475
476 /* We will now try to find the good server and store it into <s->srv>.
477 * Note that <s->srv> may be NULL in case of dispatch or proxy mode,
478 * as well as if no server is available (check error code).
479 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100480
Willy Tarreau7c669d72008-06-20 15:04:11 +0200481 s->srv = NULL;
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200482 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200483 int len;
484 /* we must check if we have at least one server available */
485 if (!s->be->lbprm.tot_weight) {
486 err = SRV_STATUS_NOSRV;
487 goto out;
488 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200489
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200490 /* First check whether we need to fetch some data or simply call
491 * the LB lookup function. Only the hashing functions will need
492 * some input data in fact, and will support multiple algorithms.
493 */
494 switch (s->be->lbprm.algo & BE_LB_LKUP) {
495 case BE_LB_LKUP_RRTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200496 s->srv = fwrr_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200497 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200498
499 case BE_LB_LKUP_LCTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200500 s->srv = fwlc_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200501 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200502
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200503 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200504 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200505 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200506 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
507 s->srv = chash_get_next_server(s->be, s->prev_srv);
508 else
509 s->srv = map_get_server_rr(s->be, s->prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200510 break;
511 }
512 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200513 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200514 err = SRV_STATUS_INTERNAL;
515 goto out;
516 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200517
518 switch (s->be->lbprm.algo & BE_LB_PARM) {
519 case BE_LB_HASH_SRC:
520 if (s->cli_addr.ss_family == AF_INET)
521 len = 4;
522 else if (s->cli_addr.ss_family == AF_INET6)
523 len = 16;
524 else {
525 /* unknown IP family */
526 err = SRV_STATUS_INTERNAL;
527 goto out;
528 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200530 s->srv = get_server_sh(s->be,
531 (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
532 len);
533 break;
534
535 case BE_LB_HASH_URI:
536 /* URI hashing */
537 s->srv = get_server_uh(s->be,
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100538 s->txn.req.sol - s->txn.req.som + s->txn.req.sl.rq.u,
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200539 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200540 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200541
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200542 case BE_LB_HASH_PRM:
543 /* URL Parameter hashing */
544 if (s->txn.meth == HTTP_METH_POST &&
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100545 memchr(s->txn.req.sol - s->txn.req.som + s->txn.req.sl.rq.u, '&',
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200546 s->txn.req.sl.rq.u_l ) == NULL)
547 s->srv = get_server_ph_post(s);
548 else
549 s->srv = get_server_ph(s->be,
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100550 s->txn.req.sol - s->txn.req.som + s->txn.req.sl.rq.u,
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200551 s->txn.req.sl.rq.u_l);
552 break;
Benoitaffb4812009-03-25 13:02:10 +0100553
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200554 case BE_LB_HASH_HDR:
555 /* Header Parameter hashing */
556 s->srv = get_server_hh(s);
557 break;
558
559 case BE_LB_HASH_RDP:
560 /* RDP Cookie hashing */
561 s->srv = get_server_rch(s);
562 break;
563
564 default:
565 /* unknown balancing algorithm */
566 err = SRV_STATUS_INTERNAL;
567 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100568 }
Emeric Brun736aa232009-06-30 17:56:00 +0200569
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200570 /* If the hashing parameter was not found, let's fall
571 * back to round robin on the map.
572 */
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200573 if (!s->srv) {
574 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
575 s->srv = chash_get_next_server(s->be, s->prev_srv);
576 else
577 s->srv = map_get_server_rr(s->be, s->prev_srv);
578 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200579
580 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200581 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200582
Willy Tarreau7c669d72008-06-20 15:04:11 +0200583 default:
584 /* unknown balancing algorithm */
585 err = SRV_STATUS_INTERNAL;
586 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200587 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200588
589 if (!s->srv) {
590 err = SRV_STATUS_FULL;
591 goto out;
592 }
593 else if (s->srv != s->prev_srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200594 s->be->counters.cum_lbconn++;
595 s->srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100596 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200597 }
598 else if (s->be->options & PR_O_HTTP_PROXY) {
599 if (!s->srv_addr.sin_addr.s_addr) {
600 err = SRV_STATUS_NOSRV;
601 goto out;
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100602 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200603 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200604 else if (!*(int *)&s->be->dispatch_addr.sin_addr &&
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100605 !(s->be->options & PR_O_TRANSP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200606 err = SRV_STATUS_NOSRV;
607 goto out;
608 }
609
610 s->flags |= SN_ASSIGNED;
611 err = SRV_STATUS_OK;
612 out:
613
614 /* Either we take back our connection slot, or we offer it to someone
615 * else if we don't need it anymore.
616 */
617 if (conn_slot) {
618 if (conn_slot == s->srv) {
619 sess_change_server(s, s->srv);
620 } else {
621 if (may_dequeue_tasks(conn_slot, s->be))
622 process_srv_queue(conn_slot);
623 }
624 }
625
626 out_err:
627 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200628}
629
630
631/*
632 * This function assigns a server address to a session, and sets SN_ADDR_SET.
633 * The address is taken from the currently assigned server, or from the
634 * dispatch or transparent address.
635 *
636 * It may return :
637 * SRV_STATUS_OK if everything is OK.
638 * SRV_STATUS_INTERNAL for other unrecoverable errors.
639 *
640 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
641 * not cleared, so it's to the caller to clear it if required.
642 *
643 */
644int assign_server_address(struct session *s)
645{
646#ifdef DEBUG_FULL
647 fprintf(stderr,"assign_server_address : s=%p\n",s);
648#endif
649
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200650 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651 /* A server is necessarily known for this session */
652 if (!(s->flags & SN_ASSIGNED))
653 return SRV_STATUS_INTERNAL;
654
655 s->srv_addr = s->srv->addr;
656
657 /* if this server remaps proxied ports, we'll use
658 * the port the client connected to with an offset. */
659 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100660 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200661 get_frt_addr(s);
662 if (s->frt_addr.ss_family == AF_INET) {
663 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
664 ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port));
665 } else {
666 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
667 ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port));
668 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669 }
670 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200671 else if (*(int *)&s->be->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672 /* connect to the defined dispatch addr */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200673 s->srv_addr = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100675 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100677 if (!(s->flags & SN_FRT_ADDR_SET))
678 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679
Willy Tarreaubd414282008-01-19 13:46:35 +0100680 memcpy(&s->srv_addr, &s->frt_addr, MIN(sizeof(s->srv_addr), sizeof(s->frt_addr)));
681 /* when we support IPv6 on the backend, we may add other tests */
682 //qfprintf(stderr, "Cannot get original server address.\n");
683 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100685 else if (s->be->options & PR_O_HTTP_PROXY) {
686 /* If HTTP PROXY option is set, then server is already assigned
687 * during incoming client request parsing. */
688 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100689 else {
690 /* no server and no LB algorithm ! */
691 return SRV_STATUS_INTERNAL;
692 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200693
694 s->flags |= SN_ADDR_SET;
695 return SRV_STATUS_OK;
696}
697
698
699/* This function assigns a server to session <s> if required, and can add the
700 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200701 * If ->srv_conn is set, the session is first released from the server.
702 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
703 * be called before any connection and after any retry or redispatch occurs.
704 *
705 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706 *
707 * Returns :
708 *
709 * SRV_STATUS_OK if everything is OK.
710 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
711 * SRV_STATUS_QUEUED if the connection has been queued.
712 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200713 * connection could not be queued in s->srv,
714 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200715 * SRV_STATUS_INTERNAL for other unrecoverable errors.
716 *
717 */
718int assign_server_and_queue(struct session *s)
719{
720 struct pendconn *p;
721 int err;
722
723 if (s->pend_pos)
724 return SRV_STATUS_INTERNAL;
725
Willy Tarreau7c669d72008-06-20 15:04:11 +0200726 err = SRV_STATUS_OK;
727 if (!(s->flags & SN_ASSIGNED)) {
728 err = assign_server(s);
729 if (s->prev_srv) {
730 /* This session was previously assigned to a server. We have to
731 * update the session's and the server's stats :
732 * - if the server changed :
733 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
734 * - set SN_REDISP if it was successfully redispatched
735 * - increment srv->redispatches and be->redispatches
736 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100737 */
738
Willy Tarreau7c669d72008-06-20 15:04:11 +0200739 if (s->prev_srv != s->srv) {
740 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
741 s->txn.flags &= ~TX_CK_MASK;
742 s->txn.flags |= TX_CK_DOWN;
743 }
744 s->flags |= SN_REDISP;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200745 s->prev_srv->counters.redispatches++;
746 s->be->counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200747 } else {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200748 s->prev_srv->counters.retries++;
749 s->be->counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100750 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100751 }
752 }
753
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754 switch (err) {
755 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200756 /* we have SN_ASSIGNED set */
757 if (!s->srv)
758 return SRV_STATUS_OK; /* dispatch or proxy mode */
759
760 /* If we already have a connection slot, no need to check any queue */
761 if (s->srv_conn == s->srv)
762 return SRV_STATUS_OK;
763
764 /* OK, this session already has an assigned server, but no
765 * connection slot yet. Either it is a redispatch, or it was
766 * assigned from persistence information (direct mode).
767 */
768 if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) {
769 /* server scheduled for redirection, and already assigned. We
770 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100771 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200772 sess_change_server(s, s->srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100773 return SRV_STATUS_OK;
774 }
775
Willy Tarreau7c669d72008-06-20 15:04:11 +0200776 /* We might have to queue this session if the assigned server is full.
777 * We know we have to queue it into the server's queue, so if a maxqueue
778 * is set on the server, we must also check that the server's queue is
779 * not full, in which case we have to return FULL.
780 */
781 if (s->srv->maxconn &&
782 (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) {
783
784 if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue)
785 return SRV_STATUS_FULL;
786
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787 p = pendconn_add(s);
788 if (p)
789 return SRV_STATUS_QUEUED;
790 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200791 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200793
794 /* OK, we can use this server. Let's reserve our place */
795 sess_change_server(s, s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 return SRV_STATUS_OK;
797
798 case SRV_STATUS_FULL:
799 /* queue this session into the proxy's queue */
800 p = pendconn_add(s);
801 if (p)
802 return SRV_STATUS_QUEUED;
803 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200804 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805
806 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200807 return err;
808
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809 case SRV_STATUS_INTERNAL:
810 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200811
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812 default:
813 return SRV_STATUS_INTERNAL;
814 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100815}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200816
817/*
818 * This function initiates a connection to the server assigned to this session
819 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
820 * It can return one of :
821 * - SN_ERR_NONE if everything's OK
822 * - SN_ERR_SRVTO if there are no more servers
823 * - SN_ERR_SRVCL if the connection was refused by the server
824 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
825 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
826 * - SN_ERR_INTERNAL for any other purely internal errors
827 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
828 */
829int connect_server(struct session *s)
830{
Willy Tarreau9650f372009-08-16 14:02:45 +0200831 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200832
833 if (!(s->flags & SN_ADDR_SET)) {
834 err = assign_server_address(s);
835 if (err != SRV_STATUS_OK)
836 return SN_ERR_INTERNAL;
837 }
838
Willy Tarreau9650f372009-08-16 14:02:45 +0200839 if (!s->req->cons->connect)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200840 return SN_ERR_INTERNAL;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200841
Willy Tarreau9650f372009-08-16 14:02:45 +0200842 err = s->req->cons->connect(s->req->cons, s->be, s->srv,
843 (struct sockaddr *)&s->srv_addr,
844 (struct sockaddr *)&s->cli_addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200845
Willy Tarreau9650f372009-08-16 14:02:45 +0200846 if (err != SN_ERR_NONE)
847 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200848
Willy Tarreaubaaee002006-06-26 02:48:02 +0200849 if (s->srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100850 s->flags |= SN_CURR_SESS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851 s->srv->cur_sess++;
Willy Tarreauac68c5d2009-10-04 23:12:44 +0200852 if (s->srv->cur_sess > s->srv->counters.cur_sess_max)
853 s->srv->counters.cur_sess_max = s->srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100854 if (s->be->lbprm.server_take_conn)
855 s->be->lbprm.server_take_conn(s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200856 }
857
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858 return SN_ERR_NONE; /* connection is OK */
859}
860
861
Willy Tarreaubaaee002006-06-26 02:48:02 +0200862/* This function performs the "redispatch" part of a connection attempt. It
863 * will assign a server if required, queue the connection if required, and
864 * handle errors that might arise at this level. It can change the server
865 * state. It will return 1 if it encounters an error, switches the server
866 * state, or has to queue a connection. Otherwise, it will return 0 indicating
867 * that the connection is ready to use.
868 */
869
870int srv_redispatch_connect(struct session *t)
871{
872 int conn_err;
873
874 /* We know that we don't have any connection pending, so we will
875 * try to get a new one, and wait in this state if it's queued
876 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200877 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200878 conn_err = assign_server_and_queue(t);
879 switch (conn_err) {
880 case SRV_STATUS_OK:
881 break;
882
Willy Tarreau7c669d72008-06-20 15:04:11 +0200883 case SRV_STATUS_FULL:
884 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
885 * and we can redispatch to another server, or it is not and we return
886 * 503. This only makes sense in DIRECT mode however, because normal LB
887 * algorithms would never select such a server, and hash algorithms
888 * would bring us on the same server again. Note that t->srv is set in
889 * this case.
890 */
891 if ((t->flags & SN_DIRECT) && (t->be->options & PR_O_REDISP)) {
892 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
893 t->prev_srv = t->srv;
894 goto redispatch;
895 }
896
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200897 if (!t->req->cons->err_type) {
898 t->req->cons->err_type = SI_ET_QUEUE_ERR;
899 t->req->cons->err_loc = t->srv;
900 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200901
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200902 t->srv->counters.failed_conns++;
903 t->be->counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200904 return 1;
905
Willy Tarreaubaaee002006-06-26 02:48:02 +0200906 case SRV_STATUS_NOSRV:
907 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200908 if (!t->req->cons->err_type) {
909 t->req->cons->err_type = SI_ET_CONN_ERR;
910 t->req->cons->err_loc = NULL;
911 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100912
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200913 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200914 return 1;
915
916 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +0200917 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200918 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200919 /* do nothing else and do not wake any other session up */
920 return 1;
921
Willy Tarreaubaaee002006-06-26 02:48:02 +0200922 case SRV_STATUS_INTERNAL:
923 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200924 if (!t->req->cons->err_type) {
925 t->req->cons->err_type = SI_ET_CONN_OTHER;
926 t->req->cons->err_loc = t->srv;
927 }
928
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929 if (t->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100930 srv_inc_sess_ctr(t->srv);
Willy Tarreau98937b82007-12-10 15:05:42 +0100931 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200932 t->srv->counters.failed_conns++;
933 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934
935 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200936 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200937 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938 return 1;
939 }
940 /* if we get here, it's because we got SRV_STATUS_OK, which also
941 * means that the connection has not been queued.
942 */
943 return 0;
944}
945
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200946int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100947 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200948 return px->down_time;
949
950 return now.tv_sec - px->last_change + px->down_time;
951}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200952
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +0100953/*
954 * This function returns a string containing the balancing
955 * mode of the proxy in a format suitable for stats.
956 */
957
958const char *backend_lb_algo_str(int algo) {
959
960 if (algo == BE_LB_ALGO_RR)
961 return "roundrobin";
962 else if (algo == BE_LB_ALGO_SRR)
963 return "static-rr";
964 else if (algo == BE_LB_ALGO_LC)
965 return "leastconn";
966 else if (algo == BE_LB_ALGO_SH)
967 return "source";
968 else if (algo == BE_LB_ALGO_UH)
969 return "uri";
970 else if (algo == BE_LB_ALGO_PH)
971 return "url_param";
972 else if (algo == BE_LB_ALGO_HH)
973 return "hdr";
974 else if (algo == BE_LB_ALGO_RCH)
975 return "rdp-cookie";
976 else
977 return NULL;
978}
979
Willy Tarreaua0cbda62007-11-01 21:39:54 +0100980/* This function parses a "balance" statement in a backend section describing
981 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
982 * returns -1, it may write an error message into ther <err> buffer, for at
983 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
984 * written. The function must be called with <args> pointing to the first word
985 * after "balance".
986 */
987int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
988{
989 if (!*(args[0])) {
990 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +0100991 curproxy->lbprm.algo &= ~BE_LB_ALGO;
992 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +0100993 return 0;
994 }
995
996 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +0100997 curproxy->lbprm.algo &= ~BE_LB_ALGO;
998 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +0100999 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001000 else if (!strcmp(args[0], "static-rr")) {
1001 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1002 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1003 }
Willy Tarreau51406232008-03-10 22:04:20 +01001004 else if (!strcmp(args[0], "leastconn")) {
1005 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1006 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1007 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001008 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001009 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1010 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001011 }
1012 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001013 int arg = 1;
1014
Willy Tarreau31682232007-11-29 15:38:04 +01001015 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1016 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001017
1018 while (*args[arg]) {
1019 if (!strcmp(args[arg], "len")) {
1020 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1021 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1022 return -1;
1023 }
1024 curproxy->uri_len_limit = atoi(args[arg+1]);
1025 arg += 2;
1026 }
1027 else if (!strcmp(args[arg], "depth")) {
1028 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1029 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1030 return -1;
1031 }
1032 /* hint: we store the position of the ending '/' (depth+1) so
1033 * that we avoid a comparison while computing the hash.
1034 */
1035 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1036 arg += 2;
1037 }
1038 else {
1039 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1040 return -1;
1041 }
1042 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001043 }
Willy Tarreau01732802007-11-01 22:48:15 +01001044 else if (!strcmp(args[0], "url_param")) {
1045 if (!*args[1]) {
1046 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1047 return -1;
1048 }
Willy Tarreau31682232007-11-29 15:38:04 +01001049 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1050 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001051
1052 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001053 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001054 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001055 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001056 if (strcmp(args[2], "check_post")) {
1057 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1058 return -1;
1059 }
1060 if (*args[3]) {
1061 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1062 curproxy->url_param_post_limit = str2ui(args[3]);
1063 }
1064 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1065 if (!curproxy->url_param_post_limit)
1066 curproxy->url_param_post_limit = 48;
1067 else if ( curproxy->url_param_post_limit < 3 )
1068 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1069 }
Benoitaffb4812009-03-25 13:02:10 +01001070 }
1071 else if (!strncmp(args[0], "hdr(", 4)) {
1072 const char *beg, *end;
1073
1074 beg = args[0] + 4;
1075 end = strchr(beg, ')');
1076
1077 if (!end || end == beg) {
1078 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1079 return -1;
1080 }
1081
1082 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1083 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1084
1085 free(curproxy->hh_name);
1086 curproxy->hh_len = end - beg;
1087 curproxy->hh_name = my_strndup(beg, end - beg);
1088 curproxy->hh_match_domain = 0;
1089
1090 if (*args[1]) {
1091 if (strcmp(args[1], "use_domain_only")) {
1092 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1093 return -1;
1094 }
1095 curproxy->hh_match_domain = 1;
1096 }
1097
Emeric Brun736aa232009-06-30 17:56:00 +02001098 }
1099 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1100 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1101 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1102
1103 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1104 const char *beg, *end;
1105
1106 beg = args[0] + 11;
1107 end = strchr(beg, ')');
1108
1109 if (!end || end == beg) {
1110 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1111 return -1;
1112 }
1113
1114 free(curproxy->hh_name);
1115 curproxy->hh_name = my_strndup(beg, end - beg);
1116 curproxy->hh_len = end - beg;
1117 }
1118 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1119 free(curproxy->hh_name);
1120 curproxy->hh_name = strdup("mstshash");
1121 curproxy->hh_len = strlen(curproxy->hh_name);
1122 }
1123 else { /* syntax */
1124 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1125 return -1;
1126 }
Willy Tarreau01732802007-11-01 22:48:15 +01001127 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001128 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001129 snprintf(err, errlen, "'balance' only supports 'roundrobin', 'static-rr', 'leastconn', 'source', 'uri', 'url_param', 'hdr(name)' and 'rdp-cookie(name)' options.");
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001130 return -1;
1131 }
1132 return 0;
1133}
1134
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001135
1136/************************************************************************/
1137/* All supported keywords must be declared here. */
1138/************************************************************************/
1139
1140/* set test->i to the number of enabled servers on the proxy */
1141static int
1142acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1143 struct acl_expr *expr, struct acl_test *test)
1144{
1145 test->flags = ACL_TEST_F_VOL_TEST;
1146 if (expr->arg_len) {
1147 /* another proxy was designated, we must look for it */
1148 for (px = proxy; px; px = px->next)
1149 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1150 break;
1151 }
1152 if (!px)
1153 return 0;
1154
1155 if (px->srv_act)
1156 test->i = px->srv_act;
1157 else if (px->lbprm.fbck)
1158 test->i = 1;
1159 else
1160 test->i = px->srv_bck;
1161
1162 return 1;
1163}
1164
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001165/* set test->i to the number of enabled servers on the proxy */
1166static int
1167acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1168 struct acl_expr *expr, struct acl_test *test)
1169{
1170 struct server *iterator;
1171 test->flags = ACL_TEST_F_VOL_TEST;
1172 if (expr->arg_len) {
1173 /* another proxy was designated, we must look for it */
1174 for (px = proxy; px; px = px->next)
1175 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1176 break;
1177 }
1178 if (!px)
1179 return 0;
1180
1181 test->i = 0;
1182 iterator = px->srv;
1183 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001184 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001185 iterator = iterator->next;
1186 continue;
1187 }
1188 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
1189 test->i = -1;
1190 return 1;
1191 }
1192
1193 test->i += (iterator->maxconn - iterator->cur_sess)
1194 + (iterator->maxqueue - iterator->nbpend);
1195 iterator = iterator->next;
1196 }
1197
1198 return 1;
1199}
1200
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001201/* set test->i to the number of connections per second reaching the frontend */
1202static int
1203acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1204 struct acl_expr *expr, struct acl_test *test)
1205{
1206 test->flags = ACL_TEST_F_VOL_TEST;
1207 if (expr->arg_len) {
1208 /* another proxy was designated, we must look for it */
1209 for (px = proxy; px; px = px->next)
1210 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
1211 break;
1212 }
1213 if (!px)
1214 return 0;
1215
1216 test->i = read_freq_ctr(&px->fe_sess_per_sec);
1217 return 1;
1218}
1219
1220/* set test->i to the number of connections per second reaching the backend */
1221static int
1222acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1223 struct acl_expr *expr, struct acl_test *test)
1224{
1225 test->flags = ACL_TEST_F_VOL_TEST;
1226 if (expr->arg_len) {
1227 /* another proxy was designated, we must look for it */
1228 for (px = proxy; px; px = px->next)
1229 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1230 break;
1231 }
1232 if (!px)
1233 return 0;
1234
1235 test->i = read_freq_ctr(&px->be_sess_per_sec);
1236 return 1;
1237}
1238
Willy Tarreaua36af912009-10-10 12:02:45 +02001239/* set test->i to the number of concurrent connections on the frontend */
1240static int
1241acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1242 struct acl_expr *expr, struct acl_test *test)
1243{
1244 test->flags = ACL_TEST_F_VOL_TEST;
1245 if (expr->arg_len) {
1246 /* another proxy was designated, we must look for it */
1247 for (px = proxy; px; px = px->next)
1248 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
1249 break;
1250 }
1251 if (!px)
1252 return 0;
1253
1254 test->i = px->feconn;
1255 return 1;
1256}
1257
1258/* set test->i to the number of concurrent connections on the backend */
1259static int
1260acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1261 struct acl_expr *expr, struct acl_test *test)
1262{
1263 test->flags = ACL_TEST_F_VOL_TEST;
1264 if (expr->arg_len) {
1265 /* another proxy was designated, we must look for it */
1266 for (px = proxy; px; px = px->next)
1267 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1268 break;
1269 }
1270 if (!px)
1271 return 0;
1272
1273 test->i = px->beconn;
1274 return 1;
1275}
1276
1277/* set test->i to the total number of queued connections on the backend */
1278static int
1279acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1280 struct acl_expr *expr, struct acl_test *test)
1281{
1282 test->flags = ACL_TEST_F_VOL_TEST;
1283 if (expr->arg_len) {
1284 /* another proxy was designated, we must look for it */
1285 for (px = proxy; px; px = px->next)
1286 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1287 break;
1288 }
1289 if (!px)
1290 return 0;
1291
1292 test->i = px->totpend;
1293 return 1;
1294}
1295
1296/* set test->i to the total number of queued connections on the backend divided
1297 * by the number of running servers and rounded up. If there is no running
1298 * server, we return twice the total, just as if we had half a running server.
1299 * This is more or less correct anyway, since we expect the last server to come
1300 * back soon.
1301 */
1302static int
1303acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1304 struct acl_expr *expr, struct acl_test *test)
1305{
1306 int nbsrv;
1307
1308 test->flags = ACL_TEST_F_VOL_TEST;
1309 if (expr->arg_len) {
1310 /* another proxy was designated, we must look for it */
1311 for (px = proxy; px; px = px->next)
1312 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1313 break;
1314 }
1315 if (!px)
1316 return 0;
1317
1318 if (px->srv_act)
1319 nbsrv = px->srv_act;
1320 else if (px->lbprm.fbck)
1321 nbsrv = 1;
1322 else
1323 nbsrv = px->srv_bck;
1324
1325 if (nbsrv > 0)
1326 test->i = (px->totpend + nbsrv - 1) / nbsrv;
1327 else
1328 test->i = px->totpend * 2;
1329
1330 return 1;
1331}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001332
1333/* Note: must not be declared <const> as its list will be overwritten */
1334static struct acl_kw_list acl_kws = {{ },{
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001335 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau3a8efeb2009-03-05 19:15:37 +01001336 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001337 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
1338 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua36af912009-10-10 12:02:45 +02001339 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
1340 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1341 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1342 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001343 { NULL, NULL, NULL, NULL },
1344}};
1345
1346
1347__attribute__((constructor))
1348static void __backend_init(void)
1349{
1350 acl_register_keywords(&acl_kws);
1351}
1352
Willy Tarreaubaaee002006-06-26 02:48:02 +02001353/*
1354 * Local variables:
1355 * c-indent-level: 8
1356 * c-basic-offset: 8
1357 * End:
1358 */