blob: 1fc6b2aaadda0e0ed8e3048eaaea04a2a2f9a5f7 [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 Tarreau03fa5df2010-05-24 21:02:37 +020032#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020033#include <proto/lb_chash.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020034#include <proto/lb_fwlc.h>
35#include <proto/lb_fwrr.h>
36#include <proto/lb_map.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020038#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010040#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020041#include <proto/session.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020042#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <proto/task.h>
44
Willy Tarreaubaaee002006-06-26 02:48:02 +020045/*
46 * This function recounts the number of usable active and backup servers for
47 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010048 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010049 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010050 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020051 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020052void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020053{
54 struct server *srv;
55
Willy Tarreau20697042007-11-15 23:26:18 +010056 px->srv_act = px->srv_bck = 0;
57 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010058 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020059 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010060 if (!srv_is_usable(srv->state, srv->eweight))
61 continue;
62
63 if (srv->state & SRV_BACKUP) {
64 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010065 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010066 px->lbprm.fbck = srv;
67 px->srv_bck++;
68 px->lbprm.tot_wbck += srv->eweight;
69 } else {
70 px->srv_act++;
71 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020072 }
73 }
Willy Tarreaub625a082007-11-26 01:15:43 +010074}
Willy Tarreau20697042007-11-15 23:26:18 +010075
Willy Tarreaub625a082007-11-26 01:15:43 +010076/* This function simply updates the backend's tot_weight and tot_used values
77 * after servers weights have been updated. It is designed to be used after
78 * recount_servers() or equivalent.
79 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020080void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010081{
Willy Tarreau20697042007-11-15 23:26:18 +010082 if (px->srv_act) {
83 px->lbprm.tot_weight = px->lbprm.tot_wact;
84 px->lbprm.tot_used = px->srv_act;
85 }
Willy Tarreaub625a082007-11-26 01:15:43 +010086 else if (px->lbprm.fbck) {
87 /* use only the first backup server */
88 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
89 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010090 }
91 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010092 px->lbprm.tot_weight = px->lbprm.tot_wbck;
93 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +010094 }
Willy Tarreaub625a082007-11-26 01:15:43 +010095}
96
Willy Tarreau39c9ba72009-10-01 21:11:15 +020097/*
98 * This function tries to find a running server for the proxy <px> following
99 * the source hash method. Depending on the number of active/backup servers,
100 * it will either look for active servers, or for backup servers.
101 * If any server is found, it will be returned. If no valid server is found,
102 * NULL is returned.
103 */
104struct server *get_server_sh(struct proxy *px, const char *addr, int len)
105{
106 unsigned int h, l;
107
108 if (px->lbprm.tot_weight == 0)
109 return NULL;
110
111 l = h = 0;
112
113 /* note: we won't hash if there's only one server left */
114 if (px->lbprm.tot_used == 1)
115 goto hash_done;
116
117 while ((l + sizeof (int)) <= len) {
118 h ^= ntohl(*(unsigned int *)(&addr[l]));
119 l += sizeof (int);
120 }
121 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200122 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
123 return chash_get_server_hash(px, h);
124 else
125 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200126}
127
128/*
129 * This function tries to find a running server for the proxy <px> following
130 * the URI hash method. In order to optimize cache hits, the hash computation
131 * ends at the question mark. Depending on the number of active/backup servers,
132 * it will either look for active servers, or for backup servers.
133 * If any server is found, it will be returned. If no valid server is found,
134 * NULL is returned.
135 *
136 * This code was contributed by Guillaume Dallaire, who also selected this hash
137 * algorithm out of a tens because it gave him the best results.
138 *
139 */
140struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
141{
142 unsigned long hash = 0;
143 int c;
144 int slashes = 0;
145
146 if (px->lbprm.tot_weight == 0)
147 return NULL;
148
149 /* note: we won't hash if there's only one server left */
150 if (px->lbprm.tot_used == 1)
151 goto hash_done;
152
153 if (px->uri_len_limit)
154 uri_len = MIN(uri_len, px->uri_len_limit);
155
156 while (uri_len--) {
157 c = *uri++;
158 if (c == '/') {
159 slashes++;
160 if (slashes == px->uri_dirs_depth1) /* depth+1 */
161 break;
162 }
163 else if (c == '?')
164 break;
165
166 hash = c + (hash << 6) + (hash << 16) - hash;
167 }
168 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200169 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
170 return chash_get_server_hash(px, hash);
171 else
172 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200173}
174
Willy Tarreau01732802007-11-01 22:48:15 +0100175/*
176 * This function tries to find a running server for the proxy <px> following
177 * the URL parameter hash method. It looks for a specific parameter in the
178 * URL and hashes it to compute the server ID. This is useful to optimize
179 * performance by avoiding bounces between servers in contexts where sessions
180 * are shared but cookies are not usable. If the parameter is not found, NULL
181 * is returned. If any server is found, it will be returned. If no valid server
182 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100183 */
184struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
185{
186 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200187 const char *p;
188 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100189 int plen;
190
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200191 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100192 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100193 return NULL;
194
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200195 if ((p = memchr(uri, '?', uri_len)) == NULL)
196 return NULL;
197
Willy Tarreau01732802007-11-01 22:48:15 +0100198 p++;
199
200 uri_len -= (p - uri);
201 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200202 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100203
204 while (uri_len > plen) {
205 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200206 if (params[plen] == '=') {
207 if (memcmp(params, px->url_param_name, plen) == 0) {
208 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100209 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200210 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100211 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200212 p += plen + 1;
213 uri_len -= plen + 1;
214
Willy Tarreau01732802007-11-01 22:48:15 +0100215 while (uri_len && *p != '&') {
216 hash = *p + (hash << 6) + (hash << 16) - hash;
217 uri_len--;
218 p++;
219 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200220 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
221 return chash_get_server_hash(px, hash);
222 else
223 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100224 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200225 }
226 /* skip to next parameter */
227 p = memchr(params, '&', uri_len);
228 if (!p)
229 return NULL;
230 p++;
231 uri_len -= (p - params);
232 params = p;
233 }
234 return NULL;
235}
236
237/*
238 * this does the same as the previous server_ph, but check the body contents
239 */
240struct server *get_server_ph_post(struct session *s)
241{
242 unsigned long hash = 0;
243 struct http_txn *txn = &s->txn;
244 struct buffer *req = s->req;
245 struct http_msg *msg = &txn->req;
246 struct proxy *px = s->be;
247 unsigned int plen = px->url_param_len;
Willy Tarreau157dd632009-12-06 19:18:09 +0100248 unsigned long len = msg->hdr_content_len;
249 const char *params = req->data + msg->sov;
250 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200251
Willy Tarreau7c96f672009-12-27 22:47:25 +0100252 if (len > req->l - (msg->sov - msg->som))
253 len = req->l - (msg->sov - msg->som);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200254
Willy Tarreau157dd632009-12-06 19:18:09 +0100255 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200256 return NULL;
257
Willy Tarreau157dd632009-12-06 19:18:09 +0100258 if (px->lbprm.tot_weight == 0)
259 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200260
261 while (len > plen) {
262 /* Look for the parameter name followed by an equal symbol */
263 if (params[plen] == '=') {
264 if (memcmp(params, px->url_param_name, plen) == 0) {
265 /* OK, we have the parameter here at <params>, and
266 * the value after the equal sign, at <p>
267 * skip the equal symbol
268 */
269 p += plen + 1;
270 len -= plen + 1;
271
272 while (len && *p != '&') {
273 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100274 /* if in a POST, body must be URI encoded or it's not a URI.
275 * Do not interprete any possible binary data as a parameter.
276 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200277 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
278 break;
279 return NULL; /* oh, no; this is not uri-encoded.
280 * This body does not contain parameters.
281 */
282 }
283 hash = *p + (hash << 6) + (hash << 16) - hash;
284 len--;
285 p++;
286 /* should we break if vlen exceeds limit? */
287 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200288 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
289 return chash_get_server_hash(px, hash);
290 else
291 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200292 }
293 }
Willy Tarreau01732802007-11-01 22:48:15 +0100294 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200295 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100296 if (!p)
297 return NULL;
298 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200299 len -= (p - params);
300 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100301 }
302 return NULL;
303}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200304
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200305
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306/*
Benoitaffb4812009-03-25 13:02:10 +0100307 * This function tries to find a running server for the proxy <px> following
308 * the Header parameter hash method. It looks for a specific parameter in the
309 * URL and hashes it to compute the server ID. This is useful to optimize
310 * performance by avoiding bounces between servers in contexts where sessions
311 * are shared but cookies are not usable. If the parameter is not found, NULL
312 * is returned. If any server is found, it will be returned. If no valid server
313 * is found, NULL is returned.
314 */
315struct server *get_server_hh(struct session *s)
316{
317 unsigned long hash = 0;
318 struct http_txn *txn = &s->txn;
319 struct http_msg *msg = &txn->req;
320 struct proxy *px = s->be;
321 unsigned int plen = px->hh_len;
322 unsigned long len;
323 struct hdr_ctx ctx;
324 const char *p;
325
326 /* tot_weight appears to mean srv_count */
327 if (px->lbprm.tot_weight == 0)
328 return NULL;
329
Benoitaffb4812009-03-25 13:02:10 +0100330 ctx.idx = 0;
331
332 /* if the message is chunked, we skip the chunk size, but use the value as len */
333 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
334
335 /* if the header is not found or empty, let's fallback to round robin */
336 if (!ctx.idx || !ctx.vlen)
337 return NULL;
338
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200339 /* note: we won't hash if there's only one server left */
340 if (px->lbprm.tot_used == 1)
341 goto hash_done;
342
Benoitaffb4812009-03-25 13:02:10 +0100343 /* Found a the hh_name in the headers.
344 * we will compute the hash based on this value ctx.val.
345 */
346 len = ctx.vlen;
347 p = (char *)ctx.line + ctx.val;
348 if (!px->hh_match_domain) {
349 while (len) {
350 hash = *p + (hash << 6) + (hash << 16) - hash;
351 len--;
352 p++;
353 }
354 } else {
355 int dohash = 0;
356 p += len - 1;
357 /* special computation, use only main domain name, not tld/host
358 * going back from the end of string, start hashing at first
359 * dot stop at next.
360 * This is designed to work with the 'Host' header, and requires
361 * a special option to activate this.
362 */
363 while (len) {
364 if (*p == '.') {
365 if (!dohash)
366 dohash = 1;
367 else
368 break;
369 } else {
370 if (dohash)
371 hash = *p + (hash << 6) + (hash << 16) - hash;
372 }
373 len--;
374 p--;
375 }
376 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200377 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200378 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
379 return chash_get_server_hash(px, hash);
380 else
381 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100382}
383
Emeric Brun736aa232009-06-30 17:56:00 +0200384struct server *get_server_rch(struct session *s)
385{
386 unsigned long hash = 0;
387 struct proxy *px = s->be;
388 unsigned long len;
389 const char *p;
390 int ret;
391 struct acl_expr expr;
392 struct acl_test test;
393
394 /* tot_weight appears to mean srv_count */
395 if (px->lbprm.tot_weight == 0)
396 return NULL;
397
Emeric Brun736aa232009-06-30 17:56:00 +0200398 memset(&expr, 0, sizeof(expr));
399 memset(&test, 0, sizeof(test));
400
401 expr.arg.str = px->hh_name;
402 expr.arg_len = px->hh_len;
403
404 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
405 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
406 return NULL;
407
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200408 /* note: we won't hash if there's only one server left */
409 if (px->lbprm.tot_used == 1)
410 goto hash_done;
411
Emeric Brun736aa232009-06-30 17:56:00 +0200412 /* Found a the hh_name in the headers.
413 * we will compute the hash based on this value ctx.val.
414 */
415 len = test.len;
416 p = (char *)test.ptr;
417 while (len) {
418 hash = *p + (hash << 6) + (hash << 16) - hash;
419 len--;
420 p++;
421 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200422 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200423 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
424 return chash_get_server_hash(px, hash);
425 else
426 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200427}
Benoitaffb4812009-03-25 13:02:10 +0100428
429/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200430 * This function applies the load-balancing algorithm to the session, as
431 * defined by the backend it is assigned to. The session is then marked as
432 * 'assigned'.
433 *
434 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
435 * had a server previously assigned, it is rebalanced, trying to avoid the same
436 * server.
437 * The function tries to keep the original connection slot if it reconnects to
438 * the same server, otherwise it releases it and tries to offer it.
439 *
440 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200441 *
442 * It may return :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200443 * SRV_STATUS_OK if everything is OK. Session assigned to ->srv
444 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
445 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200446 * SRV_STATUS_INTERNAL for other unrecoverable errors.
447 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200448 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
449 * it does not need to be called anymore. This means that s->srv can be trusted
450 * in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200451 *
452 */
453
454int assign_server(struct session *s)
455{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100456
Willy Tarreau7c669d72008-06-20 15:04:11 +0200457 struct server *conn_slot;
458 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100459
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460#ifdef DEBUG_FULL
461 fprintf(stderr,"assign_server : s=%p\n",s);
462#endif
463
Willy Tarreau7c669d72008-06-20 15:04:11 +0200464 err = SRV_STATUS_INTERNAL;
465 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
466 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100467
Willy Tarreau7c669d72008-06-20 15:04:11 +0200468 s->prev_srv = s->prev_srv;
469 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200470
Willy Tarreau7c669d72008-06-20 15:04:11 +0200471 /* We have to release any connection slot before applying any LB algo,
472 * otherwise we may erroneously end up with no available slot.
473 */
474 if (conn_slot)
475 sess_change_server(s, NULL);
476
477 /* We will now try to find the good server and store it into <s->srv>.
478 * Note that <s->srv> may be NULL in case of dispatch or proxy mode,
479 * as well as if no server is available (check error code).
480 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100481
Willy Tarreau7c669d72008-06-20 15:04:11 +0200482 s->srv = NULL;
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200483 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200484 int len;
485 /* we must check if we have at least one server available */
486 if (!s->be->lbprm.tot_weight) {
487 err = SRV_STATUS_NOSRV;
488 goto out;
489 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200490
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200491 /* First check whether we need to fetch some data or simply call
492 * the LB lookup function. Only the hashing functions will need
493 * some input data in fact, and will support multiple algorithms.
494 */
495 switch (s->be->lbprm.algo & BE_LB_LKUP) {
496 case BE_LB_LKUP_RRTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200497 s->srv = fwrr_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200498 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200499
500 case BE_LB_LKUP_LCTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200501 s->srv = fwlc_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200502 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200503
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200504 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200505 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200506 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200507 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
508 s->srv = chash_get_next_server(s->be, s->prev_srv);
509 else
510 s->srv = map_get_server_rr(s->be, s->prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200511 break;
512 }
513 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200514 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200515 err = SRV_STATUS_INTERNAL;
516 goto out;
517 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200518
519 switch (s->be->lbprm.algo & BE_LB_PARM) {
520 case BE_LB_HASH_SRC:
521 if (s->cli_addr.ss_family == AF_INET)
522 len = 4;
523 else if (s->cli_addr.ss_family == AF_INET6)
524 len = 16;
525 else {
526 /* unknown IP family */
527 err = SRV_STATUS_INTERNAL;
528 goto out;
529 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200530
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200531 s->srv = get_server_sh(s->be,
532 (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
533 len);
534 break;
535
536 case BE_LB_HASH_URI:
537 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100538 if (s->txn.req.msg_state < HTTP_MSG_BODY)
539 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200540 s->srv = get_server_uh(s->be,
Willy Tarreau962c3f42010-01-10 00:15:35 +0100541 s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200542 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200543 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200544
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200545 case BE_LB_HASH_PRM:
546 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100547 if (s->txn.req.msg_state < HTTP_MSG_BODY)
548 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200549 if (s->txn.meth == HTTP_METH_POST &&
Willy Tarreau962c3f42010-01-10 00:15:35 +0100550 memchr(s->txn.req.sol + s->txn.req.sl.rq.u, '&',
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200551 s->txn.req.sl.rq.u_l ) == NULL)
552 s->srv = get_server_ph_post(s);
553 else
554 s->srv = get_server_ph(s->be,
Willy Tarreau962c3f42010-01-10 00:15:35 +0100555 s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200556 s->txn.req.sl.rq.u_l);
557 break;
Benoitaffb4812009-03-25 13:02:10 +0100558
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200559 case BE_LB_HASH_HDR:
560 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100561 if (s->txn.req.msg_state < HTTP_MSG_BODY)
562 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200563 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 */
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200580 if (!s->srv) {
581 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
582 s->srv = chash_get_next_server(s->be, s->prev_srv);
583 else
584 s->srv = map_get_server_rr(s->be, s->prev_srv);
585 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200586
587 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200588 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200589
Willy Tarreau7c669d72008-06-20 15:04:11 +0200590 default:
591 /* unknown balancing algorithm */
592 err = SRV_STATUS_INTERNAL;
593 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200594 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200595
596 if (!s->srv) {
597 err = SRV_STATUS_FULL;
598 goto out;
599 }
600 else if (s->srv != s->prev_srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200601 s->be->counters.cum_lbconn++;
602 s->srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100603 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200604 }
605 else if (s->be->options & PR_O_HTTP_PROXY) {
606 if (!s->srv_addr.sin_addr.s_addr) {
607 err = SRV_STATUS_NOSRV;
608 goto out;
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100609 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200610 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200611 else if (!*(int *)&s->be->dispatch_addr.sin_addr &&
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100612 !(s->be->options & PR_O_TRANSP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200613 err = SRV_STATUS_NOSRV;
614 goto out;
615 }
616
617 s->flags |= SN_ASSIGNED;
618 err = SRV_STATUS_OK;
619 out:
620
621 /* Either we take back our connection slot, or we offer it to someone
622 * else if we don't need it anymore.
623 */
624 if (conn_slot) {
625 if (conn_slot == s->srv) {
626 sess_change_server(s, s->srv);
627 } else {
628 if (may_dequeue_tasks(conn_slot, s->be))
629 process_srv_queue(conn_slot);
630 }
631 }
632
633 out_err:
634 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635}
636
637
638/*
639 * This function assigns a server address to a session, and sets SN_ADDR_SET.
640 * The address is taken from the currently assigned server, or from the
641 * dispatch or transparent address.
642 *
643 * It may return :
644 * SRV_STATUS_OK if everything is OK.
645 * SRV_STATUS_INTERNAL for other unrecoverable errors.
646 *
647 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
648 * not cleared, so it's to the caller to clear it if required.
649 *
650 */
651int assign_server_address(struct session *s)
652{
653#ifdef DEBUG_FULL
654 fprintf(stderr,"assign_server_address : s=%p\n",s);
655#endif
656
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200657 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200658 /* A server is necessarily known for this session */
659 if (!(s->flags & SN_ASSIGNED))
660 return SRV_STATUS_INTERNAL;
661
662 s->srv_addr = s->srv->addr;
663
664 /* if this server remaps proxied ports, we'll use
665 * the port the client connected to with an offset. */
666 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100667 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200668 get_frt_addr(s);
669 if (s->frt_addr.ss_family == AF_INET) {
670 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
671 ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port));
672 } else {
673 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
674 ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port));
675 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676 }
677 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200678 else if (*(int *)&s->be->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679 /* connect to the defined dispatch addr */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200680 s->srv_addr = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200681 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100682 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200683 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100684 if (!(s->flags & SN_FRT_ADDR_SET))
685 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200686
Willy Tarreaubd414282008-01-19 13:46:35 +0100687 memcpy(&s->srv_addr, &s->frt_addr, MIN(sizeof(s->srv_addr), sizeof(s->frt_addr)));
688 /* when we support IPv6 on the backend, we may add other tests */
689 //qfprintf(stderr, "Cannot get original server address.\n");
690 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200691 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100692 else if (s->be->options & PR_O_HTTP_PROXY) {
693 /* If HTTP PROXY option is set, then server is already assigned
694 * during incoming client request parsing. */
695 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100696 else {
697 /* no server and no LB algorithm ! */
698 return SRV_STATUS_INTERNAL;
699 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700
701 s->flags |= SN_ADDR_SET;
702 return SRV_STATUS_OK;
703}
704
705
706/* This function assigns a server to session <s> if required, and can add the
707 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200708 * If ->srv_conn is set, the session is first released from the server.
709 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
710 * be called before any connection and after any retry or redispatch occurs.
711 *
712 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200713 *
714 * Returns :
715 *
716 * SRV_STATUS_OK if everything is OK.
717 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
718 * SRV_STATUS_QUEUED if the connection has been queued.
719 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200720 * connection could not be queued in s->srv,
721 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200722 * SRV_STATUS_INTERNAL for other unrecoverable errors.
723 *
724 */
725int assign_server_and_queue(struct session *s)
726{
727 struct pendconn *p;
728 int err;
729
730 if (s->pend_pos)
731 return SRV_STATUS_INTERNAL;
732
Willy Tarreau7c669d72008-06-20 15:04:11 +0200733 err = SRV_STATUS_OK;
734 if (!(s->flags & SN_ASSIGNED)) {
735 err = assign_server(s);
736 if (s->prev_srv) {
737 /* This session was previously assigned to a server. We have to
738 * update the session's and the server's stats :
739 * - if the server changed :
740 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
741 * - set SN_REDISP if it was successfully redispatched
742 * - increment srv->redispatches and be->redispatches
743 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100744 */
745
Willy Tarreau7c669d72008-06-20 15:04:11 +0200746 if (s->prev_srv != s->srv) {
747 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
748 s->txn.flags &= ~TX_CK_MASK;
749 s->txn.flags |= TX_CK_DOWN;
750 }
751 s->flags |= SN_REDISP;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200752 s->prev_srv->counters.redispatches++;
753 s->be->counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200754 } else {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200755 s->prev_srv->counters.retries++;
756 s->be->counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100757 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100758 }
759 }
760
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761 switch (err) {
762 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200763 /* we have SN_ASSIGNED set */
764 if (!s->srv)
765 return SRV_STATUS_OK; /* dispatch or proxy mode */
766
767 /* If we already have a connection slot, no need to check any queue */
768 if (s->srv_conn == s->srv)
769 return SRV_STATUS_OK;
770
771 /* OK, this session already has an assigned server, but no
772 * connection slot yet. Either it is a redispatch, or it was
773 * assigned from persistence information (direct mode).
774 */
775 if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) {
776 /* server scheduled for redirection, and already assigned. We
777 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100778 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200779 sess_change_server(s, s->srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100780 return SRV_STATUS_OK;
781 }
782
Willy Tarreau7c669d72008-06-20 15:04:11 +0200783 /* We might have to queue this session if the assigned server is full.
784 * We know we have to queue it into the server's queue, so if a maxqueue
785 * is set on the server, we must also check that the server's queue is
786 * not full, in which case we have to return FULL.
787 */
788 if (s->srv->maxconn &&
789 (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) {
790
791 if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue)
792 return SRV_STATUS_FULL;
793
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794 p = pendconn_add(s);
795 if (p)
796 return SRV_STATUS_QUEUED;
797 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200798 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200800
801 /* OK, we can use this server. Let's reserve our place */
802 sess_change_server(s, s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803 return SRV_STATUS_OK;
804
805 case SRV_STATUS_FULL:
806 /* queue this session into the proxy's queue */
807 p = pendconn_add(s);
808 if (p)
809 return SRV_STATUS_QUEUED;
810 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200811 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812
813 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200814 return err;
815
Willy Tarreaubaaee002006-06-26 02:48:02 +0200816 case SRV_STATUS_INTERNAL:
817 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200818
Willy Tarreaubaaee002006-06-26 02:48:02 +0200819 default:
820 return SRV_STATUS_INTERNAL;
821 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100822}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200823
Willy Tarreaub1d67742010-03-29 19:36:59 +0200824/* If an explicit source binding is specified on the server and/or backend, and
825 * this source makes use of the transparent proxy, then it is extracted now and
826 * assigned to the session's from_addr entry.
827 */
828static void assign_tproxy_address(struct session *s)
829{
830#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
831 if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) {
832 switch (s->srv->state & SRV_TPROXY_MASK) {
833 case SRV_TPROXY_ADDR:
834 s->from_addr = *(struct sockaddr_in *)&s->srv->tproxy_addr;
835 break;
836 case SRV_TPROXY_CLI:
837 case SRV_TPROXY_CIP:
838 /* FIXME: what can we do if the client connects in IPv6 ? */
839 s->from_addr = *(struct sockaddr_in *)&s->cli_addr;
840 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200841 case SRV_TPROXY_DYN:
842 if (s->srv->bind_hdr_occ) {
843 /* bind to the IP in a header */
844 s->from_addr.sin_port = 0;
845 s->from_addr.sin_addr.s_addr = htonl(get_ip_from_hdr2(&s->txn.req,
846 s->srv->bind_hdr_name,
847 s->srv->bind_hdr_len,
848 &s->txn.hdr_idx,
849 s->srv->bind_hdr_occ));
850 }
851 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200852 default:
Willy Tarreaubce70882009-09-07 11:51:47 +0200853 memset(&s->from_addr, 0, sizeof(s->from_addr));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200854 }
855 }
856 else if (s->be->options & PR_O_BIND_SRC) {
857 switch (s->be->options & PR_O_TPXY_MASK) {
858 case PR_O_TPXY_ADDR:
859 s->from_addr = *(struct sockaddr_in *)&s->be->tproxy_addr;
860 break;
861 case PR_O_TPXY_CLI:
862 case PR_O_TPXY_CIP:
863 /* FIXME: what can we do if the client connects in IPv6 ? */
864 s->from_addr = *(struct sockaddr_in *)&s->cli_addr;
865 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200866 case PR_O_TPXY_DYN:
867 if (s->be->bind_hdr_occ) {
868 /* bind to the IP in a header */
869 s->from_addr.sin_port = 0;
870 s->from_addr.sin_addr.s_addr = htonl(get_ip_from_hdr2(&s->txn.req,
871 s->be->bind_hdr_name,
872 s->be->bind_hdr_len,
873 &s->txn.hdr_idx,
874 s->be->bind_hdr_occ));
875 }
876 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200877 default:
Willy Tarreaubce70882009-09-07 11:51:47 +0200878 memset(&s->from_addr, 0, sizeof(s->from_addr));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200879 }
880 }
881#endif
882}
883
884
Willy Tarreaubaaee002006-06-26 02:48:02 +0200885/*
886 * This function initiates a connection to the server assigned to this session
887 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
888 * It can return one of :
889 * - SN_ERR_NONE if everything's OK
890 * - SN_ERR_SRVTO if there are no more servers
891 * - SN_ERR_SRVCL if the connection was refused by the server
892 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
893 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
894 * - SN_ERR_INTERNAL for any other purely internal errors
895 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
896 */
897int connect_server(struct session *s)
898{
Willy Tarreau9650f372009-08-16 14:02:45 +0200899 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200900
901 if (!(s->flags & SN_ADDR_SET)) {
902 err = assign_server_address(s);
903 if (err != SRV_STATUS_OK)
904 return SN_ERR_INTERNAL;
905 }
906
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200907 /* Prepare the stream interface for a TCP connection. Later
908 * we may assign a protocol-specific connect() function.
909 */
910 stream_sock_prepare_interface(s->req->cons);
911 s->req->cons->connect = tcpv4_connect_server;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200912
Willy Tarreaub1d67742010-03-29 19:36:59 +0200913 assign_tproxy_address(s);
914
Willy Tarreau9650f372009-08-16 14:02:45 +0200915 err = s->req->cons->connect(s->req->cons, s->be, s->srv,
916 (struct sockaddr *)&s->srv_addr,
Willy Tarreaub1d67742010-03-29 19:36:59 +0200917 (struct sockaddr *)&s->from_addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918
Willy Tarreau9650f372009-08-16 14:02:45 +0200919 if (err != SN_ERR_NONE)
920 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200921
Willy Tarreaubaaee002006-06-26 02:48:02 +0200922 if (s->srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100923 s->flags |= SN_CURR_SESS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924 s->srv->cur_sess++;
Willy Tarreauac68c5d2009-10-04 23:12:44 +0200925 if (s->srv->cur_sess > s->srv->counters.cur_sess_max)
926 s->srv->counters.cur_sess_max = s->srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100927 if (s->be->lbprm.server_take_conn)
928 s->be->lbprm.server_take_conn(s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929 }
930
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931 return SN_ERR_NONE; /* connection is OK */
932}
933
934
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935/* This function performs the "redispatch" part of a connection attempt. It
936 * will assign a server if required, queue the connection if required, and
937 * handle errors that might arise at this level. It can change the server
938 * state. It will return 1 if it encounters an error, switches the server
939 * state, or has to queue a connection. Otherwise, it will return 0 indicating
940 * that the connection is ready to use.
941 */
942
943int srv_redispatch_connect(struct session *t)
944{
945 int conn_err;
946
947 /* We know that we don't have any connection pending, so we will
948 * try to get a new one, and wait in this state if it's queued
949 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200950 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951 conn_err = assign_server_and_queue(t);
952 switch (conn_err) {
953 case SRV_STATUS_OK:
954 break;
955
Willy Tarreau7c669d72008-06-20 15:04:11 +0200956 case SRV_STATUS_FULL:
957 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
958 * and we can redispatch to another server, or it is not and we return
959 * 503. This only makes sense in DIRECT mode however, because normal LB
960 * algorithms would never select such a server, and hash algorithms
961 * would bring us on the same server again. Note that t->srv is set in
962 * this case.
963 */
Willy Tarreau4de91492010-01-22 19:10:05 +0100964 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
965 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200966 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
967 t->prev_srv = t->srv;
968 goto redispatch;
969 }
970
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200971 if (!t->req->cons->err_type) {
972 t->req->cons->err_type = SI_ET_QUEUE_ERR;
973 t->req->cons->err_loc = t->srv;
974 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200975
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200976 t->srv->counters.failed_conns++;
977 t->be->counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200978 return 1;
979
Willy Tarreaubaaee002006-06-26 02:48:02 +0200980 case SRV_STATUS_NOSRV:
981 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200982 if (!t->req->cons->err_type) {
983 t->req->cons->err_type = SI_ET_CONN_ERR;
984 t->req->cons->err_loc = NULL;
985 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100986
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200987 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200988 return 1;
989
990 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +0200991 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200992 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200993 /* do nothing else and do not wake any other session up */
994 return 1;
995
Willy Tarreaubaaee002006-06-26 02:48:02 +0200996 case SRV_STATUS_INTERNAL:
997 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200998 if (!t->req->cons->err_type) {
999 t->req->cons->err_type = SI_ET_CONN_OTHER;
1000 t->req->cons->err_loc = t->srv;
1001 }
1002
Willy Tarreaubaaee002006-06-26 02:48:02 +02001003 if (t->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +01001004 srv_inc_sess_ctr(t->srv);
Willy Tarreau98937b82007-12-10 15:05:42 +01001005 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001006 t->srv->counters.failed_conns++;
1007 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001008
1009 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001010 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001011 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001012 return 1;
1013 }
1014 /* if we get here, it's because we got SRV_STATUS_OK, which also
1015 * means that the connection has not been queued.
1016 */
1017 return 0;
1018}
1019
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001020/* Apply RDP cookie persistence to the current session. For this, the function
1021 * tries to extract an RDP cookie from the request buffer, and look for the
1022 * matching server in the list. If the server is found, it is assigned to the
1023 * session. This always returns 1, and the analyser removes itself from the
1024 * list. Nothing is performed if a server was already assigned.
1025 */
1026int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1027{
1028 struct proxy *px = s->be;
1029 int ret;
1030 struct acl_expr expr;
1031 struct acl_test test;
1032 struct server *srv = px->srv;
1033 struct sockaddr_in addr;
1034 char *p;
1035
1036 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1037 now_ms, __FUNCTION__,
1038 s,
1039 req,
1040 req->rex, req->wex,
1041 req->flags,
1042 req->l,
1043 req->analysers);
1044
1045 if (s->flags & SN_ASSIGNED)
1046 goto no_cookie;
1047
1048 memset(&expr, 0, sizeof(expr));
1049 memset(&test, 0, sizeof(test));
1050
1051 expr.arg.str = s->be->rdp_cookie_name;
1052 expr.arg_len = s->be->rdp_cookie_len;
1053
1054 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
1055 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
1056 goto no_cookie;
1057
1058 memset(&addr, 0, sizeof(addr));
1059 addr.sin_family = AF_INET;
1060
1061 /* Considering an rdp cookie detected using acl, test.ptr ended with <cr><lf> and should return */
1062 addr.sin_addr.s_addr = strtoul(test.ptr, &p, 10);
1063 if (*p != '.')
1064 goto no_cookie;
1065 p++;
1066 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1067 if (*p != '.')
1068 goto no_cookie;
1069
1070 while (srv) {
1071 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1072 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1073 /* we found the server and it is usable */
1074 s->flags |= SN_DIRECT | SN_ASSIGNED;
1075 s->srv = srv;
1076 break;
1077 }
1078 }
1079 srv = srv->next;
1080 }
1081
1082no_cookie:
1083 req->analysers &= ~an_bit;
1084 req->analyse_exp = TICK_ETERNITY;
1085 return 1;
1086}
1087
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001088int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001089 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001090 return px->down_time;
1091
1092 return now.tv_sec - px->last_change + px->down_time;
1093}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001094
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001095/*
1096 * This function returns a string containing the balancing
1097 * mode of the proxy in a format suitable for stats.
1098 */
1099
1100const char *backend_lb_algo_str(int algo) {
1101
1102 if (algo == BE_LB_ALGO_RR)
1103 return "roundrobin";
1104 else if (algo == BE_LB_ALGO_SRR)
1105 return "static-rr";
1106 else if (algo == BE_LB_ALGO_LC)
1107 return "leastconn";
1108 else if (algo == BE_LB_ALGO_SH)
1109 return "source";
1110 else if (algo == BE_LB_ALGO_UH)
1111 return "uri";
1112 else if (algo == BE_LB_ALGO_PH)
1113 return "url_param";
1114 else if (algo == BE_LB_ALGO_HH)
1115 return "hdr";
1116 else if (algo == BE_LB_ALGO_RCH)
1117 return "rdp-cookie";
1118 else
1119 return NULL;
1120}
1121
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001122/* This function parses a "balance" statement in a backend section describing
1123 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1124 * returns -1, it may write an error message into ther <err> buffer, for at
1125 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1126 * written. The function must be called with <args> pointing to the first word
1127 * after "balance".
1128 */
1129int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1130{
1131 if (!*(args[0])) {
1132 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001133 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1134 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001135 return 0;
1136 }
1137
1138 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001139 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1140 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001141 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001142 else if (!strcmp(args[0], "static-rr")) {
1143 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1144 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1145 }
Willy Tarreau51406232008-03-10 22:04:20 +01001146 else if (!strcmp(args[0], "leastconn")) {
1147 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1148 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1149 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001150 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001151 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1152 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001153 }
1154 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001155 int arg = 1;
1156
Willy Tarreau31682232007-11-29 15:38:04 +01001157 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1158 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001159
1160 while (*args[arg]) {
1161 if (!strcmp(args[arg], "len")) {
1162 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1163 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1164 return -1;
1165 }
1166 curproxy->uri_len_limit = atoi(args[arg+1]);
1167 arg += 2;
1168 }
1169 else if (!strcmp(args[arg], "depth")) {
1170 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1171 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1172 return -1;
1173 }
1174 /* hint: we store the position of the ending '/' (depth+1) so
1175 * that we avoid a comparison while computing the hash.
1176 */
1177 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1178 arg += 2;
1179 }
1180 else {
1181 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1182 return -1;
1183 }
1184 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001185 }
Willy Tarreau01732802007-11-01 22:48:15 +01001186 else if (!strcmp(args[0], "url_param")) {
1187 if (!*args[1]) {
1188 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1189 return -1;
1190 }
Willy Tarreau31682232007-11-29 15:38:04 +01001191 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1192 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001193
1194 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001195 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001196 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001197 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001198 if (strcmp(args[2], "check_post")) {
1199 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1200 return -1;
1201 }
1202 if (*args[3]) {
1203 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1204 curproxy->url_param_post_limit = str2ui(args[3]);
1205 }
1206 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1207 if (!curproxy->url_param_post_limit)
1208 curproxy->url_param_post_limit = 48;
1209 else if ( curproxy->url_param_post_limit < 3 )
1210 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1211 }
Benoitaffb4812009-03-25 13:02:10 +01001212 }
1213 else if (!strncmp(args[0], "hdr(", 4)) {
1214 const char *beg, *end;
1215
1216 beg = args[0] + 4;
1217 end = strchr(beg, ')');
1218
1219 if (!end || end == beg) {
1220 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1221 return -1;
1222 }
1223
1224 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1225 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1226
1227 free(curproxy->hh_name);
1228 curproxy->hh_len = end - beg;
1229 curproxy->hh_name = my_strndup(beg, end - beg);
1230 curproxy->hh_match_domain = 0;
1231
1232 if (*args[1]) {
1233 if (strcmp(args[1], "use_domain_only")) {
1234 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1235 return -1;
1236 }
1237 curproxy->hh_match_domain = 1;
1238 }
1239
Emeric Brun736aa232009-06-30 17:56:00 +02001240 }
1241 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1242 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1243 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1244
1245 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1246 const char *beg, *end;
1247
1248 beg = args[0] + 11;
1249 end = strchr(beg, ')');
1250
1251 if (!end || end == beg) {
1252 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1253 return -1;
1254 }
1255
1256 free(curproxy->hh_name);
1257 curproxy->hh_name = my_strndup(beg, end - beg);
1258 curproxy->hh_len = end - beg;
1259 }
1260 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1261 free(curproxy->hh_name);
1262 curproxy->hh_name = strdup("mstshash");
1263 curproxy->hh_len = strlen(curproxy->hh_name);
1264 }
1265 else { /* syntax */
1266 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1267 return -1;
1268 }
Willy Tarreau01732802007-11-01 22:48:15 +01001269 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001270 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001271 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 +01001272 return -1;
1273 }
1274 return 0;
1275}
1276
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001277
1278/************************************************************************/
1279/* All supported keywords must be declared here. */
1280/************************************************************************/
1281
1282/* set test->i to the number of enabled servers on the proxy */
1283static int
1284acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1285 struct acl_expr *expr, struct acl_test *test)
1286{
1287 test->flags = ACL_TEST_F_VOL_TEST;
1288 if (expr->arg_len) {
1289 /* another proxy was designated, we must look for it */
1290 for (px = proxy; px; px = px->next)
1291 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1292 break;
1293 }
1294 if (!px)
1295 return 0;
1296
1297 if (px->srv_act)
1298 test->i = px->srv_act;
1299 else if (px->lbprm.fbck)
1300 test->i = 1;
1301 else
1302 test->i = px->srv_bck;
1303
1304 return 1;
1305}
1306
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001307/* report in test->flags a success or failure depending on the designated
1308 * server's state. There is no match function involved since there's no pattern.
1309 */
1310static int
1311acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, int dir,
1312 struct acl_expr *expr, struct acl_test *test)
1313{
1314 struct server *srv = expr->arg.srv;
1315
1316 test->flags = ACL_TEST_F_VOL_TEST;
1317 if (!(srv->state & SRV_MAINTAIN) &&
1318 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
1319 test->flags |= ACL_TEST_F_SET_RES_PASS;
1320 else
1321 test->flags |= ACL_TEST_F_SET_RES_FAIL;
1322 return 1;
1323}
1324
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001325/* set test->i to the number of enabled servers on the proxy */
1326static int
1327acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1328 struct acl_expr *expr, struct acl_test *test)
1329{
1330 struct server *iterator;
1331 test->flags = ACL_TEST_F_VOL_TEST;
1332 if (expr->arg_len) {
1333 /* another proxy was designated, we must look for it */
1334 for (px = proxy; px; px = px->next)
1335 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1336 break;
1337 }
1338 if (!px)
1339 return 0;
1340
1341 test->i = 0;
1342 iterator = px->srv;
1343 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001344 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001345 iterator = iterator->next;
1346 continue;
1347 }
1348 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
1349 test->i = -1;
1350 return 1;
1351 }
1352
1353 test->i += (iterator->maxconn - iterator->cur_sess)
1354 + (iterator->maxqueue - iterator->nbpend);
1355 iterator = iterator->next;
1356 }
1357
1358 return 1;
1359}
1360
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001361/* set test->i to the number of connections per second reaching the backend */
1362static int
1363acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1364 struct acl_expr *expr, struct acl_test *test)
1365{
1366 test->flags = ACL_TEST_F_VOL_TEST;
1367 if (expr->arg_len) {
1368 /* another proxy was designated, we must look for it */
1369 for (px = proxy; px; px = px->next)
1370 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1371 break;
1372 }
1373 if (!px)
1374 return 0;
1375
1376 test->i = read_freq_ctr(&px->be_sess_per_sec);
1377 return 1;
1378}
1379
Willy Tarreaua36af912009-10-10 12:02:45 +02001380/* set test->i to the number of concurrent connections on the backend */
1381static int
1382acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1383 struct acl_expr *expr, struct acl_test *test)
1384{
1385 test->flags = ACL_TEST_F_VOL_TEST;
1386 if (expr->arg_len) {
1387 /* another proxy was designated, we must look for it */
1388 for (px = proxy; px; px = px->next)
1389 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1390 break;
1391 }
1392 if (!px)
1393 return 0;
1394
1395 test->i = px->beconn;
1396 return 1;
1397}
1398
1399/* set test->i to the total number of queued connections on the backend */
1400static int
1401acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1402 struct acl_expr *expr, struct acl_test *test)
1403{
1404 test->flags = ACL_TEST_F_VOL_TEST;
1405 if (expr->arg_len) {
1406 /* another proxy was designated, we must look for it */
1407 for (px = proxy; px; px = px->next)
1408 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1409 break;
1410 }
1411 if (!px)
1412 return 0;
1413
1414 test->i = px->totpend;
1415 return 1;
1416}
1417
1418/* set test->i to the total number of queued connections on the backend divided
1419 * by the number of running servers and rounded up. If there is no running
1420 * server, we return twice the total, just as if we had half a running server.
1421 * This is more or less correct anyway, since we expect the last server to come
1422 * back soon.
1423 */
1424static int
1425acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1426 struct acl_expr *expr, struct acl_test *test)
1427{
1428 int nbsrv;
1429
1430 test->flags = ACL_TEST_F_VOL_TEST;
1431 if (expr->arg_len) {
1432 /* another proxy was designated, we must look for it */
1433 for (px = proxy; px; px = px->next)
1434 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1435 break;
1436 }
1437 if (!px)
1438 return 0;
1439
1440 if (px->srv_act)
1441 nbsrv = px->srv_act;
1442 else if (px->lbprm.fbck)
1443 nbsrv = 1;
1444 else
1445 nbsrv = px->srv_bck;
1446
1447 if (nbsrv > 0)
1448 test->i = (px->totpend + nbsrv - 1) / nbsrv;
1449 else
1450 test->i = px->totpend * 2;
1451
1452 return 1;
1453}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001454
1455/* Note: must not be declared <const> as its list will be overwritten */
1456static struct acl_kw_list acl_kws = {{ },{
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001457 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau3a8efeb2009-03-05 19:15:37 +01001458 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001459 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua36af912009-10-10 12:02:45 +02001460 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1461 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1462 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001463 { "srv_is_up", acl_parse_nothing, acl_fetch_srv_is_up, acl_match_nothing, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001464 { NULL, NULL, NULL, NULL },
1465}};
1466
1467
1468__attribute__((constructor))
1469static void __backend_init(void)
1470{
1471 acl_register_keywords(&acl_kws);
1472}
1473
Willy Tarreaubaaee002006-06-26 02:48:02 +02001474/*
1475 * Local variables:
1476 * c-indent-level: 8
1477 * c-basic-offset: 8
1478 * End:
1479 */