blob: 33a81ab91efd0790d9a2307a28fa660a63fc83ec [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
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200664 if (!s->srv_addr.sin_addr.s_addr) {
665 /* if the server has no address, we use the same address
666 * the client asked, which is handy for remapping ports
667 * locally on multiple addresses at once.
668 */
669 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
670 get_frt_addr(s);
671
672 s->srv_addr.sin_addr = ((struct sockaddr_in *)&s->frt_addr)->sin_addr;
673 }
674
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 /* if this server remaps proxied ports, we'll use
676 * the port the client connected to with an offset. */
677 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100678 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200679 get_frt_addr(s);
680 if (s->frt_addr.ss_family == AF_INET) {
681 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
682 ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port));
683 } else {
684 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
685 ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port));
686 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200687 }
688 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200689 else if (*(int *)&s->be->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690 /* connect to the defined dispatch addr */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200691 s->srv_addr = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200692 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100693 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200694 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100695 if (!(s->flags & SN_FRT_ADDR_SET))
696 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200697
Willy Tarreaubd414282008-01-19 13:46:35 +0100698 memcpy(&s->srv_addr, &s->frt_addr, MIN(sizeof(s->srv_addr), sizeof(s->frt_addr)));
699 /* when we support IPv6 on the backend, we may add other tests */
700 //qfprintf(stderr, "Cannot get original server address.\n");
701 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200702 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100703 else if (s->be->options & PR_O_HTTP_PROXY) {
704 /* If HTTP PROXY option is set, then server is already assigned
705 * during incoming client request parsing. */
706 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100707 else {
708 /* no server and no LB algorithm ! */
709 return SRV_STATUS_INTERNAL;
710 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200711
712 s->flags |= SN_ADDR_SET;
713 return SRV_STATUS_OK;
714}
715
716
717/* This function assigns a server to session <s> if required, and can add the
718 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200719 * If ->srv_conn is set, the session is first released from the server.
720 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
721 * be called before any connection and after any retry or redispatch occurs.
722 *
723 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724 *
725 * Returns :
726 *
727 * SRV_STATUS_OK if everything is OK.
728 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
729 * SRV_STATUS_QUEUED if the connection has been queued.
730 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200731 * connection could not be queued in s->srv,
732 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733 * SRV_STATUS_INTERNAL for other unrecoverable errors.
734 *
735 */
736int assign_server_and_queue(struct session *s)
737{
738 struct pendconn *p;
739 int err;
740
741 if (s->pend_pos)
742 return SRV_STATUS_INTERNAL;
743
Willy Tarreau7c669d72008-06-20 15:04:11 +0200744 err = SRV_STATUS_OK;
745 if (!(s->flags & SN_ASSIGNED)) {
746 err = assign_server(s);
747 if (s->prev_srv) {
748 /* This session was previously assigned to a server. We have to
749 * update the session's and the server's stats :
750 * - if the server changed :
751 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
752 * - set SN_REDISP if it was successfully redispatched
753 * - increment srv->redispatches and be->redispatches
754 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100755 */
756
Willy Tarreau7c669d72008-06-20 15:04:11 +0200757 if (s->prev_srv != s->srv) {
758 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
759 s->txn.flags &= ~TX_CK_MASK;
760 s->txn.flags |= TX_CK_DOWN;
761 }
762 s->flags |= SN_REDISP;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200763 s->prev_srv->counters.redispatches++;
764 s->be->counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200765 } else {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200766 s->prev_srv->counters.retries++;
767 s->be->counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100768 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100769 }
770 }
771
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 switch (err) {
773 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200774 /* we have SN_ASSIGNED set */
775 if (!s->srv)
776 return SRV_STATUS_OK; /* dispatch or proxy mode */
777
778 /* If we already have a connection slot, no need to check any queue */
779 if (s->srv_conn == s->srv)
780 return SRV_STATUS_OK;
781
782 /* OK, this session already has an assigned server, but no
783 * connection slot yet. Either it is a redispatch, or it was
784 * assigned from persistence information (direct mode).
785 */
786 if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) {
787 /* server scheduled for redirection, and already assigned. We
788 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100789 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200790 sess_change_server(s, s->srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100791 return SRV_STATUS_OK;
792 }
793
Willy Tarreau7c669d72008-06-20 15:04:11 +0200794 /* We might have to queue this session if the assigned server is full.
795 * We know we have to queue it into the server's queue, so if a maxqueue
796 * is set on the server, we must also check that the server's queue is
797 * not full, in which case we have to return FULL.
798 */
799 if (s->srv->maxconn &&
800 (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) {
801
802 if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue)
803 return SRV_STATUS_FULL;
804
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 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 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200811
812 /* OK, we can use this server. Let's reserve our place */
813 sess_change_server(s, s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200814 return SRV_STATUS_OK;
815
816 case SRV_STATUS_FULL:
817 /* queue this session into the proxy's queue */
818 p = pendconn_add(s);
819 if (p)
820 return SRV_STATUS_QUEUED;
821 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200822 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200823
824 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200825 return err;
826
Willy Tarreaubaaee002006-06-26 02:48:02 +0200827 case SRV_STATUS_INTERNAL:
828 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200829
Willy Tarreaubaaee002006-06-26 02:48:02 +0200830 default:
831 return SRV_STATUS_INTERNAL;
832 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100833}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200834
Willy Tarreaub1d67742010-03-29 19:36:59 +0200835/* If an explicit source binding is specified on the server and/or backend, and
836 * this source makes use of the transparent proxy, then it is extracted now and
837 * assigned to the session's from_addr entry.
838 */
839static void assign_tproxy_address(struct session *s)
840{
841#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
842 if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) {
843 switch (s->srv->state & SRV_TPROXY_MASK) {
844 case SRV_TPROXY_ADDR:
845 s->from_addr = *(struct sockaddr_in *)&s->srv->tproxy_addr;
846 break;
847 case SRV_TPROXY_CLI:
848 case SRV_TPROXY_CIP:
849 /* FIXME: what can we do if the client connects in IPv6 ? */
850 s->from_addr = *(struct sockaddr_in *)&s->cli_addr;
851 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200852 case SRV_TPROXY_DYN:
853 if (s->srv->bind_hdr_occ) {
854 /* bind to the IP in a header */
855 s->from_addr.sin_port = 0;
856 s->from_addr.sin_addr.s_addr = htonl(get_ip_from_hdr2(&s->txn.req,
857 s->srv->bind_hdr_name,
858 s->srv->bind_hdr_len,
859 &s->txn.hdr_idx,
860 s->srv->bind_hdr_occ));
861 }
862 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200863 default:
Willy Tarreaubce70882009-09-07 11:51:47 +0200864 memset(&s->from_addr, 0, sizeof(s->from_addr));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200865 }
866 }
867 else if (s->be->options & PR_O_BIND_SRC) {
868 switch (s->be->options & PR_O_TPXY_MASK) {
869 case PR_O_TPXY_ADDR:
870 s->from_addr = *(struct sockaddr_in *)&s->be->tproxy_addr;
871 break;
872 case PR_O_TPXY_CLI:
873 case PR_O_TPXY_CIP:
874 /* FIXME: what can we do if the client connects in IPv6 ? */
875 s->from_addr = *(struct sockaddr_in *)&s->cli_addr;
876 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200877 case PR_O_TPXY_DYN:
878 if (s->be->bind_hdr_occ) {
879 /* bind to the IP in a header */
880 s->from_addr.sin_port = 0;
881 s->from_addr.sin_addr.s_addr = htonl(get_ip_from_hdr2(&s->txn.req,
882 s->be->bind_hdr_name,
883 s->be->bind_hdr_len,
884 &s->txn.hdr_idx,
885 s->be->bind_hdr_occ));
886 }
887 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200888 default:
Willy Tarreaubce70882009-09-07 11:51:47 +0200889 memset(&s->from_addr, 0, sizeof(s->from_addr));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200890 }
891 }
892#endif
893}
894
895
Willy Tarreaubaaee002006-06-26 02:48:02 +0200896/*
897 * This function initiates a connection to the server assigned to this session
898 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
899 * It can return one of :
900 * - SN_ERR_NONE if everything's OK
901 * - SN_ERR_SRVTO if there are no more servers
902 * - SN_ERR_SRVCL if the connection was refused by the server
903 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
904 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
905 * - SN_ERR_INTERNAL for any other purely internal errors
906 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
907 */
908int connect_server(struct session *s)
909{
Willy Tarreau9650f372009-08-16 14:02:45 +0200910 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200911
912 if (!(s->flags & SN_ADDR_SET)) {
913 err = assign_server_address(s);
914 if (err != SRV_STATUS_OK)
915 return SN_ERR_INTERNAL;
916 }
917
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200918 /* Prepare the stream interface for a TCP connection. Later
919 * we may assign a protocol-specific connect() function.
920 */
921 stream_sock_prepare_interface(s->req->cons);
922 s->req->cons->connect = tcpv4_connect_server;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200923
Willy Tarreaub1d67742010-03-29 19:36:59 +0200924 assign_tproxy_address(s);
925
Willy Tarreau9650f372009-08-16 14:02:45 +0200926 err = s->req->cons->connect(s->req->cons, s->be, s->srv,
927 (struct sockaddr *)&s->srv_addr,
Willy Tarreaub1d67742010-03-29 19:36:59 +0200928 (struct sockaddr *)&s->from_addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929
Willy Tarreau9650f372009-08-16 14:02:45 +0200930 if (err != SN_ERR_NONE)
931 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200932
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933 if (s->srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100934 s->flags |= SN_CURR_SESS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935 s->srv->cur_sess++;
Willy Tarreauac68c5d2009-10-04 23:12:44 +0200936 if (s->srv->cur_sess > s->srv->counters.cur_sess_max)
937 s->srv->counters.cur_sess_max = s->srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100938 if (s->be->lbprm.server_take_conn)
939 s->be->lbprm.server_take_conn(s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200940 }
941
Willy Tarreaubaaee002006-06-26 02:48:02 +0200942 return SN_ERR_NONE; /* connection is OK */
943}
944
945
Willy Tarreaubaaee002006-06-26 02:48:02 +0200946/* This function performs the "redispatch" part of a connection attempt. It
947 * will assign a server if required, queue the connection if required, and
948 * handle errors that might arise at this level. It can change the server
949 * state. It will return 1 if it encounters an error, switches the server
950 * state, or has to queue a connection. Otherwise, it will return 0 indicating
951 * that the connection is ready to use.
952 */
953
954int srv_redispatch_connect(struct session *t)
955{
956 int conn_err;
957
958 /* We know that we don't have any connection pending, so we will
959 * try to get a new one, and wait in this state if it's queued
960 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200961 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200962 conn_err = assign_server_and_queue(t);
963 switch (conn_err) {
964 case SRV_STATUS_OK:
965 break;
966
Willy Tarreau7c669d72008-06-20 15:04:11 +0200967 case SRV_STATUS_FULL:
968 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
969 * and we can redispatch to another server, or it is not and we return
970 * 503. This only makes sense in DIRECT mode however, because normal LB
971 * algorithms would never select such a server, and hash algorithms
972 * would bring us on the same server again. Note that t->srv is set in
973 * this case.
974 */
Willy Tarreau4de91492010-01-22 19:10:05 +0100975 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
976 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200977 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
978 t->prev_srv = t->srv;
979 goto redispatch;
980 }
981
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200982 if (!t->req->cons->err_type) {
983 t->req->cons->err_type = SI_ET_QUEUE_ERR;
984 t->req->cons->err_loc = t->srv;
985 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200986
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200987 t->srv->counters.failed_conns++;
988 t->be->counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200989 return 1;
990
Willy Tarreaubaaee002006-06-26 02:48:02 +0200991 case SRV_STATUS_NOSRV:
992 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200993 if (!t->req->cons->err_type) {
994 t->req->cons->err_type = SI_ET_CONN_ERR;
995 t->req->cons->err_loc = NULL;
996 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100997
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200998 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200999 return 1;
1000
1001 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001002 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001003 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001004 /* do nothing else and do not wake any other session up */
1005 return 1;
1006
Willy Tarreaubaaee002006-06-26 02:48:02 +02001007 case SRV_STATUS_INTERNAL:
1008 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001009 if (!t->req->cons->err_type) {
1010 t->req->cons->err_type = SI_ET_CONN_OTHER;
1011 t->req->cons->err_loc = t->srv;
1012 }
1013
Willy Tarreaubaaee002006-06-26 02:48:02 +02001014 if (t->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +01001015 srv_inc_sess_ctr(t->srv);
Willy Tarreau98937b82007-12-10 15:05:42 +01001016 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001017 t->srv->counters.failed_conns++;
1018 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019
1020 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001021 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001022 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001023 return 1;
1024 }
1025 /* if we get here, it's because we got SRV_STATUS_OK, which also
1026 * means that the connection has not been queued.
1027 */
1028 return 0;
1029}
1030
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001031/* Apply RDP cookie persistence to the current session. For this, the function
1032 * tries to extract an RDP cookie from the request buffer, and look for the
1033 * matching server in the list. If the server is found, it is assigned to the
1034 * session. This always returns 1, and the analyser removes itself from the
1035 * list. Nothing is performed if a server was already assigned.
1036 */
1037int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1038{
1039 struct proxy *px = s->be;
1040 int ret;
1041 struct acl_expr expr;
1042 struct acl_test test;
1043 struct server *srv = px->srv;
1044 struct sockaddr_in addr;
1045 char *p;
1046
1047 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1048 now_ms, __FUNCTION__,
1049 s,
1050 req,
1051 req->rex, req->wex,
1052 req->flags,
1053 req->l,
1054 req->analysers);
1055
1056 if (s->flags & SN_ASSIGNED)
1057 goto no_cookie;
1058
1059 memset(&expr, 0, sizeof(expr));
1060 memset(&test, 0, sizeof(test));
1061
1062 expr.arg.str = s->be->rdp_cookie_name;
1063 expr.arg_len = s->be->rdp_cookie_len;
1064
1065 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
1066 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
1067 goto no_cookie;
1068
1069 memset(&addr, 0, sizeof(addr));
1070 addr.sin_family = AF_INET;
1071
1072 /* Considering an rdp cookie detected using acl, test.ptr ended with <cr><lf> and should return */
1073 addr.sin_addr.s_addr = strtoul(test.ptr, &p, 10);
1074 if (*p != '.')
1075 goto no_cookie;
1076 p++;
1077 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1078 if (*p != '.')
1079 goto no_cookie;
1080
1081 while (srv) {
1082 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1083 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1084 /* we found the server and it is usable */
1085 s->flags |= SN_DIRECT | SN_ASSIGNED;
1086 s->srv = srv;
1087 break;
1088 }
1089 }
1090 srv = srv->next;
1091 }
1092
1093no_cookie:
1094 req->analysers &= ~an_bit;
1095 req->analyse_exp = TICK_ETERNITY;
1096 return 1;
1097}
1098
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001099int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001100 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001101 return px->down_time;
1102
1103 return now.tv_sec - px->last_change + px->down_time;
1104}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001105
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001106/*
1107 * This function returns a string containing the balancing
1108 * mode of the proxy in a format suitable for stats.
1109 */
1110
1111const char *backend_lb_algo_str(int algo) {
1112
1113 if (algo == BE_LB_ALGO_RR)
1114 return "roundrobin";
1115 else if (algo == BE_LB_ALGO_SRR)
1116 return "static-rr";
1117 else if (algo == BE_LB_ALGO_LC)
1118 return "leastconn";
1119 else if (algo == BE_LB_ALGO_SH)
1120 return "source";
1121 else if (algo == BE_LB_ALGO_UH)
1122 return "uri";
1123 else if (algo == BE_LB_ALGO_PH)
1124 return "url_param";
1125 else if (algo == BE_LB_ALGO_HH)
1126 return "hdr";
1127 else if (algo == BE_LB_ALGO_RCH)
1128 return "rdp-cookie";
1129 else
1130 return NULL;
1131}
1132
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001133/* This function parses a "balance" statement in a backend section describing
1134 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1135 * returns -1, it may write an error message into ther <err> buffer, for at
1136 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1137 * written. The function must be called with <args> pointing to the first word
1138 * after "balance".
1139 */
1140int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1141{
1142 if (!*(args[0])) {
1143 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001144 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1145 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001146 return 0;
1147 }
1148
1149 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001150 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1151 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001152 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001153 else if (!strcmp(args[0], "static-rr")) {
1154 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1155 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1156 }
Willy Tarreau51406232008-03-10 22:04:20 +01001157 else if (!strcmp(args[0], "leastconn")) {
1158 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1159 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1160 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001161 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001162 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1163 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001164 }
1165 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001166 int arg = 1;
1167
Willy Tarreau31682232007-11-29 15:38:04 +01001168 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1169 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001170
1171 while (*args[arg]) {
1172 if (!strcmp(args[arg], "len")) {
1173 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1174 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1175 return -1;
1176 }
1177 curproxy->uri_len_limit = atoi(args[arg+1]);
1178 arg += 2;
1179 }
1180 else if (!strcmp(args[arg], "depth")) {
1181 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1182 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1183 return -1;
1184 }
1185 /* hint: we store the position of the ending '/' (depth+1) so
1186 * that we avoid a comparison while computing the hash.
1187 */
1188 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1189 arg += 2;
1190 }
1191 else {
1192 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1193 return -1;
1194 }
1195 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001196 }
Willy Tarreau01732802007-11-01 22:48:15 +01001197 else if (!strcmp(args[0], "url_param")) {
1198 if (!*args[1]) {
1199 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1200 return -1;
1201 }
Willy Tarreau31682232007-11-29 15:38:04 +01001202 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1203 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001204
1205 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001206 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001207 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001208 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001209 if (strcmp(args[2], "check_post")) {
1210 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1211 return -1;
1212 }
1213 if (*args[3]) {
1214 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1215 curproxy->url_param_post_limit = str2ui(args[3]);
1216 }
1217 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1218 if (!curproxy->url_param_post_limit)
1219 curproxy->url_param_post_limit = 48;
1220 else if ( curproxy->url_param_post_limit < 3 )
1221 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1222 }
Benoitaffb4812009-03-25 13:02:10 +01001223 }
1224 else if (!strncmp(args[0], "hdr(", 4)) {
1225 const char *beg, *end;
1226
1227 beg = args[0] + 4;
1228 end = strchr(beg, ')');
1229
1230 if (!end || end == beg) {
1231 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1232 return -1;
1233 }
1234
1235 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1236 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1237
1238 free(curproxy->hh_name);
1239 curproxy->hh_len = end - beg;
1240 curproxy->hh_name = my_strndup(beg, end - beg);
1241 curproxy->hh_match_domain = 0;
1242
1243 if (*args[1]) {
1244 if (strcmp(args[1], "use_domain_only")) {
1245 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1246 return -1;
1247 }
1248 curproxy->hh_match_domain = 1;
1249 }
1250
Emeric Brun736aa232009-06-30 17:56:00 +02001251 }
1252 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1253 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1254 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1255
1256 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1257 const char *beg, *end;
1258
1259 beg = args[0] + 11;
1260 end = strchr(beg, ')');
1261
1262 if (!end || end == beg) {
1263 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1264 return -1;
1265 }
1266
1267 free(curproxy->hh_name);
1268 curproxy->hh_name = my_strndup(beg, end - beg);
1269 curproxy->hh_len = end - beg;
1270 }
1271 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1272 free(curproxy->hh_name);
1273 curproxy->hh_name = strdup("mstshash");
1274 curproxy->hh_len = strlen(curproxy->hh_name);
1275 }
1276 else { /* syntax */
1277 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1278 return -1;
1279 }
Willy Tarreau01732802007-11-01 22:48:15 +01001280 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001281 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001282 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 +01001283 return -1;
1284 }
1285 return 0;
1286}
1287
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001288
1289/************************************************************************/
1290/* All supported keywords must be declared here. */
1291/************************************************************************/
1292
1293/* set test->i to the number of enabled servers on the proxy */
1294static int
1295acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1296 struct acl_expr *expr, struct acl_test *test)
1297{
1298 test->flags = ACL_TEST_F_VOL_TEST;
1299 if (expr->arg_len) {
1300 /* another proxy was designated, we must look for it */
1301 for (px = proxy; px; px = px->next)
1302 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1303 break;
1304 }
1305 if (!px)
1306 return 0;
1307
1308 if (px->srv_act)
1309 test->i = px->srv_act;
1310 else if (px->lbprm.fbck)
1311 test->i = 1;
1312 else
1313 test->i = px->srv_bck;
1314
1315 return 1;
1316}
1317
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001318/* report in test->flags a success or failure depending on the designated
1319 * server's state. There is no match function involved since there's no pattern.
1320 */
1321static int
1322acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, int dir,
1323 struct acl_expr *expr, struct acl_test *test)
1324{
1325 struct server *srv = expr->arg.srv;
1326
1327 test->flags = ACL_TEST_F_VOL_TEST;
1328 if (!(srv->state & SRV_MAINTAIN) &&
1329 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
1330 test->flags |= ACL_TEST_F_SET_RES_PASS;
1331 else
1332 test->flags |= ACL_TEST_F_SET_RES_FAIL;
1333 return 1;
1334}
1335
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001336/* set test->i to the number of enabled servers on the proxy */
1337static int
1338acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1339 struct acl_expr *expr, struct acl_test *test)
1340{
1341 struct server *iterator;
1342 test->flags = ACL_TEST_F_VOL_TEST;
1343 if (expr->arg_len) {
1344 /* another proxy was designated, we must look for it */
1345 for (px = proxy; px; px = px->next)
1346 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1347 break;
1348 }
1349 if (!px)
1350 return 0;
1351
1352 test->i = 0;
1353 iterator = px->srv;
1354 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001355 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001356 iterator = iterator->next;
1357 continue;
1358 }
1359 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
1360 test->i = -1;
1361 return 1;
1362 }
1363
1364 test->i += (iterator->maxconn - iterator->cur_sess)
1365 + (iterator->maxqueue - iterator->nbpend);
1366 iterator = iterator->next;
1367 }
1368
1369 return 1;
1370}
1371
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001372/* set test->i to the number of connections per second reaching the backend */
1373static int
1374acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1375 struct acl_expr *expr, struct acl_test *test)
1376{
1377 test->flags = ACL_TEST_F_VOL_TEST;
1378 if (expr->arg_len) {
1379 /* another proxy was designated, we must look for it */
1380 for (px = proxy; px; px = px->next)
1381 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1382 break;
1383 }
1384 if (!px)
1385 return 0;
1386
1387 test->i = read_freq_ctr(&px->be_sess_per_sec);
1388 return 1;
1389}
1390
Willy Tarreaua36af912009-10-10 12:02:45 +02001391/* set test->i to the number of concurrent connections on the backend */
1392static int
1393acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1394 struct acl_expr *expr, struct acl_test *test)
1395{
1396 test->flags = ACL_TEST_F_VOL_TEST;
1397 if (expr->arg_len) {
1398 /* another proxy was designated, we must look for it */
1399 for (px = proxy; px; px = px->next)
1400 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1401 break;
1402 }
1403 if (!px)
1404 return 0;
1405
1406 test->i = px->beconn;
1407 return 1;
1408}
1409
1410/* set test->i to the total number of queued connections on the backend */
1411static int
1412acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1413 struct acl_expr *expr, struct acl_test *test)
1414{
1415 test->flags = ACL_TEST_F_VOL_TEST;
1416 if (expr->arg_len) {
1417 /* another proxy was designated, we must look for it */
1418 for (px = proxy; px; px = px->next)
1419 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1420 break;
1421 }
1422 if (!px)
1423 return 0;
1424
1425 test->i = px->totpend;
1426 return 1;
1427}
1428
1429/* set test->i to the total number of queued connections on the backend divided
1430 * by the number of running servers and rounded up. If there is no running
1431 * server, we return twice the total, just as if we had half a running server.
1432 * This is more or less correct anyway, since we expect the last server to come
1433 * back soon.
1434 */
1435static int
1436acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1437 struct acl_expr *expr, struct acl_test *test)
1438{
1439 int nbsrv;
1440
1441 test->flags = ACL_TEST_F_VOL_TEST;
1442 if (expr->arg_len) {
1443 /* another proxy was designated, we must look for it */
1444 for (px = proxy; px; px = px->next)
1445 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1446 break;
1447 }
1448 if (!px)
1449 return 0;
1450
1451 if (px->srv_act)
1452 nbsrv = px->srv_act;
1453 else if (px->lbprm.fbck)
1454 nbsrv = 1;
1455 else
1456 nbsrv = px->srv_bck;
1457
1458 if (nbsrv > 0)
1459 test->i = (px->totpend + nbsrv - 1) / nbsrv;
1460 else
1461 test->i = px->totpend * 2;
1462
1463 return 1;
1464}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001465
1466/* Note: must not be declared <const> as its list will be overwritten */
1467static struct acl_kw_list acl_kws = {{ },{
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001468 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau3a8efeb2009-03-05 19:15:37 +01001469 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001470 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua36af912009-10-10 12:02:45 +02001471 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1472 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1473 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001474 { "srv_is_up", acl_parse_nothing, acl_fetch_srv_is_up, acl_match_nothing, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001475 { NULL, NULL, NULL, NULL },
1476}};
1477
1478
1479__attribute__((constructor))
1480static void __backend_init(void)
1481{
1482 acl_register_keywords(&acl_kws);
1483}
1484
Willy Tarreaubaaee002006-06-26 02:48:02 +02001485/*
1486 * Local variables:
1487 * c-indent-level: 8
1488 * c-basic-offset: 8
1489 * End:
1490 */