blob: bd9ee14d749225f4de4b59693ddff9a9b6555493 [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 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100121 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
122 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200123 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200124 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
125 return chash_get_server_hash(px, h);
126 else
127 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200128}
129
130/*
131 * This function tries to find a running server for the proxy <px> following
132 * the URI hash method. In order to optimize cache hits, the hash computation
133 * ends at the question mark. Depending on the number of active/backup servers,
134 * it will either look for active servers, or for backup servers.
135 * If any server is found, it will be returned. If no valid server is found,
136 * NULL is returned.
137 *
138 * This code was contributed by Guillaume Dallaire, who also selected this hash
139 * algorithm out of a tens because it gave him the best results.
140 *
141 */
142struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
143{
144 unsigned long hash = 0;
145 int c;
146 int slashes = 0;
147
148 if (px->lbprm.tot_weight == 0)
149 return NULL;
150
151 /* note: we won't hash if there's only one server left */
152 if (px->lbprm.tot_used == 1)
153 goto hash_done;
154
155 if (px->uri_len_limit)
156 uri_len = MIN(uri_len, px->uri_len_limit);
157
158 while (uri_len--) {
159 c = *uri++;
160 if (c == '/') {
161 slashes++;
162 if (slashes == px->uri_dirs_depth1) /* depth+1 */
163 break;
164 }
165 else if (c == '?')
166 break;
167
168 hash = c + (hash << 6) + (hash << 16) - hash;
169 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100170 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
171 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200172 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200173 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
174 return chash_get_server_hash(px, hash);
175 else
176 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200177}
178
Willy Tarreau01732802007-11-01 22:48:15 +0100179/*
180 * This function tries to find a running server for the proxy <px> following
181 * the URL parameter hash method. It looks for a specific parameter in the
182 * URL and hashes it to compute the server ID. This is useful to optimize
183 * performance by avoiding bounces between servers in contexts where sessions
184 * are shared but cookies are not usable. If the parameter is not found, NULL
185 * is returned. If any server is found, it will be returned. If no valid server
186 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100187 */
188struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
189{
190 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200191 const char *p;
192 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100193 int plen;
194
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200195 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100196 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100197 return NULL;
198
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200199 if ((p = memchr(uri, '?', uri_len)) == NULL)
200 return NULL;
201
Willy Tarreau01732802007-11-01 22:48:15 +0100202 p++;
203
204 uri_len -= (p - uri);
205 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200206 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100207
208 while (uri_len > plen) {
209 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200210 if (params[plen] == '=') {
211 if (memcmp(params, px->url_param_name, plen) == 0) {
212 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100213 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200214 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100215 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200216 p += plen + 1;
217 uri_len -= plen + 1;
218
Willy Tarreau01732802007-11-01 22:48:15 +0100219 while (uri_len && *p != '&') {
220 hash = *p + (hash << 6) + (hash << 16) - hash;
221 uri_len--;
222 p++;
223 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100224 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
225 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200226 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
227 return chash_get_server_hash(px, hash);
228 else
229 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100230 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200231 }
232 /* skip to next parameter */
233 p = memchr(params, '&', uri_len);
234 if (!p)
235 return NULL;
236 p++;
237 uri_len -= (p - params);
238 params = p;
239 }
240 return NULL;
241}
242
243/*
244 * this does the same as the previous server_ph, but check the body contents
245 */
246struct server *get_server_ph_post(struct session *s)
247{
248 unsigned long hash = 0;
249 struct http_txn *txn = &s->txn;
250 struct buffer *req = s->req;
251 struct http_msg *msg = &txn->req;
252 struct proxy *px = s->be;
253 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100254 unsigned long len = msg->body_len;
Willy Tarreau157dd632009-12-06 19:18:09 +0100255 const char *params = req->data + msg->sov;
256 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200257
Willy Tarreau7c96f672009-12-27 22:47:25 +0100258 if (len > req->l - (msg->sov - msg->som))
259 len = req->l - (msg->sov - msg->som);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200260
Willy Tarreau157dd632009-12-06 19:18:09 +0100261 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200262 return NULL;
263
Willy Tarreau157dd632009-12-06 19:18:09 +0100264 if (px->lbprm.tot_weight == 0)
265 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200266
267 while (len > plen) {
268 /* Look for the parameter name followed by an equal symbol */
269 if (params[plen] == '=') {
270 if (memcmp(params, px->url_param_name, plen) == 0) {
271 /* OK, we have the parameter here at <params>, and
272 * the value after the equal sign, at <p>
273 * skip the equal symbol
274 */
275 p += plen + 1;
276 len -= plen + 1;
277
278 while (len && *p != '&') {
279 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100280 /* if in a POST, body must be URI encoded or it's not a URI.
281 * Do not interprete any possible binary data as a parameter.
282 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200283 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
284 break;
285 return NULL; /* oh, no; this is not uri-encoded.
286 * This body does not contain parameters.
287 */
288 }
289 hash = *p + (hash << 6) + (hash << 16) - hash;
290 len--;
291 p++;
292 /* should we break if vlen exceeds limit? */
293 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100294 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
295 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200296 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
297 return chash_get_server_hash(px, hash);
298 else
299 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200300 }
301 }
Willy Tarreau01732802007-11-01 22:48:15 +0100302 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200303 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100304 if (!p)
305 return NULL;
306 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200307 len -= (p - params);
308 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100309 }
310 return NULL;
311}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200312
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200313
Willy Tarreaubaaee002006-06-26 02:48:02 +0200314/*
Benoitaffb4812009-03-25 13:02:10 +0100315 * This function tries to find a running server for the proxy <px> following
316 * the Header parameter hash method. It looks for a specific parameter in the
317 * URL and hashes it to compute the server ID. This is useful to optimize
318 * performance by avoiding bounces between servers in contexts where sessions
319 * are shared but cookies are not usable. If the parameter is not found, NULL
320 * is returned. If any server is found, it will be returned. If no valid server
321 * is found, NULL is returned.
322 */
323struct server *get_server_hh(struct session *s)
324{
325 unsigned long hash = 0;
326 struct http_txn *txn = &s->txn;
327 struct http_msg *msg = &txn->req;
328 struct proxy *px = s->be;
329 unsigned int plen = px->hh_len;
330 unsigned long len;
331 struct hdr_ctx ctx;
332 const char *p;
333
334 /* tot_weight appears to mean srv_count */
335 if (px->lbprm.tot_weight == 0)
336 return NULL;
337
Benoitaffb4812009-03-25 13:02:10 +0100338 ctx.idx = 0;
339
340 /* if the message is chunked, we skip the chunk size, but use the value as len */
341 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
342
343 /* if the header is not found or empty, let's fallback to round robin */
344 if (!ctx.idx || !ctx.vlen)
345 return NULL;
346
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200347 /* note: we won't hash if there's only one server left */
348 if (px->lbprm.tot_used == 1)
349 goto hash_done;
350
Benoitaffb4812009-03-25 13:02:10 +0100351 /* Found a the hh_name in the headers.
352 * we will compute the hash based on this value ctx.val.
353 */
354 len = ctx.vlen;
355 p = (char *)ctx.line + ctx.val;
356 if (!px->hh_match_domain) {
357 while (len) {
358 hash = *p + (hash << 6) + (hash << 16) - hash;
359 len--;
360 p++;
361 }
362 } else {
363 int dohash = 0;
364 p += len - 1;
365 /* special computation, use only main domain name, not tld/host
366 * going back from the end of string, start hashing at first
367 * dot stop at next.
368 * This is designed to work with the 'Host' header, and requires
369 * a special option to activate this.
370 */
371 while (len) {
372 if (*p == '.') {
373 if (!dohash)
374 dohash = 1;
375 else
376 break;
377 } else {
378 if (dohash)
379 hash = *p + (hash << 6) + (hash << 16) - hash;
380 }
381 len--;
382 p--;
383 }
384 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100385 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
386 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200387 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200388 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
389 return chash_get_server_hash(px, hash);
390 else
391 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100392}
393
Emeric Brun736aa232009-06-30 17:56:00 +0200394struct server *get_server_rch(struct session *s)
395{
396 unsigned long hash = 0;
397 struct proxy *px = s->be;
398 unsigned long len;
399 const char *p;
400 int ret;
401 struct acl_expr expr;
402 struct acl_test test;
403
404 /* tot_weight appears to mean srv_count */
405 if (px->lbprm.tot_weight == 0)
406 return NULL;
407
Emeric Brun736aa232009-06-30 17:56:00 +0200408 memset(&expr, 0, sizeof(expr));
409 memset(&test, 0, sizeof(test));
410
411 expr.arg.str = px->hh_name;
412 expr.arg_len = px->hh_len;
413
414 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
415 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
416 return NULL;
417
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200418 /* note: we won't hash if there's only one server left */
419 if (px->lbprm.tot_used == 1)
420 goto hash_done;
421
Emeric Brun736aa232009-06-30 17:56:00 +0200422 /* Found a the hh_name in the headers.
423 * we will compute the hash based on this value ctx.val.
424 */
425 len = test.len;
426 p = (char *)test.ptr;
427 while (len) {
428 hash = *p + (hash << 6) + (hash << 16) - hash;
429 len--;
430 p++;
431 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100432 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
433 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200434 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200435 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
436 return chash_get_server_hash(px, hash);
437 else
438 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200439}
Benoitaffb4812009-03-25 13:02:10 +0100440
441/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200442 * This function applies the load-balancing algorithm to the session, as
443 * defined by the backend it is assigned to. The session is then marked as
444 * 'assigned'.
445 *
446 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
447 * had a server previously assigned, it is rebalanced, trying to avoid the same
448 * server.
449 * The function tries to keep the original connection slot if it reconnects to
450 * the same server, otherwise it releases it and tries to offer it.
451 *
452 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200453 *
454 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100455 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200456 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
457 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200458 * SRV_STATUS_INTERNAL for other unrecoverable errors.
459 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200460 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
461 * it does not need to be called anymore. This means that s->srv can be trusted
462 * in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463 *
464 */
465
466int assign_server(struct session *s)
467{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100468
Willy Tarreau7c669d72008-06-20 15:04:11 +0200469 struct server *conn_slot;
470 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100471
Willy Tarreaubaaee002006-06-26 02:48:02 +0200472#ifdef DEBUG_FULL
473 fprintf(stderr,"assign_server : s=%p\n",s);
474#endif
475
Willy Tarreau7c669d72008-06-20 15:04:11 +0200476 err = SRV_STATUS_INTERNAL;
477 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
478 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100479
Willy Tarreau7c669d72008-06-20 15:04:11 +0200480 s->prev_srv = s->prev_srv;
481 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200482
Willy Tarreau7c669d72008-06-20 15:04:11 +0200483 /* We have to release any connection slot before applying any LB algo,
484 * otherwise we may erroneously end up with no available slot.
485 */
486 if (conn_slot)
487 sess_change_server(s, NULL);
488
489 /* We will now try to find the good server and store it into <s->srv>.
490 * Note that <s->srv> may be NULL in case of dispatch or proxy mode,
491 * as well as if no server is available (check error code).
492 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100493
Willy Tarreau7c669d72008-06-20 15:04:11 +0200494 s->srv = NULL;
Willy Tarreau664beb82011-03-10 11:38:29 +0100495 s->target.type = TARG_TYPE_NONE;
496 s->target.ptr.v = NULL;
497
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200498 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200499 int len;
500 /* we must check if we have at least one server available */
501 if (!s->be->lbprm.tot_weight) {
502 err = SRV_STATUS_NOSRV;
503 goto out;
504 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200505
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200506 /* First check whether we need to fetch some data or simply call
507 * the LB lookup function. Only the hashing functions will need
508 * some input data in fact, and will support multiple algorithms.
509 */
510 switch (s->be->lbprm.algo & BE_LB_LKUP) {
511 case BE_LB_LKUP_RRTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200512 s->srv = fwrr_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200513 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200514
515 case BE_LB_LKUP_LCTREE:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200516 s->srv = fwlc_get_next_server(s->be, s->prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200517 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200518
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200519 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200520 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200521 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200522 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
523 s->srv = chash_get_next_server(s->be, s->prev_srv);
524 else
525 s->srv = map_get_server_rr(s->be, s->prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200526 break;
527 }
528 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200529 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200530 err = SRV_STATUS_INTERNAL;
531 goto out;
532 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200533
534 switch (s->be->lbprm.algo & BE_LB_PARM) {
535 case BE_LB_HASH_SRC:
Willy Tarreau957c0a52011-03-03 17:42:23 +0100536 if (s->req->prod->addr.c.from.ss_family == AF_INET)
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200537 len = 4;
Willy Tarreau957c0a52011-03-03 17:42:23 +0100538 else if (s->req->prod->addr.c.from.ss_family == AF_INET6)
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200539 len = 16;
540 else {
541 /* unknown IP family */
542 err = SRV_STATUS_INTERNAL;
543 goto out;
544 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200546 s->srv = get_server_sh(s->be,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100547 (void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr,
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200548 len);
549 break;
550
551 case BE_LB_HASH_URI:
552 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100553 if (s->txn.req.msg_state < HTTP_MSG_BODY)
554 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200555 s->srv = get_server_uh(s->be,
Willy Tarreau962c3f42010-01-10 00:15:35 +0100556 s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200557 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200558 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200559
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200560 case BE_LB_HASH_PRM:
561 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100562 if (s->txn.req.msg_state < HTTP_MSG_BODY)
563 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100564
565 s->srv = get_server_ph(s->be,
566 s->txn.req.sol + s->txn.req.sl.rq.u,
567 s->txn.req.sl.rq.u_l);
568
569 if (!s->srv && s->txn.meth == HTTP_METH_POST)
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200570 s->srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200571 break;
Benoitaffb4812009-03-25 13:02:10 +0100572
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200573 case BE_LB_HASH_HDR:
574 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100575 if (s->txn.req.msg_state < HTTP_MSG_BODY)
576 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200577 s->srv = get_server_hh(s);
578 break;
579
580 case BE_LB_HASH_RDP:
581 /* RDP Cookie hashing */
582 s->srv = get_server_rch(s);
583 break;
584
585 default:
586 /* unknown balancing algorithm */
587 err = SRV_STATUS_INTERNAL;
588 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100589 }
Emeric Brun736aa232009-06-30 17:56:00 +0200590
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200591 /* If the hashing parameter was not found, let's fall
592 * back to round robin on the map.
593 */
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200594 if (!s->srv) {
595 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
596 s->srv = chash_get_next_server(s->be, s->prev_srv);
597 else
598 s->srv = map_get_server_rr(s->be, s->prev_srv);
599 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200600
601 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200602 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200603
Willy Tarreau7c669d72008-06-20 15:04:11 +0200604 default:
605 /* unknown balancing algorithm */
606 err = SRV_STATUS_INTERNAL;
607 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200608 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200609
610 if (!s->srv) {
611 err = SRV_STATUS_FULL;
612 goto out;
613 }
614 else if (s->srv != s->prev_srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200615 s->be->counters.cum_lbconn++;
616 s->srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100617 }
Willy Tarreau664beb82011-03-10 11:38:29 +0100618
619 s->target.type = TARG_TYPE_SERVER;
620 s->target.ptr.s = s->srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200621 }
Willy Tarreau664beb82011-03-10 11:38:29 +0100622 else if ((s->be->options2 & PR_O2_DISPATCH) || (s->be->options & PR_O_TRANSP)) {
623 s->target.type = TARG_TYPE_PROXY;
624 s->target.ptr.p = s->be;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625 }
Willy Tarreau664beb82011-03-10 11:38:29 +0100626 else if ((s->be->options & PR_O_HTTP_PROXY) && s->req->cons->addr.s.to.sin_addr.s_addr) {
627 /* in proxy mode, we need a valid destination address */
628 s->target.type = TARG_TYPE_PROXY;
629 s->target.ptr.p = s->be;
630 }
631 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200632 err = SRV_STATUS_NOSRV;
633 goto out;
634 }
635
636 s->flags |= SN_ASSIGNED;
637 err = SRV_STATUS_OK;
638 out:
639
640 /* Either we take back our connection slot, or we offer it to someone
641 * else if we don't need it anymore.
642 */
643 if (conn_slot) {
644 if (conn_slot == s->srv) {
645 sess_change_server(s, s->srv);
646 } else {
647 if (may_dequeue_tasks(conn_slot, s->be))
648 process_srv_queue(conn_slot);
649 }
650 }
651
652 out_err:
653 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654}
655
656
657/*
658 * This function assigns a server address to a session, and sets SN_ADDR_SET.
659 * The address is taken from the currently assigned server, or from the
660 * dispatch or transparent address.
661 *
662 * It may return :
663 * SRV_STATUS_OK if everything is OK.
664 * SRV_STATUS_INTERNAL for other unrecoverable errors.
665 *
666 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
667 * not cleared, so it's to the caller to clear it if required.
668 *
669 */
670int assign_server_address(struct session *s)
671{
672#ifdef DEBUG_FULL
673 fprintf(stderr,"assign_server_address : s=%p\n",s);
674#endif
675
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200676 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677 /* A server is necessarily known for this session */
678 if (!(s->flags & SN_ASSIGNED))
679 return SRV_STATUS_INTERNAL;
680
Willy Tarreau957c0a52011-03-03 17:42:23 +0100681 s->req->cons->addr.s.to = s->srv->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682
Willy Tarreau957c0a52011-03-03 17:42:23 +0100683 if (!s->req->cons->addr.s.to.sin_addr.s_addr) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200684 /* if the server has no address, we use the same address
685 * the client asked, which is handy for remapping ports
686 * locally on multiple addresses at once.
687 */
688 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
689 get_frt_addr(s);
690
Willy Tarreau957c0a52011-03-03 17:42:23 +0100691 if (s->req->prod->addr.c.to.ss_family == AF_INET) {
692 s->req->cons->addr.s.to.sin_addr = ((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr;
Emeric Brunec810d12010-10-22 16:36:33 +0200693 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200694 }
695
Willy Tarreaubaaee002006-06-26 02:48:02 +0200696 /* if this server remaps proxied ports, we'll use
697 * the port the client connected to with an offset. */
698 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100699 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200700 get_frt_addr(s);
Willy Tarreau957c0a52011-03-03 17:42:23 +0100701 if (s->req->prod->addr.c.to.ss_family == AF_INET) {
702 s->req->cons->addr.s.to.sin_port = htons(ntohs(s->req->cons->addr.s.to.sin_port) +
703 ntohs(((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_port));
Emeric Brunec810d12010-10-22 16:36:33 +0200704 }
Willy Tarreau957c0a52011-03-03 17:42:23 +0100705 else if (s->req->prod->addr.c.to.ss_family == AF_INET6) {
706 s->req->cons->addr.s.to.sin_port = htons(ntohs(s->req->cons->addr.s.to.sin_port) +
707 ntohs(((struct sockaddr_in6 *)&s->req->prod->addr.c.to)->sin6_port));
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200708 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200709 }
710 }
Willy Tarreauf5ab69a2011-03-04 22:44:16 +0100711 else if (s->be->options2 & PR_O2_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200712 /* connect to the defined dispatch addr */
Willy Tarreau957c0a52011-03-03 17:42:23 +0100713 s->req->cons->addr.s.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100715 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200716 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100717 if (!(s->flags & SN_FRT_ADDR_SET))
718 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719
Willy Tarreau957c0a52011-03-03 17:42:23 +0100720 if (s->req->prod->addr.c.to.ss_family == AF_INET) {
721 memcpy(&s->req->cons->addr.s.to, &s->req->prod->addr.c.to, MIN(sizeof(s->req->cons->addr.s.to), sizeof(s->req->prod->addr.c.to)));
Emeric Brunec810d12010-10-22 16:36:33 +0200722 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100723 /* when we support IPv6 on the backend, we may add other tests */
724 //qfprintf(stderr, "Cannot get original server address.\n");
725 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100727 else if (s->be->options & PR_O_HTTP_PROXY) {
728 /* If HTTP PROXY option is set, then server is already assigned
729 * during incoming client request parsing. */
730 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100731 else {
732 /* no server and no LB algorithm ! */
733 return SRV_STATUS_INTERNAL;
734 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735
736 s->flags |= SN_ADDR_SET;
737 return SRV_STATUS_OK;
738}
739
740
741/* This function assigns a server to session <s> if required, and can add the
742 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200743 * If ->srv_conn is set, the session is first released from the server.
744 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
745 * be called before any connection and after any retry or redispatch occurs.
746 *
747 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748 *
749 * Returns :
750 *
751 * SRV_STATUS_OK if everything is OK.
752 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
753 * SRV_STATUS_QUEUED if the connection has been queued.
754 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200755 * connection could not be queued in s->srv,
756 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757 * SRV_STATUS_INTERNAL for other unrecoverable errors.
758 *
759 */
760int assign_server_and_queue(struct session *s)
761{
762 struct pendconn *p;
763 int err;
764
765 if (s->pend_pos)
766 return SRV_STATUS_INTERNAL;
767
Willy Tarreau7c669d72008-06-20 15:04:11 +0200768 err = SRV_STATUS_OK;
769 if (!(s->flags & SN_ASSIGNED)) {
770 err = assign_server(s);
771 if (s->prev_srv) {
772 /* This session was previously assigned to a server. We have to
773 * update the session's and the server's stats :
774 * - if the server changed :
775 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
776 * - set SN_REDISP if it was successfully redispatched
777 * - increment srv->redispatches and be->redispatches
778 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100779 */
780
Willy Tarreau7c669d72008-06-20 15:04:11 +0200781 if (s->prev_srv != s->srv) {
782 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
783 s->txn.flags &= ~TX_CK_MASK;
784 s->txn.flags |= TX_CK_DOWN;
785 }
786 s->flags |= SN_REDISP;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200787 s->prev_srv->counters.redispatches++;
788 s->be->counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200789 } else {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200790 s->prev_srv->counters.retries++;
791 s->be->counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100792 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100793 }
794 }
795
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 switch (err) {
797 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200798 /* we have SN_ASSIGNED set */
799 if (!s->srv)
800 return SRV_STATUS_OK; /* dispatch or proxy mode */
801
802 /* If we already have a connection slot, no need to check any queue */
803 if (s->srv_conn == s->srv)
804 return SRV_STATUS_OK;
805
806 /* OK, this session already has an assigned server, but no
807 * connection slot yet. Either it is a redispatch, or it was
808 * assigned from persistence information (direct mode).
809 */
810 if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) {
811 /* server scheduled for redirection, and already assigned. We
812 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100813 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200814 sess_change_server(s, s->srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100815 return SRV_STATUS_OK;
816 }
817
Willy Tarreau7c669d72008-06-20 15:04:11 +0200818 /* We might have to queue this session if the assigned server is full.
819 * We know we have to queue it into the server's queue, so if a maxqueue
820 * is set on the server, we must also check that the server's queue is
821 * not full, in which case we have to return FULL.
822 */
823 if (s->srv->maxconn &&
824 (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) {
825
826 if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue)
827 return SRV_STATUS_FULL;
828
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829 p = pendconn_add(s);
830 if (p)
831 return SRV_STATUS_QUEUED;
832 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200833 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200834 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200835
836 /* OK, we can use this server. Let's reserve our place */
837 sess_change_server(s, s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838 return SRV_STATUS_OK;
839
840 case SRV_STATUS_FULL:
841 /* queue this session into the proxy's queue */
842 p = pendconn_add(s);
843 if (p)
844 return SRV_STATUS_QUEUED;
845 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200846 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847
848 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200849 return err;
850
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851 case SRV_STATUS_INTERNAL:
852 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200853
Willy Tarreaubaaee002006-06-26 02:48:02 +0200854 default:
855 return SRV_STATUS_INTERNAL;
856 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100857}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858
Willy Tarreaub1d67742010-03-29 19:36:59 +0200859/* If an explicit source binding is specified on the server and/or backend, and
860 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau957c0a52011-03-03 17:42:23 +0100861 * assigned to the session's req->cons->addr.s.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200862 */
863static void assign_tproxy_address(struct session *s)
864{
865#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
866 if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) {
867 switch (s->srv->state & SRV_TPROXY_MASK) {
868 case SRV_TPROXY_ADDR:
Willy Tarreau957c0a52011-03-03 17:42:23 +0100869 s->req->cons->addr.s.from = *(struct sockaddr_in *)&s->srv->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200870 break;
871 case SRV_TPROXY_CLI:
872 case SRV_TPROXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200873 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreau957c0a52011-03-03 17:42:23 +0100874 s->req->cons->addr.s.from = *(struct sockaddr_in *)&s->req->prod->addr.c.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200875 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200876 case SRV_TPROXY_DYN:
877 if (s->srv->bind_hdr_occ) {
878 /* bind to the IP in a header */
Willy Tarreau957c0a52011-03-03 17:42:23 +0100879 s->req->cons->addr.s.from.sin_port = 0;
880 s->req->cons->addr.s.from.sin_addr.s_addr = htonl(get_ip_from_hdr2(&s->txn.req,
Willy Tarreaubce70882009-09-07 11:51:47 +0200881 s->srv->bind_hdr_name,
882 s->srv->bind_hdr_len,
883 &s->txn.hdr_idx,
884 s->srv->bind_hdr_occ));
885 }
886 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200887 default:
Willy Tarreau957c0a52011-03-03 17:42:23 +0100888 memset(&s->req->cons->addr.s.from, 0, sizeof(s->req->cons->addr.s.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200889 }
890 }
891 else if (s->be->options & PR_O_BIND_SRC) {
892 switch (s->be->options & PR_O_TPXY_MASK) {
893 case PR_O_TPXY_ADDR:
Willy Tarreau957c0a52011-03-03 17:42:23 +0100894 s->req->cons->addr.s.from = *(struct sockaddr_in *)&s->be->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200895 break;
896 case PR_O_TPXY_CLI:
897 case PR_O_TPXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200898 /* FIXME: what can we do if the client connects in IPv6 or socket unix? */
Willy Tarreau957c0a52011-03-03 17:42:23 +0100899 s->req->cons->addr.s.from = *(struct sockaddr_in *)&s->req->prod->addr.c.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200900 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200901 case PR_O_TPXY_DYN:
902 if (s->be->bind_hdr_occ) {
903 /* bind to the IP in a header */
Willy Tarreau957c0a52011-03-03 17:42:23 +0100904 s->req->cons->addr.s.from.sin_port = 0;
905 s->req->cons->addr.s.from.sin_addr.s_addr = htonl(get_ip_from_hdr2(&s->txn.req,
Willy Tarreaubce70882009-09-07 11:51:47 +0200906 s->be->bind_hdr_name,
907 s->be->bind_hdr_len,
908 &s->txn.hdr_idx,
909 s->be->bind_hdr_occ));
910 }
911 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200912 default:
Willy Tarreau957c0a52011-03-03 17:42:23 +0100913 memset(&s->req->cons->addr.s.from, 0, sizeof(s->req->cons->addr.s.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200914 }
915 }
916#endif
917}
918
919
Willy Tarreaubaaee002006-06-26 02:48:02 +0200920/*
921 * This function initiates a connection to the server assigned to this session
Willy Tarreau664beb82011-03-10 11:38:29 +0100922 * (s->srv, s->target, s->req->cons->addr.s.to). It will assign a server if none
923 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924 * It can return one of :
925 * - SN_ERR_NONE if everything's OK
926 * - SN_ERR_SRVTO if there are no more servers
927 * - SN_ERR_SRVCL if the connection was refused by the server
928 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
929 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
930 * - SN_ERR_INTERNAL for any other purely internal errors
931 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
932 */
933int connect_server(struct session *s)
934{
Willy Tarreau9650f372009-08-16 14:02:45 +0200935 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200936
937 if (!(s->flags & SN_ADDR_SET)) {
938 err = assign_server_address(s);
939 if (err != SRV_STATUS_OK)
940 return SN_ERR_INTERNAL;
941 }
942
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200943 /* Prepare the stream interface for a TCP connection. Later
944 * we may assign a protocol-specific connect() function.
Willy Tarreau664beb82011-03-10 11:38:29 +0100945 * NOTE: when we later support HTTP keep-alive, we'll have to
946 * decide here if we can reuse the connection by comparing the
947 * session's freshly assigned target with the stream interface's.
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200948 */
949 stream_sock_prepare_interface(s->req->cons);
950 s->req->cons->connect = tcpv4_connect_server;
Willy Tarreau664beb82011-03-10 11:38:29 +0100951 s->req->cons->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200952
Willy Tarreaub1d67742010-03-29 19:36:59 +0200953 assign_tproxy_address(s);
954
Willy Tarreauac825402011-03-04 22:04:29 +0100955 err = s->req->cons->connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200956
Willy Tarreau9650f372009-08-16 14:02:45 +0200957 if (err != SN_ERR_NONE)
958 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200959
Willy Tarreaubaaee002006-06-26 02:48:02 +0200960 if (s->srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +0100961 s->flags |= SN_CURR_SESS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200962 s->srv->cur_sess++;
Willy Tarreauac68c5d2009-10-04 23:12:44 +0200963 if (s->srv->cur_sess > s->srv->counters.cur_sess_max)
964 s->srv->counters.cur_sess_max = s->srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +0100965 if (s->be->lbprm.server_take_conn)
966 s->be->lbprm.server_take_conn(s->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200967 }
968
Willy Tarreaubaaee002006-06-26 02:48:02 +0200969 return SN_ERR_NONE; /* connection is OK */
970}
971
972
Willy Tarreaubaaee002006-06-26 02:48:02 +0200973/* This function performs the "redispatch" part of a connection attempt. It
974 * will assign a server if required, queue the connection if required, and
975 * handle errors that might arise at this level. It can change the server
976 * state. It will return 1 if it encounters an error, switches the server
977 * state, or has to queue a connection. Otherwise, it will return 0 indicating
978 * that the connection is ready to use.
979 */
980
981int srv_redispatch_connect(struct session *t)
982{
983 int conn_err;
984
985 /* We know that we don't have any connection pending, so we will
986 * try to get a new one, and wait in this state if it's queued
987 */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200988 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200989 conn_err = assign_server_and_queue(t);
990 switch (conn_err) {
991 case SRV_STATUS_OK:
992 break;
993
Willy Tarreau7c669d72008-06-20 15:04:11 +0200994 case SRV_STATUS_FULL:
995 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
996 * and we can redispatch to another server, or it is not and we return
997 * 503. This only makes sense in DIRECT mode however, because normal LB
998 * algorithms would never select such a server, and hash algorithms
999 * would bring us on the same server again. Note that t->srv is set in
1000 * this case.
1001 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001002 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1003 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001004 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
1005 t->prev_srv = t->srv;
1006 goto redispatch;
1007 }
1008
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001009 if (!t->req->cons->err_type) {
1010 t->req->cons->err_type = SI_ET_QUEUE_ERR;
1011 t->req->cons->err_loc = t->srv;
1012 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001013
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001014 t->srv->counters.failed_conns++;
1015 t->be->counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001016 return 1;
1017
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018 case SRV_STATUS_NOSRV:
1019 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001020 if (!t->req->cons->err_type) {
1021 t->req->cons->err_type = SI_ET_CONN_ERR;
1022 t->req->cons->err_loc = NULL;
1023 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001024
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001025 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001026 return 1;
1027
1028 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001029 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001030 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001031 /* do nothing else and do not wake any other session up */
1032 return 1;
1033
Willy Tarreaubaaee002006-06-26 02:48:02 +02001034 case SRV_STATUS_INTERNAL:
1035 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001036 if (!t->req->cons->err_type) {
1037 t->req->cons->err_type = SI_ET_CONN_OTHER;
1038 t->req->cons->err_loc = t->srv;
1039 }
1040
Willy Tarreaubaaee002006-06-26 02:48:02 +02001041 if (t->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +01001042 srv_inc_sess_ctr(t->srv);
Willy Tarreau98937b82007-12-10 15:05:42 +01001043 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001044 t->srv->counters.failed_conns++;
1045 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001046
1047 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001048 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001049 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001050 return 1;
1051 }
1052 /* if we get here, it's because we got SRV_STATUS_OK, which also
1053 * means that the connection has not been queued.
1054 */
1055 return 0;
1056}
1057
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001058/* Apply RDP cookie persistence to the current session. For this, the function
1059 * tries to extract an RDP cookie from the request buffer, and look for the
1060 * matching server in the list. If the server is found, it is assigned to the
1061 * session. This always returns 1, and the analyser removes itself from the
1062 * list. Nothing is performed if a server was already assigned.
1063 */
1064int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1065{
1066 struct proxy *px = s->be;
1067 int ret;
1068 struct acl_expr expr;
1069 struct acl_test test;
1070 struct server *srv = px->srv;
1071 struct sockaddr_in addr;
1072 char *p;
1073
1074 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1075 now_ms, __FUNCTION__,
1076 s,
1077 req,
1078 req->rex, req->wex,
1079 req->flags,
1080 req->l,
1081 req->analysers);
1082
1083 if (s->flags & SN_ASSIGNED)
1084 goto no_cookie;
1085
1086 memset(&expr, 0, sizeof(expr));
1087 memset(&test, 0, sizeof(test));
1088
1089 expr.arg.str = s->be->rdp_cookie_name;
1090 expr.arg_len = s->be->rdp_cookie_len;
1091
1092 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
1093 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
1094 goto no_cookie;
1095
1096 memset(&addr, 0, sizeof(addr));
1097 addr.sin_family = AF_INET;
1098
1099 /* Considering an rdp cookie detected using acl, test.ptr ended with <cr><lf> and should return */
1100 addr.sin_addr.s_addr = strtoul(test.ptr, &p, 10);
1101 if (*p != '.')
1102 goto no_cookie;
1103 p++;
1104 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1105 if (*p != '.')
1106 goto no_cookie;
1107
Willy Tarreau664beb82011-03-10 11:38:29 +01001108 s->target.type = TARG_TYPE_NONE;
1109 s->target.ptr.v = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001110 while (srv) {
1111 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1112 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1113 /* we found the server and it is usable */
1114 s->flags |= SN_DIRECT | SN_ASSIGNED;
1115 s->srv = srv;
Willy Tarreau664beb82011-03-10 11:38:29 +01001116 s->target.type = TARG_TYPE_SERVER;
1117 s->target.ptr.s = s->srv;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001118 break;
1119 }
1120 }
1121 srv = srv->next;
1122 }
1123
1124no_cookie:
1125 req->analysers &= ~an_bit;
1126 req->analyse_exp = TICK_ETERNITY;
1127 return 1;
1128}
1129
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001130int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001131 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001132 return px->down_time;
1133
1134 return now.tv_sec - px->last_change + px->down_time;
1135}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001136
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001137/*
1138 * This function returns a string containing the balancing
1139 * mode of the proxy in a format suitable for stats.
1140 */
1141
1142const char *backend_lb_algo_str(int algo) {
1143
1144 if (algo == BE_LB_ALGO_RR)
1145 return "roundrobin";
1146 else if (algo == BE_LB_ALGO_SRR)
1147 return "static-rr";
1148 else if (algo == BE_LB_ALGO_LC)
1149 return "leastconn";
1150 else if (algo == BE_LB_ALGO_SH)
1151 return "source";
1152 else if (algo == BE_LB_ALGO_UH)
1153 return "uri";
1154 else if (algo == BE_LB_ALGO_PH)
1155 return "url_param";
1156 else if (algo == BE_LB_ALGO_HH)
1157 return "hdr";
1158 else if (algo == BE_LB_ALGO_RCH)
1159 return "rdp-cookie";
1160 else
1161 return NULL;
1162}
1163
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001164/* This function parses a "balance" statement in a backend section describing
1165 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1166 * returns -1, it may write an error message into ther <err> buffer, for at
1167 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1168 * written. The function must be called with <args> pointing to the first word
1169 * after "balance".
1170 */
1171int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1172{
1173 if (!*(args[0])) {
1174 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001175 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1176 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001177 return 0;
1178 }
1179
1180 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001181 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1182 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001183 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001184 else if (!strcmp(args[0], "static-rr")) {
1185 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1186 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1187 }
Willy Tarreau51406232008-03-10 22:04:20 +01001188 else if (!strcmp(args[0], "leastconn")) {
1189 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1190 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1191 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001192 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001193 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1194 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001195 }
1196 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001197 int arg = 1;
1198
Willy Tarreau31682232007-11-29 15:38:04 +01001199 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1200 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001201
1202 while (*args[arg]) {
1203 if (!strcmp(args[arg], "len")) {
1204 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1205 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1206 return -1;
1207 }
1208 curproxy->uri_len_limit = atoi(args[arg+1]);
1209 arg += 2;
1210 }
1211 else if (!strcmp(args[arg], "depth")) {
1212 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1213 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1214 return -1;
1215 }
1216 /* hint: we store the position of the ending '/' (depth+1) so
1217 * that we avoid a comparison while computing the hash.
1218 */
1219 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1220 arg += 2;
1221 }
1222 else {
1223 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1224 return -1;
1225 }
1226 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001227 }
Willy Tarreau01732802007-11-01 22:48:15 +01001228 else if (!strcmp(args[0], "url_param")) {
1229 if (!*args[1]) {
1230 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1231 return -1;
1232 }
Willy Tarreau31682232007-11-29 15:38:04 +01001233 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1234 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001235
1236 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001237 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001238 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001239 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001240 if (strcmp(args[2], "check_post")) {
1241 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1242 return -1;
1243 }
1244 if (*args[3]) {
1245 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1246 curproxy->url_param_post_limit = str2ui(args[3]);
1247 }
1248 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1249 if (!curproxy->url_param_post_limit)
1250 curproxy->url_param_post_limit = 48;
1251 else if ( curproxy->url_param_post_limit < 3 )
1252 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1253 }
Benoitaffb4812009-03-25 13:02:10 +01001254 }
1255 else if (!strncmp(args[0], "hdr(", 4)) {
1256 const char *beg, *end;
1257
1258 beg = args[0] + 4;
1259 end = strchr(beg, ')');
1260
1261 if (!end || end == beg) {
1262 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1263 return -1;
1264 }
1265
1266 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1267 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1268
1269 free(curproxy->hh_name);
1270 curproxy->hh_len = end - beg;
1271 curproxy->hh_name = my_strndup(beg, end - beg);
1272 curproxy->hh_match_domain = 0;
1273
1274 if (*args[1]) {
1275 if (strcmp(args[1], "use_domain_only")) {
1276 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1277 return -1;
1278 }
1279 curproxy->hh_match_domain = 1;
1280 }
1281
Emeric Brun736aa232009-06-30 17:56:00 +02001282 }
1283 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1284 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1285 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1286
1287 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1288 const char *beg, *end;
1289
1290 beg = args[0] + 11;
1291 end = strchr(beg, ')');
1292
1293 if (!end || end == beg) {
1294 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1295 return -1;
1296 }
1297
1298 free(curproxy->hh_name);
1299 curproxy->hh_name = my_strndup(beg, end - beg);
1300 curproxy->hh_len = end - beg;
1301 }
1302 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1303 free(curproxy->hh_name);
1304 curproxy->hh_name = strdup("mstshash");
1305 curproxy->hh_len = strlen(curproxy->hh_name);
1306 }
1307 else { /* syntax */
1308 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1309 return -1;
1310 }
Willy Tarreau01732802007-11-01 22:48:15 +01001311 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001312 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001313 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 +01001314 return -1;
1315 }
1316 return 0;
1317}
1318
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001319
1320/************************************************************************/
1321/* All supported keywords must be declared here. */
1322/************************************************************************/
1323
1324/* set test->i to the number of enabled servers on the proxy */
1325static int
1326acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1327 struct acl_expr *expr, struct acl_test *test)
1328{
1329 test->flags = ACL_TEST_F_VOL_TEST;
1330 if (expr->arg_len) {
1331 /* another proxy was designated, we must look for it */
1332 for (px = proxy; px; px = px->next)
1333 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1334 break;
1335 }
1336 if (!px)
1337 return 0;
1338
1339 if (px->srv_act)
1340 test->i = px->srv_act;
1341 else if (px->lbprm.fbck)
1342 test->i = 1;
1343 else
1344 test->i = px->srv_bck;
1345
1346 return 1;
1347}
1348
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001349/* report in test->flags a success or failure depending on the designated
1350 * server's state. There is no match function involved since there's no pattern.
1351 */
1352static int
1353acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, int dir,
1354 struct acl_expr *expr, struct acl_test *test)
1355{
1356 struct server *srv = expr->arg.srv;
1357
1358 test->flags = ACL_TEST_F_VOL_TEST;
1359 if (!(srv->state & SRV_MAINTAIN) &&
1360 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
1361 test->flags |= ACL_TEST_F_SET_RES_PASS;
1362 else
1363 test->flags |= ACL_TEST_F_SET_RES_FAIL;
1364 return 1;
1365}
1366
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001367/* set test->i to the number of enabled servers on the proxy */
1368static int
1369acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1370 struct acl_expr *expr, struct acl_test *test)
1371{
1372 struct server *iterator;
1373 test->flags = ACL_TEST_F_VOL_TEST;
1374 if (expr->arg_len) {
1375 /* another proxy was designated, we must look for it */
1376 for (px = proxy; px; px = px->next)
1377 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1378 break;
1379 }
1380 if (!px)
1381 return 0;
1382
1383 test->i = 0;
1384 iterator = px->srv;
1385 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001386 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001387 iterator = iterator->next;
1388 continue;
1389 }
1390 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
1391 test->i = -1;
1392 return 1;
1393 }
1394
1395 test->i += (iterator->maxconn - iterator->cur_sess)
1396 + (iterator->maxqueue - iterator->nbpend);
1397 iterator = iterator->next;
1398 }
1399
1400 return 1;
1401}
1402
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001403/* set test->i to the id of the backend */
1404static int
1405acl_fetch_be_id(struct proxy *px, struct session *l4, void *l7, int dir,
1406 struct acl_expr *expr, struct acl_test *test) {
1407
1408 test->flags = ACL_TEST_F_READ_ONLY;
1409
1410 test->i = l4->be->uuid;
1411
1412 return 1;
1413}
1414
1415/* set test->i to the id of the server */
1416static int
1417acl_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, int dir,
1418 struct acl_expr *expr, struct acl_test *test) {
1419
Willy Tarreau17af4192011-02-23 14:27:06 +01001420 if (!l4->srv)
1421 return 0;
1422
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001423 test->flags = ACL_TEST_F_READ_ONLY;
1424
1425 test->i = l4->srv->puid;
1426
1427 return 1;
1428}
1429
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001430/* set test->i to the number of connections per second reaching the backend */
1431static int
1432acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1433 struct acl_expr *expr, struct acl_test *test)
1434{
1435 test->flags = ACL_TEST_F_VOL_TEST;
1436 if (expr->arg_len) {
1437 /* another proxy was designated, we must look for it */
1438 for (px = proxy; px; px = px->next)
1439 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1440 break;
1441 }
1442 if (!px)
1443 return 0;
1444
1445 test->i = read_freq_ctr(&px->be_sess_per_sec);
1446 return 1;
1447}
1448
Willy Tarreaua36af912009-10-10 12:02:45 +02001449/* set test->i to the number of concurrent connections on the backend */
1450static int
1451acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1452 struct acl_expr *expr, struct acl_test *test)
1453{
1454 test->flags = ACL_TEST_F_VOL_TEST;
1455 if (expr->arg_len) {
1456 /* another proxy was designated, we must look for it */
1457 for (px = proxy; px; px = px->next)
1458 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1459 break;
1460 }
1461 if (!px)
1462 return 0;
1463
1464 test->i = px->beconn;
1465 return 1;
1466}
1467
1468/* set test->i to the total number of queued connections on the backend */
1469static int
1470acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1471 struct acl_expr *expr, struct acl_test *test)
1472{
1473 test->flags = ACL_TEST_F_VOL_TEST;
1474 if (expr->arg_len) {
1475 /* another proxy was designated, we must look for it */
1476 for (px = proxy; px; px = px->next)
1477 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1478 break;
1479 }
1480 if (!px)
1481 return 0;
1482
1483 test->i = px->totpend;
1484 return 1;
1485}
1486
1487/* set test->i to the total number of queued connections on the backend divided
1488 * by the number of running servers and rounded up. If there is no running
1489 * server, we return twice the total, just as if we had half a running server.
1490 * This is more or less correct anyway, since we expect the last server to come
1491 * back soon.
1492 */
1493static int
1494acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1495 struct acl_expr *expr, struct acl_test *test)
1496{
1497 int nbsrv;
1498
1499 test->flags = ACL_TEST_F_VOL_TEST;
1500 if (expr->arg_len) {
1501 /* another proxy was designated, we must look for it */
1502 for (px = proxy; px; px = px->next)
1503 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1504 break;
1505 }
1506 if (!px)
1507 return 0;
1508
1509 if (px->srv_act)
1510 nbsrv = px->srv_act;
1511 else if (px->lbprm.fbck)
1512 nbsrv = 1;
1513 else
1514 nbsrv = px->srv_bck;
1515
1516 if (nbsrv > 0)
1517 test->i = (px->totpend + nbsrv - 1) / nbsrv;
1518 else
1519 test->i = px->totpend * 2;
1520
1521 return 1;
1522}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001523
1524/* Note: must not be declared <const> as its list will be overwritten */
1525static struct acl_kw_list acl_kws = {{ },{
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001526 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
1527 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
1528 { "be_id", acl_parse_int, acl_fetch_be_id, acl_match_int, ACL_USE_NOTHING },
1529 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
1530 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1531 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1532 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
1533 { "srv_is_up", acl_parse_nothing, acl_fetch_srv_is_up, acl_match_nothing, ACL_USE_NOTHING },
Willy Tarreau4a0d8282011-02-23 15:24:16 +01001534 { "srv_id", acl_parse_int, acl_fetch_srv_id, acl_match_int, ACL_USE_RTR_INTERNAL },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001535 { NULL, NULL, NULL, NULL },
1536}};
1537
1538
1539__attribute__((constructor))
1540static void __backend_init(void)
1541{
1542 acl_register_keywords(&acl_kws);
1543}
1544
Willy Tarreaubaaee002006-06-26 02:48:02 +02001545/*
1546 * Local variables:
1547 * c-indent-level: 8
1548 * c-basic-offset: 8
1549 * End:
1550 */