blob: 8367328a2ce880ffe2855f9e4df4641d955f8f13 [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 Tarreau9e000c62011-03-10 14:03:36 +010042#include <proto/stream_interface.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020043#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/task.h>
45
Willy Tarreaubaaee002006-06-26 02:48:02 +020046/*
47 * This function recounts the number of usable active and backup servers for
48 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010049 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010050 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010051 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020052 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020053void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020054{
55 struct server *srv;
56
Willy Tarreau20697042007-11-15 23:26:18 +010057 px->srv_act = px->srv_bck = 0;
58 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010059 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020060 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010061 if (!srv_is_usable(srv->state, srv->eweight))
62 continue;
63
64 if (srv->state & SRV_BACKUP) {
65 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010066 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010067 px->lbprm.fbck = srv;
68 px->srv_bck++;
69 px->lbprm.tot_wbck += srv->eweight;
70 } else {
71 px->srv_act++;
72 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020073 }
74 }
Willy Tarreaub625a082007-11-26 01:15:43 +010075}
Willy Tarreau20697042007-11-15 23:26:18 +010076
Willy Tarreaub625a082007-11-26 01:15:43 +010077/* This function simply updates the backend's tot_weight and tot_used values
78 * after servers weights have been updated. It is designed to be used after
79 * recount_servers() or equivalent.
80 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020081void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010082{
Willy Tarreau20697042007-11-15 23:26:18 +010083 if (px->srv_act) {
84 px->lbprm.tot_weight = px->lbprm.tot_wact;
85 px->lbprm.tot_used = px->srv_act;
86 }
Willy Tarreaub625a082007-11-26 01:15:43 +010087 else if (px->lbprm.fbck) {
88 /* use only the first backup server */
89 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
90 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010091 }
92 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010093 px->lbprm.tot_weight = px->lbprm.tot_wbck;
94 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +010095 }
Willy Tarreaub625a082007-11-26 01:15:43 +010096}
97
Willy Tarreau39c9ba72009-10-01 21:11:15 +020098/*
99 * This function tries to find a running server for the proxy <px> following
100 * the source hash method. Depending on the number of active/backup servers,
101 * it will either look for active servers, or for backup servers.
102 * If any server is found, it will be returned. If no valid server is found,
103 * NULL is returned.
104 */
105struct server *get_server_sh(struct proxy *px, const char *addr, int len)
106{
107 unsigned int h, l;
108
109 if (px->lbprm.tot_weight == 0)
110 return NULL;
111
112 l = h = 0;
113
114 /* note: we won't hash if there's only one server left */
115 if (px->lbprm.tot_used == 1)
116 goto hash_done;
117
118 while ((l + sizeof (int)) <= len) {
119 h ^= ntohl(*(unsigned int *)(&addr[l]));
120 l += sizeof (int);
121 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100122 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
123 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200124 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200125 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
126 return chash_get_server_hash(px, h);
127 else
128 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200129}
130
131/*
132 * This function tries to find a running server for the proxy <px> following
133 * the URI hash method. In order to optimize cache hits, the hash computation
134 * ends at the question mark. Depending on the number of active/backup servers,
135 * it will either look for active servers, or for backup servers.
136 * If any server is found, it will be returned. If no valid server is found,
137 * NULL is returned.
138 *
139 * This code was contributed by Guillaume Dallaire, who also selected this hash
140 * algorithm out of a tens because it gave him the best results.
141 *
142 */
143struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
144{
145 unsigned long hash = 0;
146 int c;
147 int slashes = 0;
148
149 if (px->lbprm.tot_weight == 0)
150 return NULL;
151
152 /* note: we won't hash if there's only one server left */
153 if (px->lbprm.tot_used == 1)
154 goto hash_done;
155
156 if (px->uri_len_limit)
157 uri_len = MIN(uri_len, px->uri_len_limit);
158
159 while (uri_len--) {
160 c = *uri++;
161 if (c == '/') {
162 slashes++;
163 if (slashes == px->uri_dirs_depth1) /* depth+1 */
164 break;
165 }
166 else if (c == '?')
167 break;
168
169 hash = c + (hash << 6) + (hash << 16) - hash;
170 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100171 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
172 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200173 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200174 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
175 return chash_get_server_hash(px, hash);
176 else
177 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200178}
179
Willy Tarreau01732802007-11-01 22:48:15 +0100180/*
181 * This function tries to find a running server for the proxy <px> following
182 * the URL parameter hash method. It looks for a specific parameter in the
183 * URL and hashes it to compute the server ID. This is useful to optimize
184 * performance by avoiding bounces between servers in contexts where sessions
185 * are shared but cookies are not usable. If the parameter is not found, NULL
186 * is returned. If any server is found, it will be returned. If no valid server
187 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100188 */
189struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
190{
191 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200192 const char *p;
193 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100194 int plen;
195
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200196 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100197 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100198 return NULL;
199
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200200 if ((p = memchr(uri, '?', uri_len)) == NULL)
201 return NULL;
202
Willy Tarreau01732802007-11-01 22:48:15 +0100203 p++;
204
205 uri_len -= (p - uri);
206 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200207 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100208
209 while (uri_len > plen) {
210 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200211 if (params[plen] == '=') {
212 if (memcmp(params, px->url_param_name, plen) == 0) {
213 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100214 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200215 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100216 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200217 p += plen + 1;
218 uri_len -= plen + 1;
219
Willy Tarreau01732802007-11-01 22:48:15 +0100220 while (uri_len && *p != '&') {
221 hash = *p + (hash << 6) + (hash << 16) - hash;
222 uri_len--;
223 p++;
224 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100225 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
226 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200227 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
228 return chash_get_server_hash(px, hash);
229 else
230 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100231 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200232 }
233 /* skip to next parameter */
234 p = memchr(params, '&', uri_len);
235 if (!p)
236 return NULL;
237 p++;
238 uri_len -= (p - params);
239 params = p;
240 }
241 return NULL;
242}
243
244/*
245 * this does the same as the previous server_ph, but check the body contents
246 */
247struct server *get_server_ph_post(struct session *s)
248{
249 unsigned long hash = 0;
250 struct http_txn *txn = &s->txn;
251 struct buffer *req = s->req;
252 struct http_msg *msg = &txn->req;
253 struct proxy *px = s->be;
254 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100255 unsigned long len = msg->body_len;
Willy Tarreau157dd632009-12-06 19:18:09 +0100256 const char *params = req->data + msg->sov;
257 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200258
Willy Tarreau7c96f672009-12-27 22:47:25 +0100259 if (len > req->l - (msg->sov - msg->som))
260 len = req->l - (msg->sov - msg->som);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200261
Willy Tarreau157dd632009-12-06 19:18:09 +0100262 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200263 return NULL;
264
Willy Tarreau157dd632009-12-06 19:18:09 +0100265 if (px->lbprm.tot_weight == 0)
266 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200267
268 while (len > plen) {
269 /* Look for the parameter name followed by an equal symbol */
270 if (params[plen] == '=') {
271 if (memcmp(params, px->url_param_name, plen) == 0) {
272 /* OK, we have the parameter here at <params>, and
273 * the value after the equal sign, at <p>
274 * skip the equal symbol
275 */
276 p += plen + 1;
277 len -= plen + 1;
278
279 while (len && *p != '&') {
280 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100281 /* if in a POST, body must be URI encoded or it's not a URI.
282 * Do not interprete any possible binary data as a parameter.
283 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200284 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
285 break;
286 return NULL; /* oh, no; this is not uri-encoded.
287 * This body does not contain parameters.
288 */
289 }
290 hash = *p + (hash << 6) + (hash << 16) - hash;
291 len--;
292 p++;
293 /* should we break if vlen exceeds limit? */
294 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100295 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
296 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200297 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
298 return chash_get_server_hash(px, hash);
299 else
300 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200301 }
302 }
Willy Tarreau01732802007-11-01 22:48:15 +0100303 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200304 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100305 if (!p)
306 return NULL;
307 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200308 len -= (p - params);
309 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100310 }
311 return NULL;
312}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200314
Willy Tarreaubaaee002006-06-26 02:48:02 +0200315/*
Benoitaffb4812009-03-25 13:02:10 +0100316 * This function tries to find a running server for the proxy <px> following
317 * the Header parameter hash method. It looks for a specific parameter in the
318 * URL and hashes it to compute the server ID. This is useful to optimize
319 * performance by avoiding bounces between servers in contexts where sessions
320 * are shared but cookies are not usable. If the parameter is not found, NULL
321 * is returned. If any server is found, it will be returned. If no valid server
322 * is found, NULL is returned.
323 */
324struct server *get_server_hh(struct session *s)
325{
326 unsigned long hash = 0;
327 struct http_txn *txn = &s->txn;
328 struct http_msg *msg = &txn->req;
329 struct proxy *px = s->be;
330 unsigned int plen = px->hh_len;
331 unsigned long len;
332 struct hdr_ctx ctx;
333 const char *p;
334
335 /* tot_weight appears to mean srv_count */
336 if (px->lbprm.tot_weight == 0)
337 return NULL;
338
Benoitaffb4812009-03-25 13:02:10 +0100339 ctx.idx = 0;
340
341 /* if the message is chunked, we skip the chunk size, but use the value as len */
342 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
343
344 /* if the header is not found or empty, let's fallback to round robin */
345 if (!ctx.idx || !ctx.vlen)
346 return NULL;
347
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200348 /* note: we won't hash if there's only one server left */
349 if (px->lbprm.tot_used == 1)
350 goto hash_done;
351
Benoitaffb4812009-03-25 13:02:10 +0100352 /* Found a the hh_name in the headers.
353 * we will compute the hash based on this value ctx.val.
354 */
355 len = ctx.vlen;
356 p = (char *)ctx.line + ctx.val;
357 if (!px->hh_match_domain) {
358 while (len) {
359 hash = *p + (hash << 6) + (hash << 16) - hash;
360 len--;
361 p++;
362 }
363 } else {
364 int dohash = 0;
365 p += len - 1;
366 /* special computation, use only main domain name, not tld/host
367 * going back from the end of string, start hashing at first
368 * dot stop at next.
369 * This is designed to work with the 'Host' header, and requires
370 * a special option to activate this.
371 */
372 while (len) {
373 if (*p == '.') {
374 if (!dohash)
375 dohash = 1;
376 else
377 break;
378 } else {
379 if (dohash)
380 hash = *p + (hash << 6) + (hash << 16) - hash;
381 }
382 len--;
383 p--;
384 }
385 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100386 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
387 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200388 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200389 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
390 return chash_get_server_hash(px, hash);
391 else
392 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100393}
394
Emeric Brun736aa232009-06-30 17:56:00 +0200395struct server *get_server_rch(struct session *s)
396{
397 unsigned long hash = 0;
398 struct proxy *px = s->be;
399 unsigned long len;
400 const char *p;
401 int ret;
402 struct acl_expr expr;
403 struct acl_test test;
404
405 /* tot_weight appears to mean srv_count */
406 if (px->lbprm.tot_weight == 0)
407 return NULL;
408
Emeric Brun736aa232009-06-30 17:56:00 +0200409 memset(&expr, 0, sizeof(expr));
410 memset(&test, 0, sizeof(test));
411
412 expr.arg.str = px->hh_name;
413 expr.arg_len = px->hh_len;
414
415 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
416 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
417 return NULL;
418
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200419 /* note: we won't hash if there's only one server left */
420 if (px->lbprm.tot_used == 1)
421 goto hash_done;
422
Emeric Brun736aa232009-06-30 17:56:00 +0200423 /* Found a the hh_name in the headers.
424 * we will compute the hash based on this value ctx.val.
425 */
426 len = test.len;
427 p = (char *)test.ptr;
428 while (len) {
429 hash = *p + (hash << 6) + (hash << 16) - hash;
430 len--;
431 p++;
432 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100433 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
434 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200435 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200436 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
437 return chash_get_server_hash(px, hash);
438 else
439 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200440}
Benoitaffb4812009-03-25 13:02:10 +0100441
442/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200443 * This function applies the load-balancing algorithm to the session, as
444 * defined by the backend it is assigned to. The session is then marked as
445 * 'assigned'.
446 *
447 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
448 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau3d80d912011-03-10 11:42:13 +0100449 * server, which should still be present in s->srv before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200450 * The function tries to keep the original connection slot if it reconnects to
451 * the same server, otherwise it releases it and tries to offer it.
452 *
453 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200454 *
455 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100456 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200457 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
458 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200459 * SRV_STATUS_INTERNAL for other unrecoverable errors.
460 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200461 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
462 * it does not need to be called anymore. This means that s->srv can be trusted
463 * in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464 *
465 */
466
467int assign_server(struct session *s)
468{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100469
Willy Tarreau7c669d72008-06-20 15:04:11 +0200470 struct server *conn_slot;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100471 struct server *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200472 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100473
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474#ifdef DEBUG_FULL
475 fprintf(stderr,"assign_server : s=%p\n",s);
476#endif
477
Willy Tarreau7c669d72008-06-20 15:04:11 +0200478 err = SRV_STATUS_INTERNAL;
479 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
480 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100481
Willy Tarreau3d80d912011-03-10 11:42:13 +0100482 prev_srv = s->srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200483 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200484
Willy Tarreau7c669d72008-06-20 15:04:11 +0200485 /* We have to release any connection slot before applying any LB algo,
486 * otherwise we may erroneously end up with no available slot.
487 */
488 if (conn_slot)
489 sess_change_server(s, NULL);
490
491 /* We will now try to find the good server and store it into <s->srv>.
492 * Note that <s->srv> may be NULL in case of dispatch or proxy mode,
493 * as well as if no server is available (check error code).
494 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100495
Willy Tarreau7c669d72008-06-20 15:04:11 +0200496 s->srv = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100497 clear_target(&s->target);
Willy Tarreau664beb82011-03-10 11:38:29 +0100498
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200499 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200500 int len;
501 /* we must check if we have at least one server available */
502 if (!s->be->lbprm.tot_weight) {
503 err = SRV_STATUS_NOSRV;
504 goto out;
505 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200506
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200507 /* First check whether we need to fetch some data or simply call
508 * the LB lookup function. Only the hashing functions will need
509 * some input data in fact, and will support multiple algorithms.
510 */
511 switch (s->be->lbprm.algo & BE_LB_LKUP) {
512 case BE_LB_LKUP_RRTREE:
Willy Tarreau3d80d912011-03-10 11:42:13 +0100513 s->srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200514 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200515
516 case BE_LB_LKUP_LCTREE:
Willy Tarreau3d80d912011-03-10 11:42:13 +0100517 s->srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200518 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200519
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200520 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200521 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200522 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200523 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau3d80d912011-03-10 11:42:13 +0100524 s->srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200525 else
Willy Tarreau3d80d912011-03-10 11:42:13 +0100526 s->srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200527 break;
528 }
529 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200530 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200531 err = SRV_STATUS_INTERNAL;
532 goto out;
533 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200534
535 switch (s->be->lbprm.algo & BE_LB_PARM) {
536 case BE_LB_HASH_SRC:
Willy Tarreau957c0a52011-03-03 17:42:23 +0100537 if (s->req->prod->addr.c.from.ss_family == AF_INET)
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200538 len = 4;
Willy Tarreau957c0a52011-03-03 17:42:23 +0100539 else if (s->req->prod->addr.c.from.ss_family == AF_INET6)
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200540 len = 16;
541 else {
542 /* unknown IP family */
543 err = SRV_STATUS_INTERNAL;
544 goto out;
545 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200547 s->srv = get_server_sh(s->be,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100548 (void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr,
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200549 len);
550 break;
551
552 case BE_LB_HASH_URI:
553 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100554 if (s->txn.req.msg_state < HTTP_MSG_BODY)
555 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200556 s->srv = get_server_uh(s->be,
Willy Tarreau962c3f42010-01-10 00:15:35 +0100557 s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau2fcb5002007-05-08 13:35:26 +0200558 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200559 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200560
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200561 case BE_LB_HASH_PRM:
562 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100563 if (s->txn.req.msg_state < HTTP_MSG_BODY)
564 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100565
566 s->srv = get_server_ph(s->be,
567 s->txn.req.sol + s->txn.req.sl.rq.u,
568 s->txn.req.sl.rq.u_l);
569
570 if (!s->srv && s->txn.meth == HTTP_METH_POST)
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200571 s->srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200572 break;
Benoitaffb4812009-03-25 13:02:10 +0100573
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200574 case BE_LB_HASH_HDR:
575 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100576 if (s->txn.req.msg_state < HTTP_MSG_BODY)
577 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200578 s->srv = get_server_hh(s);
579 break;
580
581 case BE_LB_HASH_RDP:
582 /* RDP Cookie hashing */
583 s->srv = get_server_rch(s);
584 break;
585
586 default:
587 /* unknown balancing algorithm */
588 err = SRV_STATUS_INTERNAL;
589 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100590 }
Emeric Brun736aa232009-06-30 17:56:00 +0200591
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200592 /* If the hashing parameter was not found, let's fall
593 * back to round robin on the map.
594 */
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200595 if (!s->srv) {
596 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau3d80d912011-03-10 11:42:13 +0100597 s->srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200598 else
Willy Tarreau3d80d912011-03-10 11:42:13 +0100599 s->srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200600 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200601
602 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200603 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200604
Willy Tarreau7c669d72008-06-20 15:04:11 +0200605 default:
606 /* unknown balancing algorithm */
607 err = SRV_STATUS_INTERNAL;
608 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200609 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200610
611 if (!s->srv) {
612 err = SRV_STATUS_FULL;
613 goto out;
614 }
Willy Tarreau3d80d912011-03-10 11:42:13 +0100615 else if (s->srv != prev_srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200616 s->be->counters.cum_lbconn++;
617 s->srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100618 }
Willy Tarreau664beb82011-03-10 11:38:29 +0100619
Willy Tarreau9e000c62011-03-10 14:03:36 +0100620 set_target_server(&s->target, 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)) {
Willy Tarreau9e000c62011-03-10 14:03:36 +0100623 set_target_proxy(&s->target, s->be);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624 }
Willy Tarreau664beb82011-03-10 11:38:29 +0100625 else if ((s->be->options & PR_O_HTTP_PROXY) && s->req->cons->addr.s.to.sin_addr.s_addr) {
626 /* in proxy mode, we need a valid destination address */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100627 set_target_proxy(&s->target, s->be);
Willy Tarreau664beb82011-03-10 11:38:29 +0100628 }
629 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200630 err = SRV_STATUS_NOSRV;
631 goto out;
632 }
633
634 s->flags |= SN_ASSIGNED;
635 err = SRV_STATUS_OK;
636 out:
637
638 /* Either we take back our connection slot, or we offer it to someone
639 * else if we don't need it anymore.
640 */
641 if (conn_slot) {
642 if (conn_slot == s->srv) {
643 sess_change_server(s, s->srv);
644 } else {
645 if (may_dequeue_tasks(conn_slot, s->be))
646 process_srv_queue(conn_slot);
647 }
648 }
649
650 out_err:
651 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200652}
653
654
655/*
656 * This function assigns a server address to a session, and sets SN_ADDR_SET.
657 * The address is taken from the currently assigned server, or from the
658 * dispatch or transparent address.
659 *
660 * It may return :
661 * SRV_STATUS_OK if everything is OK.
662 * SRV_STATUS_INTERNAL for other unrecoverable errors.
663 *
664 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
665 * not cleared, so it's to the caller to clear it if required.
666 *
667 */
668int assign_server_address(struct session *s)
669{
670#ifdef DEBUG_FULL
671 fprintf(stderr,"assign_server_address : s=%p\n",s);
672#endif
673
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200674 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 /* A server is necessarily known for this session */
676 if (!(s->flags & SN_ASSIGNED))
677 return SRV_STATUS_INTERNAL;
678
Willy Tarreau957c0a52011-03-03 17:42:23 +0100679 s->req->cons->addr.s.to = s->srv->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680
Willy Tarreau957c0a52011-03-03 17:42:23 +0100681 if (!s->req->cons->addr.s.to.sin_addr.s_addr) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200682 /* if the server has no address, we use the same address
683 * the client asked, which is handy for remapping ports
684 * locally on multiple addresses at once.
685 */
686 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
687 get_frt_addr(s);
688
Willy Tarreau957c0a52011-03-03 17:42:23 +0100689 if (s->req->prod->addr.c.to.ss_family == AF_INET) {
690 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 +0200691 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200692 }
693
Willy Tarreaubaaee002006-06-26 02:48:02 +0200694 /* if this server remaps proxied ports, we'll use
695 * the port the client connected to with an offset. */
696 if (s->srv->state & SRV_MAPPORTS) {
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100697 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200698 get_frt_addr(s);
Willy Tarreau957c0a52011-03-03 17:42:23 +0100699 if (s->req->prod->addr.c.to.ss_family == AF_INET) {
700 s->req->cons->addr.s.to.sin_port = htons(ntohs(s->req->cons->addr.s.to.sin_port) +
701 ntohs(((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_port));
Emeric Brunec810d12010-10-22 16:36:33 +0200702 }
Willy Tarreau957c0a52011-03-03 17:42:23 +0100703 else if (s->req->prod->addr.c.to.ss_family == AF_INET6) {
704 s->req->cons->addr.s.to.sin_port = htons(ntohs(s->req->cons->addr.s.to.sin_port) +
705 ntohs(((struct sockaddr_in6 *)&s->req->prod->addr.c.to)->sin6_port));
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200706 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200707 }
708 }
Willy Tarreauf5ab69a2011-03-04 22:44:16 +0100709 else if (s->be->options2 & PR_O2_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200710 /* connect to the defined dispatch addr */
Willy Tarreau957c0a52011-03-03 17:42:23 +0100711 s->req->cons->addr.s.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200712 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100713 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100715 if (!(s->flags & SN_FRT_ADDR_SET))
716 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717
Willy Tarreau957c0a52011-03-03 17:42:23 +0100718 if (s->req->prod->addr.c.to.ss_family == AF_INET) {
719 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 +0200720 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100721 /* when we support IPv6 on the backend, we may add other tests */
722 //qfprintf(stderr, "Cannot get original server address.\n");
723 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100725 else if (s->be->options & PR_O_HTTP_PROXY) {
726 /* If HTTP PROXY option is set, then server is already assigned
727 * during incoming client request parsing. */
728 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100729 else {
730 /* no server and no LB algorithm ! */
731 return SRV_STATUS_INTERNAL;
732 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733
734 s->flags |= SN_ADDR_SET;
735 return SRV_STATUS_OK;
736}
737
738
739/* This function assigns a server to session <s> if required, and can add the
740 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200741 * If ->srv_conn is set, the session is first released from the server.
742 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
743 * be called before any connection and after any retry or redispatch occurs.
744 *
745 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200746 *
747 * Returns :
748 *
749 * SRV_STATUS_OK if everything is OK.
750 * SRV_STATUS_NOSRV if no server is available. s->srv = NULL.
751 * SRV_STATUS_QUEUED if the connection has been queued.
752 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200753 * connection could not be queued in s->srv,
754 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755 * SRV_STATUS_INTERNAL for other unrecoverable errors.
756 *
757 */
758int assign_server_and_queue(struct session *s)
759{
760 struct pendconn *p;
761 int err;
762
763 if (s->pend_pos)
764 return SRV_STATUS_INTERNAL;
765
Willy Tarreau7c669d72008-06-20 15:04:11 +0200766 err = SRV_STATUS_OK;
767 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100768 struct server *prev_srv = s->srv;
769
Willy Tarreau7c669d72008-06-20 15:04:11 +0200770 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100771 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200772 /* 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 Tarreau3d80d912011-03-10 11:42:13 +0100781 if (prev_srv != s->srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200782 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;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100787 prev_srv->counters.redispatches++;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200788 s->be->counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200789 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100790 prev_srv->counters.retries++;
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200791 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 Tarreau9e000c62011-03-10 14:03:36 +0100951 copy_target(&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);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001005 goto redispatch;
1006 }
1007
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001008 if (!t->req->cons->err_type) {
1009 t->req->cons->err_type = SI_ET_QUEUE_ERR;
1010 t->req->cons->err_loc = t->srv;
1011 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001012
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001013 t->srv->counters.failed_conns++;
1014 t->be->counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001015 return 1;
1016
Willy Tarreaubaaee002006-06-26 02:48:02 +02001017 case SRV_STATUS_NOSRV:
1018 /* note: it is guaranteed that t->srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001019 if (!t->req->cons->err_type) {
1020 t->req->cons->err_type = SI_ET_CONN_ERR;
1021 t->req->cons->err_loc = NULL;
1022 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001023
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001024 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001025 return 1;
1026
1027 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001028 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001029 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001030 /* do nothing else and do not wake any other session up */
1031 return 1;
1032
Willy Tarreaubaaee002006-06-26 02:48:02 +02001033 case SRV_STATUS_INTERNAL:
1034 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001035 if (!t->req->cons->err_type) {
1036 t->req->cons->err_type = SI_ET_CONN_OTHER;
1037 t->req->cons->err_loc = t->srv;
1038 }
1039
Willy Tarreaubaaee002006-06-26 02:48:02 +02001040 if (t->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +01001041 srv_inc_sess_ctr(t->srv);
Willy Tarreau98937b82007-12-10 15:05:42 +01001042 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001043 t->srv->counters.failed_conns++;
1044 t->be->counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001045
1046 /* release other sessions waiting for this server */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001047 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02001048 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001049 return 1;
1050 }
1051 /* if we get here, it's because we got SRV_STATUS_OK, which also
1052 * means that the connection has not been queued.
1053 */
1054 return 0;
1055}
1056
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001057/* Apply RDP cookie persistence to the current session. For this, the function
1058 * tries to extract an RDP cookie from the request buffer, and look for the
1059 * matching server in the list. If the server is found, it is assigned to the
1060 * session. This always returns 1, and the analyser removes itself from the
1061 * list. Nothing is performed if a server was already assigned.
1062 */
1063int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1064{
1065 struct proxy *px = s->be;
1066 int ret;
1067 struct acl_expr expr;
1068 struct acl_test test;
1069 struct server *srv = px->srv;
1070 struct sockaddr_in addr;
1071 char *p;
1072
1073 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1074 now_ms, __FUNCTION__,
1075 s,
1076 req,
1077 req->rex, req->wex,
1078 req->flags,
1079 req->l,
1080 req->analysers);
1081
1082 if (s->flags & SN_ASSIGNED)
1083 goto no_cookie;
1084
1085 memset(&expr, 0, sizeof(expr));
1086 memset(&test, 0, sizeof(test));
1087
1088 expr.arg.str = s->be->rdp_cookie_name;
1089 expr.arg_len = s->be->rdp_cookie_len;
1090
1091 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
1092 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
1093 goto no_cookie;
1094
1095 memset(&addr, 0, sizeof(addr));
1096 addr.sin_family = AF_INET;
1097
1098 /* Considering an rdp cookie detected using acl, test.ptr ended with <cr><lf> and should return */
1099 addr.sin_addr.s_addr = strtoul(test.ptr, &p, 10);
1100 if (*p != '.')
1101 goto no_cookie;
1102 p++;
1103 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1104 if (*p != '.')
1105 goto no_cookie;
1106
Willy Tarreau9e000c62011-03-10 14:03:36 +01001107 clear_target(&s->target);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001108 while (srv) {
1109 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1110 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1111 /* we found the server and it is usable */
1112 s->flags |= SN_DIRECT | SN_ASSIGNED;
1113 s->srv = srv;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001114 set_target_server(&s->target, srv);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001115 break;
1116 }
1117 }
1118 srv = srv->next;
1119 }
1120
1121no_cookie:
1122 req->analysers &= ~an_bit;
1123 req->analyse_exp = TICK_ETERNITY;
1124 return 1;
1125}
1126
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001127int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001128 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001129 return px->down_time;
1130
1131 return now.tv_sec - px->last_change + px->down_time;
1132}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001133
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001134/*
1135 * This function returns a string containing the balancing
1136 * mode of the proxy in a format suitable for stats.
1137 */
1138
1139const char *backend_lb_algo_str(int algo) {
1140
1141 if (algo == BE_LB_ALGO_RR)
1142 return "roundrobin";
1143 else if (algo == BE_LB_ALGO_SRR)
1144 return "static-rr";
1145 else if (algo == BE_LB_ALGO_LC)
1146 return "leastconn";
1147 else if (algo == BE_LB_ALGO_SH)
1148 return "source";
1149 else if (algo == BE_LB_ALGO_UH)
1150 return "uri";
1151 else if (algo == BE_LB_ALGO_PH)
1152 return "url_param";
1153 else if (algo == BE_LB_ALGO_HH)
1154 return "hdr";
1155 else if (algo == BE_LB_ALGO_RCH)
1156 return "rdp-cookie";
1157 else
1158 return NULL;
1159}
1160
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001161/* This function parses a "balance" statement in a backend section describing
1162 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1163 * returns -1, it may write an error message into ther <err> buffer, for at
1164 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1165 * written. The function must be called with <args> pointing to the first word
1166 * after "balance".
1167 */
1168int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1169{
1170 if (!*(args[0])) {
1171 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001172 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1173 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001174 return 0;
1175 }
1176
1177 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001178 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1179 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001180 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001181 else if (!strcmp(args[0], "static-rr")) {
1182 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1183 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1184 }
Willy Tarreau51406232008-03-10 22:04:20 +01001185 else if (!strcmp(args[0], "leastconn")) {
1186 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1187 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1188 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001189 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001190 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1191 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001192 }
1193 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001194 int arg = 1;
1195
Willy Tarreau31682232007-11-29 15:38:04 +01001196 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1197 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001198
1199 while (*args[arg]) {
1200 if (!strcmp(args[arg], "len")) {
1201 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1202 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1203 return -1;
1204 }
1205 curproxy->uri_len_limit = atoi(args[arg+1]);
1206 arg += 2;
1207 }
1208 else if (!strcmp(args[arg], "depth")) {
1209 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1210 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1211 return -1;
1212 }
1213 /* hint: we store the position of the ending '/' (depth+1) so
1214 * that we avoid a comparison while computing the hash.
1215 */
1216 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1217 arg += 2;
1218 }
1219 else {
1220 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1221 return -1;
1222 }
1223 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001224 }
Willy Tarreau01732802007-11-01 22:48:15 +01001225 else if (!strcmp(args[0], "url_param")) {
1226 if (!*args[1]) {
1227 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1228 return -1;
1229 }
Willy Tarreau31682232007-11-29 15:38:04 +01001230 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1231 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001232
1233 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001234 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001235 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001236 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001237 if (strcmp(args[2], "check_post")) {
1238 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1239 return -1;
1240 }
1241 if (*args[3]) {
1242 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1243 curproxy->url_param_post_limit = str2ui(args[3]);
1244 }
1245 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1246 if (!curproxy->url_param_post_limit)
1247 curproxy->url_param_post_limit = 48;
1248 else if ( curproxy->url_param_post_limit < 3 )
1249 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1250 }
Benoitaffb4812009-03-25 13:02:10 +01001251 }
1252 else if (!strncmp(args[0], "hdr(", 4)) {
1253 const char *beg, *end;
1254
1255 beg = args[0] + 4;
1256 end = strchr(beg, ')');
1257
1258 if (!end || end == beg) {
1259 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1260 return -1;
1261 }
1262
1263 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1264 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1265
1266 free(curproxy->hh_name);
1267 curproxy->hh_len = end - beg;
1268 curproxy->hh_name = my_strndup(beg, end - beg);
1269 curproxy->hh_match_domain = 0;
1270
1271 if (*args[1]) {
1272 if (strcmp(args[1], "use_domain_only")) {
1273 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1274 return -1;
1275 }
1276 curproxy->hh_match_domain = 1;
1277 }
1278
Emeric Brun736aa232009-06-30 17:56:00 +02001279 }
1280 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1281 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1282 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1283
1284 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1285 const char *beg, *end;
1286
1287 beg = args[0] + 11;
1288 end = strchr(beg, ')');
1289
1290 if (!end || end == beg) {
1291 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1292 return -1;
1293 }
1294
1295 free(curproxy->hh_name);
1296 curproxy->hh_name = my_strndup(beg, end - beg);
1297 curproxy->hh_len = end - beg;
1298 }
1299 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1300 free(curproxy->hh_name);
1301 curproxy->hh_name = strdup("mstshash");
1302 curproxy->hh_len = strlen(curproxy->hh_name);
1303 }
1304 else { /* syntax */
1305 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1306 return -1;
1307 }
Willy Tarreau01732802007-11-01 22:48:15 +01001308 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001309 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001310 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 +01001311 return -1;
1312 }
1313 return 0;
1314}
1315
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001316
1317/************************************************************************/
1318/* All supported keywords must be declared here. */
1319/************************************************************************/
1320
1321/* set test->i to the number of enabled servers on the proxy */
1322static int
1323acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1324 struct acl_expr *expr, struct acl_test *test)
1325{
1326 test->flags = ACL_TEST_F_VOL_TEST;
1327 if (expr->arg_len) {
1328 /* another proxy was designated, we must look for it */
1329 for (px = proxy; px; px = px->next)
1330 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1331 break;
1332 }
1333 if (!px)
1334 return 0;
1335
1336 if (px->srv_act)
1337 test->i = px->srv_act;
1338 else if (px->lbprm.fbck)
1339 test->i = 1;
1340 else
1341 test->i = px->srv_bck;
1342
1343 return 1;
1344}
1345
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001346/* report in test->flags a success or failure depending on the designated
1347 * server's state. There is no match function involved since there's no pattern.
1348 */
1349static int
1350acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, int dir,
1351 struct acl_expr *expr, struct acl_test *test)
1352{
1353 struct server *srv = expr->arg.srv;
1354
1355 test->flags = ACL_TEST_F_VOL_TEST;
1356 if (!(srv->state & SRV_MAINTAIN) &&
1357 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
1358 test->flags |= ACL_TEST_F_SET_RES_PASS;
1359 else
1360 test->flags |= ACL_TEST_F_SET_RES_FAIL;
1361 return 1;
1362}
1363
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001364/* set test->i to the number of enabled servers on the proxy */
1365static int
1366acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1367 struct acl_expr *expr, struct acl_test *test)
1368{
1369 struct server *iterator;
1370 test->flags = ACL_TEST_F_VOL_TEST;
1371 if (expr->arg_len) {
1372 /* another proxy was designated, we must look for it */
1373 for (px = proxy; px; px = px->next)
1374 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1375 break;
1376 }
1377 if (!px)
1378 return 0;
1379
1380 test->i = 0;
1381 iterator = px->srv;
1382 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001383 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001384 iterator = iterator->next;
1385 continue;
1386 }
1387 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
1388 test->i = -1;
1389 return 1;
1390 }
1391
1392 test->i += (iterator->maxconn - iterator->cur_sess)
1393 + (iterator->maxqueue - iterator->nbpend);
1394 iterator = iterator->next;
1395 }
1396
1397 return 1;
1398}
1399
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001400/* set test->i to the id of the backend */
1401static int
1402acl_fetch_be_id(struct proxy *px, struct session *l4, void *l7, int dir,
1403 struct acl_expr *expr, struct acl_test *test) {
1404
1405 test->flags = ACL_TEST_F_READ_ONLY;
1406
1407 test->i = l4->be->uuid;
1408
1409 return 1;
1410}
1411
1412/* set test->i to the id of the server */
1413static int
1414acl_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, int dir,
1415 struct acl_expr *expr, struct acl_test *test) {
1416
Willy Tarreau17af4192011-02-23 14:27:06 +01001417 if (!l4->srv)
1418 return 0;
1419
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001420 test->flags = ACL_TEST_F_READ_ONLY;
1421
1422 test->i = l4->srv->puid;
1423
1424 return 1;
1425}
1426
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001427/* set test->i to the number of connections per second reaching the backend */
1428static int
1429acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1430 struct acl_expr *expr, struct acl_test *test)
1431{
1432 test->flags = ACL_TEST_F_VOL_TEST;
1433 if (expr->arg_len) {
1434 /* another proxy was designated, we must look for it */
1435 for (px = proxy; px; px = px->next)
1436 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1437 break;
1438 }
1439 if (!px)
1440 return 0;
1441
1442 test->i = read_freq_ctr(&px->be_sess_per_sec);
1443 return 1;
1444}
1445
Willy Tarreaua36af912009-10-10 12:02:45 +02001446/* set test->i to the number of concurrent connections on the backend */
1447static int
1448acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1449 struct acl_expr *expr, struct acl_test *test)
1450{
1451 test->flags = ACL_TEST_F_VOL_TEST;
1452 if (expr->arg_len) {
1453 /* another proxy was designated, we must look for it */
1454 for (px = proxy; px; px = px->next)
1455 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1456 break;
1457 }
1458 if (!px)
1459 return 0;
1460
1461 test->i = px->beconn;
1462 return 1;
1463}
1464
1465/* set test->i to the total number of queued connections on the backend */
1466static int
1467acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1468 struct acl_expr *expr, struct acl_test *test)
1469{
1470 test->flags = ACL_TEST_F_VOL_TEST;
1471 if (expr->arg_len) {
1472 /* another proxy was designated, we must look for it */
1473 for (px = proxy; px; px = px->next)
1474 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1475 break;
1476 }
1477 if (!px)
1478 return 0;
1479
1480 test->i = px->totpend;
1481 return 1;
1482}
1483
1484/* set test->i to the total number of queued connections on the backend divided
1485 * by the number of running servers and rounded up. If there is no running
1486 * server, we return twice the total, just as if we had half a running server.
1487 * This is more or less correct anyway, since we expect the last server to come
1488 * back soon.
1489 */
1490static int
1491acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1492 struct acl_expr *expr, struct acl_test *test)
1493{
1494 int nbsrv;
1495
1496 test->flags = ACL_TEST_F_VOL_TEST;
1497 if (expr->arg_len) {
1498 /* another proxy was designated, we must look for it */
1499 for (px = proxy; px; px = px->next)
1500 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1501 break;
1502 }
1503 if (!px)
1504 return 0;
1505
1506 if (px->srv_act)
1507 nbsrv = px->srv_act;
1508 else if (px->lbprm.fbck)
1509 nbsrv = 1;
1510 else
1511 nbsrv = px->srv_bck;
1512
1513 if (nbsrv > 0)
1514 test->i = (px->totpend + nbsrv - 1) / nbsrv;
1515 else
1516 test->i = px->totpend * 2;
1517
1518 return 1;
1519}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001520
1521/* Note: must not be declared <const> as its list will be overwritten */
1522static struct acl_kw_list acl_kws = {{ },{
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001523 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
1524 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
1525 { "be_id", acl_parse_int, acl_fetch_be_id, acl_match_int, ACL_USE_NOTHING },
1526 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
1527 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1528 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1529 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
1530 { "srv_is_up", acl_parse_nothing, acl_fetch_srv_is_up, acl_match_nothing, ACL_USE_NOTHING },
Willy Tarreau4a0d8282011-02-23 15:24:16 +01001531 { "srv_id", acl_parse_int, acl_fetch_srv_id, acl_match_int, ACL_USE_RTR_INTERNAL },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001532 { NULL, NULL, NULL, NULL },
1533}};
1534
1535
1536__attribute__((constructor))
1537static void __backend_init(void)
1538{
1539 acl_register_keywords(&acl_kws);
1540}
1541
Willy Tarreaubaaee002006-06-26 02:48:02 +02001542/*
1543 * Local variables:
1544 * c-indent-level: 8
1545 * c-basic-offset: 8
1546 * End:
1547 */