blob: 14ae7055aa75f8a5bc6eee7edb0f738b4fcf6da5 [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 Tarreauf89c1872009-10-01 11:19:37 +020033#include <proto/lb_fwlc.h>
34#include <proto/lb_fwrr.h>
35#include <proto/lb_map.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010037#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010039#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020040#include <proto/session.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/task.h>
42
Willy Tarreaubaaee002006-06-26 02:48:02 +020043/*
44 * This function recounts the number of usable active and backup servers for
45 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010046 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010047 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010048 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020049 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020050void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020051{
52 struct server *srv;
53
Willy Tarreau20697042007-11-15 23:26:18 +010054 px->srv_act = px->srv_bck = 0;
55 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010056 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010058 if (!srv_is_usable(srv->state, srv->eweight))
59 continue;
60
61 if (srv->state & SRV_BACKUP) {
62 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010063 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010064 px->lbprm.fbck = srv;
65 px->srv_bck++;
66 px->lbprm.tot_wbck += srv->eweight;
67 } else {
68 px->srv_act++;
69 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020070 }
71 }
Willy Tarreaub625a082007-11-26 01:15:43 +010072}
Willy Tarreau20697042007-11-15 23:26:18 +010073
Willy Tarreaub625a082007-11-26 01:15:43 +010074/* This function simply updates the backend's tot_weight and tot_used values
75 * after servers weights have been updated. It is designed to be used after
76 * recount_servers() or equivalent.
77 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020078void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010079{
Willy Tarreau20697042007-11-15 23:26:18 +010080 if (px->srv_act) {
81 px->lbprm.tot_weight = px->lbprm.tot_wact;
82 px->lbprm.tot_used = px->srv_act;
83 }
Willy Tarreaub625a082007-11-26 01:15:43 +010084 else if (px->lbprm.fbck) {
85 /* use only the first backup server */
86 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
87 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010088 }
89 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010090 px->lbprm.tot_weight = px->lbprm.tot_wbck;
91 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +010092 }
Willy Tarreaub625a082007-11-26 01:15:43 +010093}
94
Willy Tarreau39c9ba72009-10-01 21:11:15 +020095/*
96 * This function tries to find a running server for the proxy <px> following
97 * the source hash method. Depending on the number of active/backup servers,
98 * it will either look for active servers, or for backup servers.
99 * If any server is found, it will be returned. If no valid server is found,
100 * NULL is returned.
101 */
102struct server *get_server_sh(struct proxy *px, const char *addr, int len)
103{
104 unsigned int h, l;
105
106 if (px->lbprm.tot_weight == 0)
107 return NULL;
108
109 l = h = 0;
110
111 /* note: we won't hash if there's only one server left */
112 if (px->lbprm.tot_used == 1)
113 goto hash_done;
114
115 while ((l + sizeof (int)) <= len) {
116 h ^= ntohl(*(unsigned int *)(&addr[l]));
117 l += sizeof (int);
118 }
119 hash_done:
120 return map_get_server_hash(px, h);
121}
122
123/*
124 * This function tries to find a running server for the proxy <px> following
125 * the URI hash method. In order to optimize cache hits, the hash computation
126 * ends at the question mark. Depending on the number of active/backup servers,
127 * it will either look for active servers, or for backup servers.
128 * If any server is found, it will be returned. If no valid server is found,
129 * NULL is returned.
130 *
131 * This code was contributed by Guillaume Dallaire, who also selected this hash
132 * algorithm out of a tens because it gave him the best results.
133 *
134 */
135struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
136{
137 unsigned long hash = 0;
138 int c;
139 int slashes = 0;
140
141 if (px->lbprm.tot_weight == 0)
142 return NULL;
143
144 /* note: we won't hash if there's only one server left */
145 if (px->lbprm.tot_used == 1)
146 goto hash_done;
147
148 if (px->uri_len_limit)
149 uri_len = MIN(uri_len, px->uri_len_limit);
150
151 while (uri_len--) {
152 c = *uri++;
153 if (c == '/') {
154 slashes++;
155 if (slashes == px->uri_dirs_depth1) /* depth+1 */
156 break;
157 }
158 else if (c == '?')
159 break;
160
161 hash = c + (hash << 6) + (hash << 16) - hash;
162 }
163 hash_done:
164 return map_get_server_hash(px, hash);
165}
166
Willy Tarreau01732802007-11-01 22:48:15 +0100167/*
168 * This function tries to find a running server for the proxy <px> following
169 * the URL parameter hash method. It looks for a specific parameter in the
170 * URL and hashes it to compute the server ID. This is useful to optimize
171 * performance by avoiding bounces between servers in contexts where sessions
172 * are shared but cookies are not usable. If the parameter is not found, NULL
173 * is returned. If any server is found, it will be returned. If no valid server
174 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100175 */
176struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
177{
178 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200179 const char *p;
180 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100181 int plen;
182
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200183 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100184 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100185 return NULL;
186
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200187 if ((p = memchr(uri, '?', uri_len)) == NULL)
188 return NULL;
189
Willy Tarreau01732802007-11-01 22:48:15 +0100190 p++;
191
192 uri_len -= (p - uri);
193 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200194 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100195
196 while (uri_len > plen) {
197 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200198 if (params[plen] == '=') {
199 if (memcmp(params, px->url_param_name, plen) == 0) {
200 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100201 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200202 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100203 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200204 p += plen + 1;
205 uri_len -= plen + 1;
206
Willy Tarreau01732802007-11-01 22:48:15 +0100207 while (uri_len && *p != '&') {
208 hash = *p + (hash << 6) + (hash << 16) - hash;
209 uri_len--;
210 p++;
211 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200212 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100213 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200214 }
215 /* skip to next parameter */
216 p = memchr(params, '&', uri_len);
217 if (!p)
218 return NULL;
219 p++;
220 uri_len -= (p - params);
221 params = p;
222 }
223 return NULL;
224}
225
226/*
227 * this does the same as the previous server_ph, but check the body contents
228 */
229struct server *get_server_ph_post(struct session *s)
230{
231 unsigned long hash = 0;
232 struct http_txn *txn = &s->txn;
233 struct buffer *req = s->req;
234 struct http_msg *msg = &txn->req;
235 struct proxy *px = s->be;
236 unsigned int plen = px->url_param_len;
Willy Tarreau192ee3e2008-04-19 21:24:56 +0200237 unsigned long body;
238 unsigned long len;
239 const char *params;
240 struct hdr_ctx ctx;
241 const char *p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200242
243 /* tot_weight appears to mean srv_count */
244 if (px->lbprm.tot_weight == 0)
245 return NULL;
246
Willy Tarreau192ee3e2008-04-19 21:24:56 +0200247 body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
Willy Tarreaufb0528b2008-08-11 00:21:56 +0200248 len = req->l - body;
Willy Tarreau192ee3e2008-04-19 21:24:56 +0200249 params = req->data + body;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200250
251 if ( len == 0 )
252 return NULL;
253
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200254 ctx.idx = 0;
255
256 /* if the message is chunked, we skip the chunk size, but use the value as len */
257 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +0200258 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200259 unsigned int chunk = 0;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100260 while ( params < (req->data+req->max_len) && !HTTP_IS_CRLF(*params)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200261 char c = *params;
262 if (ishex(c)) {
263 unsigned int hex = toupper(c) - '0';
264 if ( hex > 9 )
265 hex -= 'A' - '9' - 1;
266 chunk = (chunk << 4) | hex;
267 }
268 else
269 return NULL;
270 params++;
271 len--;
Willy Tarreau01732802007-11-01 22:48:15 +0100272 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200273 /* spec says we get CRLF */
274 if (HTTP_IS_CRLF(*params) && HTTP_IS_CRLF(params[1]))
275 params += 2;
276 else
277 return NULL;
278 /* ok we have some encoded length, just inspect the first chunk */
279 len = chunk;
280 }
Willy Tarreau01732802007-11-01 22:48:15 +0100281
Willy Tarreau192ee3e2008-04-19 21:24:56 +0200282 p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200283
284 while (len > plen) {
285 /* Look for the parameter name followed by an equal symbol */
286 if (params[plen] == '=') {
287 if (memcmp(params, px->url_param_name, plen) == 0) {
288 /* OK, we have the parameter here at <params>, and
289 * the value after the equal sign, at <p>
290 * skip the equal symbol
291 */
292 p += plen + 1;
293 len -= plen + 1;
294
295 while (len && *p != '&') {
296 if (unlikely(!HTTP_IS_TOKEN(*p))) {
297 /* if in a POST, body must be URI encoded or its not a URI.
298 * Do not interprete any possible binary data as a parameter.
299 */
300 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
301 break;
302 return NULL; /* oh, no; this is not uri-encoded.
303 * This body does not contain parameters.
304 */
305 }
306 hash = *p + (hash << 6) + (hash << 16) - hash;
307 len--;
308 p++;
309 /* should we break if vlen exceeds limit? */
310 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200311 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200312 }
313 }
Willy Tarreau01732802007-11-01 22:48:15 +0100314 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200315 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100316 if (!p)
317 return NULL;
318 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200319 len -= (p - params);
320 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100321 }
322 return NULL;
323}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200325
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326/*
Benoitaffb4812009-03-25 13:02:10 +0100327 * This function tries to find a running server for the proxy <px> following
328 * the Header parameter hash method. It looks for a specific parameter in the
329 * URL and hashes it to compute the server ID. This is useful to optimize
330 * performance by avoiding bounces between servers in contexts where sessions
331 * are shared but cookies are not usable. If the parameter is not found, NULL
332 * is returned. If any server is found, it will be returned. If no valid server
333 * is found, NULL is returned.
334 */
335struct server *get_server_hh(struct session *s)
336{
337 unsigned long hash = 0;
338 struct http_txn *txn = &s->txn;
339 struct http_msg *msg = &txn->req;
340 struct proxy *px = s->be;
341 unsigned int plen = px->hh_len;
342 unsigned long len;
343 struct hdr_ctx ctx;
344 const char *p;
345
346 /* tot_weight appears to mean srv_count */
347 if (px->lbprm.tot_weight == 0)
348 return NULL;
349
Benoitaffb4812009-03-25 13:02:10 +0100350 ctx.idx = 0;
351
352 /* if the message is chunked, we skip the chunk size, but use the value as len */
353 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
354
355 /* if the header is not found or empty, let's fallback to round robin */
356 if (!ctx.idx || !ctx.vlen)
357 return NULL;
358
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200359 /* note: we won't hash if there's only one server left */
360 if (px->lbprm.tot_used == 1)
361 goto hash_done;
362
Benoitaffb4812009-03-25 13:02:10 +0100363 /* Found a the hh_name in the headers.
364 * we will compute the hash based on this value ctx.val.
365 */
366 len = ctx.vlen;
367 p = (char *)ctx.line + ctx.val;
368 if (!px->hh_match_domain) {
369 while (len) {
370 hash = *p + (hash << 6) + (hash << 16) - hash;
371 len--;
372 p++;
373 }
374 } else {
375 int dohash = 0;
376 p += len - 1;
377 /* special computation, use only main domain name, not tld/host
378 * going back from the end of string, start hashing at first
379 * dot stop at next.
380 * This is designed to work with the 'Host' header, and requires
381 * a special option to activate this.
382 */
383 while (len) {
384 if (*p == '.') {
385 if (!dohash)
386 dohash = 1;
387 else
388 break;
389 } else {
390 if (dohash)
391 hash = *p + (hash << 6) + (hash << 16) - hash;
392 }
393 len--;
394 p--;
395 }
396 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200397 hash_done:
398 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100399}
400
Emeric Brun736aa232009-06-30 17:56:00 +0200401struct server *get_server_rch(struct session *s)
402{
403 unsigned long hash = 0;
404 struct proxy *px = s->be;
405 unsigned long len;
406 const char *p;
407 int ret;
408 struct acl_expr expr;
409 struct acl_test test;
410
411 /* tot_weight appears to mean srv_count */
412 if (px->lbprm.tot_weight == 0)
413 return NULL;
414
Emeric Brun736aa232009-06-30 17:56:00 +0200415 memset(&expr, 0, sizeof(expr));
416 memset(&test, 0, sizeof(test));
417
418 expr.arg.str = px->hh_name;
419 expr.arg_len = px->hh_len;
420
421 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
422 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
423 return NULL;
424
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200425 /* note: we won't hash if there's only one server left */
426 if (px->lbprm.tot_used == 1)
427 goto hash_done;
428
Emeric Brun736aa232009-06-30 17:56:00 +0200429 /* Found a the hh_name in the headers.
430 * we will compute the hash based on this value ctx.val.
431 */
432 len = test.len;
433 p = (char *)test.ptr;
434 while (len) {
435 hash = *p + (hash << 6) + (hash << 16) - hash;
436 len--;
437 p++;
438 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200439 hash_done:
440 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200441}
Benoitaffb4812009-03-25 13:02:10 +0100442
443/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200444 * This function applies the load-balancing algorithm to the session, as
445 * defined by the backend it is assigned to. The session is then marked as
446 * 'assigned'.
447 *
448 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
449 * had a server previously assigned, it is rebalanced, trying to avoid the same
450 * server.
451 * The function tries to keep the original connection slot if it reconnects to
452 * the same server, otherwise it releases it and tries to offer it.
453 *
454 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455 *
456 * It may return :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200457 * SRV_STATUS_OK if everything is OK. Session assigned to ->srv
458 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
459 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460 * SRV_STATUS_INTERNAL for other unrecoverable errors.
461 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200462 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
463 * it does not need to be called anymore. This means that s->srv can be trusted
464 * in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465 *
466 */
467
468int assign_server(struct session *s)
469{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100470
Willy Tarreau7c669d72008-06-20 15:04:11 +0200471 struct server *conn_slot;
472 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100473
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474#ifdef DEBUG_FULL
475 fprintf(stderr,"assign_server : s=%p\n",s);
476#endif
477
Willy Tarreau7c669d72008-06-20 15:04:11 +0200478 err = SRV_STATUS_INTERNAL;
479 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
480 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100481
Willy Tarreau7c669d72008-06-20 15:04:11 +0200482 s->prev_srv = s->prev_srv;
483 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200484
Willy Tarreau7c669d72008-06-20 15:04:11 +0200485 /* We have to release any connection slot before applying any LB algo,
486 * otherwise we may erroneously end up with no available slot.
487 */
488 if (conn_slot)
489 sess_change_server(s, NULL);
490
491 /* We will now try to find the good server and store it into <s->srv>.
492 * Note that <s->srv> may be NULL in case of dispatch or proxy mode,
493 * as well as if no server is available (check error code).
494 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100495
Willy Tarreau7c669d72008-06-20 15:04:11 +0200496 s->srv = NULL;
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200497 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200498 int len;
499 /* we must check if we have at least one server available */
500 if (!s->be->lbprm.tot_weight) {
501 err = SRV_STATUS_NOSRV;
502 goto out;
503 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200504
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200505 /* First check whether we need to fetch some data or simply call
506 * the LB lookup function. Only the hashing functions will need
507 * some input data in fact, and will support multiple algorithms.
508 */
509 switch (s->be->lbprm.algo & BE_LB_LKUP) {
510 case BE_LB_LKUP_RRTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200511 s->srv = fwrr_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200512 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200513
514 case BE_LB_LKUP_LCTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200515 s->srv = fwlc_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200516 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200517
518 case BE_LB_LKUP_MAP:
519 if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
520 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200521 err = SRV_STATUS_INTERNAL;
522 goto out;
523 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200524
525 switch (s->be->lbprm.algo & BE_LB_PARM) {
526 case BE_LB_HASH_SRC:
527 if (s->cli_addr.ss_family == AF_INET)
528 len = 4;
529 else if (s->cli_addr.ss_family == AF_INET6)
530 len = 16;
531 else {
532 /* unknown IP family */
533 err = SRV_STATUS_INTERNAL;
534 goto out;
535 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200536
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200537 s->srv = get_server_sh(s->be,
538 (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
539 len);
540 break;
541
542 case BE_LB_HASH_URI:
543 /* URI hashing */
544 s->srv = get_server_uh(s->be,
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200545 s->txn.req.sol + s->txn.req.sl.rq.u,
546 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200547 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200548
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200549 case BE_LB_HASH_PRM:
550 /* URL Parameter hashing */
551 if (s->txn.meth == HTTP_METH_POST &&
552 memchr(s->txn.req.sol + s->txn.req.sl.rq.u, '&',
553 s->txn.req.sl.rq.u_l ) == NULL)
554 s->srv = get_server_ph_post(s);
555 else
556 s->srv = get_server_ph(s->be,
557 s->txn.req.sol + s->txn.req.sl.rq.u,
558 s->txn.req.sl.rq.u_l);
559 break;
Benoitaffb4812009-03-25 13:02:10 +0100560
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200561 case BE_LB_HASH_HDR:
562 /* Header Parameter hashing */
563 s->srv = get_server_hh(s);
564 break;
565
566 case BE_LB_HASH_RDP:
567 /* RDP Cookie hashing */
568 s->srv = get_server_rch(s);
569 break;
570
571 default:
572 /* unknown balancing algorithm */
573 err = SRV_STATUS_INTERNAL;
574 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100575 }
Emeric Brun736aa232009-06-30 17:56:00 +0200576
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200577 /* If the hashing parameter was not found, let's fall
578 * back to round robin on the map.
579 */
580 if (!s->srv)
Willy Tarreauf89c1872009-10-01 11:19:37 +0200581 s->srv = map_get_server_rr(s->be, s->prev_srv);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200582
583 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200584 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200585
Willy Tarreau7c669d72008-06-20 15:04:11 +0200586 default:
587 /* unknown balancing algorithm */
588 err = SRV_STATUS_INTERNAL;
589 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200590 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200591
592 if (!s->srv) {
593 err = SRV_STATUS_FULL;
594 goto out;
595 }
596 else if (s->srv != s->prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200597 s->be->cum_lbconn++;
598 s->srv->cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100599 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200600 }
601 else if (s->be->options & PR_O_HTTP_PROXY) {
602 if (!s->srv_addr.sin_addr.s_addr) {
603 err = SRV_STATUS_NOSRV;
604 goto out;
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100605 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200606 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200607 else if (!*(int *)&s->be->dispatch_addr.sin_addr &&
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100608 !(s->be->options & PR_O_TRANSP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200609 err = SRV_STATUS_NOSRV;
610 goto out;
611 }
612
613 s->flags |= SN_ASSIGNED;
614 err = SRV_STATUS_OK;
615 out:
616
617 /* Either we take back our connection slot, or we offer it to someone
618 * else if we don't need it anymore.
619 */
620 if (conn_slot) {
621 if (conn_slot == s->srv) {
622 sess_change_server(s, s->srv);
623 } else {
624 if (may_dequeue_tasks(conn_slot, s->be))
625 process_srv_queue(conn_slot);
626 }
627 }
628
629 out_err:
630 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200631}
632
633
634/*
635 * This function assigns a server address to a session, and sets SN_ADDR_SET.
636 * The address is taken from the currently assigned server, or from the
637 * dispatch or transparent address.
638 *
639 * It may return :
640 * SRV_STATUS_OK if everything is OK.
641 * SRV_STATUS_INTERNAL for other unrecoverable errors.
642 *
643 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
644 * not cleared, so it's to the caller to clear it if required.
645 *
646 */
647int assign_server_address(struct session *s)
648{
649#ifdef DEBUG_FULL
650 fprintf(stderr,"assign_server_address : s=%p\n",s);
651#endif
652
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200653 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654 /* A server is necessarily known for this session */
655 if (!(s->flags & SN_ASSIGNED))
656 return SRV_STATUS_INTERNAL;
657
658 s->srv_addr = s->srv->addr;
659
660 /* if this server remaps proxied ports, we'll use
661 * the port the client connected to with an offset. */
662 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100663 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200664 get_frt_addr(s);
665 if (s->frt_addr.ss_family == AF_INET) {
666 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
667 ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port));
668 } else {
669 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
670 ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port));
671 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672 }
673 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200674 else if (*(int *)&s->be->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 /* connect to the defined dispatch addr */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200676 s->srv_addr = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100678 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100680 if (!(s->flags & SN_FRT_ADDR_SET))
681 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682
Willy Tarreaubd414282008-01-19 13:46:35 +0100683 memcpy(&s->srv_addr, &s->frt_addr, MIN(sizeof(s->srv_addr), sizeof(s->frt_addr)));
684 /* when we support IPv6 on the backend, we may add other tests */
685 //qfprintf(stderr, "Cannot get original server address.\n");
686 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200687 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100688 else if (s->be->options & PR_O_HTTP_PROXY) {
689 /* If HTTP PROXY option is set, then server is already assigned
690 * during incoming client request parsing. */
691 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100692 else {
693 /* no server and no LB algorithm ! */
694 return SRV_STATUS_INTERNAL;
695 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200696
697 s->flags |= SN_ADDR_SET;
698 return SRV_STATUS_OK;
699}
700
701
702/* This function assigns a server to session <s> if required, and can add the
703 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200704 * If ->srv_conn is set, the session is first released from the server.
705 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
706 * be called before any connection and after any retry or redispatch occurs.
707 *
708 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200709 *
710 * Returns :
711 *
712 * SRV_STATUS_OK if everything is OK.
713 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
714 * SRV_STATUS_QUEUED if the connection has been queued.
715 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200716 * connection could not be queued in s->srv,
717 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200718 * SRV_STATUS_INTERNAL for other unrecoverable errors.
719 *
720 */
721int assign_server_and_queue(struct session *s)
722{
723 struct pendconn *p;
724 int err;
725
726 if (s->pend_pos)
727 return SRV_STATUS_INTERNAL;
728
Willy Tarreau7c669d72008-06-20 15:04:11 +0200729 err = SRV_STATUS_OK;
730 if (!(s->flags & SN_ASSIGNED)) {
731 err = assign_server(s);
732 if (s->prev_srv) {
733 /* This session was previously assigned to a server. We have to
734 * update the session's and the server's stats :
735 * - if the server changed :
736 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
737 * - set SN_REDISP if it was successfully redispatched
738 * - increment srv->redispatches and be->redispatches
739 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100740 */
741
Willy Tarreau7c669d72008-06-20 15:04:11 +0200742 if (s->prev_srv != s->srv) {
743 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
744 s->txn.flags &= ~TX_CK_MASK;
745 s->txn.flags |= TX_CK_DOWN;
746 }
747 s->flags |= SN_REDISP;
748 s->prev_srv->redispatches++;
749 s->be->redispatches++;
750 } else {
751 s->prev_srv->retries++;
752 s->be->retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100753 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100754 }
755 }
756
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757 switch (err) {
758 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200759 /* we have SN_ASSIGNED set */
760 if (!s->srv)
761 return SRV_STATUS_OK; /* dispatch or proxy mode */
762
763 /* If we already have a connection slot, no need to check any queue */
764 if (s->srv_conn == s->srv)
765 return SRV_STATUS_OK;
766
767 /* OK, this session already has an assigned server, but no
768 * connection slot yet. Either it is a redispatch, or it was
769 * assigned from persistence information (direct mode).
770 */
771 if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) {
772 /* server scheduled for redirection, and already assigned. We
773 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100774 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200775 sess_change_server(s, s->srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100776 return SRV_STATUS_OK;
777 }
778
Willy Tarreau7c669d72008-06-20 15:04:11 +0200779 /* We might have to queue this session if the assigned server is full.
780 * We know we have to queue it into the server's queue, so if a maxqueue
781 * is set on the server, we must also check that the server's queue is
782 * not full, in which case we have to return FULL.
783 */
784 if (s->srv->maxconn &&
785 (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) {
786
787 if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue)
788 return SRV_STATUS_FULL;
789
Willy Tarreaubaaee002006-06-26 02:48:02 +0200790 p = pendconn_add(s);
791 if (p)
792 return SRV_STATUS_QUEUED;
793 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200794 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200795 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200796
797 /* OK, we can use this server. Let's reserve our place */
798 sess_change_server(s, s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799 return SRV_STATUS_OK;
800
801 case SRV_STATUS_FULL:
802 /* queue this session into the proxy's queue */
803 p = pendconn_add(s);
804 if (p)
805 return SRV_STATUS_QUEUED;
806 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200807 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200808
809 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200810 return err;
811
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812 case SRV_STATUS_INTERNAL:
813 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200814
Willy Tarreaubaaee002006-06-26 02:48:02 +0200815 default:
816 return SRV_STATUS_INTERNAL;
817 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100818}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200819
820/*
821 * This function initiates a connection to the server assigned to this session
822 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
823 * It can return one of :
824 * - SN_ERR_NONE if everything's OK
825 * - SN_ERR_SRVTO if there are no more servers
826 * - SN_ERR_SRVCL if the connection was refused by the server
827 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
828 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
829 * - SN_ERR_INTERNAL for any other purely internal errors
830 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
831 */
832int connect_server(struct session *s)
833{
Willy Tarreau9650f372009-08-16 14:02:45 +0200834 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200835
836 if (!(s->flags & SN_ADDR_SET)) {
837 err = assign_server_address(s);
838 if (err != SRV_STATUS_OK)
839 return SN_ERR_INTERNAL;
840 }
841
Willy Tarreau9650f372009-08-16 14:02:45 +0200842 if (!s->req->cons->connect)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843 return SN_ERR_INTERNAL;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200844
Willy Tarreau9650f372009-08-16 14:02:45 +0200845 err = s->req->cons->connect(s->req->cons, s->be, s->srv,
846 (struct sockaddr *)&s->srv_addr,
847 (struct sockaddr *)&s->cli_addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200848
Willy Tarreau9650f372009-08-16 14:02:45 +0200849 if (err != SN_ERR_NONE)
850 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200851
Willy Tarreaubaaee002006-06-26 02:48:02 +0200852 if (s->srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100853 s->flags |= SN_CURR_SESS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200854 s->srv->cur_sess++;
855 if (s->srv->cur_sess > s->srv->cur_sess_max)
856 s->srv->cur_sess_max = s->srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100857 if (s->be->lbprm.server_take_conn)
858 s->be->lbprm.server_take_conn(s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200859 }
860
Willy Tarreaubaaee002006-06-26 02:48:02 +0200861 return SN_ERR_NONE; /* connection is OK */
862}
863
864
Willy Tarreaubaaee002006-06-26 02:48:02 +0200865/* This function performs the "redispatch" part of a connection attempt. It
866 * will assign a server if required, queue the connection if required, and
867 * handle errors that might arise at this level. It can change the server
868 * state. It will return 1 if it encounters an error, switches the server
869 * state, or has to queue a connection. Otherwise, it will return 0 indicating
870 * that the connection is ready to use.
871 */
872
873int srv_redispatch_connect(struct session *t)
874{
875 int conn_err;
876
877 /* We know that we don't have any connection pending, so we will
878 * try to get a new one, and wait in this state if it's queued
879 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200880 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200881 conn_err = assign_server_and_queue(t);
882 switch (conn_err) {
883 case SRV_STATUS_OK:
884 break;
885
Willy Tarreau7c669d72008-06-20 15:04:11 +0200886 case SRV_STATUS_FULL:
887 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
888 * and we can redispatch to another server, or it is not and we return
889 * 503. This only makes sense in DIRECT mode however, because normal LB
890 * algorithms would never select such a server, and hash algorithms
891 * would bring us on the same server again. Note that t->srv is set in
892 * this case.
893 */
894 if ((t->flags & SN_DIRECT) && (t->be->options & PR_O_REDISP)) {
895 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
896 t->prev_srv = t->srv;
897 goto redispatch;
898 }
899
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200900 if (!t->req->cons->err_type) {
901 t->req->cons->err_type = SI_ET_QUEUE_ERR;
902 t->req->cons->err_loc = t->srv;
903 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200904
905 t->srv->failed_conns++;
906 t->be->failed_conns++;
907 return 1;
908
Willy Tarreaubaaee002006-06-26 02:48:02 +0200909 case SRV_STATUS_NOSRV:
910 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200911 if (!t->req->cons->err_type) {
912 t->req->cons->err_type = SI_ET_CONN_ERR;
913 t->req->cons->err_loc = NULL;
914 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100915
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200916 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200917 return 1;
918
919 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +0200920 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200921 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200922 /* do nothing else and do not wake any other session up */
923 return 1;
924
Willy Tarreaubaaee002006-06-26 02:48:02 +0200925 case SRV_STATUS_INTERNAL:
926 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200927 if (!t->req->cons->err_type) {
928 t->req->cons->err_type = SI_ET_CONN_OTHER;
929 t->req->cons->err_loc = t->srv;
930 }
931
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 if (t->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100933 srv_inc_sess_ctr(t->srv);
Willy Tarreau98937b82007-12-10 15:05:42 +0100934 if (t->srv)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200936 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937
938 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200939 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200940 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200941 return 1;
942 }
943 /* if we get here, it's because we got SRV_STATUS_OK, which also
944 * means that the connection has not been queued.
945 */
946 return 0;
947}
948
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200949int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100950 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200951 return px->down_time;
952
953 return now.tv_sec - px->last_change + px->down_time;
954}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200955
Willy Tarreaua0cbda62007-11-01 21:39:54 +0100956/* This function parses a "balance" statement in a backend section describing
957 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
958 * returns -1, it may write an error message into ther <err> buffer, for at
959 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
960 * written. The function must be called with <args> pointing to the first word
961 * after "balance".
962 */
963int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
964{
965 if (!*(args[0])) {
966 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +0100967 curproxy->lbprm.algo &= ~BE_LB_ALGO;
968 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +0100969 return 0;
970 }
971
972 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +0100973 curproxy->lbprm.algo &= ~BE_LB_ALGO;
974 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +0100975 }
Willy Tarreau51406232008-03-10 22:04:20 +0100976 else if (!strcmp(args[0], "leastconn")) {
977 curproxy->lbprm.algo &= ~BE_LB_ALGO;
978 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
979 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +0100980 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +0100981 curproxy->lbprm.algo &= ~BE_LB_ALGO;
982 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +0100983 }
984 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +0200985 int arg = 1;
986
Willy Tarreau31682232007-11-29 15:38:04 +0100987 curproxy->lbprm.algo &= ~BE_LB_ALGO;
988 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +0200989
990 while (*args[arg]) {
991 if (!strcmp(args[arg], "len")) {
992 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
993 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
994 return -1;
995 }
996 curproxy->uri_len_limit = atoi(args[arg+1]);
997 arg += 2;
998 }
999 else if (!strcmp(args[arg], "depth")) {
1000 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1001 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1002 return -1;
1003 }
1004 /* hint: we store the position of the ending '/' (depth+1) so
1005 * that we avoid a comparison while computing the hash.
1006 */
1007 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1008 arg += 2;
1009 }
1010 else {
1011 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1012 return -1;
1013 }
1014 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001015 }
Willy Tarreau01732802007-11-01 22:48:15 +01001016 else if (!strcmp(args[0], "url_param")) {
1017 if (!*args[1]) {
1018 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1019 return -1;
1020 }
Willy Tarreau31682232007-11-29 15:38:04 +01001021 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1022 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001023
1024 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001025 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001026 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001027 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001028 if (strcmp(args[2], "check_post")) {
1029 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1030 return -1;
1031 }
1032 if (*args[3]) {
1033 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1034 curproxy->url_param_post_limit = str2ui(args[3]);
1035 }
1036 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1037 if (!curproxy->url_param_post_limit)
1038 curproxy->url_param_post_limit = 48;
1039 else if ( curproxy->url_param_post_limit < 3 )
1040 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1041 }
Benoitaffb4812009-03-25 13:02:10 +01001042 }
1043 else if (!strncmp(args[0], "hdr(", 4)) {
1044 const char *beg, *end;
1045
1046 beg = args[0] + 4;
1047 end = strchr(beg, ')');
1048
1049 if (!end || end == beg) {
1050 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1051 return -1;
1052 }
1053
1054 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1055 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1056
1057 free(curproxy->hh_name);
1058 curproxy->hh_len = end - beg;
1059 curproxy->hh_name = my_strndup(beg, end - beg);
1060 curproxy->hh_match_domain = 0;
1061
1062 if (*args[1]) {
1063 if (strcmp(args[1], "use_domain_only")) {
1064 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1065 return -1;
1066 }
1067 curproxy->hh_match_domain = 1;
1068 }
1069
Emeric Brun736aa232009-06-30 17:56:00 +02001070 }
1071 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1072 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1073 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1074
1075 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1076 const char *beg, *end;
1077
1078 beg = args[0] + 11;
1079 end = strchr(beg, ')');
1080
1081 if (!end || end == beg) {
1082 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1083 return -1;
1084 }
1085
1086 free(curproxy->hh_name);
1087 curproxy->hh_name = my_strndup(beg, end - beg);
1088 curproxy->hh_len = end - beg;
1089 }
1090 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1091 free(curproxy->hh_name);
1092 curproxy->hh_name = strdup("mstshash");
1093 curproxy->hh_len = strlen(curproxy->hh_name);
1094 }
1095 else { /* syntax */
1096 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1097 return -1;
1098 }
Willy Tarreau01732802007-11-01 22:48:15 +01001099 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001100 else {
Emeric Brun736aa232009-06-30 17:56:00 +02001101 snprintf(err, errlen, "'balance' only supports 'roundrobin', 'leastconn', 'source', 'uri', 'url_param', 'hdr(name)' and 'rdp-cookie(name)' options.");
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001102 return -1;
1103 }
1104 return 0;
1105}
1106
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001107
1108/************************************************************************/
1109/* All supported keywords must be declared here. */
1110/************************************************************************/
1111
1112/* set test->i to the number of enabled servers on the proxy */
1113static int
1114acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1115 struct acl_expr *expr, struct acl_test *test)
1116{
1117 test->flags = ACL_TEST_F_VOL_TEST;
1118 if (expr->arg_len) {
1119 /* another proxy was designated, we must look for it */
1120 for (px = proxy; px; px = px->next)
1121 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1122 break;
1123 }
1124 if (!px)
1125 return 0;
1126
1127 if (px->srv_act)
1128 test->i = px->srv_act;
1129 else if (px->lbprm.fbck)
1130 test->i = 1;
1131 else
1132 test->i = px->srv_bck;
1133
1134 return 1;
1135}
1136
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001137/* set test->i to the number of enabled servers on the proxy */
1138static int
1139acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1140 struct acl_expr *expr, struct acl_test *test)
1141{
1142 struct server *iterator;
1143 test->flags = ACL_TEST_F_VOL_TEST;
1144 if (expr->arg_len) {
1145 /* another proxy was designated, we must look for it */
1146 for (px = proxy; px; px = px->next)
1147 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1148 break;
1149 }
1150 if (!px)
1151 return 0;
1152
1153 test->i = 0;
1154 iterator = px->srv;
1155 while (iterator) {
1156 if ((iterator->state & 1) == 0) {
1157 iterator = iterator->next;
1158 continue;
1159 }
1160 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
1161 test->i = -1;
1162 return 1;
1163 }
1164
1165 test->i += (iterator->maxconn - iterator->cur_sess)
1166 + (iterator->maxqueue - iterator->nbpend);
1167 iterator = iterator->next;
1168 }
1169
1170 return 1;
1171}
1172
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001173/* set test->i to the number of connections per second reaching the frontend */
1174static int
1175acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1176 struct acl_expr *expr, struct acl_test *test)
1177{
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_FE) && !strcmp(px->id, expr->arg.str))
1183 break;
1184 }
1185 if (!px)
1186 return 0;
1187
1188 test->i = read_freq_ctr(&px->fe_sess_per_sec);
1189 return 1;
1190}
1191
1192/* set test->i to the number of connections per second reaching the backend */
1193static int
1194acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1195 struct acl_expr *expr, struct acl_test *test)
1196{
1197 test->flags = ACL_TEST_F_VOL_TEST;
1198 if (expr->arg_len) {
1199 /* another proxy was designated, we must look for it */
1200 for (px = proxy; px; px = px->next)
1201 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1202 break;
1203 }
1204 if (!px)
1205 return 0;
1206
1207 test->i = read_freq_ctr(&px->be_sess_per_sec);
1208 return 1;
1209}
1210
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001211
1212/* Note: must not be declared <const> as its list will be overwritten */
1213static struct acl_kw_list acl_kws = {{ },{
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001214 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau3a8efeb2009-03-05 19:15:37 +01001215 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001216 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
1217 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001218 { NULL, NULL, NULL, NULL },
1219}};
1220
1221
1222__attribute__((constructor))
1223static void __backend_init(void)
1224{
1225 acl_register_keywords(&acl_kws);
1226}
1227
Willy Tarreaubaaee002006-06-26 02:48:02 +02001228/*
1229 * Local variables:
1230 * c-indent-level: 8
1231 * c-basic-offset: 8
1232 * End:
1233 */