blob: b51c27677d5c3639c0e3bce956c48b9b9838c200 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreau44b90cc2010-05-24 20:27:29 +02004 * Copyright 2000-2010 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>
38#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:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200120 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
121 return chash_get_server_hash(px, h);
122 else
123 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200124}
125
126/*
127 * This function tries to find a running server for the proxy <px> following
128 * the URI hash method. In order to optimize cache hits, the hash computation
129 * ends at the question mark. Depending on the number of active/backup servers,
130 * it will either look for active servers, or for backup servers.
131 * If any server is found, it will be returned. If no valid server is found,
132 * NULL is returned.
133 *
134 * This code was contributed by Guillaume Dallaire, who also selected this hash
135 * algorithm out of a tens because it gave him the best results.
136 *
137 */
138struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
139{
140 unsigned long hash = 0;
141 int c;
142 int slashes = 0;
143
144 if (px->lbprm.tot_weight == 0)
145 return NULL;
146
147 /* note: we won't hash if there's only one server left */
148 if (px->lbprm.tot_used == 1)
149 goto hash_done;
150
151 if (px->uri_len_limit)
152 uri_len = MIN(uri_len, px->uri_len_limit);
153
154 while (uri_len--) {
155 c = *uri++;
156 if (c == '/') {
157 slashes++;
158 if (slashes == px->uri_dirs_depth1) /* depth+1 */
159 break;
160 }
161 else if (c == '?')
162 break;
163
164 hash = c + (hash << 6) + (hash << 16) - hash;
165 }
166 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200167 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
168 return chash_get_server_hash(px, hash);
169 else
170 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200171}
172
Willy Tarreau01732802007-11-01 22:48:15 +0100173/*
174 * This function tries to find a running server for the proxy <px> following
175 * the URL parameter hash method. It looks for a specific parameter in the
176 * URL and hashes it to compute the server ID. This is useful to optimize
177 * performance by avoiding bounces between servers in contexts where sessions
178 * are shared but cookies are not usable. If the parameter is not found, NULL
179 * is returned. If any server is found, it will be returned. If no valid server
180 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100181 */
182struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
183{
184 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200185 const char *p;
186 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100187 int plen;
188
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200189 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100190 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100191 return NULL;
192
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200193 if ((p = memchr(uri, '?', uri_len)) == NULL)
194 return NULL;
195
Willy Tarreau01732802007-11-01 22:48:15 +0100196 p++;
197
198 uri_len -= (p - uri);
199 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200200 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100201
202 while (uri_len > plen) {
203 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200204 if (params[plen] == '=') {
205 if (memcmp(params, px->url_param_name, plen) == 0) {
206 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100207 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200208 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100209 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200210 p += plen + 1;
211 uri_len -= plen + 1;
212
Willy Tarreau01732802007-11-01 22:48:15 +0100213 while (uri_len && *p != '&') {
214 hash = *p + (hash << 6) + (hash << 16) - hash;
215 uri_len--;
216 p++;
217 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200218 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
219 return chash_get_server_hash(px, hash);
220 else
221 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100222 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200223 }
224 /* skip to next parameter */
225 p = memchr(params, '&', uri_len);
226 if (!p)
227 return NULL;
228 p++;
229 uri_len -= (p - params);
230 params = p;
231 }
232 return NULL;
233}
234
235/*
236 * this does the same as the previous server_ph, but check the body contents
237 */
238struct server *get_server_ph_post(struct session *s)
239{
240 unsigned long hash = 0;
241 struct http_txn *txn = &s->txn;
242 struct buffer *req = s->req;
243 struct http_msg *msg = &txn->req;
244 struct proxy *px = s->be;
245 unsigned int plen = px->url_param_len;
Willy Tarreau157dd632009-12-06 19:18:09 +0100246 unsigned long len = msg->hdr_content_len;
247 const char *params = req->data + msg->sov;
248 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200249
Willy Tarreau7c96f672009-12-27 22:47:25 +0100250 if (len > req->l - (msg->sov - msg->som))
251 len = req->l - (msg->sov - msg->som);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200252
Willy Tarreau157dd632009-12-06 19:18:09 +0100253 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200254 return NULL;
255
Willy Tarreau157dd632009-12-06 19:18:09 +0100256 if (px->lbprm.tot_weight == 0)
257 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200258
259 while (len > plen) {
260 /* Look for the parameter name followed by an equal symbol */
261 if (params[plen] == '=') {
262 if (memcmp(params, px->url_param_name, plen) == 0) {
263 /* OK, we have the parameter here at <params>, and
264 * the value after the equal sign, at <p>
265 * skip the equal symbol
266 */
267 p += plen + 1;
268 len -= plen + 1;
269
270 while (len && *p != '&') {
271 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100272 /* if in a POST, body must be URI encoded or it's not a URI.
273 * Do not interprete any possible binary data as a parameter.
274 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200275 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
276 break;
277 return NULL; /* oh, no; this is not uri-encoded.
278 * This body does not contain parameters.
279 */
280 }
281 hash = *p + (hash << 6) + (hash << 16) - hash;
282 len--;
283 p++;
284 /* should we break if vlen exceeds limit? */
285 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200286 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
287 return chash_get_server_hash(px, hash);
288 else
289 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200290 }
291 }
Willy Tarreau01732802007-11-01 22:48:15 +0100292 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200293 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100294 if (!p)
295 return NULL;
296 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200297 len -= (p - params);
298 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100299 }
300 return NULL;
301}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200302
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200303
Willy Tarreaubaaee002006-06-26 02:48:02 +0200304/*
Benoitaffb4812009-03-25 13:02:10 +0100305 * This function tries to find a running server for the proxy <px> following
306 * the Header parameter hash method. It looks for a specific parameter in the
307 * URL and hashes it to compute the server ID. This is useful to optimize
308 * performance by avoiding bounces between servers in contexts where sessions
309 * are shared but cookies are not usable. If the parameter is not found, NULL
310 * is returned. If any server is found, it will be returned. If no valid server
311 * is found, NULL is returned.
312 */
313struct server *get_server_hh(struct session *s)
314{
315 unsigned long hash = 0;
316 struct http_txn *txn = &s->txn;
317 struct http_msg *msg = &txn->req;
318 struct proxy *px = s->be;
319 unsigned int plen = px->hh_len;
320 unsigned long len;
321 struct hdr_ctx ctx;
322 const char *p;
323
324 /* tot_weight appears to mean srv_count */
325 if (px->lbprm.tot_weight == 0)
326 return NULL;
327
Benoitaffb4812009-03-25 13:02:10 +0100328 ctx.idx = 0;
329
330 /* if the message is chunked, we skip the chunk size, but use the value as len */
331 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
332
333 /* if the header is not found or empty, let's fallback to round robin */
334 if (!ctx.idx || !ctx.vlen)
335 return NULL;
336
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200337 /* note: we won't hash if there's only one server left */
338 if (px->lbprm.tot_used == 1)
339 goto hash_done;
340
Benoitaffb4812009-03-25 13:02:10 +0100341 /* Found a the hh_name in the headers.
342 * we will compute the hash based on this value ctx.val.
343 */
344 len = ctx.vlen;
345 p = (char *)ctx.line + ctx.val;
346 if (!px->hh_match_domain) {
347 while (len) {
348 hash = *p + (hash << 6) + (hash << 16) - hash;
349 len--;
350 p++;
351 }
352 } else {
353 int dohash = 0;
354 p += len - 1;
355 /* special computation, use only main domain name, not tld/host
356 * going back from the end of string, start hashing at first
357 * dot stop at next.
358 * This is designed to work with the 'Host' header, and requires
359 * a special option to activate this.
360 */
361 while (len) {
362 if (*p == '.') {
363 if (!dohash)
364 dohash = 1;
365 else
366 break;
367 } else {
368 if (dohash)
369 hash = *p + (hash << 6) + (hash << 16) - hash;
370 }
371 len--;
372 p--;
373 }
374 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200375 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200376 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
377 return chash_get_server_hash(px, hash);
378 else
379 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100380}
381
Emeric Brun736aa232009-06-30 17:56:00 +0200382struct server *get_server_rch(struct session *s)
383{
384 unsigned long hash = 0;
385 struct proxy *px = s->be;
386 unsigned long len;
387 const char *p;
388 int ret;
389 struct acl_expr expr;
390 struct acl_test test;
391
392 /* tot_weight appears to mean srv_count */
393 if (px->lbprm.tot_weight == 0)
394 return NULL;
395
Emeric Brun736aa232009-06-30 17:56:00 +0200396 memset(&expr, 0, sizeof(expr));
397 memset(&test, 0, sizeof(test));
398
399 expr.arg.str = px->hh_name;
400 expr.arg_len = px->hh_len;
401
402 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
403 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
404 return NULL;
405
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200406 /* note: we won't hash if there's only one server left */
407 if (px->lbprm.tot_used == 1)
408 goto hash_done;
409
Emeric Brun736aa232009-06-30 17:56:00 +0200410 /* Found a the hh_name in the headers.
411 * we will compute the hash based on this value ctx.val.
412 */
413 len = test.len;
414 p = (char *)test.ptr;
415 while (len) {
416 hash = *p + (hash << 6) + (hash << 16) - hash;
417 len--;
418 p++;
419 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200420 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200421 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
422 return chash_get_server_hash(px, hash);
423 else
424 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200425}
Benoitaffb4812009-03-25 13:02:10 +0100426
427/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200428 * This function applies the load-balancing algorithm to the session, as
429 * defined by the backend it is assigned to. The session is then marked as
430 * 'assigned'.
431 *
432 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
433 * had a server previously assigned, it is rebalanced, trying to avoid the same
434 * server.
435 * The function tries to keep the original connection slot if it reconnects to
436 * the same server, otherwise it releases it and tries to offer it.
437 *
438 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439 *
440 * It may return :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200441 * SRV_STATUS_OK if everything is OK. Session assigned to ->srv
442 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
443 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200444 * SRV_STATUS_INTERNAL for other unrecoverable errors.
445 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200446 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
447 * it does not need to be called anymore. This means that s->srv can be trusted
448 * in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449 *
450 */
451
452int assign_server(struct session *s)
453{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100454
Willy Tarreau7c669d72008-06-20 15:04:11 +0200455 struct server *conn_slot;
456 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100457
Willy Tarreaubaaee002006-06-26 02:48:02 +0200458#ifdef DEBUG_FULL
459 fprintf(stderr,"assign_server : s=%p\n",s);
460#endif
461
Willy Tarreau7c669d72008-06-20 15:04:11 +0200462 err = SRV_STATUS_INTERNAL;
463 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
464 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100465
Willy Tarreau7c669d72008-06-20 15:04:11 +0200466 s->prev_srv = s->prev_srv;
467 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200468
Willy Tarreau7c669d72008-06-20 15:04:11 +0200469 /* We have to release any connection slot before applying any LB algo,
470 * otherwise we may erroneously end up with no available slot.
471 */
472 if (conn_slot)
473 sess_change_server(s, NULL);
474
475 /* We will now try to find the good server and store it into <s->srv>.
476 * Note that <s->srv> may be NULL in case of dispatch or proxy mode,
477 * as well as if no server is available (check error code).
478 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100479
Willy Tarreau7c669d72008-06-20 15:04:11 +0200480 s->srv = NULL;
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200481 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200482 int len;
483 /* we must check if we have at least one server available */
484 if (!s->be->lbprm.tot_weight) {
485 err = SRV_STATUS_NOSRV;
486 goto out;
487 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200488
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200489 /* First check whether we need to fetch some data or simply call
490 * the LB lookup function. Only the hashing functions will need
491 * some input data in fact, and will support multiple algorithms.
492 */
493 switch (s->be->lbprm.algo & BE_LB_LKUP) {
494 case BE_LB_LKUP_RRTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200495 s->srv = fwrr_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200496 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200497
498 case BE_LB_LKUP_LCTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200499 s->srv = fwlc_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200500 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200501
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200502 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200503 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200504 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200505 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
506 s->srv = chash_get_next_server(s->be, s->prev_srv);
507 else
508 s->srv = map_get_server_rr(s->be, s->prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200509 break;
510 }
511 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200512 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200513 err = SRV_STATUS_INTERNAL;
514 goto out;
515 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200516
517 switch (s->be->lbprm.algo & BE_LB_PARM) {
518 case BE_LB_HASH_SRC:
519 if (s->cli_addr.ss_family == AF_INET)
520 len = 4;
521 else if (s->cli_addr.ss_family == AF_INET6)
522 len = 16;
523 else {
524 /* unknown IP family */
525 err = SRV_STATUS_INTERNAL;
526 goto out;
527 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200528
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200529 s->srv = get_server_sh(s->be,
530 (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
531 len);
532 break;
533
534 case BE_LB_HASH_URI:
535 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100536 if (s->txn.req.msg_state < HTTP_MSG_BODY)
537 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200538 s->srv = get_server_uh(s->be,
Willy Tarreau962c3f42010-01-10 00:15:35 +0100539 s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200540 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200541 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200542
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200543 case BE_LB_HASH_PRM:
544 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100545 if (s->txn.req.msg_state < HTTP_MSG_BODY)
546 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200547 if (s->txn.meth == HTTP_METH_POST &&
Willy Tarreau962c3f42010-01-10 00:15:35 +0100548 memchr(s->txn.req.sol + s->txn.req.sl.rq.u, '&',
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200549 s->txn.req.sl.rq.u_l ) == NULL)
550 s->srv = get_server_ph_post(s);
551 else
552 s->srv = get_server_ph(s->be,
Willy Tarreau962c3f42010-01-10 00:15:35 +0100553 s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200554 s->txn.req.sl.rq.u_l);
555 break;
Benoitaffb4812009-03-25 13:02:10 +0100556
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200557 case BE_LB_HASH_HDR:
558 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100559 if (s->txn.req.msg_state < HTTP_MSG_BODY)
560 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200561 s->srv = get_server_hh(s);
562 break;
563
564 case BE_LB_HASH_RDP:
565 /* RDP Cookie hashing */
566 s->srv = get_server_rch(s);
567 break;
568
569 default:
570 /* unknown balancing algorithm */
571 err = SRV_STATUS_INTERNAL;
572 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100573 }
Emeric Brun736aa232009-06-30 17:56:00 +0200574
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200575 /* If the hashing parameter was not found, let's fall
576 * back to round robin on the map.
577 */
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200578 if (!s->srv) {
579 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
580 s->srv = chash_get_next_server(s->be, s->prev_srv);
581 else
582 s->srv = map_get_server_rr(s->be, s->prev_srv);
583 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200584
585 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200586 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200587
Willy Tarreau7c669d72008-06-20 15:04:11 +0200588 default:
589 /* unknown balancing algorithm */
590 err = SRV_STATUS_INTERNAL;
591 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200592 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200593
594 if (!s->srv) {
595 err = SRV_STATUS_FULL;
596 goto out;
597 }
598 else if (s->srv != s->prev_srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200599 s->be->counters.cum_lbconn++;
600 s->srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100601 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200602 }
603 else if (s->be->options & PR_O_HTTP_PROXY) {
604 if (!s->srv_addr.sin_addr.s_addr) {
605 err = SRV_STATUS_NOSRV;
606 goto out;
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100607 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200608 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200609 else if (!*(int *)&s->be->dispatch_addr.sin_addr &&
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100610 !(s->be->options & PR_O_TRANSP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200611 err = SRV_STATUS_NOSRV;
612 goto out;
613 }
614
615 s->flags |= SN_ASSIGNED;
616 err = SRV_STATUS_OK;
617 out:
618
619 /* Either we take back our connection slot, or we offer it to someone
620 * else if we don't need it anymore.
621 */
622 if (conn_slot) {
623 if (conn_slot == s->srv) {
624 sess_change_server(s, s->srv);
625 } else {
626 if (may_dequeue_tasks(conn_slot, s->be))
627 process_srv_queue(conn_slot);
628 }
629 }
630
631 out_err:
632 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200633}
634
635
636/*
637 * This function assigns a server address to a session, and sets SN_ADDR_SET.
638 * The address is taken from the currently assigned server, or from the
639 * dispatch or transparent address.
640 *
641 * It may return :
642 * SRV_STATUS_OK if everything is OK.
643 * SRV_STATUS_INTERNAL for other unrecoverable errors.
644 *
645 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
646 * not cleared, so it's to the caller to clear it if required.
647 *
648 */
649int assign_server_address(struct session *s)
650{
651#ifdef DEBUG_FULL
652 fprintf(stderr,"assign_server_address : s=%p\n",s);
653#endif
654
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200655 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200656 /* A server is necessarily known for this session */
657 if (!(s->flags & SN_ASSIGNED))
658 return SRV_STATUS_INTERNAL;
659
660 s->srv_addr = s->srv->addr;
661
662 /* if this server remaps proxied ports, we'll use
663 * the port the client connected to with an offset. */
664 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100665 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200666 get_frt_addr(s);
667 if (s->frt_addr.ss_family == AF_INET) {
668 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
669 ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port));
670 } else {
671 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
672 ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port));
673 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 }
675 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200676 else if (*(int *)&s->be->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677 /* connect to the defined dispatch addr */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200678 s->srv_addr = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100680 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200681 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100682 if (!(s->flags & SN_FRT_ADDR_SET))
683 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684
Willy Tarreaubd414282008-01-19 13:46:35 +0100685 memcpy(&s->srv_addr, &s->frt_addr, MIN(sizeof(s->srv_addr), sizeof(s->frt_addr)));
686 /* when we support IPv6 on the backend, we may add other tests */
687 //qfprintf(stderr, "Cannot get original server address.\n");
688 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200689 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100690 else if (s->be->options & PR_O_HTTP_PROXY) {
691 /* If HTTP PROXY option is set, then server is already assigned
692 * during incoming client request parsing. */
693 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100694 else {
695 /* no server and no LB algorithm ! */
696 return SRV_STATUS_INTERNAL;
697 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200698
699 s->flags |= SN_ADDR_SET;
700 return SRV_STATUS_OK;
701}
702
703
704/* This function assigns a server to session <s> if required, and can add the
705 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200706 * If ->srv_conn is set, the session is first released from the server.
707 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
708 * be called before any connection and after any retry or redispatch occurs.
709 *
710 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200711 *
712 * Returns :
713 *
714 * SRV_STATUS_OK if everything is OK.
715 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
716 * SRV_STATUS_QUEUED if the connection has been queued.
717 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200718 * connection could not be queued in s->srv,
719 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200720 * SRV_STATUS_INTERNAL for other unrecoverable errors.
721 *
722 */
723int assign_server_and_queue(struct session *s)
724{
725 struct pendconn *p;
726 int err;
727
728 if (s->pend_pos)
729 return SRV_STATUS_INTERNAL;
730
Willy Tarreau7c669d72008-06-20 15:04:11 +0200731 err = SRV_STATUS_OK;
732 if (!(s->flags & SN_ASSIGNED)) {
733 err = assign_server(s);
734 if (s->prev_srv) {
735 /* This session was previously assigned to a server. We have to
736 * update the session's and the server's stats :
737 * - if the server changed :
738 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
739 * - set SN_REDISP if it was successfully redispatched
740 * - increment srv->redispatches and be->redispatches
741 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100742 */
743
Willy Tarreau7c669d72008-06-20 15:04:11 +0200744 if (s->prev_srv != s->srv) {
745 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
746 s->txn.flags &= ~TX_CK_MASK;
747 s->txn.flags |= TX_CK_DOWN;
748 }
749 s->flags |= SN_REDISP;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200750 s->prev_srv->counters.redispatches++;
751 s->be->counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200752 } else {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200753 s->prev_srv->counters.retries++;
754 s->be->counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100755 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100756 }
757 }
758
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759 switch (err) {
760 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200761 /* we have SN_ASSIGNED set */
762 if (!s->srv)
763 return SRV_STATUS_OK; /* dispatch or proxy mode */
764
765 /* If we already have a connection slot, no need to check any queue */
766 if (s->srv_conn == s->srv)
767 return SRV_STATUS_OK;
768
769 /* OK, this session already has an assigned server, but no
770 * connection slot yet. Either it is a redispatch, or it was
771 * assigned from persistence information (direct mode).
772 */
773 if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) {
774 /* server scheduled for redirection, and already assigned. We
775 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100776 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200777 sess_change_server(s, s->srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100778 return SRV_STATUS_OK;
779 }
780
Willy Tarreau7c669d72008-06-20 15:04:11 +0200781 /* We might have to queue this session if the assigned server is full.
782 * We know we have to queue it into the server's queue, so if a maxqueue
783 * is set on the server, we must also check that the server's queue is
784 * not full, in which case we have to return FULL.
785 */
786 if (s->srv->maxconn &&
787 (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) {
788
789 if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue)
790 return SRV_STATUS_FULL;
791
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792 p = pendconn_add(s);
793 if (p)
794 return SRV_STATUS_QUEUED;
795 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200796 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200798
799 /* OK, we can use this server. Let's reserve our place */
800 sess_change_server(s, s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 return SRV_STATUS_OK;
802
803 case SRV_STATUS_FULL:
804 /* queue this session into the proxy's queue */
805 p = pendconn_add(s);
806 if (p)
807 return SRV_STATUS_QUEUED;
808 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200809 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200810
811 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200812 return err;
813
Willy Tarreaubaaee002006-06-26 02:48:02 +0200814 case SRV_STATUS_INTERNAL:
815 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200816
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817 default:
818 return SRV_STATUS_INTERNAL;
819 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100820}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200821
Willy Tarreaub1d67742010-03-29 19:36:59 +0200822/* If an explicit source binding is specified on the server and/or backend, and
823 * this source makes use of the transparent proxy, then it is extracted now and
824 * assigned to the session's from_addr entry.
825 */
826static void assign_tproxy_address(struct session *s)
827{
828#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
829 if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) {
830 switch (s->srv->state & SRV_TPROXY_MASK) {
831 case SRV_TPROXY_ADDR:
832 s->from_addr = *(struct sockaddr_in *)&s->srv->tproxy_addr;
833 break;
834 case SRV_TPROXY_CLI:
835 case SRV_TPROXY_CIP:
836 /* FIXME: what can we do if the client connects in IPv6 ? */
837 s->from_addr = *(struct sockaddr_in *)&s->cli_addr;
838 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200839 case SRV_TPROXY_DYN:
840 if (s->srv->bind_hdr_occ) {
841 /* bind to the IP in a header */
842 s->from_addr.sin_port = 0;
843 s->from_addr.sin_addr.s_addr = htonl(get_ip_from_hdr2(&s->txn.req,
844 s->srv->bind_hdr_name,
845 s->srv->bind_hdr_len,
846 &s->txn.hdr_idx,
847 s->srv->bind_hdr_occ));
848 }
849 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200850 default:
Willy Tarreaubce70882009-09-07 11:51:47 +0200851 memset(&s->from_addr, 0, sizeof(s->from_addr));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200852 }
853 }
854 else if (s->be->options & PR_O_BIND_SRC) {
855 switch (s->be->options & PR_O_TPXY_MASK) {
856 case PR_O_TPXY_ADDR:
857 s->from_addr = *(struct sockaddr_in *)&s->be->tproxy_addr;
858 break;
859 case PR_O_TPXY_CLI:
860 case PR_O_TPXY_CIP:
861 /* FIXME: what can we do if the client connects in IPv6 ? */
862 s->from_addr = *(struct sockaddr_in *)&s->cli_addr;
863 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200864 case PR_O_TPXY_DYN:
865 if (s->be->bind_hdr_occ) {
866 /* bind to the IP in a header */
867 s->from_addr.sin_port = 0;
868 s->from_addr.sin_addr.s_addr = htonl(get_ip_from_hdr2(&s->txn.req,
869 s->be->bind_hdr_name,
870 s->be->bind_hdr_len,
871 &s->txn.hdr_idx,
872 s->be->bind_hdr_occ));
873 }
874 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200875 default:
Willy Tarreaubce70882009-09-07 11:51:47 +0200876 memset(&s->from_addr, 0, sizeof(s->from_addr));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200877 }
878 }
879#endif
880}
881
882
Willy Tarreaubaaee002006-06-26 02:48:02 +0200883/*
884 * This function initiates a connection to the server assigned to this session
885 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
886 * It can return one of :
887 * - SN_ERR_NONE if everything's OK
888 * - SN_ERR_SRVTO if there are no more servers
889 * - SN_ERR_SRVCL if the connection was refused by the server
890 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
891 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
892 * - SN_ERR_INTERNAL for any other purely internal errors
893 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
894 */
895int connect_server(struct session *s)
896{
Willy Tarreau9650f372009-08-16 14:02:45 +0200897 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200898
899 if (!(s->flags & SN_ADDR_SET)) {
900 err = assign_server_address(s);
901 if (err != SRV_STATUS_OK)
902 return SN_ERR_INTERNAL;
903 }
904
Willy Tarreau9650f372009-08-16 14:02:45 +0200905 if (!s->req->cons->connect)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200906 return SN_ERR_INTERNAL;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200907
Willy Tarreaub1d67742010-03-29 19:36:59 +0200908 assign_tproxy_address(s);
909
Willy Tarreau9650f372009-08-16 14:02:45 +0200910 err = s->req->cons->connect(s->req->cons, s->be, s->srv,
911 (struct sockaddr *)&s->srv_addr,
Willy Tarreaub1d67742010-03-29 19:36:59 +0200912 (struct sockaddr *)&s->from_addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200913
Willy Tarreau9650f372009-08-16 14:02:45 +0200914 if (err != SN_ERR_NONE)
915 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200916
Willy Tarreaubaaee002006-06-26 02:48:02 +0200917 if (s->srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100918 s->flags |= SN_CURR_SESS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200919 s->srv->cur_sess++;
Willy Tarreauac68c5d2009-10-04 23:12:44 +0200920 if (s->srv->cur_sess > s->srv->counters.cur_sess_max)
921 s->srv->counters.cur_sess_max = s->srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100922 if (s->be->lbprm.server_take_conn)
923 s->be->lbprm.server_take_conn(s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924 }
925
Willy Tarreaubaaee002006-06-26 02:48:02 +0200926 return SN_ERR_NONE; /* connection is OK */
927}
928
929
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930/* This function performs the "redispatch" part of a connection attempt. It
931 * will assign a server if required, queue the connection if required, and
932 * handle errors that might arise at this level. It can change the server
933 * state. It will return 1 if it encounters an error, switches the server
934 * state, or has to queue a connection. Otherwise, it will return 0 indicating
935 * that the connection is ready to use.
936 */
937
938int srv_redispatch_connect(struct session *t)
939{
940 int conn_err;
941
942 /* We know that we don't have any connection pending, so we will
943 * try to get a new one, and wait in this state if it's queued
944 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200945 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200946 conn_err = assign_server_and_queue(t);
947 switch (conn_err) {
948 case SRV_STATUS_OK:
949 break;
950
Willy Tarreau7c669d72008-06-20 15:04:11 +0200951 case SRV_STATUS_FULL:
952 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
953 * and we can redispatch to another server, or it is not and we return
954 * 503. This only makes sense in DIRECT mode however, because normal LB
955 * algorithms would never select such a server, and hash algorithms
956 * would bring us on the same server again. Note that t->srv is set in
957 * this case.
958 */
Willy Tarreau4de91492010-01-22 19:10:05 +0100959 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
960 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200961 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
962 t->prev_srv = t->srv;
963 goto redispatch;
964 }
965
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200966 if (!t->req->cons->err_type) {
967 t->req->cons->err_type = SI_ET_QUEUE_ERR;
968 t->req->cons->err_loc = t->srv;
969 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200970
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200971 t->srv->counters.failed_conns++;
972 t->be->counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200973 return 1;
974
Willy Tarreaubaaee002006-06-26 02:48:02 +0200975 case SRV_STATUS_NOSRV:
976 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200977 if (!t->req->cons->err_type) {
978 t->req->cons->err_type = SI_ET_CONN_ERR;
979 t->req->cons->err_loc = NULL;
980 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100981
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200982 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200983 return 1;
984
985 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +0200986 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200987 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200988 /* do nothing else and do not wake any other session up */
989 return 1;
990
Willy Tarreaubaaee002006-06-26 02:48:02 +0200991 case SRV_STATUS_INTERNAL:
992 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200993 if (!t->req->cons->err_type) {
994 t->req->cons->err_type = SI_ET_CONN_OTHER;
995 t->req->cons->err_loc = t->srv;
996 }
997
Willy Tarreaubaaee002006-06-26 02:48:02 +0200998 if (t->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100999 srv_inc_sess_ctr(t->srv);
Willy Tarreau98937b82007-12-10 15:05:42 +01001000 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001001 t->srv->counters.failed_conns++;
1002 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001003
1004 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001005 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001006 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001007 return 1;
1008 }
1009 /* if we get here, it's because we got SRV_STATUS_OK, which also
1010 * means that the connection has not been queued.
1011 */
1012 return 0;
1013}
1014
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001015/* Apply RDP cookie persistence to the current session. For this, the function
1016 * tries to extract an RDP cookie from the request buffer, and look for the
1017 * matching server in the list. If the server is found, it is assigned to the
1018 * session. This always returns 1, and the analyser removes itself from the
1019 * list. Nothing is performed if a server was already assigned.
1020 */
1021int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1022{
1023 struct proxy *px = s->be;
1024 int ret;
1025 struct acl_expr expr;
1026 struct acl_test test;
1027 struct server *srv = px->srv;
1028 struct sockaddr_in addr;
1029 char *p;
1030
1031 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1032 now_ms, __FUNCTION__,
1033 s,
1034 req,
1035 req->rex, req->wex,
1036 req->flags,
1037 req->l,
1038 req->analysers);
1039
1040 if (s->flags & SN_ASSIGNED)
1041 goto no_cookie;
1042
1043 memset(&expr, 0, sizeof(expr));
1044 memset(&test, 0, sizeof(test));
1045
1046 expr.arg.str = s->be->rdp_cookie_name;
1047 expr.arg_len = s->be->rdp_cookie_len;
1048
1049 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
1050 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
1051 goto no_cookie;
1052
1053 memset(&addr, 0, sizeof(addr));
1054 addr.sin_family = AF_INET;
1055
1056 /* Considering an rdp cookie detected using acl, test.ptr ended with <cr><lf> and should return */
1057 addr.sin_addr.s_addr = strtoul(test.ptr, &p, 10);
1058 if (*p != '.')
1059 goto no_cookie;
1060 p++;
1061 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1062 if (*p != '.')
1063 goto no_cookie;
1064
1065 while (srv) {
1066 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1067 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1068 /* we found the server and it is usable */
1069 s->flags |= SN_DIRECT | SN_ASSIGNED;
1070 s->srv = srv;
1071 break;
1072 }
1073 }
1074 srv = srv->next;
1075 }
1076
1077no_cookie:
1078 req->analysers &= ~an_bit;
1079 req->analyse_exp = TICK_ETERNITY;
1080 return 1;
1081}
1082
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001083int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001084 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001085 return px->down_time;
1086
1087 return now.tv_sec - px->last_change + px->down_time;
1088}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001089
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001090/*
1091 * This function returns a string containing the balancing
1092 * mode of the proxy in a format suitable for stats.
1093 */
1094
1095const char *backend_lb_algo_str(int algo) {
1096
1097 if (algo == BE_LB_ALGO_RR)
1098 return "roundrobin";
1099 else if (algo == BE_LB_ALGO_SRR)
1100 return "static-rr";
1101 else if (algo == BE_LB_ALGO_LC)
1102 return "leastconn";
1103 else if (algo == BE_LB_ALGO_SH)
1104 return "source";
1105 else if (algo == BE_LB_ALGO_UH)
1106 return "uri";
1107 else if (algo == BE_LB_ALGO_PH)
1108 return "url_param";
1109 else if (algo == BE_LB_ALGO_HH)
1110 return "hdr";
1111 else if (algo == BE_LB_ALGO_RCH)
1112 return "rdp-cookie";
1113 else
1114 return NULL;
1115}
1116
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001117/* This function parses a "balance" statement in a backend section describing
1118 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1119 * returns -1, it may write an error message into ther <err> buffer, for at
1120 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1121 * written. The function must be called with <args> pointing to the first word
1122 * after "balance".
1123 */
1124int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1125{
1126 if (!*(args[0])) {
1127 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001128 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1129 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001130 return 0;
1131 }
1132
1133 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001134 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1135 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001136 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001137 else if (!strcmp(args[0], "static-rr")) {
1138 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1139 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1140 }
Willy Tarreau51406232008-03-10 22:04:20 +01001141 else if (!strcmp(args[0], "leastconn")) {
1142 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1143 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1144 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001145 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001146 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1147 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001148 }
1149 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001150 int arg = 1;
1151
Willy Tarreau31682232007-11-29 15:38:04 +01001152 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1153 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001154
1155 while (*args[arg]) {
1156 if (!strcmp(args[arg], "len")) {
1157 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1158 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1159 return -1;
1160 }
1161 curproxy->uri_len_limit = atoi(args[arg+1]);
1162 arg += 2;
1163 }
1164 else if (!strcmp(args[arg], "depth")) {
1165 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1166 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1167 return -1;
1168 }
1169 /* hint: we store the position of the ending '/' (depth+1) so
1170 * that we avoid a comparison while computing the hash.
1171 */
1172 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1173 arg += 2;
1174 }
1175 else {
1176 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1177 return -1;
1178 }
1179 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001180 }
Willy Tarreau01732802007-11-01 22:48:15 +01001181 else if (!strcmp(args[0], "url_param")) {
1182 if (!*args[1]) {
1183 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1184 return -1;
1185 }
Willy Tarreau31682232007-11-29 15:38:04 +01001186 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1187 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001188
1189 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001190 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001191 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001192 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001193 if (strcmp(args[2], "check_post")) {
1194 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1195 return -1;
1196 }
1197 if (*args[3]) {
1198 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1199 curproxy->url_param_post_limit = str2ui(args[3]);
1200 }
1201 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1202 if (!curproxy->url_param_post_limit)
1203 curproxy->url_param_post_limit = 48;
1204 else if ( curproxy->url_param_post_limit < 3 )
1205 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1206 }
Benoitaffb4812009-03-25 13:02:10 +01001207 }
1208 else if (!strncmp(args[0], "hdr(", 4)) {
1209 const char *beg, *end;
1210
1211 beg = args[0] + 4;
1212 end = strchr(beg, ')');
1213
1214 if (!end || end == beg) {
1215 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1216 return -1;
1217 }
1218
1219 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1220 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1221
1222 free(curproxy->hh_name);
1223 curproxy->hh_len = end - beg;
1224 curproxy->hh_name = my_strndup(beg, end - beg);
1225 curproxy->hh_match_domain = 0;
1226
1227 if (*args[1]) {
1228 if (strcmp(args[1], "use_domain_only")) {
1229 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1230 return -1;
1231 }
1232 curproxy->hh_match_domain = 1;
1233 }
1234
Emeric Brun736aa232009-06-30 17:56:00 +02001235 }
1236 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1237 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1238 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1239
1240 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1241 const char *beg, *end;
1242
1243 beg = args[0] + 11;
1244 end = strchr(beg, ')');
1245
1246 if (!end || end == beg) {
1247 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1248 return -1;
1249 }
1250
1251 free(curproxy->hh_name);
1252 curproxy->hh_name = my_strndup(beg, end - beg);
1253 curproxy->hh_len = end - beg;
1254 }
1255 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1256 free(curproxy->hh_name);
1257 curproxy->hh_name = strdup("mstshash");
1258 curproxy->hh_len = strlen(curproxy->hh_name);
1259 }
1260 else { /* syntax */
1261 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1262 return -1;
1263 }
Willy Tarreau01732802007-11-01 22:48:15 +01001264 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001265 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001266 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 +01001267 return -1;
1268 }
1269 return 0;
1270}
1271
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001272
1273/************************************************************************/
1274/* All supported keywords must be declared here. */
1275/************************************************************************/
1276
1277/* set test->i to the number of enabled servers on the proxy */
1278static int
1279acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1280 struct acl_expr *expr, struct acl_test *test)
1281{
1282 test->flags = ACL_TEST_F_VOL_TEST;
1283 if (expr->arg_len) {
1284 /* another proxy was designated, we must look for it */
1285 for (px = proxy; px; px = px->next)
1286 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1287 break;
1288 }
1289 if (!px)
1290 return 0;
1291
1292 if (px->srv_act)
1293 test->i = px->srv_act;
1294 else if (px->lbprm.fbck)
1295 test->i = 1;
1296 else
1297 test->i = px->srv_bck;
1298
1299 return 1;
1300}
1301
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001302/* report in test->flags a success or failure depending on the designated
1303 * server's state. There is no match function involved since there's no pattern.
1304 */
1305static int
1306acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, int dir,
1307 struct acl_expr *expr, struct acl_test *test)
1308{
1309 struct server *srv = expr->arg.srv;
1310
1311 test->flags = ACL_TEST_F_VOL_TEST;
1312 if (!(srv->state & SRV_MAINTAIN) &&
1313 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
1314 test->flags |= ACL_TEST_F_SET_RES_PASS;
1315 else
1316 test->flags |= ACL_TEST_F_SET_RES_FAIL;
1317 return 1;
1318}
1319
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001320/* set test->i to the number of enabled servers on the proxy */
1321static int
1322acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1323 struct acl_expr *expr, struct acl_test *test)
1324{
1325 struct server *iterator;
1326 test->flags = ACL_TEST_F_VOL_TEST;
1327 if (expr->arg_len) {
1328 /* another proxy was designated, we must look for it */
1329 for (px = proxy; px; px = px->next)
1330 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1331 break;
1332 }
1333 if (!px)
1334 return 0;
1335
1336 test->i = 0;
1337 iterator = px->srv;
1338 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001339 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001340 iterator = iterator->next;
1341 continue;
1342 }
1343 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
1344 test->i = -1;
1345 return 1;
1346 }
1347
1348 test->i += (iterator->maxconn - iterator->cur_sess)
1349 + (iterator->maxqueue - iterator->nbpend);
1350 iterator = iterator->next;
1351 }
1352
1353 return 1;
1354}
1355
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001356/* set test->i to the number of connections per second reaching the frontend */
1357static int
1358acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1359 struct acl_expr *expr, struct acl_test *test)
1360{
1361 test->flags = ACL_TEST_F_VOL_TEST;
1362 if (expr->arg_len) {
1363 /* another proxy was designated, we must look for it */
1364 for (px = proxy; px; px = px->next)
1365 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
1366 break;
1367 }
1368 if (!px)
1369 return 0;
1370
1371 test->i = read_freq_ctr(&px->fe_sess_per_sec);
1372 return 1;
1373}
1374
1375/* set test->i to the number of connections per second reaching the backend */
1376static int
1377acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1378 struct acl_expr *expr, struct acl_test *test)
1379{
1380 test->flags = ACL_TEST_F_VOL_TEST;
1381 if (expr->arg_len) {
1382 /* another proxy was designated, we must look for it */
1383 for (px = proxy; px; px = px->next)
1384 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1385 break;
1386 }
1387 if (!px)
1388 return 0;
1389
1390 test->i = read_freq_ctr(&px->be_sess_per_sec);
1391 return 1;
1392}
1393
Willy Tarreaua36af912009-10-10 12:02:45 +02001394/* set test->i to the number of concurrent connections on the frontend */
1395static int
1396acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1397 struct acl_expr *expr, struct acl_test *test)
1398{
1399 test->flags = ACL_TEST_F_VOL_TEST;
1400 if (expr->arg_len) {
1401 /* another proxy was designated, we must look for it */
1402 for (px = proxy; px; px = px->next)
1403 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
1404 break;
1405 }
1406 if (!px)
1407 return 0;
1408
1409 test->i = px->feconn;
1410 return 1;
1411}
1412
1413/* set test->i to the number of concurrent connections on the backend */
1414static int
1415acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1416 struct acl_expr *expr, struct acl_test *test)
1417{
1418 test->flags = ACL_TEST_F_VOL_TEST;
1419 if (expr->arg_len) {
1420 /* another proxy was designated, we must look for it */
1421 for (px = proxy; px; px = px->next)
1422 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1423 break;
1424 }
1425 if (!px)
1426 return 0;
1427
1428 test->i = px->beconn;
1429 return 1;
1430}
1431
1432/* set test->i to the total number of queued connections on the backend */
1433static int
1434acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1435 struct acl_expr *expr, struct acl_test *test)
1436{
1437 test->flags = ACL_TEST_F_VOL_TEST;
1438 if (expr->arg_len) {
1439 /* another proxy was designated, we must look for it */
1440 for (px = proxy; px; px = px->next)
1441 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1442 break;
1443 }
1444 if (!px)
1445 return 0;
1446
1447 test->i = px->totpend;
1448 return 1;
1449}
1450
1451/* set test->i to the total number of queued connections on the backend divided
1452 * by the number of running servers and rounded up. If there is no running
1453 * server, we return twice the total, just as if we had half a running server.
1454 * This is more or less correct anyway, since we expect the last server to come
1455 * back soon.
1456 */
1457static int
1458acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1459 struct acl_expr *expr, struct acl_test *test)
1460{
1461 int nbsrv;
1462
1463 test->flags = ACL_TEST_F_VOL_TEST;
1464 if (expr->arg_len) {
1465 /* another proxy was designated, we must look for it */
1466 for (px = proxy; px; px = px->next)
1467 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1468 break;
1469 }
1470 if (!px)
1471 return 0;
1472
1473 if (px->srv_act)
1474 nbsrv = px->srv_act;
1475 else if (px->lbprm.fbck)
1476 nbsrv = 1;
1477 else
1478 nbsrv = px->srv_bck;
1479
1480 if (nbsrv > 0)
1481 test->i = (px->totpend + nbsrv - 1) / nbsrv;
1482 else
1483 test->i = px->totpend * 2;
1484
1485 return 1;
1486}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001487
1488/* Note: must not be declared <const> as its list will be overwritten */
1489static struct acl_kw_list acl_kws = {{ },{
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001490 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau3a8efeb2009-03-05 19:15:37 +01001491 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001492 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
1493 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua36af912009-10-10 12:02:45 +02001494 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
1495 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1496 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1497 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001498 { "srv_is_up", acl_parse_nothing, acl_fetch_srv_is_up, acl_match_nothing, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001499 { NULL, NULL, NULL, NULL },
1500}};
1501
1502
1503__attribute__((constructor))
1504static void __backend_init(void)
1505{
1506 acl_register_keywords(&acl_kws);
1507}
1508
Willy Tarreaubaaee002006-06-26 02:48:02 +02001509/*
1510 * Local variables:
1511 * c-indent-level: 8
1512 * c-basic-offset: 8
1513 * End:
1514 */