blob: 26bc8828dbf73e401597c966eaa9685b0794002e [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
Emeric Brunec810d12010-10-22 16:36:33 +0200672 if (s->frt_addr.ss_family == AF_INET) {
673 s->srv_addr.sin_addr = ((struct sockaddr_in *)&s->frt_addr)->sin_addr;
674 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200675 }
676
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677 /* if this server remaps proxied ports, we'll use
678 * the port the client connected to with an offset. */
679 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100680 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200681 get_frt_addr(s);
682 if (s->frt_addr.ss_family == AF_INET) {
683 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
684 ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port));
Emeric Brunec810d12010-10-22 16:36:33 +0200685 }
686 else if (s->frt_addr.ss_family == AF_INET6) {
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200687 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
688 ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port));
689 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690 }
691 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200692 else if (*(int *)&s->be->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200693 /* connect to the defined dispatch addr */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200694 s->srv_addr = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100696 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200697 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100698 if (!(s->flags & SN_FRT_ADDR_SET))
699 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700
Emeric Brunec810d12010-10-22 16:36:33 +0200701 if (s->frt_addr.ss_family == AF_INET) {
702 memcpy(&s->srv_addr, &s->frt_addr, MIN(sizeof(s->srv_addr), sizeof(s->frt_addr)));
703 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100704 /* when we support IPv6 on the backend, we may add other tests */
705 //qfprintf(stderr, "Cannot get original server address.\n");
706 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200707 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100708 else if (s->be->options & PR_O_HTTP_PROXY) {
709 /* If HTTP PROXY option is set, then server is already assigned
710 * during incoming client request parsing. */
711 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100712 else {
713 /* no server and no LB algorithm ! */
714 return SRV_STATUS_INTERNAL;
715 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200716
717 s->flags |= SN_ADDR_SET;
718 return SRV_STATUS_OK;
719}
720
721
722/* This function assigns a server to session <s> if required, and can add the
723 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200724 * If ->srv_conn is set, the session is first released from the server.
725 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
726 * be called before any connection and after any retry or redispatch occurs.
727 *
728 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729 *
730 * Returns :
731 *
732 * SRV_STATUS_OK if everything is OK.
733 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
734 * SRV_STATUS_QUEUED if the connection has been queued.
735 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200736 * connection could not be queued in s->srv,
737 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738 * SRV_STATUS_INTERNAL for other unrecoverable errors.
739 *
740 */
741int assign_server_and_queue(struct session *s)
742{
743 struct pendconn *p;
744 int err;
745
746 if (s->pend_pos)
747 return SRV_STATUS_INTERNAL;
748
Willy Tarreau7c669d72008-06-20 15:04:11 +0200749 err = SRV_STATUS_OK;
750 if (!(s->flags & SN_ASSIGNED)) {
751 err = assign_server(s);
752 if (s->prev_srv) {
753 /* This session was previously assigned to a server. We have to
754 * update the session's and the server's stats :
755 * - if the server changed :
756 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
757 * - set SN_REDISP if it was successfully redispatched
758 * - increment srv->redispatches and be->redispatches
759 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100760 */
761
Willy Tarreau7c669d72008-06-20 15:04:11 +0200762 if (s->prev_srv != s->srv) {
763 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
764 s->txn.flags &= ~TX_CK_MASK;
765 s->txn.flags |= TX_CK_DOWN;
766 }
767 s->flags |= SN_REDISP;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200768 s->prev_srv->counters.redispatches++;
769 s->be->counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200770 } else {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200771 s->prev_srv->counters.retries++;
772 s->be->counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100773 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100774 }
775 }
776
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777 switch (err) {
778 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200779 /* we have SN_ASSIGNED set */
780 if (!s->srv)
781 return SRV_STATUS_OK; /* dispatch or proxy mode */
782
783 /* If we already have a connection slot, no need to check any queue */
784 if (s->srv_conn == s->srv)
785 return SRV_STATUS_OK;
786
787 /* OK, this session already has an assigned server, but no
788 * connection slot yet. Either it is a redispatch, or it was
789 * assigned from persistence information (direct mode).
790 */
791 if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) {
792 /* server scheduled for redirection, and already assigned. We
793 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100794 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200795 sess_change_server(s, s->srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100796 return SRV_STATUS_OK;
797 }
798
Willy Tarreau7c669d72008-06-20 15:04:11 +0200799 /* We might have to queue this session if the assigned server is full.
800 * We know we have to queue it into the server's queue, so if a maxqueue
801 * is set on the server, we must also check that the server's queue is
802 * not full, in which case we have to return FULL.
803 */
804 if (s->srv->maxconn &&
805 (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) {
806
807 if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue)
808 return SRV_STATUS_FULL;
809
Willy Tarreaubaaee002006-06-26 02:48:02 +0200810 p = pendconn_add(s);
811 if (p)
812 return SRV_STATUS_QUEUED;
813 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200814 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200815 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200816
817 /* OK, we can use this server. Let's reserve our place */
818 sess_change_server(s, s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200819 return SRV_STATUS_OK;
820
821 case SRV_STATUS_FULL:
822 /* queue this session into the proxy's queue */
823 p = pendconn_add(s);
824 if (p)
825 return SRV_STATUS_QUEUED;
826 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200827 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828
829 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200830 return err;
831
Willy Tarreaubaaee002006-06-26 02:48:02 +0200832 case SRV_STATUS_INTERNAL:
833 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200834
Willy Tarreaubaaee002006-06-26 02:48:02 +0200835 default:
836 return SRV_STATUS_INTERNAL;
837 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100838}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200839
Willy Tarreaub1d67742010-03-29 19:36:59 +0200840/* If an explicit source binding is specified on the server and/or backend, and
841 * this source makes use of the transparent proxy, then it is extracted now and
842 * assigned to the session's from_addr entry.
843 */
844static void assign_tproxy_address(struct session *s)
845{
846#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
847 if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) {
848 switch (s->srv->state & SRV_TPROXY_MASK) {
849 case SRV_TPROXY_ADDR:
850 s->from_addr = *(struct sockaddr_in *)&s->srv->tproxy_addr;
851 break;
852 case SRV_TPROXY_CLI:
853 case SRV_TPROXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200854 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaub1d67742010-03-29 19:36:59 +0200855 s->from_addr = *(struct sockaddr_in *)&s->cli_addr;
856 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200857 case SRV_TPROXY_DYN:
858 if (s->srv->bind_hdr_occ) {
859 /* bind to the IP in a header */
860 s->from_addr.sin_port = 0;
861 s->from_addr.sin_addr.s_addr = htonl(get_ip_from_hdr2(&s->txn.req,
862 s->srv->bind_hdr_name,
863 s->srv->bind_hdr_len,
864 &s->txn.hdr_idx,
865 s->srv->bind_hdr_occ));
866 }
867 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200868 default:
Willy Tarreaubce70882009-09-07 11:51:47 +0200869 memset(&s->from_addr, 0, sizeof(s->from_addr));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200870 }
871 }
872 else if (s->be->options & PR_O_BIND_SRC) {
873 switch (s->be->options & PR_O_TPXY_MASK) {
874 case PR_O_TPXY_ADDR:
875 s->from_addr = *(struct sockaddr_in *)&s->be->tproxy_addr;
876 break;
877 case PR_O_TPXY_CLI:
878 case PR_O_TPXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200879 /* FIXME: what can we do if the client connects in IPv6 or socket unix? */
Willy Tarreaub1d67742010-03-29 19:36:59 +0200880 s->from_addr = *(struct sockaddr_in *)&s->cli_addr;
881 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200882 case PR_O_TPXY_DYN:
883 if (s->be->bind_hdr_occ) {
884 /* bind to the IP in a header */
885 s->from_addr.sin_port = 0;
886 s->from_addr.sin_addr.s_addr = htonl(get_ip_from_hdr2(&s->txn.req,
887 s->be->bind_hdr_name,
888 s->be->bind_hdr_len,
889 &s->txn.hdr_idx,
890 s->be->bind_hdr_occ));
891 }
892 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200893 default:
Willy Tarreaubce70882009-09-07 11:51:47 +0200894 memset(&s->from_addr, 0, sizeof(s->from_addr));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200895 }
896 }
897#endif
898}
899
900
Willy Tarreaubaaee002006-06-26 02:48:02 +0200901/*
902 * This function initiates a connection to the server assigned to this session
903 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
904 * It can return one of :
905 * - SN_ERR_NONE if everything's OK
906 * - SN_ERR_SRVTO if there are no more servers
907 * - SN_ERR_SRVCL if the connection was refused by the server
908 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
909 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
910 * - SN_ERR_INTERNAL for any other purely internal errors
911 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
912 */
913int connect_server(struct session *s)
914{
Willy Tarreau9650f372009-08-16 14:02:45 +0200915 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200916
917 if (!(s->flags & SN_ADDR_SET)) {
918 err = assign_server_address(s);
919 if (err != SRV_STATUS_OK)
920 return SN_ERR_INTERNAL;
921 }
922
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200923 /* Prepare the stream interface for a TCP connection. Later
924 * we may assign a protocol-specific connect() function.
925 */
926 stream_sock_prepare_interface(s->req->cons);
927 s->req->cons->connect = tcpv4_connect_server;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200928
Willy Tarreaub1d67742010-03-29 19:36:59 +0200929 assign_tproxy_address(s);
930
Willy Tarreau9650f372009-08-16 14:02:45 +0200931 err = s->req->cons->connect(s->req->cons, s->be, s->srv,
932 (struct sockaddr *)&s->srv_addr,
Willy Tarreaub1d67742010-03-29 19:36:59 +0200933 (struct sockaddr *)&s->from_addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934
Willy Tarreau9650f372009-08-16 14:02:45 +0200935 if (err != SN_ERR_NONE)
936 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200937
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938 if (s->srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100939 s->flags |= SN_CURR_SESS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200940 s->srv->cur_sess++;
Willy Tarreauac68c5d2009-10-04 23:12:44 +0200941 if (s->srv->cur_sess > s->srv->counters.cur_sess_max)
942 s->srv->counters.cur_sess_max = s->srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100943 if (s->be->lbprm.server_take_conn)
944 s->be->lbprm.server_take_conn(s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945 }
946
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947 return SN_ERR_NONE; /* connection is OK */
948}
949
950
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951/* This function performs the "redispatch" part of a connection attempt. It
952 * will assign a server if required, queue the connection if required, and
953 * handle errors that might arise at this level. It can change the server
954 * state. It will return 1 if it encounters an error, switches the server
955 * state, or has to queue a connection. Otherwise, it will return 0 indicating
956 * that the connection is ready to use.
957 */
958
959int srv_redispatch_connect(struct session *t)
960{
961 int conn_err;
962
963 /* We know that we don't have any connection pending, so we will
964 * try to get a new one, and wait in this state if it's queued
965 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200966 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200967 conn_err = assign_server_and_queue(t);
968 switch (conn_err) {
969 case SRV_STATUS_OK:
970 break;
971
Willy Tarreau7c669d72008-06-20 15:04:11 +0200972 case SRV_STATUS_FULL:
973 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
974 * and we can redispatch to another server, or it is not and we return
975 * 503. This only makes sense in DIRECT mode however, because normal LB
976 * algorithms would never select such a server, and hash algorithms
977 * would bring us on the same server again. Note that t->srv is set in
978 * this case.
979 */
Willy Tarreau4de91492010-01-22 19:10:05 +0100980 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
981 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200982 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
983 t->prev_srv = t->srv;
984 goto redispatch;
985 }
986
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200987 if (!t->req->cons->err_type) {
988 t->req->cons->err_type = SI_ET_QUEUE_ERR;
989 t->req->cons->err_loc = t->srv;
990 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200991
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200992 t->srv->counters.failed_conns++;
993 t->be->counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200994 return 1;
995
Willy Tarreaubaaee002006-06-26 02:48:02 +0200996 case SRV_STATUS_NOSRV:
997 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200998 if (!t->req->cons->err_type) {
999 t->req->cons->err_type = SI_ET_CONN_ERR;
1000 t->req->cons->err_loc = NULL;
1001 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001002
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001003 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001004 return 1;
1005
1006 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001007 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001008 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001009 /* do nothing else and do not wake any other session up */
1010 return 1;
1011
Willy Tarreaubaaee002006-06-26 02:48:02 +02001012 case SRV_STATUS_INTERNAL:
1013 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001014 if (!t->req->cons->err_type) {
1015 t->req->cons->err_type = SI_ET_CONN_OTHER;
1016 t->req->cons->err_loc = t->srv;
1017 }
1018
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019 if (t->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +01001020 srv_inc_sess_ctr(t->srv);
Willy Tarreau98937b82007-12-10 15:05:42 +01001021 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001022 t->srv->counters.failed_conns++;
1023 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024
1025 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001026 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001027 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001028 return 1;
1029 }
1030 /* if we get here, it's because we got SRV_STATUS_OK, which also
1031 * means that the connection has not been queued.
1032 */
1033 return 0;
1034}
1035
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001036/* Apply RDP cookie persistence to the current session. For this, the function
1037 * tries to extract an RDP cookie from the request buffer, and look for the
1038 * matching server in the list. If the server is found, it is assigned to the
1039 * session. This always returns 1, and the analyser removes itself from the
1040 * list. Nothing is performed if a server was already assigned.
1041 */
1042int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1043{
1044 struct proxy *px = s->be;
1045 int ret;
1046 struct acl_expr expr;
1047 struct acl_test test;
1048 struct server *srv = px->srv;
1049 struct sockaddr_in addr;
1050 char *p;
1051
1052 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1053 now_ms, __FUNCTION__,
1054 s,
1055 req,
1056 req->rex, req->wex,
1057 req->flags,
1058 req->l,
1059 req->analysers);
1060
1061 if (s->flags & SN_ASSIGNED)
1062 goto no_cookie;
1063
1064 memset(&expr, 0, sizeof(expr));
1065 memset(&test, 0, sizeof(test));
1066
1067 expr.arg.str = s->be->rdp_cookie_name;
1068 expr.arg_len = s->be->rdp_cookie_len;
1069
1070 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
1071 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
1072 goto no_cookie;
1073
1074 memset(&addr, 0, sizeof(addr));
1075 addr.sin_family = AF_INET;
1076
1077 /* Considering an rdp cookie detected using acl, test.ptr ended with <cr><lf> and should return */
1078 addr.sin_addr.s_addr = strtoul(test.ptr, &p, 10);
1079 if (*p != '.')
1080 goto no_cookie;
1081 p++;
1082 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1083 if (*p != '.')
1084 goto no_cookie;
1085
1086 while (srv) {
1087 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1088 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1089 /* we found the server and it is usable */
1090 s->flags |= SN_DIRECT | SN_ASSIGNED;
1091 s->srv = srv;
1092 break;
1093 }
1094 }
1095 srv = srv->next;
1096 }
1097
1098no_cookie:
1099 req->analysers &= ~an_bit;
1100 req->analyse_exp = TICK_ETERNITY;
1101 return 1;
1102}
1103
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001104int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001105 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001106 return px->down_time;
1107
1108 return now.tv_sec - px->last_change + px->down_time;
1109}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001110
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001111/*
1112 * This function returns a string containing the balancing
1113 * mode of the proxy in a format suitable for stats.
1114 */
1115
1116const char *backend_lb_algo_str(int algo) {
1117
1118 if (algo == BE_LB_ALGO_RR)
1119 return "roundrobin";
1120 else if (algo == BE_LB_ALGO_SRR)
1121 return "static-rr";
1122 else if (algo == BE_LB_ALGO_LC)
1123 return "leastconn";
1124 else if (algo == BE_LB_ALGO_SH)
1125 return "source";
1126 else if (algo == BE_LB_ALGO_UH)
1127 return "uri";
1128 else if (algo == BE_LB_ALGO_PH)
1129 return "url_param";
1130 else if (algo == BE_LB_ALGO_HH)
1131 return "hdr";
1132 else if (algo == BE_LB_ALGO_RCH)
1133 return "rdp-cookie";
1134 else
1135 return NULL;
1136}
1137
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001138/* This function parses a "balance" statement in a backend section describing
1139 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1140 * returns -1, it may write an error message into ther <err> buffer, for at
1141 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1142 * written. The function must be called with <args> pointing to the first word
1143 * after "balance".
1144 */
1145int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1146{
1147 if (!*(args[0])) {
1148 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001149 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1150 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001151 return 0;
1152 }
1153
1154 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001155 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1156 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001157 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001158 else if (!strcmp(args[0], "static-rr")) {
1159 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1160 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1161 }
Willy Tarreau51406232008-03-10 22:04:20 +01001162 else if (!strcmp(args[0], "leastconn")) {
1163 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1164 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1165 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001166 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001167 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1168 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001169 }
1170 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001171 int arg = 1;
1172
Willy Tarreau31682232007-11-29 15:38:04 +01001173 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1174 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001175
1176 while (*args[arg]) {
1177 if (!strcmp(args[arg], "len")) {
1178 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1179 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1180 return -1;
1181 }
1182 curproxy->uri_len_limit = atoi(args[arg+1]);
1183 arg += 2;
1184 }
1185 else if (!strcmp(args[arg], "depth")) {
1186 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1187 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1188 return -1;
1189 }
1190 /* hint: we store the position of the ending '/' (depth+1) so
1191 * that we avoid a comparison while computing the hash.
1192 */
1193 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1194 arg += 2;
1195 }
1196 else {
1197 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1198 return -1;
1199 }
1200 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001201 }
Willy Tarreau01732802007-11-01 22:48:15 +01001202 else if (!strcmp(args[0], "url_param")) {
1203 if (!*args[1]) {
1204 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1205 return -1;
1206 }
Willy Tarreau31682232007-11-29 15:38:04 +01001207 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1208 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001209
1210 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001211 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001212 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001213 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001214 if (strcmp(args[2], "check_post")) {
1215 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1216 return -1;
1217 }
1218 if (*args[3]) {
1219 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1220 curproxy->url_param_post_limit = str2ui(args[3]);
1221 }
1222 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1223 if (!curproxy->url_param_post_limit)
1224 curproxy->url_param_post_limit = 48;
1225 else if ( curproxy->url_param_post_limit < 3 )
1226 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1227 }
Benoitaffb4812009-03-25 13:02:10 +01001228 }
1229 else if (!strncmp(args[0], "hdr(", 4)) {
1230 const char *beg, *end;
1231
1232 beg = args[0] + 4;
1233 end = strchr(beg, ')');
1234
1235 if (!end || end == beg) {
1236 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1237 return -1;
1238 }
1239
1240 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1241 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1242
1243 free(curproxy->hh_name);
1244 curproxy->hh_len = end - beg;
1245 curproxy->hh_name = my_strndup(beg, end - beg);
1246 curproxy->hh_match_domain = 0;
1247
1248 if (*args[1]) {
1249 if (strcmp(args[1], "use_domain_only")) {
1250 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1251 return -1;
1252 }
1253 curproxy->hh_match_domain = 1;
1254 }
1255
Emeric Brun736aa232009-06-30 17:56:00 +02001256 }
1257 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1258 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1259 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1260
1261 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1262 const char *beg, *end;
1263
1264 beg = args[0] + 11;
1265 end = strchr(beg, ')');
1266
1267 if (!end || end == beg) {
1268 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1269 return -1;
1270 }
1271
1272 free(curproxy->hh_name);
1273 curproxy->hh_name = my_strndup(beg, end - beg);
1274 curproxy->hh_len = end - beg;
1275 }
1276 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1277 free(curproxy->hh_name);
1278 curproxy->hh_name = strdup("mstshash");
1279 curproxy->hh_len = strlen(curproxy->hh_name);
1280 }
1281 else { /* syntax */
1282 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1283 return -1;
1284 }
Willy Tarreau01732802007-11-01 22:48:15 +01001285 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001286 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001287 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 +01001288 return -1;
1289 }
1290 return 0;
1291}
1292
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001293
1294/************************************************************************/
1295/* All supported keywords must be declared here. */
1296/************************************************************************/
1297
1298/* set test->i to the number of enabled servers on the proxy */
1299static int
1300acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1301 struct acl_expr *expr, struct acl_test *test)
1302{
1303 test->flags = ACL_TEST_F_VOL_TEST;
1304 if (expr->arg_len) {
1305 /* another proxy was designated, we must look for it */
1306 for (px = proxy; px; px = px->next)
1307 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1308 break;
1309 }
1310 if (!px)
1311 return 0;
1312
1313 if (px->srv_act)
1314 test->i = px->srv_act;
1315 else if (px->lbprm.fbck)
1316 test->i = 1;
1317 else
1318 test->i = px->srv_bck;
1319
1320 return 1;
1321}
1322
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001323/* report in test->flags a success or failure depending on the designated
1324 * server's state. There is no match function involved since there's no pattern.
1325 */
1326static int
1327acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, int dir,
1328 struct acl_expr *expr, struct acl_test *test)
1329{
1330 struct server *srv = expr->arg.srv;
1331
1332 test->flags = ACL_TEST_F_VOL_TEST;
1333 if (!(srv->state & SRV_MAINTAIN) &&
1334 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
1335 test->flags |= ACL_TEST_F_SET_RES_PASS;
1336 else
1337 test->flags |= ACL_TEST_F_SET_RES_FAIL;
1338 return 1;
1339}
1340
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001341/* set test->i to the number of enabled servers on the proxy */
1342static int
1343acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1344 struct acl_expr *expr, struct acl_test *test)
1345{
1346 struct server *iterator;
1347 test->flags = ACL_TEST_F_VOL_TEST;
1348 if (expr->arg_len) {
1349 /* another proxy was designated, we must look for it */
1350 for (px = proxy; px; px = px->next)
1351 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1352 break;
1353 }
1354 if (!px)
1355 return 0;
1356
1357 test->i = 0;
1358 iterator = px->srv;
1359 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001360 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001361 iterator = iterator->next;
1362 continue;
1363 }
1364 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
1365 test->i = -1;
1366 return 1;
1367 }
1368
1369 test->i += (iterator->maxconn - iterator->cur_sess)
1370 + (iterator->maxqueue - iterator->nbpend);
1371 iterator = iterator->next;
1372 }
1373
1374 return 1;
1375}
1376
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001377/* set test->i to the number of connections per second reaching the backend */
1378static int
1379acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1380 struct acl_expr *expr, struct acl_test *test)
1381{
1382 test->flags = ACL_TEST_F_VOL_TEST;
1383 if (expr->arg_len) {
1384 /* another proxy was designated, we must look for it */
1385 for (px = proxy; px; px = px->next)
1386 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1387 break;
1388 }
1389 if (!px)
1390 return 0;
1391
1392 test->i = read_freq_ctr(&px->be_sess_per_sec);
1393 return 1;
1394}
1395
Willy Tarreaua36af912009-10-10 12:02:45 +02001396/* set test->i to the number of concurrent connections on the backend */
1397static int
1398acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1399 struct acl_expr *expr, struct acl_test *test)
1400{
1401 test->flags = ACL_TEST_F_VOL_TEST;
1402 if (expr->arg_len) {
1403 /* another proxy was designated, we must look for it */
1404 for (px = proxy; px; px = px->next)
1405 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1406 break;
1407 }
1408 if (!px)
1409 return 0;
1410
1411 test->i = px->beconn;
1412 return 1;
1413}
1414
1415/* set test->i to the total number of queued connections on the backend */
1416static int
1417acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1418 struct acl_expr *expr, struct acl_test *test)
1419{
1420 test->flags = ACL_TEST_F_VOL_TEST;
1421 if (expr->arg_len) {
1422 /* another proxy was designated, we must look for it */
1423 for (px = proxy; px; px = px->next)
1424 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1425 break;
1426 }
1427 if (!px)
1428 return 0;
1429
1430 test->i = px->totpend;
1431 return 1;
1432}
1433
1434/* set test->i to the total number of queued connections on the backend divided
1435 * by the number of running servers and rounded up. If there is no running
1436 * server, we return twice the total, just as if we had half a running server.
1437 * This is more or less correct anyway, since we expect the last server to come
1438 * back soon.
1439 */
1440static int
1441acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1442 struct acl_expr *expr, struct acl_test *test)
1443{
1444 int nbsrv;
1445
1446 test->flags = ACL_TEST_F_VOL_TEST;
1447 if (expr->arg_len) {
1448 /* another proxy was designated, we must look for it */
1449 for (px = proxy; px; px = px->next)
1450 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1451 break;
1452 }
1453 if (!px)
1454 return 0;
1455
1456 if (px->srv_act)
1457 nbsrv = px->srv_act;
1458 else if (px->lbprm.fbck)
1459 nbsrv = 1;
1460 else
1461 nbsrv = px->srv_bck;
1462
1463 if (nbsrv > 0)
1464 test->i = (px->totpend + nbsrv - 1) / nbsrv;
1465 else
1466 test->i = px->totpend * 2;
1467
1468 return 1;
1469}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001470
1471/* Note: must not be declared <const> as its list will be overwritten */
1472static struct acl_kw_list acl_kws = {{ },{
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001473 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau3a8efeb2009-03-05 19:15:37 +01001474 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001475 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua36af912009-10-10 12:02:45 +02001476 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1477 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1478 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001479 { "srv_is_up", acl_parse_nothing, acl_fetch_srv_is_up, acl_match_nothing, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001480 { NULL, NULL, NULL, NULL },
1481}};
1482
1483
1484__attribute__((constructor))
1485static void __backend_init(void)
1486{
1487 acl_register_keywords(&acl_kws);
1488}
1489
Willy Tarreaubaaee002006-06-26 02:48:02 +02001490/*
1491 * Local variables:
1492 * c-indent-level: 8
1493 * c-basic-offset: 8
1494 * End:
1495 */