blob: 70201ce2c95710ab2f5e720dfd57a69c455639a4 [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 Tarreau192ee3e2008-04-19 21:24:56 +0200247 unsigned long body;
248 unsigned long len;
249 const char *params;
250 struct hdr_ctx ctx;
251 const char *p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200252
253 /* tot_weight appears to mean srv_count */
254 if (px->lbprm.tot_weight == 0)
255 return NULL;
256
Willy Tarreau192ee3e2008-04-19 21:24:56 +0200257 body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
Willy Tarreaufb0528b2008-08-11 00:21:56 +0200258 len = req->l - body;
Willy Tarreau192ee3e2008-04-19 21:24:56 +0200259 params = req->data + body;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200260
261 if ( len == 0 )
262 return NULL;
263
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200264 ctx.idx = 0;
265
266 /* if the message is chunked, we skip the chunk size, but use the value as len */
267 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +0200268 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200269 unsigned int chunk = 0;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100270 while ( params < (req->data+req->max_len) && !HTTP_IS_CRLF(*params)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200271 char c = *params;
272 if (ishex(c)) {
273 unsigned int hex = toupper(c) - '0';
274 if ( hex > 9 )
275 hex -= 'A' - '9' - 1;
276 chunk = (chunk << 4) | hex;
277 }
278 else
279 return NULL;
280 params++;
281 len--;
Willy Tarreau01732802007-11-01 22:48:15 +0100282 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200283 /* spec says we get CRLF */
284 if (HTTP_IS_CRLF(*params) && HTTP_IS_CRLF(params[1]))
285 params += 2;
286 else
287 return NULL;
288 /* ok we have some encoded length, just inspect the first chunk */
289 len = chunk;
290 }
Willy Tarreau01732802007-11-01 22:48:15 +0100291
Willy Tarreaudc8017c2009-12-06 18:16:59 +0100292 if (len > req->l - body)
293 len = req->l - body;
294
Willy Tarreau192ee3e2008-04-19 21:24:56 +0200295 p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200296
297 while (len > plen) {
298 /* Look for the parameter name followed by an equal symbol */
299 if (params[plen] == '=') {
300 if (memcmp(params, px->url_param_name, plen) == 0) {
301 /* OK, we have the parameter here at <params>, and
302 * the value after the equal sign, at <p>
303 * skip the equal symbol
304 */
305 p += plen + 1;
306 len -= plen + 1;
307
308 while (len && *p != '&') {
309 if (unlikely(!HTTP_IS_TOKEN(*p))) {
310 /* if in a POST, body must be URI encoded or its not a URI.
311 * Do not interprete any possible binary data as a parameter.
312 */
313 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
314 break;
315 return NULL; /* oh, no; this is not uri-encoded.
316 * This body does not contain parameters.
317 */
318 }
319 hash = *p + (hash << 6) + (hash << 16) - hash;
320 len--;
321 p++;
322 /* should we break if vlen exceeds limit? */
323 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200324 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
325 return chash_get_server_hash(px, hash);
326 else
327 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200328 }
329 }
Willy Tarreau01732802007-11-01 22:48:15 +0100330 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200331 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100332 if (!p)
333 return NULL;
334 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200335 len -= (p - params);
336 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100337 }
338 return NULL;
339}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200340
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200341
Willy Tarreaubaaee002006-06-26 02:48:02 +0200342/*
Benoitaffb4812009-03-25 13:02:10 +0100343 * This function tries to find a running server for the proxy <px> following
344 * the Header parameter hash method. It looks for a specific parameter in the
345 * URL and hashes it to compute the server ID. This is useful to optimize
346 * performance by avoiding bounces between servers in contexts where sessions
347 * are shared but cookies are not usable. If the parameter is not found, NULL
348 * is returned. If any server is found, it will be returned. If no valid server
349 * is found, NULL is returned.
350 */
351struct server *get_server_hh(struct session *s)
352{
353 unsigned long hash = 0;
354 struct http_txn *txn = &s->txn;
355 struct http_msg *msg = &txn->req;
356 struct proxy *px = s->be;
357 unsigned int plen = px->hh_len;
358 unsigned long len;
359 struct hdr_ctx ctx;
360 const char *p;
361
362 /* tot_weight appears to mean srv_count */
363 if (px->lbprm.tot_weight == 0)
364 return NULL;
365
Benoitaffb4812009-03-25 13:02:10 +0100366 ctx.idx = 0;
367
368 /* if the message is chunked, we skip the chunk size, but use the value as len */
369 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
370
371 /* if the header is not found or empty, let's fallback to round robin */
372 if (!ctx.idx || !ctx.vlen)
373 return NULL;
374
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200375 /* note: we won't hash if there's only one server left */
376 if (px->lbprm.tot_used == 1)
377 goto hash_done;
378
Benoitaffb4812009-03-25 13:02:10 +0100379 /* Found a the hh_name in the headers.
380 * we will compute the hash based on this value ctx.val.
381 */
382 len = ctx.vlen;
383 p = (char *)ctx.line + ctx.val;
384 if (!px->hh_match_domain) {
385 while (len) {
386 hash = *p + (hash << 6) + (hash << 16) - hash;
387 len--;
388 p++;
389 }
390 } else {
391 int dohash = 0;
392 p += len - 1;
393 /* special computation, use only main domain name, not tld/host
394 * going back from the end of string, start hashing at first
395 * dot stop at next.
396 * This is designed to work with the 'Host' header, and requires
397 * a special option to activate this.
398 */
399 while (len) {
400 if (*p == '.') {
401 if (!dohash)
402 dohash = 1;
403 else
404 break;
405 } else {
406 if (dohash)
407 hash = *p + (hash << 6) + (hash << 16) - hash;
408 }
409 len--;
410 p--;
411 }
412 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200413 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200414 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
415 return chash_get_server_hash(px, hash);
416 else
417 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100418}
419
Emeric Brun736aa232009-06-30 17:56:00 +0200420struct server *get_server_rch(struct session *s)
421{
422 unsigned long hash = 0;
423 struct proxy *px = s->be;
424 unsigned long len;
425 const char *p;
426 int ret;
427 struct acl_expr expr;
428 struct acl_test test;
429
430 /* tot_weight appears to mean srv_count */
431 if (px->lbprm.tot_weight == 0)
432 return NULL;
433
Emeric Brun736aa232009-06-30 17:56:00 +0200434 memset(&expr, 0, sizeof(expr));
435 memset(&test, 0, sizeof(test));
436
437 expr.arg.str = px->hh_name;
438 expr.arg_len = px->hh_len;
439
440 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
441 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
442 return NULL;
443
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200444 /* note: we won't hash if there's only one server left */
445 if (px->lbprm.tot_used == 1)
446 goto hash_done;
447
Emeric Brun736aa232009-06-30 17:56:00 +0200448 /* Found a the hh_name in the headers.
449 * we will compute the hash based on this value ctx.val.
450 */
451 len = test.len;
452 p = (char *)test.ptr;
453 while (len) {
454 hash = *p + (hash << 6) + (hash << 16) - hash;
455 len--;
456 p++;
457 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200458 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200459 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
460 return chash_get_server_hash(px, hash);
461 else
462 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200463}
Benoitaffb4812009-03-25 13:02:10 +0100464
465/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200466 * This function applies the load-balancing algorithm to the session, as
467 * defined by the backend it is assigned to. The session is then marked as
468 * 'assigned'.
469 *
470 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
471 * had a server previously assigned, it is rebalanced, trying to avoid the same
472 * server.
473 * The function tries to keep the original connection slot if it reconnects to
474 * the same server, otherwise it releases it and tries to offer it.
475 *
476 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200477 *
478 * It may return :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200479 * SRV_STATUS_OK if everything is OK. Session assigned to ->srv
480 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
481 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200482 * SRV_STATUS_INTERNAL for other unrecoverable errors.
483 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200484 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
485 * it does not need to be called anymore. This means that s->srv can be trusted
486 * in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200487 *
488 */
489
490int assign_server(struct session *s)
491{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100492
Willy Tarreau7c669d72008-06-20 15:04:11 +0200493 struct server *conn_slot;
494 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100495
Willy Tarreaubaaee002006-06-26 02:48:02 +0200496#ifdef DEBUG_FULL
497 fprintf(stderr,"assign_server : s=%p\n",s);
498#endif
499
Willy Tarreau7c669d72008-06-20 15:04:11 +0200500 err = SRV_STATUS_INTERNAL;
501 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
502 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100503
Willy Tarreau7c669d72008-06-20 15:04:11 +0200504 s->prev_srv = s->prev_srv;
505 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200506
Willy Tarreau7c669d72008-06-20 15:04:11 +0200507 /* We have to release any connection slot before applying any LB algo,
508 * otherwise we may erroneously end up with no available slot.
509 */
510 if (conn_slot)
511 sess_change_server(s, NULL);
512
513 /* We will now try to find the good server and store it into <s->srv>.
514 * Note that <s->srv> may be NULL in case of dispatch or proxy mode,
515 * as well as if no server is available (check error code).
516 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100517
Willy Tarreau7c669d72008-06-20 15:04:11 +0200518 s->srv = NULL;
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200519 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200520 int len;
521 /* we must check if we have at least one server available */
522 if (!s->be->lbprm.tot_weight) {
523 err = SRV_STATUS_NOSRV;
524 goto out;
525 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200527 /* First check whether we need to fetch some data or simply call
528 * the LB lookup function. Only the hashing functions will need
529 * some input data in fact, and will support multiple algorithms.
530 */
531 switch (s->be->lbprm.algo & BE_LB_LKUP) {
532 case BE_LB_LKUP_RRTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200533 s->srv = fwrr_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200534 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200535
536 case BE_LB_LKUP_LCTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200537 s->srv = fwlc_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200538 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200539
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200540 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200541 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200542 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200543 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
544 s->srv = chash_get_next_server(s->be, s->prev_srv);
545 else
546 s->srv = map_get_server_rr(s->be, s->prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200547 break;
548 }
549 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200550 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200551 err = SRV_STATUS_INTERNAL;
552 goto out;
553 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200554
555 switch (s->be->lbprm.algo & BE_LB_PARM) {
556 case BE_LB_HASH_SRC:
557 if (s->cli_addr.ss_family == AF_INET)
558 len = 4;
559 else if (s->cli_addr.ss_family == AF_INET6)
560 len = 16;
561 else {
562 /* unknown IP family */
563 err = SRV_STATUS_INTERNAL;
564 goto out;
565 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200567 s->srv = get_server_sh(s->be,
568 (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
569 len);
570 break;
571
572 case BE_LB_HASH_URI:
573 /* URI hashing */
574 s->srv = get_server_uh(s->be,
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200575 s->txn.req.sol + s->txn.req.sl.rq.u,
576 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200577 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200578
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200579 case BE_LB_HASH_PRM:
580 /* URL Parameter hashing */
581 if (s->txn.meth == HTTP_METH_POST &&
582 memchr(s->txn.req.sol + s->txn.req.sl.rq.u, '&',
583 s->txn.req.sl.rq.u_l ) == NULL)
584 s->srv = get_server_ph_post(s);
585 else
586 s->srv = get_server_ph(s->be,
587 s->txn.req.sol + s->txn.req.sl.rq.u,
588 s->txn.req.sl.rq.u_l);
589 break;
Benoitaffb4812009-03-25 13:02:10 +0100590
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200591 case BE_LB_HASH_HDR:
592 /* Header Parameter hashing */
593 s->srv = get_server_hh(s);
594 break;
595
596 case BE_LB_HASH_RDP:
597 /* RDP Cookie hashing */
598 s->srv = get_server_rch(s);
599 break;
600
601 default:
602 /* unknown balancing algorithm */
603 err = SRV_STATUS_INTERNAL;
604 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100605 }
Emeric Brun736aa232009-06-30 17:56:00 +0200606
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200607 /* If the hashing parameter was not found, let's fall
608 * back to round robin on the map.
609 */
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200610 if (!s->srv) {
611 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
612 s->srv = chash_get_next_server(s->be, s->prev_srv);
613 else
614 s->srv = map_get_server_rr(s->be, s->prev_srv);
615 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200616
617 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200618 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200619
Willy Tarreau7c669d72008-06-20 15:04:11 +0200620 default:
621 /* unknown balancing algorithm */
622 err = SRV_STATUS_INTERNAL;
623 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200625
626 if (!s->srv) {
627 err = SRV_STATUS_FULL;
628 goto out;
629 }
630 else if (s->srv != s->prev_srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200631 s->be->counters.cum_lbconn++;
632 s->srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100633 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200634 }
635 else if (s->be->options & PR_O_HTTP_PROXY) {
636 if (!s->srv_addr.sin_addr.s_addr) {
637 err = SRV_STATUS_NOSRV;
638 goto out;
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100639 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200640 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200641 else if (!*(int *)&s->be->dispatch_addr.sin_addr &&
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100642 !(s->be->options & PR_O_TRANSP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200643 err = SRV_STATUS_NOSRV;
644 goto out;
645 }
646
647 s->flags |= SN_ASSIGNED;
648 err = SRV_STATUS_OK;
649 out:
650
651 /* Either we take back our connection slot, or we offer it to someone
652 * else if we don't need it anymore.
653 */
654 if (conn_slot) {
655 if (conn_slot == s->srv) {
656 sess_change_server(s, s->srv);
657 } else {
658 if (may_dequeue_tasks(conn_slot, s->be))
659 process_srv_queue(conn_slot);
660 }
661 }
662
663 out_err:
664 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665}
666
667
668/*
669 * This function assigns a server address to a session, and sets SN_ADDR_SET.
670 * The address is taken from the currently assigned server, or from the
671 * dispatch or transparent address.
672 *
673 * It may return :
674 * SRV_STATUS_OK if everything is OK.
675 * SRV_STATUS_INTERNAL for other unrecoverable errors.
676 *
677 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
678 * not cleared, so it's to the caller to clear it if required.
679 *
680 */
681int assign_server_address(struct session *s)
682{
683#ifdef DEBUG_FULL
684 fprintf(stderr,"assign_server_address : s=%p\n",s);
685#endif
686
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200687 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200688 /* A server is necessarily known for this session */
689 if (!(s->flags & SN_ASSIGNED))
690 return SRV_STATUS_INTERNAL;
691
692 s->srv_addr = s->srv->addr;
693
694 /* if this server remaps proxied ports, we'll use
695 * the port the client connected to with an offset. */
696 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100697 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200698 get_frt_addr(s);
699 if (s->frt_addr.ss_family == AF_INET) {
700 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
701 ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port));
702 } else {
703 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
704 ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port));
705 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706 }
707 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200708 else if (*(int *)&s->be->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200709 /* connect to the defined dispatch addr */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200710 s->srv_addr = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200711 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100712 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200713 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100714 if (!(s->flags & SN_FRT_ADDR_SET))
715 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200716
Willy Tarreaubd414282008-01-19 13:46:35 +0100717 memcpy(&s->srv_addr, &s->frt_addr, MIN(sizeof(s->srv_addr), sizeof(s->frt_addr)));
718 /* when we support IPv6 on the backend, we may add other tests */
719 //qfprintf(stderr, "Cannot get original server address.\n");
720 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200721 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100722 else if (s->be->options & PR_O_HTTP_PROXY) {
723 /* If HTTP PROXY option is set, then server is already assigned
724 * during incoming client request parsing. */
725 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100726 else {
727 /* no server and no LB algorithm ! */
728 return SRV_STATUS_INTERNAL;
729 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200730
731 s->flags |= SN_ADDR_SET;
732 return SRV_STATUS_OK;
733}
734
735
736/* This function assigns a server to session <s> if required, and can add the
737 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200738 * If ->srv_conn is set, the session is first released from the server.
739 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
740 * be called before any connection and after any retry or redispatch occurs.
741 *
742 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743 *
744 * Returns :
745 *
746 * SRV_STATUS_OK if everything is OK.
747 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
748 * SRV_STATUS_QUEUED if the connection has been queued.
749 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200750 * connection could not be queued in s->srv,
751 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200752 * SRV_STATUS_INTERNAL for other unrecoverable errors.
753 *
754 */
755int assign_server_and_queue(struct session *s)
756{
757 struct pendconn *p;
758 int err;
759
760 if (s->pend_pos)
761 return SRV_STATUS_INTERNAL;
762
Willy Tarreau7c669d72008-06-20 15:04:11 +0200763 err = SRV_STATUS_OK;
764 if (!(s->flags & SN_ASSIGNED)) {
765 err = assign_server(s);
766 if (s->prev_srv) {
767 /* This session was previously assigned to a server. We have to
768 * update the session's and the server's stats :
769 * - if the server changed :
770 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
771 * - set SN_REDISP if it was successfully redispatched
772 * - increment srv->redispatches and be->redispatches
773 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100774 */
775
Willy Tarreau7c669d72008-06-20 15:04:11 +0200776 if (s->prev_srv != s->srv) {
777 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
778 s->txn.flags &= ~TX_CK_MASK;
779 s->txn.flags |= TX_CK_DOWN;
780 }
781 s->flags |= SN_REDISP;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200782 s->prev_srv->counters.redispatches++;
783 s->be->counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200784 } else {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200785 s->prev_srv->counters.retries++;
786 s->be->counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100787 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100788 }
789 }
790
Willy Tarreaubaaee002006-06-26 02:48:02 +0200791 switch (err) {
792 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200793 /* we have SN_ASSIGNED set */
794 if (!s->srv)
795 return SRV_STATUS_OK; /* dispatch or proxy mode */
796
797 /* If we already have a connection slot, no need to check any queue */
798 if (s->srv_conn == s->srv)
799 return SRV_STATUS_OK;
800
801 /* OK, this session already has an assigned server, but no
802 * connection slot yet. Either it is a redispatch, or it was
803 * assigned from persistence information (direct mode).
804 */
805 if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) {
806 /* server scheduled for redirection, and already assigned. We
807 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100808 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200809 sess_change_server(s, s->srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100810 return SRV_STATUS_OK;
811 }
812
Willy Tarreau7c669d72008-06-20 15:04:11 +0200813 /* We might have to queue this session if the assigned server is full.
814 * We know we have to queue it into the server's queue, so if a maxqueue
815 * is set on the server, we must also check that the server's queue is
816 * not full, in which case we have to return FULL.
817 */
818 if (s->srv->maxconn &&
819 (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) {
820
821 if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue)
822 return SRV_STATUS_FULL;
823
Willy Tarreaubaaee002006-06-26 02:48:02 +0200824 p = pendconn_add(s);
825 if (p)
826 return SRV_STATUS_QUEUED;
827 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200828 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200830
831 /* OK, we can use this server. Let's reserve our place */
832 sess_change_server(s, s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200833 return SRV_STATUS_OK;
834
835 case SRV_STATUS_FULL:
836 /* queue this session into the proxy's queue */
837 p = pendconn_add(s);
838 if (p)
839 return SRV_STATUS_QUEUED;
840 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200841 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200842
843 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200844 return err;
845
Willy Tarreaubaaee002006-06-26 02:48:02 +0200846 case SRV_STATUS_INTERNAL:
847 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200848
Willy Tarreaubaaee002006-06-26 02:48:02 +0200849 default:
850 return SRV_STATUS_INTERNAL;
851 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100852}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200853
854/*
855 * This function initiates a connection to the server assigned to this session
856 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
857 * It can return one of :
858 * - SN_ERR_NONE if everything's OK
859 * - SN_ERR_SRVTO if there are no more servers
860 * - SN_ERR_SRVCL if the connection was refused by the server
861 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
862 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
863 * - SN_ERR_INTERNAL for any other purely internal errors
864 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
865 */
866int connect_server(struct session *s)
867{
Willy Tarreau9650f372009-08-16 14:02:45 +0200868 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200869
870 if (!(s->flags & SN_ADDR_SET)) {
871 err = assign_server_address(s);
872 if (err != SRV_STATUS_OK)
873 return SN_ERR_INTERNAL;
874 }
875
Willy Tarreau9650f372009-08-16 14:02:45 +0200876 if (!s->req->cons->connect)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200877 return SN_ERR_INTERNAL;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200878
Willy Tarreau9650f372009-08-16 14:02:45 +0200879 err = s->req->cons->connect(s->req->cons, s->be, s->srv,
880 (struct sockaddr *)&s->srv_addr,
881 (struct sockaddr *)&s->cli_addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200882
Willy Tarreau9650f372009-08-16 14:02:45 +0200883 if (err != SN_ERR_NONE)
884 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200885
Willy Tarreaubaaee002006-06-26 02:48:02 +0200886 if (s->srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100887 s->flags |= SN_CURR_SESS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200888 s->srv->cur_sess++;
Willy Tarreauac68c5d2009-10-04 23:12:44 +0200889 if (s->srv->cur_sess > s->srv->counters.cur_sess_max)
890 s->srv->counters.cur_sess_max = s->srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100891 if (s->be->lbprm.server_take_conn)
892 s->be->lbprm.server_take_conn(s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200893 }
894
Willy Tarreaubaaee002006-06-26 02:48:02 +0200895 return SN_ERR_NONE; /* connection is OK */
896}
897
898
Willy Tarreaubaaee002006-06-26 02:48:02 +0200899/* This function performs the "redispatch" part of a connection attempt. It
900 * will assign a server if required, queue the connection if required, and
901 * handle errors that might arise at this level. It can change the server
902 * state. It will return 1 if it encounters an error, switches the server
903 * state, or has to queue a connection. Otherwise, it will return 0 indicating
904 * that the connection is ready to use.
905 */
906
907int srv_redispatch_connect(struct session *t)
908{
909 int conn_err;
910
911 /* We know that we don't have any connection pending, so we will
912 * try to get a new one, and wait in this state if it's queued
913 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200914 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200915 conn_err = assign_server_and_queue(t);
916 switch (conn_err) {
917 case SRV_STATUS_OK:
918 break;
919
Willy Tarreau7c669d72008-06-20 15:04:11 +0200920 case SRV_STATUS_FULL:
921 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
922 * and we can redispatch to another server, or it is not and we return
923 * 503. This only makes sense in DIRECT mode however, because normal LB
924 * algorithms would never select such a server, and hash algorithms
925 * would bring us on the same server again. Note that t->srv is set in
926 * this case.
927 */
928 if ((t->flags & SN_DIRECT) && (t->be->options & PR_O_REDISP)) {
929 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
930 t->prev_srv = t->srv;
931 goto redispatch;
932 }
933
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200934 if (!t->req->cons->err_type) {
935 t->req->cons->err_type = SI_ET_QUEUE_ERR;
936 t->req->cons->err_loc = t->srv;
937 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200938
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200939 t->srv->counters.failed_conns++;
940 t->be->counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200941 return 1;
942
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943 case SRV_STATUS_NOSRV:
944 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200945 if (!t->req->cons->err_type) {
946 t->req->cons->err_type = SI_ET_CONN_ERR;
947 t->req->cons->err_loc = NULL;
948 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100949
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200950 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951 return 1;
952
953 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +0200954 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200955 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200956 /* do nothing else and do not wake any other session up */
957 return 1;
958
Willy Tarreaubaaee002006-06-26 02:48:02 +0200959 case SRV_STATUS_INTERNAL:
960 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200961 if (!t->req->cons->err_type) {
962 t->req->cons->err_type = SI_ET_CONN_OTHER;
963 t->req->cons->err_loc = t->srv;
964 }
965
Willy Tarreaubaaee002006-06-26 02:48:02 +0200966 if (t->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100967 srv_inc_sess_ctr(t->srv);
Willy Tarreau98937b82007-12-10 15:05:42 +0100968 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200969 t->srv->counters.failed_conns++;
970 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200971
972 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200973 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200974 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200975 return 1;
976 }
977 /* if we get here, it's because we got SRV_STATUS_OK, which also
978 * means that the connection has not been queued.
979 */
980 return 0;
981}
982
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200983int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100984 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200985 return px->down_time;
986
987 return now.tv_sec - px->last_change + px->down_time;
988}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200989
Willy Tarreaua0cbda62007-11-01 21:39:54 +0100990/* This function parses a "balance" statement in a backend section describing
991 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
992 * returns -1, it may write an error message into ther <err> buffer, for at
993 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
994 * written. The function must be called with <args> pointing to the first word
995 * after "balance".
996 */
997int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
998{
999 if (!*(args[0])) {
1000 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001001 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1002 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001003 return 0;
1004 }
1005
1006 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001007 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1008 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001009 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001010 else if (!strcmp(args[0], "static-rr")) {
1011 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1012 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1013 }
Willy Tarreau51406232008-03-10 22:04:20 +01001014 else if (!strcmp(args[0], "leastconn")) {
1015 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1016 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1017 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001018 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001019 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1020 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001021 }
1022 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001023 int arg = 1;
1024
Willy Tarreau31682232007-11-29 15:38:04 +01001025 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1026 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001027
1028 while (*args[arg]) {
1029 if (!strcmp(args[arg], "len")) {
1030 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1031 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1032 return -1;
1033 }
1034 curproxy->uri_len_limit = atoi(args[arg+1]);
1035 arg += 2;
1036 }
1037 else if (!strcmp(args[arg], "depth")) {
1038 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1039 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1040 return -1;
1041 }
1042 /* hint: we store the position of the ending '/' (depth+1) so
1043 * that we avoid a comparison while computing the hash.
1044 */
1045 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1046 arg += 2;
1047 }
1048 else {
1049 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1050 return -1;
1051 }
1052 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001053 }
Willy Tarreau01732802007-11-01 22:48:15 +01001054 else if (!strcmp(args[0], "url_param")) {
1055 if (!*args[1]) {
1056 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1057 return -1;
1058 }
Willy Tarreau31682232007-11-29 15:38:04 +01001059 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1060 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001061
1062 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001063 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001064 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001065 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001066 if (strcmp(args[2], "check_post")) {
1067 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1068 return -1;
1069 }
1070 if (*args[3]) {
1071 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1072 curproxy->url_param_post_limit = str2ui(args[3]);
1073 }
1074 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1075 if (!curproxy->url_param_post_limit)
1076 curproxy->url_param_post_limit = 48;
1077 else if ( curproxy->url_param_post_limit < 3 )
1078 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1079 }
Benoitaffb4812009-03-25 13:02:10 +01001080 }
1081 else if (!strncmp(args[0], "hdr(", 4)) {
1082 const char *beg, *end;
1083
1084 beg = args[0] + 4;
1085 end = strchr(beg, ')');
1086
1087 if (!end || end == beg) {
1088 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1089 return -1;
1090 }
1091
1092 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1093 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1094
1095 free(curproxy->hh_name);
1096 curproxy->hh_len = end - beg;
1097 curproxy->hh_name = my_strndup(beg, end - beg);
1098 curproxy->hh_match_domain = 0;
1099
1100 if (*args[1]) {
1101 if (strcmp(args[1], "use_domain_only")) {
1102 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1103 return -1;
1104 }
1105 curproxy->hh_match_domain = 1;
1106 }
1107
Emeric Brun736aa232009-06-30 17:56:00 +02001108 }
1109 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1110 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1111 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1112
1113 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1114 const char *beg, *end;
1115
1116 beg = args[0] + 11;
1117 end = strchr(beg, ')');
1118
1119 if (!end || end == beg) {
1120 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1121 return -1;
1122 }
1123
1124 free(curproxy->hh_name);
1125 curproxy->hh_name = my_strndup(beg, end - beg);
1126 curproxy->hh_len = end - beg;
1127 }
1128 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1129 free(curproxy->hh_name);
1130 curproxy->hh_name = strdup("mstshash");
1131 curproxy->hh_len = strlen(curproxy->hh_name);
1132 }
1133 else { /* syntax */
1134 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1135 return -1;
1136 }
Willy Tarreau01732802007-11-01 22:48:15 +01001137 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001138 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001139 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 +01001140 return -1;
1141 }
1142 return 0;
1143}
1144
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001145
1146/************************************************************************/
1147/* All supported keywords must be declared here. */
1148/************************************************************************/
1149
1150/* set test->i to the number of enabled servers on the proxy */
1151static int
1152acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1153 struct acl_expr *expr, struct acl_test *test)
1154{
1155 test->flags = ACL_TEST_F_VOL_TEST;
1156 if (expr->arg_len) {
1157 /* another proxy was designated, we must look for it */
1158 for (px = proxy; px; px = px->next)
1159 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1160 break;
1161 }
1162 if (!px)
1163 return 0;
1164
1165 if (px->srv_act)
1166 test->i = px->srv_act;
1167 else if (px->lbprm.fbck)
1168 test->i = 1;
1169 else
1170 test->i = px->srv_bck;
1171
1172 return 1;
1173}
1174
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001175/* set test->i to the number of enabled servers on the proxy */
1176static int
1177acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1178 struct acl_expr *expr, struct acl_test *test)
1179{
1180 struct server *iterator;
1181 test->flags = ACL_TEST_F_VOL_TEST;
1182 if (expr->arg_len) {
1183 /* another proxy was designated, we must look for it */
1184 for (px = proxy; px; px = px->next)
1185 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1186 break;
1187 }
1188 if (!px)
1189 return 0;
1190
1191 test->i = 0;
1192 iterator = px->srv;
1193 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001194 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001195 iterator = iterator->next;
1196 continue;
1197 }
1198 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
1199 test->i = -1;
1200 return 1;
1201 }
1202
1203 test->i += (iterator->maxconn - iterator->cur_sess)
1204 + (iterator->maxqueue - iterator->nbpend);
1205 iterator = iterator->next;
1206 }
1207
1208 return 1;
1209}
1210
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001211/* set test->i to the number of connections per second reaching the frontend */
1212static int
1213acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1214 struct acl_expr *expr, struct acl_test *test)
1215{
1216 test->flags = ACL_TEST_F_VOL_TEST;
1217 if (expr->arg_len) {
1218 /* another proxy was designated, we must look for it */
1219 for (px = proxy; px; px = px->next)
1220 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
1221 break;
1222 }
1223 if (!px)
1224 return 0;
1225
1226 test->i = read_freq_ctr(&px->fe_sess_per_sec);
1227 return 1;
1228}
1229
1230/* set test->i to the number of connections per second reaching the backend */
1231static int
1232acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1233 struct acl_expr *expr, struct acl_test *test)
1234{
1235 test->flags = ACL_TEST_F_VOL_TEST;
1236 if (expr->arg_len) {
1237 /* another proxy was designated, we must look for it */
1238 for (px = proxy; px; px = px->next)
1239 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1240 break;
1241 }
1242 if (!px)
1243 return 0;
1244
1245 test->i = read_freq_ctr(&px->be_sess_per_sec);
1246 return 1;
1247}
1248
Willy Tarreaua36af912009-10-10 12:02:45 +02001249/* set test->i to the number of concurrent connections on the frontend */
1250static int
1251acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1252 struct acl_expr *expr, struct acl_test *test)
1253{
1254 test->flags = ACL_TEST_F_VOL_TEST;
1255 if (expr->arg_len) {
1256 /* another proxy was designated, we must look for it */
1257 for (px = proxy; px; px = px->next)
1258 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
1259 break;
1260 }
1261 if (!px)
1262 return 0;
1263
1264 test->i = px->feconn;
1265 return 1;
1266}
1267
1268/* set test->i to the number of concurrent connections on the backend */
1269static int
1270acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1271 struct acl_expr *expr, struct acl_test *test)
1272{
1273 test->flags = ACL_TEST_F_VOL_TEST;
1274 if (expr->arg_len) {
1275 /* another proxy was designated, we must look for it */
1276 for (px = proxy; px; px = px->next)
1277 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1278 break;
1279 }
1280 if (!px)
1281 return 0;
1282
1283 test->i = px->beconn;
1284 return 1;
1285}
1286
1287/* set test->i to the total number of queued connections on the backend */
1288static int
1289acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1290 struct acl_expr *expr, struct acl_test *test)
1291{
1292 test->flags = ACL_TEST_F_VOL_TEST;
1293 if (expr->arg_len) {
1294 /* another proxy was designated, we must look for it */
1295 for (px = proxy; px; px = px->next)
1296 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1297 break;
1298 }
1299 if (!px)
1300 return 0;
1301
1302 test->i = px->totpend;
1303 return 1;
1304}
1305
1306/* set test->i to the total number of queued connections on the backend divided
1307 * by the number of running servers and rounded up. If there is no running
1308 * server, we return twice the total, just as if we had half a running server.
1309 * This is more or less correct anyway, since we expect the last server to come
1310 * back soon.
1311 */
1312static int
1313acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1314 struct acl_expr *expr, struct acl_test *test)
1315{
1316 int nbsrv;
1317
1318 test->flags = ACL_TEST_F_VOL_TEST;
1319 if (expr->arg_len) {
1320 /* another proxy was designated, we must look for it */
1321 for (px = proxy; px; px = px->next)
1322 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1323 break;
1324 }
1325 if (!px)
1326 return 0;
1327
1328 if (px->srv_act)
1329 nbsrv = px->srv_act;
1330 else if (px->lbprm.fbck)
1331 nbsrv = 1;
1332 else
1333 nbsrv = px->srv_bck;
1334
1335 if (nbsrv > 0)
1336 test->i = (px->totpend + nbsrv - 1) / nbsrv;
1337 else
1338 test->i = px->totpend * 2;
1339
1340 return 1;
1341}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001342
1343/* Note: must not be declared <const> as its list will be overwritten */
1344static struct acl_kw_list acl_kws = {{ },{
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001345 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau3a8efeb2009-03-05 19:15:37 +01001346 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001347 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
1348 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua36af912009-10-10 12:02:45 +02001349 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
1350 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1351 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1352 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001353 { NULL, NULL, NULL, NULL },
1354}};
1355
1356
1357__attribute__((constructor))
1358static void __backend_init(void)
1359{
1360 acl_register_keywords(&acl_kws);
1361}
1362
Willy Tarreaubaaee002006-06-26 02:48:02 +02001363/*
1364 * Local variables:
1365 * c-indent-level: 8
1366 * c-basic-offset: 8
1367 * End:
1368 */