blob: 1a8653762c2aa4d52ad0b5127bdc8e89b549cb69 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreauf89c1872009-10-01 11:19:37 +02004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <syslog.h>
Willy Tarreauf19cf372006-11-14 15:40:51 +010018#include <string.h>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +020019#include <ctype.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040020#include <sys/types.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020023#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020024#include <common/debug.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020025#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010030#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <proto/backend.h>
Willy Tarreau14c8aac2007-05-08 19:46:30 +020032#include <proto/client.h>
Willy 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 Tarreaue8c66af2008-01-13 18:40:14 +010038#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 Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/task.h>
43
Willy Tarreaubaaee002006-06-26 02:48:02 +020044/*
45 * This function recounts the number of usable active and backup servers for
46 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010047 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010048 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010049 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020050 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020051void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020052{
53 struct server *srv;
54
Willy Tarreau20697042007-11-15 23:26:18 +010055 px->srv_act = px->srv_bck = 0;
56 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010057 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010059 if (!srv_is_usable(srv->state, srv->eweight))
60 continue;
61
62 if (srv->state & SRV_BACKUP) {
63 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010064 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010065 px->lbprm.fbck = srv;
66 px->srv_bck++;
67 px->lbprm.tot_wbck += srv->eweight;
68 } else {
69 px->srv_act++;
70 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020071 }
72 }
Willy Tarreaub625a082007-11-26 01:15:43 +010073}
Willy Tarreau20697042007-11-15 23:26:18 +010074
Willy Tarreaub625a082007-11-26 01:15:43 +010075/* This function simply updates the backend's tot_weight and tot_used values
76 * after servers weights have been updated. It is designed to be used after
77 * recount_servers() or equivalent.
78 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020079void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010080{
Willy Tarreau20697042007-11-15 23:26:18 +010081 if (px->srv_act) {
82 px->lbprm.tot_weight = px->lbprm.tot_wact;
83 px->lbprm.tot_used = px->srv_act;
84 }
Willy Tarreaub625a082007-11-26 01:15:43 +010085 else if (px->lbprm.fbck) {
86 /* use only the first backup server */
87 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
88 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010089 }
90 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010091 px->lbprm.tot_weight = px->lbprm.tot_wbck;
92 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +010093 }
Willy Tarreaub625a082007-11-26 01:15:43 +010094}
95
Willy Tarreau39c9ba72009-10-01 21:11:15 +020096/*
97 * This function tries to find a running server for the proxy <px> following
98 * the source hash method. Depending on the number of active/backup servers,
99 * it will either look for active servers, or for backup servers.
100 * If any server is found, it will be returned. If no valid server is found,
101 * NULL is returned.
102 */
103struct server *get_server_sh(struct proxy *px, const char *addr, int len)
104{
105 unsigned int h, l;
106
107 if (px->lbprm.tot_weight == 0)
108 return NULL;
109
110 l = h = 0;
111
112 /* note: we won't hash if there's only one server left */
113 if (px->lbprm.tot_used == 1)
114 goto hash_done;
115
116 while ((l + sizeof (int)) <= len) {
117 h ^= ntohl(*(unsigned int *)(&addr[l]));
118 l += sizeof (int);
119 }
120 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200121 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
122 return chash_get_server_hash(px, h);
123 else
124 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200125}
126
127/*
128 * This function tries to find a running server for the proxy <px> following
129 * the URI hash method. In order to optimize cache hits, the hash computation
130 * ends at the question mark. Depending on the number of active/backup servers,
131 * it will either look for active servers, or for backup servers.
132 * If any server is found, it will be returned. If no valid server is found,
133 * NULL is returned.
134 *
135 * This code was contributed by Guillaume Dallaire, who also selected this hash
136 * algorithm out of a tens because it gave him the best results.
137 *
138 */
139struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
140{
141 unsigned long hash = 0;
142 int c;
143 int slashes = 0;
144
145 if (px->lbprm.tot_weight == 0)
146 return NULL;
147
148 /* note: we won't hash if there's only one server left */
149 if (px->lbprm.tot_used == 1)
150 goto hash_done;
151
152 if (px->uri_len_limit)
153 uri_len = MIN(uri_len, px->uri_len_limit);
154
155 while (uri_len--) {
156 c = *uri++;
157 if (c == '/') {
158 slashes++;
159 if (slashes == px->uri_dirs_depth1) /* depth+1 */
160 break;
161 }
162 else if (c == '?')
163 break;
164
165 hash = c + (hash << 6) + (hash << 16) - hash;
166 }
167 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200168 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
169 return chash_get_server_hash(px, hash);
170 else
171 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200172}
173
Willy Tarreau01732802007-11-01 22:48:15 +0100174/*
175 * This function tries to find a running server for the proxy <px> following
176 * the URL parameter hash method. It looks for a specific parameter in the
177 * URL and hashes it to compute the server ID. This is useful to optimize
178 * performance by avoiding bounces between servers in contexts where sessions
179 * are shared but cookies are not usable. If the parameter is not found, NULL
180 * is returned. If any server is found, it will be returned. If no valid server
181 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100182 */
183struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
184{
185 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200186 const char *p;
187 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100188 int plen;
189
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200190 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100191 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100192 return NULL;
193
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200194 if ((p = memchr(uri, '?', uri_len)) == NULL)
195 return NULL;
196
Willy Tarreau01732802007-11-01 22:48:15 +0100197 p++;
198
199 uri_len -= (p - uri);
200 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200201 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100202
203 while (uri_len > plen) {
204 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200205 if (params[plen] == '=') {
206 if (memcmp(params, px->url_param_name, plen) == 0) {
207 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100208 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200209 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100210 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200211 p += plen + 1;
212 uri_len -= plen + 1;
213
Willy Tarreau01732802007-11-01 22:48:15 +0100214 while (uri_len && *p != '&') {
215 hash = *p + (hash << 6) + (hash << 16) - hash;
216 uri_len--;
217 p++;
218 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200219 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
220 return chash_get_server_hash(px, hash);
221 else
222 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100223 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200224 }
225 /* skip to next parameter */
226 p = memchr(params, '&', uri_len);
227 if (!p)
228 return NULL;
229 p++;
230 uri_len -= (p - params);
231 params = p;
232 }
233 return NULL;
234}
235
236/*
237 * this does the same as the previous server_ph, but check the body contents
238 */
239struct server *get_server_ph_post(struct session *s)
240{
241 unsigned long hash = 0;
242 struct http_txn *txn = &s->txn;
243 struct buffer *req = s->req;
244 struct http_msg *msg = &txn->req;
245 struct proxy *px = s->be;
246 unsigned int plen = px->url_param_len;
Willy Tarreau157dd632009-12-06 19:18:09 +0100247 unsigned long len = msg->hdr_content_len;
248 const char *params = req->data + msg->sov;
249 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200250
Willy Tarreau7c96f672009-12-27 22:47:25 +0100251 if (len > req->l - (msg->sov - msg->som))
252 len = req->l - (msg->sov - msg->som);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200253
Willy Tarreau157dd632009-12-06 19:18:09 +0100254 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200255 return NULL;
256
Willy Tarreau157dd632009-12-06 19:18:09 +0100257 if (px->lbprm.tot_weight == 0)
258 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200259
260 while (len > plen) {
261 /* Look for the parameter name followed by an equal symbol */
262 if (params[plen] == '=') {
263 if (memcmp(params, px->url_param_name, plen) == 0) {
264 /* OK, we have the parameter here at <params>, and
265 * the value after the equal sign, at <p>
266 * skip the equal symbol
267 */
268 p += plen + 1;
269 len -= plen + 1;
270
271 while (len && *p != '&') {
272 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100273 /* if in a POST, body must be URI encoded or it's not a URI.
274 * Do not interprete any possible binary data as a parameter.
275 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200276 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
277 break;
278 return NULL; /* oh, no; this is not uri-encoded.
279 * This body does not contain parameters.
280 */
281 }
282 hash = *p + (hash << 6) + (hash << 16) - hash;
283 len--;
284 p++;
285 /* should we break if vlen exceeds limit? */
286 }
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200287 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
288 return chash_get_server_hash(px, hash);
289 else
290 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200291 }
292 }
Willy Tarreau01732802007-11-01 22:48:15 +0100293 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200294 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100295 if (!p)
296 return NULL;
297 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200298 len -= (p - params);
299 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100300 }
301 return NULL;
302}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200303
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200304
Willy Tarreaubaaee002006-06-26 02:48:02 +0200305/*
Benoitaffb4812009-03-25 13:02:10 +0100306 * This function tries to find a running server for the proxy <px> following
307 * the Header parameter hash method. It looks for a specific parameter in the
308 * URL and hashes it to compute the server ID. This is useful to optimize
309 * performance by avoiding bounces between servers in contexts where sessions
310 * are shared but cookies are not usable. If the parameter is not found, NULL
311 * is returned. If any server is found, it will be returned. If no valid server
312 * is found, NULL is returned.
313 */
314struct server *get_server_hh(struct session *s)
315{
316 unsigned long hash = 0;
317 struct http_txn *txn = &s->txn;
318 struct http_msg *msg = &txn->req;
319 struct proxy *px = s->be;
320 unsigned int plen = px->hh_len;
321 unsigned long len;
322 struct hdr_ctx ctx;
323 const char *p;
324
325 /* tot_weight appears to mean srv_count */
326 if (px->lbprm.tot_weight == 0)
327 return NULL;
328
Benoitaffb4812009-03-25 13:02:10 +0100329 ctx.idx = 0;
330
331 /* if the message is chunked, we skip the chunk size, but use the value as len */
332 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
333
334 /* if the header is not found or empty, let's fallback to round robin */
335 if (!ctx.idx || !ctx.vlen)
336 return NULL;
337
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200338 /* note: we won't hash if there's only one server left */
339 if (px->lbprm.tot_used == 1)
340 goto hash_done;
341
Benoitaffb4812009-03-25 13:02:10 +0100342 /* Found a the hh_name in the headers.
343 * we will compute the hash based on this value ctx.val.
344 */
345 len = ctx.vlen;
346 p = (char *)ctx.line + ctx.val;
347 if (!px->hh_match_domain) {
348 while (len) {
349 hash = *p + (hash << 6) + (hash << 16) - hash;
350 len--;
351 p++;
352 }
353 } else {
354 int dohash = 0;
355 p += len - 1;
356 /* special computation, use only main domain name, not tld/host
357 * going back from the end of string, start hashing at first
358 * dot stop at next.
359 * This is designed to work with the 'Host' header, and requires
360 * a special option to activate this.
361 */
362 while (len) {
363 if (*p == '.') {
364 if (!dohash)
365 dohash = 1;
366 else
367 break;
368 } else {
369 if (dohash)
370 hash = *p + (hash << 6) + (hash << 16) - hash;
371 }
372 len--;
373 p--;
374 }
375 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200376 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200377 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
378 return chash_get_server_hash(px, hash);
379 else
380 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100381}
382
Emeric Brun736aa232009-06-30 17:56:00 +0200383struct server *get_server_rch(struct session *s)
384{
385 unsigned long hash = 0;
386 struct proxy *px = s->be;
387 unsigned long len;
388 const char *p;
389 int ret;
390 struct acl_expr expr;
391 struct acl_test test;
392
393 /* tot_weight appears to mean srv_count */
394 if (px->lbprm.tot_weight == 0)
395 return NULL;
396
Emeric Brun736aa232009-06-30 17:56:00 +0200397 memset(&expr, 0, sizeof(expr));
398 memset(&test, 0, sizeof(test));
399
400 expr.arg.str = px->hh_name;
401 expr.arg_len = px->hh_len;
402
403 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
404 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
405 return NULL;
406
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200407 /* note: we won't hash if there's only one server left */
408 if (px->lbprm.tot_used == 1)
409 goto hash_done;
410
Emeric Brun736aa232009-06-30 17:56:00 +0200411 /* Found a the hh_name in the headers.
412 * we will compute the hash based on this value ctx.val.
413 */
414 len = test.len;
415 p = (char *)test.ptr;
416 while (len) {
417 hash = *p + (hash << 6) + (hash << 16) - hash;
418 len--;
419 p++;
420 }
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200421 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200422 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
423 return chash_get_server_hash(px, hash);
424 else
425 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200426}
Benoitaffb4812009-03-25 13:02:10 +0100427
428/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200429 * This function applies the load-balancing algorithm to the session, as
430 * defined by the backend it is assigned to. The session is then marked as
431 * 'assigned'.
432 *
433 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
434 * had a server previously assigned, it is rebalanced, trying to avoid the same
435 * server.
436 * The function tries to keep the original connection slot if it reconnects to
437 * the same server, otherwise it releases it and tries to offer it.
438 *
439 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440 *
441 * It may return :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200442 * SRV_STATUS_OK if everything is OK. Session assigned to ->srv
443 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
444 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445 * SRV_STATUS_INTERNAL for other unrecoverable errors.
446 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200447 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
448 * it does not need to be called anymore. This means that s->srv can be trusted
449 * in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450 *
451 */
452
453int assign_server(struct session *s)
454{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100455
Willy Tarreau7c669d72008-06-20 15:04:11 +0200456 struct server *conn_slot;
457 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100458
Willy Tarreaubaaee002006-06-26 02:48:02 +0200459#ifdef DEBUG_FULL
460 fprintf(stderr,"assign_server : s=%p\n",s);
461#endif
462
Willy Tarreau7c669d72008-06-20 15:04:11 +0200463 err = SRV_STATUS_INTERNAL;
464 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
465 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100466
Willy Tarreau7c669d72008-06-20 15:04:11 +0200467 s->prev_srv = s->prev_srv;
468 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469
Willy Tarreau7c669d72008-06-20 15:04:11 +0200470 /* We have to release any connection slot before applying any LB algo,
471 * otherwise we may erroneously end up with no available slot.
472 */
473 if (conn_slot)
474 sess_change_server(s, NULL);
475
476 /* We will now try to find the good server and store it into <s->srv>.
477 * Note that <s->srv> may be NULL in case of dispatch or proxy mode,
478 * as well as if no server is available (check error code).
479 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100480
Willy Tarreau7c669d72008-06-20 15:04:11 +0200481 s->srv = NULL;
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200482 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200483 int len;
484 /* we must check if we have at least one server available */
485 if (!s->be->lbprm.tot_weight) {
486 err = SRV_STATUS_NOSRV;
487 goto out;
488 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200489
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200490 /* First check whether we need to fetch some data or simply call
491 * the LB lookup function. Only the hashing functions will need
492 * some input data in fact, and will support multiple algorithms.
493 */
494 switch (s->be->lbprm.algo & BE_LB_LKUP) {
495 case BE_LB_LKUP_RRTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200496 s->srv = fwrr_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200497 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200498
499 case BE_LB_LKUP_LCTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200500 s->srv = fwlc_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200501 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200502
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200503 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200504 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200505 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200506 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
507 s->srv = chash_get_next_server(s->be, s->prev_srv);
508 else
509 s->srv = map_get_server_rr(s->be, s->prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200510 break;
511 }
512 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200513 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200514 err = SRV_STATUS_INTERNAL;
515 goto out;
516 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200517
518 switch (s->be->lbprm.algo & BE_LB_PARM) {
519 case BE_LB_HASH_SRC:
520 if (s->cli_addr.ss_family == AF_INET)
521 len = 4;
522 else if (s->cli_addr.ss_family == AF_INET6)
523 len = 16;
524 else {
525 /* unknown IP family */
526 err = SRV_STATUS_INTERNAL;
527 goto out;
528 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200530 s->srv = get_server_sh(s->be,
531 (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
532 len);
533 break;
534
535 case BE_LB_HASH_URI:
536 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100537 if (s->txn.req.msg_state < HTTP_MSG_BODY)
538 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200539 s->srv = get_server_uh(s->be,
Willy Tarreau962c3f42010-01-10 00:15:35 +0100540 s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200541 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200542 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200543
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200544 case BE_LB_HASH_PRM:
545 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100546 if (s->txn.req.msg_state < HTTP_MSG_BODY)
547 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200548 if (s->txn.meth == HTTP_METH_POST &&
Willy Tarreau962c3f42010-01-10 00:15:35 +0100549 memchr(s->txn.req.sol + s->txn.req.sl.rq.u, '&',
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200550 s->txn.req.sl.rq.u_l ) == NULL)
551 s->srv = get_server_ph_post(s);
552 else
553 s->srv = get_server_ph(s->be,
Willy Tarreau962c3f42010-01-10 00:15:35 +0100554 s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200555 s->txn.req.sl.rq.u_l);
556 break;
Benoitaffb4812009-03-25 13:02:10 +0100557
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200558 case BE_LB_HASH_HDR:
559 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100560 if (s->txn.req.msg_state < HTTP_MSG_BODY)
561 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200562 s->srv = get_server_hh(s);
563 break;
564
565 case BE_LB_HASH_RDP:
566 /* RDP Cookie hashing */
567 s->srv = get_server_rch(s);
568 break;
569
570 default:
571 /* unknown balancing algorithm */
572 err = SRV_STATUS_INTERNAL;
573 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100574 }
Emeric Brun736aa232009-06-30 17:56:00 +0200575
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200576 /* If the hashing parameter was not found, let's fall
577 * back to round robin on the map.
578 */
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200579 if (!s->srv) {
580 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
581 s->srv = chash_get_next_server(s->be, s->prev_srv);
582 else
583 s->srv = map_get_server_rr(s->be, s->prev_srv);
584 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200585
586 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200587 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200588
Willy Tarreau7c669d72008-06-20 15:04:11 +0200589 default:
590 /* unknown balancing algorithm */
591 err = SRV_STATUS_INTERNAL;
592 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200593 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200594
595 if (!s->srv) {
596 err = SRV_STATUS_FULL;
597 goto out;
598 }
599 else if (s->srv != s->prev_srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200600 s->be->counters.cum_lbconn++;
601 s->srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100602 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200603 }
604 else if (s->be->options & PR_O_HTTP_PROXY) {
605 if (!s->srv_addr.sin_addr.s_addr) {
606 err = SRV_STATUS_NOSRV;
607 goto out;
Willy Tarreau5d65bbb2007-01-21 12:47:26 +0100608 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200609 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200610 else if (!*(int *)&s->be->dispatch_addr.sin_addr &&
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100611 !(s->be->options & PR_O_TRANSP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200612 err = SRV_STATUS_NOSRV;
613 goto out;
614 }
615
616 s->flags |= SN_ASSIGNED;
617 err = SRV_STATUS_OK;
618 out:
619
620 /* Either we take back our connection slot, or we offer it to someone
621 * else if we don't need it anymore.
622 */
623 if (conn_slot) {
624 if (conn_slot == s->srv) {
625 sess_change_server(s, s->srv);
626 } else {
627 if (may_dequeue_tasks(conn_slot, s->be))
628 process_srv_queue(conn_slot);
629 }
630 }
631
632 out_err:
633 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634}
635
636
637/*
638 * This function assigns a server address to a session, and sets SN_ADDR_SET.
639 * The address is taken from the currently assigned server, or from the
640 * dispatch or transparent address.
641 *
642 * It may return :
643 * SRV_STATUS_OK if everything is OK.
644 * SRV_STATUS_INTERNAL for other unrecoverable errors.
645 *
646 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
647 * not cleared, so it's to the caller to clear it if required.
648 *
649 */
650int assign_server_address(struct session *s)
651{
652#ifdef DEBUG_FULL
653 fprintf(stderr,"assign_server_address : s=%p\n",s);
654#endif
655
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200656 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657 /* A server is necessarily known for this session */
658 if (!(s->flags & SN_ASSIGNED))
659 return SRV_STATUS_INTERNAL;
660
661 s->srv_addr = s->srv->addr;
662
663 /* if this server remaps proxied ports, we'll use
664 * the port the client connected to with an offset. */
665 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100666 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200667 get_frt_addr(s);
668 if (s->frt_addr.ss_family == AF_INET) {
669 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
670 ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port));
671 } else {
672 s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) +
673 ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port));
674 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 }
676 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200677 else if (*(int *)&s->be->dispatch_addr.sin_addr) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200678 /* connect to the defined dispatch addr */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200679 s->srv_addr = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100681 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100683 if (!(s->flags & SN_FRT_ADDR_SET))
684 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200685
Willy Tarreaubd414282008-01-19 13:46:35 +0100686 memcpy(&s->srv_addr, &s->frt_addr, MIN(sizeof(s->srv_addr), sizeof(s->frt_addr)));
687 /* when we support IPv6 on the backend, we may add other tests */
688 //qfprintf(stderr, "Cannot get original server address.\n");
689 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100691 else if (s->be->options & PR_O_HTTP_PROXY) {
692 /* If HTTP PROXY option is set, then server is already assigned
693 * during incoming client request parsing. */
694 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100695 else {
696 /* no server and no LB algorithm ! */
697 return SRV_STATUS_INTERNAL;
698 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200699
700 s->flags |= SN_ADDR_SET;
701 return SRV_STATUS_OK;
702}
703
704
705/* This function assigns a server to session <s> if required, and can add the
706 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200707 * If ->srv_conn is set, the session is first released from the server.
708 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
709 * be called before any connection and after any retry or redispatch occurs.
710 *
711 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200712 *
713 * Returns :
714 *
715 * SRV_STATUS_OK if everything is OK.
716 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
717 * SRV_STATUS_QUEUED if the connection has been queued.
718 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200719 * connection could not be queued in s->srv,
720 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200721 * SRV_STATUS_INTERNAL for other unrecoverable errors.
722 *
723 */
724int assign_server_and_queue(struct session *s)
725{
726 struct pendconn *p;
727 int err;
728
729 if (s->pend_pos)
730 return SRV_STATUS_INTERNAL;
731
Willy Tarreau7c669d72008-06-20 15:04:11 +0200732 err = SRV_STATUS_OK;
733 if (!(s->flags & SN_ASSIGNED)) {
734 err = assign_server(s);
735 if (s->prev_srv) {
736 /* This session was previously assigned to a server. We have to
737 * update the session's and the server's stats :
738 * - if the server changed :
739 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
740 * - set SN_REDISP if it was successfully redispatched
741 * - increment srv->redispatches and be->redispatches
742 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100743 */
744
Willy Tarreau7c669d72008-06-20 15:04:11 +0200745 if (s->prev_srv != s->srv) {
746 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
747 s->txn.flags &= ~TX_CK_MASK;
748 s->txn.flags |= TX_CK_DOWN;
749 }
750 s->flags |= SN_REDISP;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200751 s->prev_srv->counters.redispatches++;
752 s->be->counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200753 } else {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200754 s->prev_srv->counters.retries++;
755 s->be->counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100756 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100757 }
758 }
759
Willy Tarreaubaaee002006-06-26 02:48:02 +0200760 switch (err) {
761 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200762 /* we have SN_ASSIGNED set */
763 if (!s->srv)
764 return SRV_STATUS_OK; /* dispatch or proxy mode */
765
766 /* If we already have a connection slot, no need to check any queue */
767 if (s->srv_conn == s->srv)
768 return SRV_STATUS_OK;
769
770 /* OK, this session already has an assigned server, but no
771 * connection slot yet. Either it is a redispatch, or it was
772 * assigned from persistence information (direct mode).
773 */
774 if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) {
775 /* server scheduled for redirection, and already assigned. We
776 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100777 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200778 sess_change_server(s, s->srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100779 return SRV_STATUS_OK;
780 }
781
Willy Tarreau7c669d72008-06-20 15:04:11 +0200782 /* We might have to queue this session if the assigned server is full.
783 * We know we have to queue it into the server's queue, so if a maxqueue
784 * is set on the server, we must also check that the server's queue is
785 * not full, in which case we have to return FULL.
786 */
787 if (s->srv->maxconn &&
788 (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) {
789
790 if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue)
791 return SRV_STATUS_FULL;
792
Willy Tarreaubaaee002006-06-26 02:48:02 +0200793 p = pendconn_add(s);
794 if (p)
795 return SRV_STATUS_QUEUED;
796 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200797 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200798 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200799
800 /* OK, we can use this server. Let's reserve our place */
801 sess_change_server(s, s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802 return SRV_STATUS_OK;
803
804 case SRV_STATUS_FULL:
805 /* queue this session into the proxy's queue */
806 p = pendconn_add(s);
807 if (p)
808 return SRV_STATUS_QUEUED;
809 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200810 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811
812 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200813 return err;
814
Willy Tarreaubaaee002006-06-26 02:48:02 +0200815 case SRV_STATUS_INTERNAL:
816 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200817
Willy Tarreaubaaee002006-06-26 02:48:02 +0200818 default:
819 return SRV_STATUS_INTERNAL;
820 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100821}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200822
823/*
824 * This function initiates a connection to the server assigned to this session
825 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
826 * It can return one of :
827 * - SN_ERR_NONE if everything's OK
828 * - SN_ERR_SRVTO if there are no more servers
829 * - SN_ERR_SRVCL if the connection was refused by the server
830 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
831 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
832 * - SN_ERR_INTERNAL for any other purely internal errors
833 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
834 */
835int connect_server(struct session *s)
836{
Willy Tarreau9650f372009-08-16 14:02:45 +0200837 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838
839 if (!(s->flags & SN_ADDR_SET)) {
840 err = assign_server_address(s);
841 if (err != SRV_STATUS_OK)
842 return SN_ERR_INTERNAL;
843 }
844
Willy Tarreau9650f372009-08-16 14:02:45 +0200845 if (!s->req->cons->connect)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200846 return SN_ERR_INTERNAL;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200847
Willy Tarreau9650f372009-08-16 14:02:45 +0200848 err = s->req->cons->connect(s->req->cons, s->be, s->srv,
849 (struct sockaddr *)&s->srv_addr,
850 (struct sockaddr *)&s->cli_addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851
Willy Tarreau9650f372009-08-16 14:02:45 +0200852 if (err != SN_ERR_NONE)
853 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200854
Willy Tarreaubaaee002006-06-26 02:48:02 +0200855 if (s->srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100856 s->flags |= SN_CURR_SESS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200857 s->srv->cur_sess++;
Willy Tarreauac68c5d2009-10-04 23:12:44 +0200858 if (s->srv->cur_sess > s->srv->counters.cur_sess_max)
859 s->srv->counters.cur_sess_max = s->srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100860 if (s->be->lbprm.server_take_conn)
861 s->be->lbprm.server_take_conn(s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200862 }
863
Willy Tarreaubaaee002006-06-26 02:48:02 +0200864 return SN_ERR_NONE; /* connection is OK */
865}
866
867
Willy Tarreaubaaee002006-06-26 02:48:02 +0200868/* This function performs the "redispatch" part of a connection attempt. It
869 * will assign a server if required, queue the connection if required, and
870 * handle errors that might arise at this level. It can change the server
871 * state. It will return 1 if it encounters an error, switches the server
872 * state, or has to queue a connection. Otherwise, it will return 0 indicating
873 * that the connection is ready to use.
874 */
875
876int srv_redispatch_connect(struct session *t)
877{
878 int conn_err;
879
880 /* We know that we don't have any connection pending, so we will
881 * try to get a new one, and wait in this state if it's queued
882 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200883 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200884 conn_err = assign_server_and_queue(t);
885 switch (conn_err) {
886 case SRV_STATUS_OK:
887 break;
888
Willy Tarreau7c669d72008-06-20 15:04:11 +0200889 case SRV_STATUS_FULL:
890 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
891 * and we can redispatch to another server, or it is not and we return
892 * 503. This only makes sense in DIRECT mode however, because normal LB
893 * algorithms would never select such a server, and hash algorithms
894 * would bring us on the same server again. Note that t->srv is set in
895 * this case.
896 */
Willy Tarreau4de91492010-01-22 19:10:05 +0100897 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
898 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200899 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
900 t->prev_srv = t->srv;
901 goto redispatch;
902 }
903
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200904 if (!t->req->cons->err_type) {
905 t->req->cons->err_type = SI_ET_QUEUE_ERR;
906 t->req->cons->err_loc = t->srv;
907 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200908
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200909 t->srv->counters.failed_conns++;
910 t->be->counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200911 return 1;
912
Willy Tarreaubaaee002006-06-26 02:48:02 +0200913 case SRV_STATUS_NOSRV:
914 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200915 if (!t->req->cons->err_type) {
916 t->req->cons->err_type = SI_ET_CONN_ERR;
917 t->req->cons->err_loc = NULL;
918 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100919
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200920 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200921 return 1;
922
923 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +0200924 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200925 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200926 /* do nothing else and do not wake any other session up */
927 return 1;
928
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929 case SRV_STATUS_INTERNAL:
930 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200931 if (!t->req->cons->err_type) {
932 t->req->cons->err_type = SI_ET_CONN_OTHER;
933 t->req->cons->err_loc = t->srv;
934 }
935
Willy Tarreaubaaee002006-06-26 02:48:02 +0200936 if (t->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100937 srv_inc_sess_ctr(t->srv);
Willy Tarreau98937b82007-12-10 15:05:42 +0100938 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200939 t->srv->counters.failed_conns++;
940 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200941
942 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200943 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200944 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945 return 1;
946 }
947 /* if we get here, it's because we got SRV_STATUS_OK, which also
948 * means that the connection has not been queued.
949 */
950 return 0;
951}
952
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200953int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100954 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200955 return px->down_time;
956
957 return now.tv_sec - px->last_change + px->down_time;
958}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200959
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +0100960/*
961 * This function returns a string containing the balancing
962 * mode of the proxy in a format suitable for stats.
963 */
964
965const char *backend_lb_algo_str(int algo) {
966
967 if (algo == BE_LB_ALGO_RR)
968 return "roundrobin";
969 else if (algo == BE_LB_ALGO_SRR)
970 return "static-rr";
971 else if (algo == BE_LB_ALGO_LC)
972 return "leastconn";
973 else if (algo == BE_LB_ALGO_SH)
974 return "source";
975 else if (algo == BE_LB_ALGO_UH)
976 return "uri";
977 else if (algo == BE_LB_ALGO_PH)
978 return "url_param";
979 else if (algo == BE_LB_ALGO_HH)
980 return "hdr";
981 else if (algo == BE_LB_ALGO_RCH)
982 return "rdp-cookie";
983 else
984 return NULL;
985}
986
Willy Tarreaua0cbda62007-11-01 21:39:54 +0100987/* This function parses a "balance" statement in a backend section describing
988 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
989 * returns -1, it may write an error message into ther <err> buffer, for at
990 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
991 * written. The function must be called with <args> pointing to the first word
992 * after "balance".
993 */
994int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
995{
996 if (!*(args[0])) {
997 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +0100998 curproxy->lbprm.algo &= ~BE_LB_ALGO;
999 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001000 return 0;
1001 }
1002
1003 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001004 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1005 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001006 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001007 else if (!strcmp(args[0], "static-rr")) {
1008 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1009 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1010 }
Willy Tarreau51406232008-03-10 22:04:20 +01001011 else if (!strcmp(args[0], "leastconn")) {
1012 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1013 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1014 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001015 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001016 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1017 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001018 }
1019 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001020 int arg = 1;
1021
Willy Tarreau31682232007-11-29 15:38:04 +01001022 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1023 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001024
1025 while (*args[arg]) {
1026 if (!strcmp(args[arg], "len")) {
1027 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1028 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1029 return -1;
1030 }
1031 curproxy->uri_len_limit = atoi(args[arg+1]);
1032 arg += 2;
1033 }
1034 else if (!strcmp(args[arg], "depth")) {
1035 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1036 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1037 return -1;
1038 }
1039 /* hint: we store the position of the ending '/' (depth+1) so
1040 * that we avoid a comparison while computing the hash.
1041 */
1042 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1043 arg += 2;
1044 }
1045 else {
1046 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1047 return -1;
1048 }
1049 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001050 }
Willy Tarreau01732802007-11-01 22:48:15 +01001051 else if (!strcmp(args[0], "url_param")) {
1052 if (!*args[1]) {
1053 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1054 return -1;
1055 }
Willy Tarreau31682232007-11-29 15:38:04 +01001056 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1057 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001058
1059 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001060 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001061 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001062 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001063 if (strcmp(args[2], "check_post")) {
1064 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1065 return -1;
1066 }
1067 if (*args[3]) {
1068 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1069 curproxy->url_param_post_limit = str2ui(args[3]);
1070 }
1071 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1072 if (!curproxy->url_param_post_limit)
1073 curproxy->url_param_post_limit = 48;
1074 else if ( curproxy->url_param_post_limit < 3 )
1075 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1076 }
Benoitaffb4812009-03-25 13:02:10 +01001077 }
1078 else if (!strncmp(args[0], "hdr(", 4)) {
1079 const char *beg, *end;
1080
1081 beg = args[0] + 4;
1082 end = strchr(beg, ')');
1083
1084 if (!end || end == beg) {
1085 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1086 return -1;
1087 }
1088
1089 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1090 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1091
1092 free(curproxy->hh_name);
1093 curproxy->hh_len = end - beg;
1094 curproxy->hh_name = my_strndup(beg, end - beg);
1095 curproxy->hh_match_domain = 0;
1096
1097 if (*args[1]) {
1098 if (strcmp(args[1], "use_domain_only")) {
1099 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1100 return -1;
1101 }
1102 curproxy->hh_match_domain = 1;
1103 }
1104
Emeric Brun736aa232009-06-30 17:56:00 +02001105 }
1106 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1107 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1108 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1109
1110 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1111 const char *beg, *end;
1112
1113 beg = args[0] + 11;
1114 end = strchr(beg, ')');
1115
1116 if (!end || end == beg) {
1117 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1118 return -1;
1119 }
1120
1121 free(curproxy->hh_name);
1122 curproxy->hh_name = my_strndup(beg, end - beg);
1123 curproxy->hh_len = end - beg;
1124 }
1125 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1126 free(curproxy->hh_name);
1127 curproxy->hh_name = strdup("mstshash");
1128 curproxy->hh_len = strlen(curproxy->hh_name);
1129 }
1130 else { /* syntax */
1131 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1132 return -1;
1133 }
Willy Tarreau01732802007-11-01 22:48:15 +01001134 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001135 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001136 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 +01001137 return -1;
1138 }
1139 return 0;
1140}
1141
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001142
1143/************************************************************************/
1144/* All supported keywords must be declared here. */
1145/************************************************************************/
1146
1147/* set test->i to the number of enabled servers on the proxy */
1148static int
1149acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1150 struct acl_expr *expr, struct acl_test *test)
1151{
1152 test->flags = ACL_TEST_F_VOL_TEST;
1153 if (expr->arg_len) {
1154 /* another proxy was designated, we must look for it */
1155 for (px = proxy; px; px = px->next)
1156 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1157 break;
1158 }
1159 if (!px)
1160 return 0;
1161
1162 if (px->srv_act)
1163 test->i = px->srv_act;
1164 else if (px->lbprm.fbck)
1165 test->i = 1;
1166 else
1167 test->i = px->srv_bck;
1168
1169 return 1;
1170}
1171
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001172/* set test->i to the number of enabled servers on the proxy */
1173static int
1174acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1175 struct acl_expr *expr, struct acl_test *test)
1176{
1177 struct server *iterator;
1178 test->flags = ACL_TEST_F_VOL_TEST;
1179 if (expr->arg_len) {
1180 /* another proxy was designated, we must look for it */
1181 for (px = proxy; px; px = px->next)
1182 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1183 break;
1184 }
1185 if (!px)
1186 return 0;
1187
1188 test->i = 0;
1189 iterator = px->srv;
1190 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001191 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001192 iterator = iterator->next;
1193 continue;
1194 }
1195 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
1196 test->i = -1;
1197 return 1;
1198 }
1199
1200 test->i += (iterator->maxconn - iterator->cur_sess)
1201 + (iterator->maxqueue - iterator->nbpend);
1202 iterator = iterator->next;
1203 }
1204
1205 return 1;
1206}
1207
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001208/* set test->i to the number of connections per second reaching the frontend */
1209static int
1210acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1211 struct acl_expr *expr, struct acl_test *test)
1212{
1213 test->flags = ACL_TEST_F_VOL_TEST;
1214 if (expr->arg_len) {
1215 /* another proxy was designated, we must look for it */
1216 for (px = proxy; px; px = px->next)
1217 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
1218 break;
1219 }
1220 if (!px)
1221 return 0;
1222
1223 test->i = read_freq_ctr(&px->fe_sess_per_sec);
1224 return 1;
1225}
1226
1227/* set test->i to the number of connections per second reaching the backend */
1228static int
1229acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1230 struct acl_expr *expr, struct acl_test *test)
1231{
1232 test->flags = ACL_TEST_F_VOL_TEST;
1233 if (expr->arg_len) {
1234 /* another proxy was designated, we must look for it */
1235 for (px = proxy; px; px = px->next)
1236 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1237 break;
1238 }
1239 if (!px)
1240 return 0;
1241
1242 test->i = read_freq_ctr(&px->be_sess_per_sec);
1243 return 1;
1244}
1245
Willy Tarreaua36af912009-10-10 12:02:45 +02001246/* set test->i to the number of concurrent connections on the frontend */
1247static int
1248acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1249 struct acl_expr *expr, struct acl_test *test)
1250{
1251 test->flags = ACL_TEST_F_VOL_TEST;
1252 if (expr->arg_len) {
1253 /* another proxy was designated, we must look for it */
1254 for (px = proxy; px; px = px->next)
1255 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
1256 break;
1257 }
1258 if (!px)
1259 return 0;
1260
1261 test->i = px->feconn;
1262 return 1;
1263}
1264
1265/* set test->i to the number of concurrent connections on the backend */
1266static int
1267acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1268 struct acl_expr *expr, struct acl_test *test)
1269{
1270 test->flags = ACL_TEST_F_VOL_TEST;
1271 if (expr->arg_len) {
1272 /* another proxy was designated, we must look for it */
1273 for (px = proxy; px; px = px->next)
1274 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1275 break;
1276 }
1277 if (!px)
1278 return 0;
1279
1280 test->i = px->beconn;
1281 return 1;
1282}
1283
1284/* set test->i to the total number of queued connections on the backend */
1285static int
1286acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1287 struct acl_expr *expr, struct acl_test *test)
1288{
1289 test->flags = ACL_TEST_F_VOL_TEST;
1290 if (expr->arg_len) {
1291 /* another proxy was designated, we must look for it */
1292 for (px = proxy; px; px = px->next)
1293 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1294 break;
1295 }
1296 if (!px)
1297 return 0;
1298
1299 test->i = px->totpend;
1300 return 1;
1301}
1302
1303/* set test->i to the total number of queued connections on the backend divided
1304 * by the number of running servers and rounded up. If there is no running
1305 * server, we return twice the total, just as if we had half a running server.
1306 * This is more or less correct anyway, since we expect the last server to come
1307 * back soon.
1308 */
1309static int
1310acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1311 struct acl_expr *expr, struct acl_test *test)
1312{
1313 int nbsrv;
1314
1315 test->flags = ACL_TEST_F_VOL_TEST;
1316 if (expr->arg_len) {
1317 /* another proxy was designated, we must look for it */
1318 for (px = proxy; px; px = px->next)
1319 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1320 break;
1321 }
1322 if (!px)
1323 return 0;
1324
1325 if (px->srv_act)
1326 nbsrv = px->srv_act;
1327 else if (px->lbprm.fbck)
1328 nbsrv = 1;
1329 else
1330 nbsrv = px->srv_bck;
1331
1332 if (nbsrv > 0)
1333 test->i = (px->totpend + nbsrv - 1) / nbsrv;
1334 else
1335 test->i = px->totpend * 2;
1336
1337 return 1;
1338}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001339
1340/* Note: must not be declared <const> as its list will be overwritten */
1341static struct acl_kw_list acl_kws = {{ },{
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001342 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau3a8efeb2009-03-05 19:15:37 +01001343 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001344 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
1345 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua36af912009-10-10 12:02:45 +02001346 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
1347 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1348 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1349 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001350 { NULL, NULL, NULL, NULL },
1351}};
1352
1353
1354__attribute__((constructor))
1355static void __backend_init(void)
1356{
1357 acl_register_keywords(&acl_kws);
1358}
1359
Willy Tarreaubaaee002006-06-26 02:48:02 +02001360/*
1361 * Local variables:
1362 * c-indent-level: 8
1363 * c-basic-offset: 8
1364 * End:
1365 */