blob: f4e6110f977c858b3a57ac77399aa9b7780b1d5f [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 Tarreau192ee3e2008-04-19 21:24:56 +0200292 p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200293
294 while (len > plen) {
295 /* Look for the parameter name followed by an equal symbol */
296 if (params[plen] == '=') {
297 if (memcmp(params, px->url_param_name, plen) == 0) {
298 /* OK, we have the parameter here at <params>, and
299 * the value after the equal sign, at <p>
300 * skip the equal symbol
301 */
302 p += plen + 1;
303 len -= plen + 1;
304
305 while (len && *p != '&') {
306 if (unlikely(!HTTP_IS_TOKEN(*p))) {
307 /* if in a POST, body must be URI encoded or its not a URI.
308 * Do not interprete any possible binary data as a parameter.
309 */
310 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
311 break;
312 return NULL; /* oh, no; this is not uri-encoded.
313 * This body does not contain parameters.
314 */
315 }
316 hash = *p + (hash << 6) + (hash << 16) - hash;
317 len--;
318 p++;
319 /* should we break if vlen exceeds limit? */
320 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200321 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
322 return chash_get_server_hash(px, hash);
323 else
324 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200325 }
326 }
Willy Tarreau01732802007-11-01 22:48:15 +0100327 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200328 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100329 if (!p)
330 return NULL;
331 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200332 len -= (p - params);
333 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100334 }
335 return NULL;
336}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200337
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200338
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339/*
Benoitaffb4812009-03-25 13:02:10 +0100340 * This function tries to find a running server for the proxy <px> following
341 * the Header parameter hash method. It looks for a specific parameter in the
342 * URL and hashes it to compute the server ID. This is useful to optimize
343 * performance by avoiding bounces between servers in contexts where sessions
344 * are shared but cookies are not usable. If the parameter is not found, NULL
345 * is returned. If any server is found, it will be returned. If no valid server
346 * is found, NULL is returned.
347 */
348struct server *get_server_hh(struct session *s)
349{
350 unsigned long hash = 0;
351 struct http_txn *txn = &s->txn;
352 struct http_msg *msg = &txn->req;
353 struct proxy *px = s->be;
354 unsigned int plen = px->hh_len;
355 unsigned long len;
356 struct hdr_ctx ctx;
357 const char *p;
358
359 /* tot_weight appears to mean srv_count */
360 if (px->lbprm.tot_weight == 0)
361 return NULL;
362
Benoitaffb4812009-03-25 13:02:10 +0100363 ctx.idx = 0;
364
365 /* if the message is chunked, we skip the chunk size, but use the value as len */
366 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
367
368 /* if the header is not found or empty, let's fallback to round robin */
369 if (!ctx.idx || !ctx.vlen)
370 return NULL;
371
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200372 /* note: we won't hash if there's only one server left */
373 if (px->lbprm.tot_used == 1)
374 goto hash_done;
375
Benoitaffb4812009-03-25 13:02:10 +0100376 /* Found a the hh_name in the headers.
377 * we will compute the hash based on this value ctx.val.
378 */
379 len = ctx.vlen;
380 p = (char *)ctx.line + ctx.val;
381 if (!px->hh_match_domain) {
382 while (len) {
383 hash = *p + (hash << 6) + (hash << 16) - hash;
384 len--;
385 p++;
386 }
387 } else {
388 int dohash = 0;
389 p += len - 1;
390 /* special computation, use only main domain name, not tld/host
391 * going back from the end of string, start hashing at first
392 * dot stop at next.
393 * This is designed to work with the 'Host' header, and requires
394 * a special option to activate this.
395 */
396 while (len) {
397 if (*p == '.') {
398 if (!dohash)
399 dohash = 1;
400 else
401 break;
402 } else {
403 if (dohash)
404 hash = *p + (hash << 6) + (hash << 16) - hash;
405 }
406 len--;
407 p--;
408 }
409 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200410 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200411 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
412 return chash_get_server_hash(px, hash);
413 else
414 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100415}
416
Emeric Brun736aa232009-06-30 17:56:00 +0200417struct server *get_server_rch(struct session *s)
418{
419 unsigned long hash = 0;
420 struct proxy *px = s->be;
421 unsigned long len;
422 const char *p;
423 int ret;
424 struct acl_expr expr;
425 struct acl_test test;
426
427 /* tot_weight appears to mean srv_count */
428 if (px->lbprm.tot_weight == 0)
429 return NULL;
430
Emeric Brun736aa232009-06-30 17:56:00 +0200431 memset(&expr, 0, sizeof(expr));
432 memset(&test, 0, sizeof(test));
433
434 expr.arg.str = px->hh_name;
435 expr.arg_len = px->hh_len;
436
437 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
438 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
439 return NULL;
440
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200441 /* note: we won't hash if there's only one server left */
442 if (px->lbprm.tot_used == 1)
443 goto hash_done;
444
Emeric Brun736aa232009-06-30 17:56:00 +0200445 /* Found a the hh_name in the headers.
446 * we will compute the hash based on this value ctx.val.
447 */
448 len = test.len;
449 p = (char *)test.ptr;
450 while (len) {
451 hash = *p + (hash << 6) + (hash << 16) - hash;
452 len--;
453 p++;
454 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200455 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200456 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
457 return chash_get_server_hash(px, hash);
458 else
459 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200460}
Benoitaffb4812009-03-25 13:02:10 +0100461
462/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200463 * This function applies the load-balancing algorithm to the session, as
464 * defined by the backend it is assigned to. The session is then marked as
465 * 'assigned'.
466 *
467 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
468 * had a server previously assigned, it is rebalanced, trying to avoid the same
469 * server.
470 * The function tries to keep the original connection slot if it reconnects to
471 * the same server, otherwise it releases it and tries to offer it.
472 *
473 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474 *
475 * It may return :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200476 * SRV_STATUS_OK if everything is OK. Session assigned to ->srv
477 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
478 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200479 * SRV_STATUS_INTERNAL for other unrecoverable errors.
480 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200481 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
482 * it does not need to be called anymore. This means that s->srv can be trusted
483 * in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200484 *
485 */
486
487int assign_server(struct session *s)
488{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100489
Willy Tarreau7c669d72008-06-20 15:04:11 +0200490 struct server *conn_slot;
491 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100492
Willy Tarreaubaaee002006-06-26 02:48:02 +0200493#ifdef DEBUG_FULL
494 fprintf(stderr,"assign_server : s=%p\n",s);
495#endif
496
Willy Tarreau7c669d72008-06-20 15:04:11 +0200497 err = SRV_STATUS_INTERNAL;
498 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
499 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100500
Willy Tarreau7c669d72008-06-20 15:04:11 +0200501 s->prev_srv = s->prev_srv;
502 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200503
Willy Tarreau7c669d72008-06-20 15:04:11 +0200504 /* We have to release any connection slot before applying any LB algo,
505 * otherwise we may erroneously end up with no available slot.
506 */
507 if (conn_slot)
508 sess_change_server(s, NULL);
509
510 /* We will now try to find the good server and store it into <s->srv>.
511 * Note that <s->srv> may be NULL in case of dispatch or proxy mode,
512 * as well as if no server is available (check error code).
513 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100514
Willy Tarreau7c669d72008-06-20 15:04:11 +0200515 s->srv = NULL;
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200516 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200517 int len;
518 /* we must check if we have at least one server available */
519 if (!s->be->lbprm.tot_weight) {
520 err = SRV_STATUS_NOSRV;
521 goto out;
522 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200524 /* First check whether we need to fetch some data or simply call
525 * the LB lookup function. Only the hashing functions will need
526 * some input data in fact, and will support multiple algorithms.
527 */
528 switch (s->be->lbprm.algo & BE_LB_LKUP) {
529 case BE_LB_LKUP_RRTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200530 s->srv = fwrr_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200531 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200532
533 case BE_LB_LKUP_LCTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200534 s->srv = fwlc_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200535 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200536
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200537 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200538 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200539 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200540 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
541 s->srv = chash_get_next_server(s->be, s->prev_srv);
542 else
543 s->srv = map_get_server_rr(s->be, s->prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200544 break;
545 }
546 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200547 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200548 err = SRV_STATUS_INTERNAL;
549 goto out;
550 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200551
552 switch (s->be->lbprm.algo & BE_LB_PARM) {
553 case BE_LB_HASH_SRC:
554 if (s->cli_addr.ss_family == AF_INET)
555 len = 4;
556 else if (s->cli_addr.ss_family == AF_INET6)
557 len = 16;
558 else {
559 /* unknown IP family */
560 err = SRV_STATUS_INTERNAL;
561 goto out;
562 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200563
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200564 s->srv = get_server_sh(s->be,
565 (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
566 len);
567 break;
568
569 case BE_LB_HASH_URI:
570 /* URI hashing */
571 s->srv = get_server_uh(s->be,
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200572 s->txn.req.sol + s->txn.req.sl.rq.u,
573 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200574 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200575
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200576 case BE_LB_HASH_PRM:
577 /* URL Parameter hashing */
578 if (s->txn.meth == HTTP_METH_POST &&
579 memchr(s->txn.req.sol + s->txn.req.sl.rq.u, '&',
580 s->txn.req.sl.rq.u_l ) == NULL)
581 s->srv = get_server_ph_post(s);
582 else
583 s->srv = get_server_ph(s->be,
584 s->txn.req.sol + s->txn.req.sl.rq.u,
585 s->txn.req.sl.rq.u_l);
586 break;
Benoitaffb4812009-03-25 13:02:10 +0100587
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200588 case BE_LB_HASH_HDR:
589 /* Header Parameter hashing */
590 s->srv = get_server_hh(s);
591 break;
592
593 case BE_LB_HASH_RDP:
594 /* RDP Cookie hashing */
595 s->srv = get_server_rch(s);
596 break;
597
598 default:
599 /* unknown balancing algorithm */
600 err = SRV_STATUS_INTERNAL;
601 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100602 }
Emeric Brun736aa232009-06-30 17:56:00 +0200603
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200604 /* If the hashing parameter was not found, let's fall
605 * back to round robin on the map.
606 */
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200607 if (!s->srv) {
608 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
609 s->srv = chash_get_next_server(s->be, s->prev_srv);
610 else
611 s->srv = map_get_server_rr(s->be, s->prev_srv);
612 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200613
614 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200615 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200616
Willy Tarreau7c669d72008-06-20 15:04:11 +0200617 default:
618 /* unknown balancing algorithm */
619 err = SRV_STATUS_INTERNAL;
620 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200622
623 if (!s->srv) {
624 err = SRV_STATUS_FULL;
625 goto out;
626 }
627 else if (s->srv != s->prev_srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200628 s->be->counters.cum_lbconn++;
629 s->srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100630 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200631 }
632 else if (s->be->options & PR_O_HTTP_PROXY) {
633 if (!s->srv_addr.sin_addr.s_addr) {
634 err = SRV_STATUS_NOSRV;
635 goto out;
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100636 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200638 else if (!*(int *)&s->be->dispatch_addr.sin_addr &&
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100639 !(s->be->options & PR_O_TRANSP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200640 err = SRV_STATUS_NOSRV;
641 goto out;
642 }
643
644 s->flags |= SN_ASSIGNED;
645 err = SRV_STATUS_OK;
646 out:
647
648 /* Either we take back our connection slot, or we offer it to someone
649 * else if we don't need it anymore.
650 */
651 if (conn_slot) {
652 if (conn_slot == s->srv) {
653 sess_change_server(s, s->srv);
654 } else {
655 if (may_dequeue_tasks(conn_slot, s->be))
656 process_srv_queue(conn_slot);
657 }
658 }
659
660 out_err:
661 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662}
663
664
665/*
666 * This function assigns a server address to a session, and sets SN_ADDR_SET.
667 * The address is taken from the currently assigned server, or from the
668 * dispatch or transparent address.
669 *
670 * It may return :
671 * SRV_STATUS_OK if everything is OK.
672 * SRV_STATUS_INTERNAL for other unrecoverable errors.
673 *
674 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
675 * not cleared, so it's to the caller to clear it if required.
676 *
677 */
678int assign_server_address(struct session *s)
679{
680#ifdef DEBUG_FULL
681 fprintf(stderr,"assign_server_address : s=%p\n",s);
682#endif
683
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200684 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200685 /* A server is necessarily known for this session */
686 if (!(s->flags & SN_ASSIGNED))
687 return SRV_STATUS_INTERNAL;
688
689 s->srv_addr = s->srv->addr;
690
691 /* if this server remaps proxied ports, we'll use
692 * the port the client connected to with an offset. */
693 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100694 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200695 get_frt_addr(s);
696 if (s->frt_addr.ss_family == AF_INET) {
697 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
698 ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port));
699 } else {
700 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
701 ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port));
702 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200703 }
704 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200705 else if (*(int *)&s->be->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706 /* connect to the defined dispatch addr */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200707 s->srv_addr = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100709 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200710 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100711 if (!(s->flags & SN_FRT_ADDR_SET))
712 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200713
Willy Tarreaubd414282008-01-19 13:46:35 +0100714 memcpy(&s->srv_addr, &s->frt_addr, MIN(sizeof(s->srv_addr), sizeof(s->frt_addr)));
715 /* when we support IPv6 on the backend, we may add other tests */
716 //qfprintf(stderr, "Cannot get original server address.\n");
717 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200718 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100719 else if (s->be->options & PR_O_HTTP_PROXY) {
720 /* If HTTP PROXY option is set, then server is already assigned
721 * during incoming client request parsing. */
722 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100723 else {
724 /* no server and no LB algorithm ! */
725 return SRV_STATUS_INTERNAL;
726 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727
728 s->flags |= SN_ADDR_SET;
729 return SRV_STATUS_OK;
730}
731
732
733/* This function assigns a server to session <s> if required, and can add the
734 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200735 * If ->srv_conn is set, the session is first released from the server.
736 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
737 * be called before any connection and after any retry or redispatch occurs.
738 *
739 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200740 *
741 * Returns :
742 *
743 * SRV_STATUS_OK if everything is OK.
744 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
745 * SRV_STATUS_QUEUED if the connection has been queued.
746 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200747 * connection could not be queued in s->srv,
748 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200749 * SRV_STATUS_INTERNAL for other unrecoverable errors.
750 *
751 */
752int assign_server_and_queue(struct session *s)
753{
754 struct pendconn *p;
755 int err;
756
757 if (s->pend_pos)
758 return SRV_STATUS_INTERNAL;
759
Willy Tarreau7c669d72008-06-20 15:04:11 +0200760 err = SRV_STATUS_OK;
761 if (!(s->flags & SN_ASSIGNED)) {
762 err = assign_server(s);
763 if (s->prev_srv) {
764 /* This session was previously assigned to a server. We have to
765 * update the session's and the server's stats :
766 * - if the server changed :
767 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
768 * - set SN_REDISP if it was successfully redispatched
769 * - increment srv->redispatches and be->redispatches
770 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100771 */
772
Willy Tarreau7c669d72008-06-20 15:04:11 +0200773 if (s->prev_srv != s->srv) {
774 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
775 s->txn.flags &= ~TX_CK_MASK;
776 s->txn.flags |= TX_CK_DOWN;
777 }
778 s->flags |= SN_REDISP;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200779 s->prev_srv->counters.redispatches++;
780 s->be->counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200781 } else {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200782 s->prev_srv->counters.retries++;
783 s->be->counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100784 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100785 }
786 }
787
Willy Tarreaubaaee002006-06-26 02:48:02 +0200788 switch (err) {
789 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200790 /* we have SN_ASSIGNED set */
791 if (!s->srv)
792 return SRV_STATUS_OK; /* dispatch or proxy mode */
793
794 /* If we already have a connection slot, no need to check any queue */
795 if (s->srv_conn == s->srv)
796 return SRV_STATUS_OK;
797
798 /* OK, this session already has an assigned server, but no
799 * connection slot yet. Either it is a redispatch, or it was
800 * assigned from persistence information (direct mode).
801 */
802 if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) {
803 /* server scheduled for redirection, and already assigned. We
804 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100805 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200806 sess_change_server(s, s->srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100807 return SRV_STATUS_OK;
808 }
809
Willy Tarreau7c669d72008-06-20 15:04:11 +0200810 /* We might have to queue this session if the assigned server is full.
811 * We know we have to queue it into the server's queue, so if a maxqueue
812 * is set on the server, we must also check that the server's queue is
813 * not full, in which case we have to return FULL.
814 */
815 if (s->srv->maxconn &&
816 (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) {
817
818 if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue)
819 return SRV_STATUS_FULL;
820
Willy Tarreaubaaee002006-06-26 02:48:02 +0200821 p = pendconn_add(s);
822 if (p)
823 return SRV_STATUS_QUEUED;
824 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200825 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200826 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200827
828 /* OK, we can use this server. Let's reserve our place */
829 sess_change_server(s, s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200830 return SRV_STATUS_OK;
831
832 case SRV_STATUS_FULL:
833 /* queue this session into the proxy's queue */
834 p = pendconn_add(s);
835 if (p)
836 return SRV_STATUS_QUEUED;
837 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200838 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200839
840 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200841 return err;
842
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843 case SRV_STATUS_INTERNAL:
844 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200845
Willy Tarreaubaaee002006-06-26 02:48:02 +0200846 default:
847 return SRV_STATUS_INTERNAL;
848 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100849}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200850
851/*
852 * This function initiates a connection to the server assigned to this session
853 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
854 * It can return one of :
855 * - SN_ERR_NONE if everything's OK
856 * - SN_ERR_SRVTO if there are no more servers
857 * - SN_ERR_SRVCL if the connection was refused by the server
858 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
859 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
860 * - SN_ERR_INTERNAL for any other purely internal errors
861 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
862 */
863int connect_server(struct session *s)
864{
Willy Tarreau9650f372009-08-16 14:02:45 +0200865 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200866
867 if (!(s->flags & SN_ADDR_SET)) {
868 err = assign_server_address(s);
869 if (err != SRV_STATUS_OK)
870 return SN_ERR_INTERNAL;
871 }
872
Willy Tarreau9650f372009-08-16 14:02:45 +0200873 if (!s->req->cons->connect)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200874 return SN_ERR_INTERNAL;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200875
Willy Tarreau9650f372009-08-16 14:02:45 +0200876 err = s->req->cons->connect(s->req->cons, s->be, s->srv,
877 (struct sockaddr *)&s->srv_addr,
878 (struct sockaddr *)&s->cli_addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200879
Willy Tarreau9650f372009-08-16 14:02:45 +0200880 if (err != SN_ERR_NONE)
881 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200882
Willy Tarreaubaaee002006-06-26 02:48:02 +0200883 if (s->srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100884 s->flags |= SN_CURR_SESS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200885 s->srv->cur_sess++;
Willy Tarreauac68c5d2009-10-04 23:12:44 +0200886 if (s->srv->cur_sess > s->srv->counters.cur_sess_max)
887 s->srv->counters.cur_sess_max = s->srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100888 if (s->be->lbprm.server_take_conn)
889 s->be->lbprm.server_take_conn(s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200890 }
891
Willy Tarreaubaaee002006-06-26 02:48:02 +0200892 return SN_ERR_NONE; /* connection is OK */
893}
894
895
Willy Tarreaubaaee002006-06-26 02:48:02 +0200896/* This function performs the "redispatch" part of a connection attempt. It
897 * will assign a server if required, queue the connection if required, and
898 * handle errors that might arise at this level. It can change the server
899 * state. It will return 1 if it encounters an error, switches the server
900 * state, or has to queue a connection. Otherwise, it will return 0 indicating
901 * that the connection is ready to use.
902 */
903
904int srv_redispatch_connect(struct session *t)
905{
906 int conn_err;
907
908 /* We know that we don't have any connection pending, so we will
909 * try to get a new one, and wait in this state if it's queued
910 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200911 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200912 conn_err = assign_server_and_queue(t);
913 switch (conn_err) {
914 case SRV_STATUS_OK:
915 break;
916
Willy Tarreau7c669d72008-06-20 15:04:11 +0200917 case SRV_STATUS_FULL:
918 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
919 * and we can redispatch to another server, or it is not and we return
920 * 503. This only makes sense in DIRECT mode however, because normal LB
921 * algorithms would never select such a server, and hash algorithms
922 * would bring us on the same server again. Note that t->srv is set in
923 * this case.
924 */
925 if ((t->flags & SN_DIRECT) && (t->be->options & PR_O_REDISP)) {
926 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
927 t->prev_srv = t->srv;
928 goto redispatch;
929 }
930
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200931 if (!t->req->cons->err_type) {
932 t->req->cons->err_type = SI_ET_QUEUE_ERR;
933 t->req->cons->err_loc = t->srv;
934 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200935
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200936 t->srv->counters.failed_conns++;
937 t->be->counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200938 return 1;
939
Willy Tarreaubaaee002006-06-26 02:48:02 +0200940 case SRV_STATUS_NOSRV:
941 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200942 if (!t->req->cons->err_type) {
943 t->req->cons->err_type = SI_ET_CONN_ERR;
944 t->req->cons->err_loc = NULL;
945 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100946
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200947 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948 return 1;
949
950 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +0200951 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200952 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200953 /* do nothing else and do not wake any other session up */
954 return 1;
955
Willy Tarreaubaaee002006-06-26 02:48:02 +0200956 case SRV_STATUS_INTERNAL:
957 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200958 if (!t->req->cons->err_type) {
959 t->req->cons->err_type = SI_ET_CONN_OTHER;
960 t->req->cons->err_loc = t->srv;
961 }
962
Willy Tarreaubaaee002006-06-26 02:48:02 +0200963 if (t->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100964 srv_inc_sess_ctr(t->srv);
Willy Tarreau98937b82007-12-10 15:05:42 +0100965 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200966 t->srv->counters.failed_conns++;
967 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200968
969 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200970 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200971 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200972 return 1;
973 }
974 /* if we get here, it's because we got SRV_STATUS_OK, which also
975 * means that the connection has not been queued.
976 */
977 return 0;
978}
979
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200980int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100981 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200982 return px->down_time;
983
984 return now.tv_sec - px->last_change + px->down_time;
985}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200986
Willy Tarreaua0cbda62007-11-01 21:39:54 +0100987/* This function parses a "balance" statement in a backend section describing
988 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
989 * returns -1, it may write an error message into ther <err> buffer, for at
990 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
991 * written. The function must be called with <args> pointing to the first word
992 * after "balance".
993 */
994int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
995{
996 if (!*(args[0])) {
997 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +0100998 curproxy->lbprm.algo &= ~BE_LB_ALGO;
999 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001000 return 0;
1001 }
1002
1003 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001004 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1005 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001006 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001007 else if (!strcmp(args[0], "static-rr")) {
1008 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1009 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1010 }
Willy Tarreau51406232008-03-10 22:04:20 +01001011 else if (!strcmp(args[0], "leastconn")) {
1012 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1013 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1014 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001015 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001016 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1017 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001018 }
1019 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001020 int arg = 1;
1021
Willy Tarreau31682232007-11-29 15:38:04 +01001022 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1023 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001024
1025 while (*args[arg]) {
1026 if (!strcmp(args[arg], "len")) {
1027 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1028 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1029 return -1;
1030 }
1031 curproxy->uri_len_limit = atoi(args[arg+1]);
1032 arg += 2;
1033 }
1034 else if (!strcmp(args[arg], "depth")) {
1035 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1036 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1037 return -1;
1038 }
1039 /* hint: we store the position of the ending '/' (depth+1) so
1040 * that we avoid a comparison while computing the hash.
1041 */
1042 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1043 arg += 2;
1044 }
1045 else {
1046 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1047 return -1;
1048 }
1049 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001050 }
Willy Tarreau01732802007-11-01 22:48:15 +01001051 else if (!strcmp(args[0], "url_param")) {
1052 if (!*args[1]) {
1053 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1054 return -1;
1055 }
Willy Tarreau31682232007-11-29 15:38:04 +01001056 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1057 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001058
1059 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001060 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001061 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001062 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001063 if (strcmp(args[2], "check_post")) {
1064 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1065 return -1;
1066 }
1067 if (*args[3]) {
1068 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1069 curproxy->url_param_post_limit = str2ui(args[3]);
1070 }
1071 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1072 if (!curproxy->url_param_post_limit)
1073 curproxy->url_param_post_limit = 48;
1074 else if ( curproxy->url_param_post_limit < 3 )
1075 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1076 }
Benoitaffb4812009-03-25 13:02:10 +01001077 }
1078 else if (!strncmp(args[0], "hdr(", 4)) {
1079 const char *beg, *end;
1080
1081 beg = args[0] + 4;
1082 end = strchr(beg, ')');
1083
1084 if (!end || end == beg) {
1085 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1086 return -1;
1087 }
1088
1089 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1090 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1091
1092 free(curproxy->hh_name);
1093 curproxy->hh_len = end - beg;
1094 curproxy->hh_name = my_strndup(beg, end - beg);
1095 curproxy->hh_match_domain = 0;
1096
1097 if (*args[1]) {
1098 if (strcmp(args[1], "use_domain_only")) {
1099 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1100 return -1;
1101 }
1102 curproxy->hh_match_domain = 1;
1103 }
1104
Emeric Brun736aa232009-06-30 17:56:00 +02001105 }
1106 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1107 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1108 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1109
1110 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1111 const char *beg, *end;
1112
1113 beg = args[0] + 11;
1114 end = strchr(beg, ')');
1115
1116 if (!end || end == beg) {
1117 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1118 return -1;
1119 }
1120
1121 free(curproxy->hh_name);
1122 curproxy->hh_name = my_strndup(beg, end - beg);
1123 curproxy->hh_len = end - beg;
1124 }
1125 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1126 free(curproxy->hh_name);
1127 curproxy->hh_name = strdup("mstshash");
1128 curproxy->hh_len = strlen(curproxy->hh_name);
1129 }
1130 else { /* syntax */
1131 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1132 return -1;
1133 }
Willy Tarreau01732802007-11-01 22:48:15 +01001134 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001135 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001136 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 +01001137 return -1;
1138 }
1139 return 0;
1140}
1141
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001142
1143/************************************************************************/
1144/* All supported keywords must be declared here. */
1145/************************************************************************/
1146
1147/* set test->i to the number of enabled servers on the proxy */
1148static int
1149acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1150 struct acl_expr *expr, struct acl_test *test)
1151{
1152 test->flags = ACL_TEST_F_VOL_TEST;
1153 if (expr->arg_len) {
1154 /* another proxy was designated, we must look for it */
1155 for (px = proxy; px; px = px->next)
1156 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1157 break;
1158 }
1159 if (!px)
1160 return 0;
1161
1162 if (px->srv_act)
1163 test->i = px->srv_act;
1164 else if (px->lbprm.fbck)
1165 test->i = 1;
1166 else
1167 test->i = px->srv_bck;
1168
1169 return 1;
1170}
1171
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001172/* set test->i to the number of enabled servers on the proxy */
1173static int
1174acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1175 struct acl_expr *expr, struct acl_test *test)
1176{
1177 struct server *iterator;
1178 test->flags = ACL_TEST_F_VOL_TEST;
1179 if (expr->arg_len) {
1180 /* another proxy was designated, we must look for it */
1181 for (px = proxy; px; px = px->next)
1182 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1183 break;
1184 }
1185 if (!px)
1186 return 0;
1187
1188 test->i = 0;
1189 iterator = px->srv;
1190 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001191 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001192 iterator = iterator->next;
1193 continue;
1194 }
1195 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
1196 test->i = -1;
1197 return 1;
1198 }
1199
1200 test->i += (iterator->maxconn - iterator->cur_sess)
1201 + (iterator->maxqueue - iterator->nbpend);
1202 iterator = iterator->next;
1203 }
1204
1205 return 1;
1206}
1207
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001208/* set test->i to the number of connections per second reaching the frontend */
1209static int
1210acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1211 struct acl_expr *expr, struct acl_test *test)
1212{
1213 test->flags = ACL_TEST_F_VOL_TEST;
1214 if (expr->arg_len) {
1215 /* another proxy was designated, we must look for it */
1216 for (px = proxy; px; px = px->next)
1217 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
1218 break;
1219 }
1220 if (!px)
1221 return 0;
1222
1223 test->i = read_freq_ctr(&px->fe_sess_per_sec);
1224 return 1;
1225}
1226
1227/* set test->i to the number of connections per second reaching the backend */
1228static int
1229acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1230 struct acl_expr *expr, struct acl_test *test)
1231{
1232 test->flags = ACL_TEST_F_VOL_TEST;
1233 if (expr->arg_len) {
1234 /* another proxy was designated, we must look for it */
1235 for (px = proxy; px; px = px->next)
1236 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1237 break;
1238 }
1239 if (!px)
1240 return 0;
1241
1242 test->i = read_freq_ctr(&px->be_sess_per_sec);
1243 return 1;
1244}
1245
Willy Tarreaua36af912009-10-10 12:02:45 +02001246/* set test->i to the number of concurrent connections on the frontend */
1247static int
1248acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1249 struct acl_expr *expr, struct acl_test *test)
1250{
1251 test->flags = ACL_TEST_F_VOL_TEST;
1252 if (expr->arg_len) {
1253 /* another proxy was designated, we must look for it */
1254 for (px = proxy; px; px = px->next)
1255 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
1256 break;
1257 }
1258 if (!px)
1259 return 0;
1260
1261 test->i = px->feconn;
1262 return 1;
1263}
1264
1265/* set test->i to the number of concurrent connections on the backend */
1266static int
1267acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1268 struct acl_expr *expr, struct acl_test *test)
1269{
1270 test->flags = ACL_TEST_F_VOL_TEST;
1271 if (expr->arg_len) {
1272 /* another proxy was designated, we must look for it */
1273 for (px = proxy; px; px = px->next)
1274 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1275 break;
1276 }
1277 if (!px)
1278 return 0;
1279
1280 test->i = px->beconn;
1281 return 1;
1282}
1283
1284/* set test->i to the total number of queued connections on the backend */
1285static int
1286acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1287 struct acl_expr *expr, struct acl_test *test)
1288{
1289 test->flags = ACL_TEST_F_VOL_TEST;
1290 if (expr->arg_len) {
1291 /* another proxy was designated, we must look for it */
1292 for (px = proxy; px; px = px->next)
1293 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1294 break;
1295 }
1296 if (!px)
1297 return 0;
1298
1299 test->i = px->totpend;
1300 return 1;
1301}
1302
1303/* set test->i to the total number of queued connections on the backend divided
1304 * by the number of running servers and rounded up. If there is no running
1305 * server, we return twice the total, just as if we had half a running server.
1306 * This is more or less correct anyway, since we expect the last server to come
1307 * back soon.
1308 */
1309static int
1310acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1311 struct acl_expr *expr, struct acl_test *test)
1312{
1313 int nbsrv;
1314
1315 test->flags = ACL_TEST_F_VOL_TEST;
1316 if (expr->arg_len) {
1317 /* another proxy was designated, we must look for it */
1318 for (px = proxy; px; px = px->next)
1319 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1320 break;
1321 }
1322 if (!px)
1323 return 0;
1324
1325 if (px->srv_act)
1326 nbsrv = px->srv_act;
1327 else if (px->lbprm.fbck)
1328 nbsrv = 1;
1329 else
1330 nbsrv = px->srv_bck;
1331
1332 if (nbsrv > 0)
1333 test->i = (px->totpend + nbsrv - 1) / nbsrv;
1334 else
1335 test->i = px->totpend * 2;
1336
1337 return 1;
1338}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001339
1340/* Note: must not be declared <const> as its list will be overwritten */
1341static struct acl_kw_list acl_kws = {{ },{
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001342 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau3a8efeb2009-03-05 19:15:37 +01001343 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001344 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
1345 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua36af912009-10-10 12:02:45 +02001346 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
1347 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1348 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1349 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001350 { NULL, NULL, NULL, NULL },
1351}};
1352
1353
1354__attribute__((constructor))
1355static void __backend_init(void)
1356{
1357 acl_register_keywords(&acl_kws);
1358}
1359
Willy Tarreaubaaee002006-06-26 02:48:02 +02001360/*
1361 * Local variables:
1362 * c-indent-level: 8
1363 * c-basic-offset: 8
1364 * End:
1365 */