blob: f6e2d731ba8b787f0a04c77b63b016a589f7df70 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreauf09c6602012-02-13 17:12:08 +01004 * Copyright 2000-2012 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 Tarreauf09c6602012-02-13 17:12:08 +010034#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020035#include <proto/lb_fwlc.h>
36#include <proto/lb_fwrr.h>
37#include <proto/lb_map.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020039#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010041#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020042#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010043#include <proto/stream_interface.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020044#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/task.h>
46
Willy Tarreaubaaee002006-06-26 02:48:02 +020047/*
48 * This function recounts the number of usable active and backup servers for
49 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010050 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010051 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010052 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020053 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020054void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020055{
56 struct server *srv;
57
Willy Tarreau20697042007-11-15 23:26:18 +010058 px->srv_act = px->srv_bck = 0;
59 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010060 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020061 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010062 if (!srv_is_usable(srv->state, srv->eweight))
63 continue;
64
65 if (srv->state & SRV_BACKUP) {
66 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010067 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010068 px->lbprm.fbck = srv;
69 px->srv_bck++;
70 px->lbprm.tot_wbck += srv->eweight;
71 } else {
72 px->srv_act++;
73 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020074 }
75 }
Willy Tarreaub625a082007-11-26 01:15:43 +010076}
Willy Tarreau20697042007-11-15 23:26:18 +010077
Willy Tarreaub625a082007-11-26 01:15:43 +010078/* This function simply updates the backend's tot_weight and tot_used values
79 * after servers weights have been updated. It is designed to be used after
80 * recount_servers() or equivalent.
81 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020082void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010083{
Willy Tarreau20697042007-11-15 23:26:18 +010084 if (px->srv_act) {
85 px->lbprm.tot_weight = px->lbprm.tot_wact;
86 px->lbprm.tot_used = px->srv_act;
87 }
Willy Tarreaub625a082007-11-26 01:15:43 +010088 else if (px->lbprm.fbck) {
89 /* use only the first backup server */
90 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
91 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010092 }
93 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010094 px->lbprm.tot_weight = px->lbprm.tot_wbck;
95 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +010096 }
Willy Tarreaub625a082007-11-26 01:15:43 +010097}
98
Willy Tarreau39c9ba72009-10-01 21:11:15 +020099/*
100 * This function tries to find a running server for the proxy <px> following
101 * the source hash method. Depending on the number of active/backup servers,
102 * it will either look for active servers, or for backup servers.
103 * If any server is found, it will be returned. If no valid server is found,
104 * NULL is returned.
105 */
106struct server *get_server_sh(struct proxy *px, const char *addr, int len)
107{
108 unsigned int h, l;
109
110 if (px->lbprm.tot_weight == 0)
111 return NULL;
112
113 l = h = 0;
114
115 /* note: we won't hash if there's only one server left */
116 if (px->lbprm.tot_used == 1)
117 goto hash_done;
118
119 while ((l + sizeof (int)) <= len) {
120 h ^= ntohl(*(unsigned int *)(&addr[l]));
121 l += sizeof (int);
122 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100123 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
124 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200125 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200126 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
127 return chash_get_server_hash(px, h);
128 else
129 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200130}
131
132/*
133 * This function tries to find a running server for the proxy <px> following
134 * the URI hash method. In order to optimize cache hits, the hash computation
135 * ends at the question mark. Depending on the number of active/backup servers,
136 * it will either look for active servers, or for backup servers.
137 * If any server is found, it will be returned. If no valid server is found,
138 * NULL is returned.
139 *
140 * This code was contributed by Guillaume Dallaire, who also selected this hash
141 * algorithm out of a tens because it gave him the best results.
142 *
143 */
144struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
145{
146 unsigned long hash = 0;
147 int c;
148 int slashes = 0;
149
150 if (px->lbprm.tot_weight == 0)
151 return NULL;
152
153 /* note: we won't hash if there's only one server left */
154 if (px->lbprm.tot_used == 1)
155 goto hash_done;
156
157 if (px->uri_len_limit)
158 uri_len = MIN(uri_len, px->uri_len_limit);
159
160 while (uri_len--) {
161 c = *uri++;
162 if (c == '/') {
163 slashes++;
164 if (slashes == px->uri_dirs_depth1) /* depth+1 */
165 break;
166 }
167 else if (c == '?')
168 break;
169
170 hash = c + (hash << 6) + (hash << 16) - hash;
171 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100172 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
173 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200174 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200175 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
176 return chash_get_server_hash(px, hash);
177 else
178 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200179}
180
Willy Tarreau01732802007-11-01 22:48:15 +0100181/*
182 * This function tries to find a running server for the proxy <px> following
183 * the URL parameter hash method. It looks for a specific parameter in the
184 * URL and hashes it to compute the server ID. This is useful to optimize
185 * performance by avoiding bounces between servers in contexts where sessions
186 * are shared but cookies are not usable. If the parameter is not found, NULL
187 * is returned. If any server is found, it will be returned. If no valid server
188 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100189 */
190struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
191{
192 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200193 const char *p;
194 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100195 int plen;
196
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200197 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100198 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100199 return NULL;
200
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200201 if ((p = memchr(uri, '?', uri_len)) == NULL)
202 return NULL;
203
Willy Tarreau01732802007-11-01 22:48:15 +0100204 p++;
205
206 uri_len -= (p - uri);
207 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200208 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100209
210 while (uri_len > plen) {
211 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200212 if (params[plen] == '=') {
213 if (memcmp(params, px->url_param_name, plen) == 0) {
214 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100215 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200216 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100217 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200218 p += plen + 1;
219 uri_len -= plen + 1;
220
Willy Tarreau01732802007-11-01 22:48:15 +0100221 while (uri_len && *p != '&') {
222 hash = *p + (hash << 6) + (hash << 16) - hash;
223 uri_len--;
224 p++;
225 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100226 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
227 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200228 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
229 return chash_get_server_hash(px, hash);
230 else
231 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100232 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200233 }
234 /* skip to next parameter */
235 p = memchr(params, '&', uri_len);
236 if (!p)
237 return NULL;
238 p++;
239 uri_len -= (p - params);
240 params = p;
241 }
242 return NULL;
243}
244
245/*
246 * this does the same as the previous server_ph, but check the body contents
247 */
248struct server *get_server_ph_post(struct session *s)
249{
250 unsigned long hash = 0;
251 struct http_txn *txn = &s->txn;
252 struct buffer *req = s->req;
253 struct http_msg *msg = &txn->req;
254 struct proxy *px = s->be;
255 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100256 unsigned long len = msg->body_len;
Willy Tarreau157dd632009-12-06 19:18:09 +0100257 const char *params = req->data + msg->sov;
258 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200259
Willy Tarreau7c96f672009-12-27 22:47:25 +0100260 if (len > req->l - (msg->sov - msg->som))
261 len = req->l - (msg->sov - msg->som);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200262
Willy Tarreau157dd632009-12-06 19:18:09 +0100263 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200264 return NULL;
265
Willy Tarreau157dd632009-12-06 19:18:09 +0100266 if (px->lbprm.tot_weight == 0)
267 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200268
269 while (len > plen) {
270 /* Look for the parameter name followed by an equal symbol */
271 if (params[plen] == '=') {
272 if (memcmp(params, px->url_param_name, plen) == 0) {
273 /* OK, we have the parameter here at <params>, and
274 * the value after the equal sign, at <p>
275 * skip the equal symbol
276 */
277 p += plen + 1;
278 len -= plen + 1;
279
280 while (len && *p != '&') {
281 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100282 /* if in a POST, body must be URI encoded or it's not a URI.
283 * Do not interprete any possible binary data as a parameter.
284 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200285 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
286 break;
287 return NULL; /* oh, no; this is not uri-encoded.
288 * This body does not contain parameters.
289 */
290 }
291 hash = *p + (hash << 6) + (hash << 16) - hash;
292 len--;
293 p++;
294 /* should we break if vlen exceeds limit? */
295 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100296 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
297 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200298 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
299 return chash_get_server_hash(px, hash);
300 else
301 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200302 }
303 }
Willy Tarreau01732802007-11-01 22:48:15 +0100304 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200305 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100306 if (!p)
307 return NULL;
308 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200309 len -= (p - params);
310 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100311 }
312 return NULL;
313}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200314
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200315
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316/*
Benoitaffb4812009-03-25 13:02:10 +0100317 * This function tries to find a running server for the proxy <px> following
318 * the Header parameter hash method. It looks for a specific parameter in the
319 * URL and hashes it to compute the server ID. This is useful to optimize
320 * performance by avoiding bounces between servers in contexts where sessions
321 * are shared but cookies are not usable. If the parameter is not found, NULL
322 * is returned. If any server is found, it will be returned. If no valid server
323 * is found, NULL is returned.
324 */
325struct server *get_server_hh(struct session *s)
326{
327 unsigned long hash = 0;
328 struct http_txn *txn = &s->txn;
329 struct http_msg *msg = &txn->req;
330 struct proxy *px = s->be;
331 unsigned int plen = px->hh_len;
332 unsigned long len;
333 struct hdr_ctx ctx;
334 const char *p;
335
336 /* tot_weight appears to mean srv_count */
337 if (px->lbprm.tot_weight == 0)
338 return NULL;
339
Benoitaffb4812009-03-25 13:02:10 +0100340 ctx.idx = 0;
341
342 /* if the message is chunked, we skip the chunk size, but use the value as len */
343 http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx);
344
345 /* if the header is not found or empty, let's fallback to round robin */
346 if (!ctx.idx || !ctx.vlen)
347 return NULL;
348
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200349 /* note: we won't hash if there's only one server left */
350 if (px->lbprm.tot_used == 1)
351 goto hash_done;
352
Benoitaffb4812009-03-25 13:02:10 +0100353 /* Found a the hh_name in the headers.
354 * we will compute the hash based on this value ctx.val.
355 */
356 len = ctx.vlen;
357 p = (char *)ctx.line + ctx.val;
358 if (!px->hh_match_domain) {
359 while (len) {
360 hash = *p + (hash << 6) + (hash << 16) - hash;
361 len--;
362 p++;
363 }
364 } else {
365 int dohash = 0;
366 p += len - 1;
367 /* special computation, use only main domain name, not tld/host
368 * going back from the end of string, start hashing at first
369 * dot stop at next.
370 * This is designed to work with the 'Host' header, and requires
371 * a special option to activate this.
372 */
373 while (len) {
374 if (*p == '.') {
375 if (!dohash)
376 dohash = 1;
377 else
378 break;
379 } else {
380 if (dohash)
381 hash = *p + (hash << 6) + (hash << 16) - hash;
382 }
383 len--;
384 p--;
385 }
386 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100387 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
388 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200389 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200390 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
391 return chash_get_server_hash(px, hash);
392 else
393 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100394}
395
Emeric Brun736aa232009-06-30 17:56:00 +0200396struct server *get_server_rch(struct session *s)
397{
398 unsigned long hash = 0;
399 struct proxy *px = s->be;
400 unsigned long len;
401 const char *p;
402 int ret;
403 struct acl_expr expr;
404 struct acl_test test;
405
406 /* tot_weight appears to mean srv_count */
407 if (px->lbprm.tot_weight == 0)
408 return NULL;
409
Emeric Brun736aa232009-06-30 17:56:00 +0200410 memset(&expr, 0, sizeof(expr));
411 memset(&test, 0, sizeof(test));
412
413 expr.arg.str = px->hh_name;
414 expr.arg_len = px->hh_len;
415
416 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
Willy Tarreau664092c2011-12-16 19:11:42 +0100417 len = temp_pattern.data.str.len;
418
419 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200420 return NULL;
421
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200422 /* note: we won't hash if there's only one server left */
423 if (px->lbprm.tot_used == 1)
424 goto hash_done;
425
Emeric Brun736aa232009-06-30 17:56:00 +0200426 /* Found a the hh_name in the headers.
427 * we will compute the hash based on this value ctx.val.
428 */
Willy Tarreau664092c2011-12-16 19:11:42 +0100429 p = temp_pattern.data.str.str;
Emeric Brun736aa232009-06-30 17:56:00 +0200430 while (len) {
431 hash = *p + (hash << 6) + (hash << 16) - hash;
432 len--;
433 p++;
434 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100435 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
436 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200437 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200438 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
439 return chash_get_server_hash(px, hash);
440 else
441 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200442}
Benoitaffb4812009-03-25 13:02:10 +0100443
444/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200445 * This function applies the load-balancing algorithm to the session, as
446 * defined by the backend it is assigned to. The session is then marked as
447 * 'assigned'.
448 *
449 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
450 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100451 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200452 * The function tries to keep the original connection slot if it reconnects to
453 * the same server, otherwise it releases it and tries to offer it.
454 *
455 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200456 *
457 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100458 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200459 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
460 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461 * SRV_STATUS_INTERNAL for other unrecoverable errors.
462 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200463 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100464 * it does not need to be called anymore. This means that target_srv(&s->target)
465 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466 *
467 */
468
469int assign_server(struct session *s)
470{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100471
Willy Tarreau7c669d72008-06-20 15:04:11 +0200472 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100473 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200474 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100475
Simon Horman8effd3d2011-08-13 08:03:52 +0900476 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200477
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 Tarreau827aee92011-03-10 16:55:02 +0100482 prev_srv = target_srv(&s->target);
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
Willy Tarreau827aee92011-03-10 16:55:02 +0100491 /* We will now try to find the good server and store it into <target_srv(&s->target)>.
492 * Note that <target_srv(&s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200493 * as well as if no server is available (check error code).
494 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100495
Willy Tarreau827aee92011-03-10 16:55:02 +0100496 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 /* 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 Tarreau827aee92011-03-10 16:55:02 +0100512 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200513 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200514
Willy Tarreauf09c6602012-02-13 17:12:08 +0100515 case BE_LB_LKUP_FSTREE:
516 srv = fas_get_next_server(s->be, prev_srv);
517 break;
518
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200519 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100520 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200521 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200522
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200523 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200524 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200525 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200526 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100527 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200528 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100529 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200530 break;
531 }
532 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200533 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200534 err = SRV_STATUS_INTERNAL;
535 goto out;
536 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200537
538 switch (s->be->lbprm.algo & BE_LB_PARM) {
539 case BE_LB_HASH_SRC:
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200540 if (s->req->prod->addr.from.ss_family == AF_INET) {
541 srv = get_server_sh(s->be,
542 (void *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr,
543 4);
544 }
545 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
546 srv = get_server_sh(s->be,
547 (void *)&((struct sockaddr_in6 *)&s->req->prod->addr.from)->sin6_addr,
548 16);
549 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200550 else {
551 /* unknown IP family */
552 err = SRV_STATUS_INTERNAL;
553 goto out;
554 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200555 break;
556
557 case BE_LB_HASH_URI:
558 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100559 if (s->txn.req.msg_state < HTTP_MSG_BODY)
560 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100561 srv = get_server_uh(s->be,
562 s->txn.req.sol + s->txn.req.sl.rq.u,
563 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200564 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200565
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200566 case BE_LB_HASH_PRM:
567 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100568 if (s->txn.req.msg_state < HTTP_MSG_BODY)
569 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100570
Willy Tarreau827aee92011-03-10 16:55:02 +0100571 srv = get_server_ph(s->be,
572 s->txn.req.sol + s->txn.req.sl.rq.u,
573 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100574
Willy Tarreau827aee92011-03-10 16:55:02 +0100575 if (!srv && s->txn.meth == HTTP_METH_POST)
576 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200577 break;
Benoitaffb4812009-03-25 13:02:10 +0100578
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200579 case BE_LB_HASH_HDR:
580 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100581 if (s->txn.req.msg_state < HTTP_MSG_BODY)
582 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100583 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200584 break;
585
586 case BE_LB_HASH_RDP:
587 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100588 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200589 break;
590
591 default:
592 /* unknown balancing algorithm */
593 err = SRV_STATUS_INTERNAL;
594 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100595 }
Emeric Brun736aa232009-06-30 17:56:00 +0200596
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200597 /* If the hashing parameter was not found, let's fall
598 * back to round robin on the map.
599 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100600 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200601 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100602 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200603 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100604 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200605 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200606
607 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200608 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200609
Willy Tarreau7c669d72008-06-20 15:04:11 +0200610 default:
611 /* unknown balancing algorithm */
612 err = SRV_STATUS_INTERNAL;
613 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200614 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200615
Willy Tarreau827aee92011-03-10 16:55:02 +0100616 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200617 err = SRV_STATUS_FULL;
618 goto out;
619 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100620 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100621 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100622 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100623 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100624 set_target_server(&s->target, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200625 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200626 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau9e000c62011-03-10 14:03:36 +0100627 set_target_proxy(&s->target, s->be);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200628 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100629 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreau6471afb2011-09-23 10:54:59 +0200630 is_addr(&s->req->cons->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100631 /* in proxy mode, we need a valid destination address */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100632 set_target_proxy(&s->target, s->be);
Willy Tarreau664beb82011-03-10 11:38:29 +0100633 }
634 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200635 err = SRV_STATUS_NOSRV;
636 goto out;
637 }
638
639 s->flags |= SN_ASSIGNED;
640 err = SRV_STATUS_OK;
641 out:
642
643 /* Either we take back our connection slot, or we offer it to someone
644 * else if we don't need it anymore.
645 */
646 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100647 if (conn_slot == srv) {
648 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200649 } else {
650 if (may_dequeue_tasks(conn_slot, s->be))
651 process_srv_queue(conn_slot);
652 }
653 }
654
655 out_err:
656 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657}
658
659
660/*
661 * This function assigns a server address to a session, and sets SN_ADDR_SET.
662 * The address is taken from the currently assigned server, or from the
663 * dispatch or transparent address.
664 *
665 * It may return :
666 * SRV_STATUS_OK if everything is OK.
667 * SRV_STATUS_INTERNAL for other unrecoverable errors.
668 *
669 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
670 * not cleared, so it's to the caller to clear it if required.
671 *
672 */
673int assign_server_address(struct session *s)
674{
675#ifdef DEBUG_FULL
676 fprintf(stderr,"assign_server_address : s=%p\n",s);
677#endif
678
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200679 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680 /* A server is necessarily known for this session */
681 if (!(s->flags & SN_ASSIGNED))
682 return SRV_STATUS_INTERNAL;
683
Willy Tarreau6471afb2011-09-23 10:54:59 +0200684 s->req->cons->addr.to = target_srv(&s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200685
Willy Tarreau6471afb2011-09-23 10:54:59 +0200686 if (!is_addr(&s->req->cons->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200687 /* if the server has no address, we use the same address
688 * the client asked, which is handy for remapping ports
689 * locally on multiple addresses at once.
690 */
691 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
692 get_frt_addr(s);
693
Willy Tarreau6471afb2011-09-23 10:54:59 +0200694 if (s->req->prod->addr.to.ss_family == AF_INET) {
695 ((struct sockaddr_in *)&s->req->cons->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
696 } else if (s->req->prod->addr.to.ss_family == AF_INET6) {
697 ((struct sockaddr_in6 *)&s->req->cons->addr.to)->sin6_addr = ((struct sockaddr_in6 *)&s->req->prod->addr.to)->sin6_addr;
Emeric Brunec810d12010-10-22 16:36:33 +0200698 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200699 }
700
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701 /* if this server remaps proxied ports, we'll use
702 * the port the client connected to with an offset. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100703 if (target_srv(&s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100704 int base_port;
705
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100706 if (!(s->be->options & PR_O_TRANSP) && !(s->flags & SN_FRT_ADDR_SET))
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200707 get_frt_addr(s);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100708
709 /* First, retrieve the port from the incoming connection */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200710 base_port = get_host_port(&s->req->prod->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100711
712 /* Second, assign the outgoing connection's port */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200713 base_port += get_host_port(&s->req->cons->addr.to);
714 set_host_port(&s->req->cons->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200715 }
716 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200717 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200718 /* connect to the defined dispatch addr */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200719 s->req->cons->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200720 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100721 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200722 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaubd414282008-01-19 13:46:35 +0100723 if (!(s->flags & SN_FRT_ADDR_SET))
724 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725
Willy Tarreau6471afb2011-09-23 10:54:59 +0200726 if (s->req->prod->addr.to.ss_family == AF_INET || s->req->prod->addr.to.ss_family == AF_INET6) {
727 memcpy(&s->req->cons->addr.to, &s->req->prod->addr.to, MIN(sizeof(s->req->cons->addr.to), sizeof(s->req->prod->addr.to)));
Emeric Brunec810d12010-10-22 16:36:33 +0200728 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100729 /* when we support IPv6 on the backend, we may add other tests */
730 //qfprintf(stderr, "Cannot get original server address.\n");
731 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100733 else if (s->be->options & PR_O_HTTP_PROXY) {
734 /* If HTTP PROXY option is set, then server is already assigned
735 * during incoming client request parsing. */
736 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100737 else {
738 /* no server and no LB algorithm ! */
739 return SRV_STATUS_INTERNAL;
740 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741
742 s->flags |= SN_ADDR_SET;
743 return SRV_STATUS_OK;
744}
745
746
747/* This function assigns a server to session <s> if required, and can add the
748 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200749 * If ->srv_conn is set, the session is first released from the server.
750 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
751 * be called before any connection and after any retry or redispatch occurs.
752 *
753 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754 *
755 * Returns :
756 *
757 * SRV_STATUS_OK if everything is OK.
Willy Tarreau827aee92011-03-10 16:55:02 +0100758 * SRV_STATUS_NOSRV if no server is available. target_srv(&s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759 * SRV_STATUS_QUEUED if the connection has been queued.
760 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200762 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763 * SRV_STATUS_INTERNAL for other unrecoverable errors.
764 *
765 */
766int assign_server_and_queue(struct session *s)
767{
768 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100769 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200770 int err;
771
772 if (s->pend_pos)
773 return SRV_STATUS_INTERNAL;
774
Willy Tarreau7c669d72008-06-20 15:04:11 +0200775 err = SRV_STATUS_OK;
776 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100777 struct server *prev_srv = target_srv(&s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100778
Willy Tarreau7c669d72008-06-20 15:04:11 +0200779 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100780 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200781 /* This session was previously assigned to a server. We have to
782 * update the session's and the server's stats :
783 * - if the server changed :
784 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
785 * - set SN_REDISP if it was successfully redispatched
786 * - increment srv->redispatches and be->redispatches
787 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100788 */
789
Willy Tarreau827aee92011-03-10 16:55:02 +0100790 if (prev_srv != target_srv(&s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200791 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
792 s->txn.flags &= ~TX_CK_MASK;
793 s->txn.flags |= TX_CK_DOWN;
794 }
795 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100796 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100797 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200798 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100799 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100800 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100801 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100802 }
803 }
804
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 switch (err) {
806 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200807 /* we have SN_ASSIGNED set */
Willy Tarreau827aee92011-03-10 16:55:02 +0100808 srv = target_srv(&s->target);
809 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200810 return SRV_STATUS_OK; /* dispatch or proxy mode */
811
812 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100813 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200814 return SRV_STATUS_OK;
815
816 /* OK, this session already has an assigned server, but no
817 * connection slot yet. Either it is a redispatch, or it was
818 * assigned from persistence information (direct mode).
819 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100820 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200821 /* server scheduled for redirection, and already assigned. We
822 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100823 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100824 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100825 return SRV_STATUS_OK;
826 }
827
Willy Tarreau7c669d72008-06-20 15:04:11 +0200828 /* We might have to queue this session if the assigned server is full.
829 * We know we have to queue it into the server's queue, so if a maxqueue
830 * is set on the server, we must also check that the server's queue is
831 * not full, in which case we have to return FULL.
832 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100833 if (srv->maxconn &&
834 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200835
Willy Tarreau827aee92011-03-10 16:55:02 +0100836 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200837 return SRV_STATUS_FULL;
838
Willy Tarreaubaaee002006-06-26 02:48:02 +0200839 p = pendconn_add(s);
840 if (p)
841 return SRV_STATUS_QUEUED;
842 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200843 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200845
846 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100847 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200848 return SRV_STATUS_OK;
849
850 case SRV_STATUS_FULL:
851 /* queue this session into the proxy's queue */
852 p = pendconn_add(s);
853 if (p)
854 return SRV_STATUS_QUEUED;
855 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200856 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200857
858 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200859 return err;
860
Willy Tarreaubaaee002006-06-26 02:48:02 +0200861 case SRV_STATUS_INTERNAL:
862 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200863
Willy Tarreaubaaee002006-06-26 02:48:02 +0200864 default:
865 return SRV_STATUS_INTERNAL;
866 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100867}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200868
Willy Tarreaub1d67742010-03-29 19:36:59 +0200869/* If an explicit source binding is specified on the server and/or backend, and
870 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200871 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200872 */
873static void assign_tproxy_address(struct session *s)
874{
875#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau827aee92011-03-10 16:55:02 +0100876 struct server *srv = target_srv(&s->target);
877
878 if (srv && srv->state & SRV_BIND_SRC) {
879 switch (srv->state & SRV_TPROXY_MASK) {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200880 case SRV_TPROXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200881 s->req->cons->addr.from = srv->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200882 break;
883 case SRV_TPROXY_CLI:
884 case SRV_TPROXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200885 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200886 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200887 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200888 case SRV_TPROXY_DYN:
Willy Tarreau827aee92011-03-10 16:55:02 +0100889 if (srv->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100890 char *vptr;
891 int vlen;
892
Willy Tarreaubce70882009-09-07 11:51:47 +0200893 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100894 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200895 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100896 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
897
898 if (http_get_hdr(&s->txn.req, srv->bind_hdr_name, srv->bind_hdr_len,
899 &s->txn.hdr_idx, srv->bind_hdr_occ, NULL, &vptr, &vlen)) {
900 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
901 htonl(inetaddr_host_lim(vptr, vptr + vlen));
902 }
Willy Tarreaubce70882009-09-07 11:51:47 +0200903 }
904 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200905 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200906 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200907 }
908 }
909 else if (s->be->options & PR_O_BIND_SRC) {
910 switch (s->be->options & PR_O_TPXY_MASK) {
911 case PR_O_TPXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200912 s->req->cons->addr.from = s->be->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200913 break;
914 case PR_O_TPXY_CLI:
915 case PR_O_TPXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200916 /* FIXME: what can we do if the client connects in IPv6 or socket unix? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200917 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200918 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200919 case PR_O_TPXY_DYN:
920 if (s->be->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100921 char *vptr;
922 int vlen;
923
Willy Tarreaubce70882009-09-07 11:51:47 +0200924 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100925 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200926 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100927 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
928
929 if (http_get_hdr(&s->txn.req, s->be->bind_hdr_name, s->be->bind_hdr_len,
930 &s->txn.hdr_idx, s->be->bind_hdr_occ, NULL, &vptr, &vlen)) {
931 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
932 htonl(inetaddr_host_lim(vptr, vptr + vlen));
933 }
Willy Tarreaubce70882009-09-07 11:51:47 +0200934 }
935 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200936 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200937 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200938 }
939 }
940#endif
941}
942
943
Willy Tarreaubaaee002006-06-26 02:48:02 +0200944/*
945 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200946 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100947 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948 * It can return one of :
949 * - SN_ERR_NONE if everything's OK
950 * - SN_ERR_SRVTO if there are no more servers
951 * - SN_ERR_SRVCL if the connection was refused by the server
952 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
953 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
954 * - SN_ERR_INTERNAL for any other purely internal errors
955 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
956 */
957int connect_server(struct session *s)
958{
Willy Tarreau827aee92011-03-10 16:55:02 +0100959 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200960 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200961
962 if (!(s->flags & SN_ADDR_SET)) {
963 err = assign_server_address(s);
964 if (err != SRV_STATUS_OK)
965 return SN_ERR_INTERNAL;
966 }
967
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200968 /* Prepare the stream interface for a TCP connection. Later
969 * we may assign a protocol-specific connect() function.
Willy Tarreau664beb82011-03-10 11:38:29 +0100970 * NOTE: when we later support HTTP keep-alive, we'll have to
971 * decide here if we can reuse the connection by comparing the
972 * session's freshly assigned target with the stream interface's.
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200973 */
974 stream_sock_prepare_interface(s->req->cons);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100975 s->req->cons->connect = tcp_connect_server;
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200976 /* the target was only on the session, assign it to the SI now */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100977 copy_target(&s->req->cons->target, &s->target);
Willy Tarreaud88edf22009-06-14 15:48:17 +0200978
Willy Tarreau5ab04ec2011-03-20 10:32:26 +0100979 /* process the case where the server requires the PROXY protocol to be sent */
980 s->req->cons->send_proxy_ofs = 0;
981 if (s->target.type == TARG_TYPE_SERVER && (s->target.ptr.s->state & SRV_SEND_PROXY)) {
982 s->req->cons->send_proxy_ofs = 1; /* must compute size */
983 if (!(s->flags & SN_FRT_ADDR_SET))
984 get_frt_addr(s);
985 }
986
Willy Tarreaub1d67742010-03-29 19:36:59 +0200987 assign_tproxy_address(s);
988
William Lallemandb7ff6a32012-03-02 14:35:21 +0100989 /* flag for logging source ip/port */
990 if (s->fe->options2 & PR_O2_SRC_ADDR)
991 s->req->cons->flags |= SI_FL_SRC_ADDR;
992
Willy Tarreauac825402011-03-04 22:04:29 +0100993 err = s->req->cons->connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200994
Willy Tarreau9650f372009-08-16 14:02:45 +0200995 if (err != SN_ERR_NONE)
996 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200997
Willy Tarreau827aee92011-03-10 16:55:02 +0100998 srv = target_srv(&s->target);
999 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001000 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001001 srv->cur_sess++;
1002 if (srv->cur_sess > srv->counters.cur_sess_max)
1003 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001004 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001005 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001006 }
1007
Willy Tarreaubaaee002006-06-26 02:48:02 +02001008 return SN_ERR_NONE; /* connection is OK */
1009}
1010
1011
Willy Tarreaubaaee002006-06-26 02:48:02 +02001012/* This function performs the "redispatch" part of a connection attempt. It
1013 * will assign a server if required, queue the connection if required, and
1014 * handle errors that might arise at this level. It can change the server
1015 * state. It will return 1 if it encounters an error, switches the server
1016 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1017 * that the connection is ready to use.
1018 */
1019
1020int srv_redispatch_connect(struct session *t)
1021{
Willy Tarreau827aee92011-03-10 16:55:02 +01001022 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001023 int conn_err;
1024
1025 /* We know that we don't have any connection pending, so we will
1026 * try to get a new one, and wait in this state if it's queued
1027 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001028 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001029 conn_err = assign_server_and_queue(t);
Willy Tarreau827aee92011-03-10 16:55:02 +01001030 srv = target_srv(&t->target);
1031
Willy Tarreaubaaee002006-06-26 02:48:02 +02001032 switch (conn_err) {
1033 case SRV_STATUS_OK:
1034 break;
1035
Willy Tarreau7c669d72008-06-20 15:04:11 +02001036 case SRV_STATUS_FULL:
1037 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1038 * and we can redispatch to another server, or it is not and we return
1039 * 503. This only makes sense in DIRECT mode however, because normal LB
1040 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001041 * would bring us on the same server again. Note that t->target is set
1042 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001043 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001044 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1045 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001046 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001047 goto redispatch;
1048 }
1049
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001050 if (!t->req->cons->err_type) {
1051 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001052 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001053 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001054
Willy Tarreau827aee92011-03-10 16:55:02 +01001055 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001056 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001057 return 1;
1058
Willy Tarreaubaaee002006-06-26 02:48:02 +02001059 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001060 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001061 if (!t->req->cons->err_type) {
1062 t->req->cons->err_type = SI_ET_CONN_ERR;
1063 t->req->cons->err_loc = NULL;
1064 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001065
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001066 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001067 return 1;
1068
1069 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001070 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001071 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001072 /* do nothing else and do not wake any other session up */
1073 return 1;
1074
Willy Tarreaubaaee002006-06-26 02:48:02 +02001075 case SRV_STATUS_INTERNAL:
1076 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001077 if (!t->req->cons->err_type) {
1078 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001079 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001080 }
1081
Willy Tarreau827aee92011-03-10 16:55:02 +01001082 if (srv)
1083 srv_inc_sess_ctr(srv);
1084 if (srv)
1085 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001086 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001087
1088 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001089 if (may_dequeue_tasks(srv, t->be))
1090 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001091 return 1;
1092 }
1093 /* if we get here, it's because we got SRV_STATUS_OK, which also
1094 * means that the connection has not been queued.
1095 */
1096 return 0;
1097}
1098
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001099/* Apply RDP cookie persistence to the current session. For this, the function
1100 * tries to extract an RDP cookie from the request buffer, and look for the
1101 * matching server in the list. If the server is found, it is assigned to the
1102 * session. This always returns 1, and the analyser removes itself from the
1103 * list. Nothing is performed if a server was already assigned.
1104 */
1105int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1106{
1107 struct proxy *px = s->be;
1108 int ret;
1109 struct acl_expr expr;
1110 struct acl_test test;
1111 struct server *srv = px->srv;
1112 struct sockaddr_in addr;
1113 char *p;
1114
1115 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1116 now_ms, __FUNCTION__,
1117 s,
1118 req,
1119 req->rex, req->wex,
1120 req->flags,
1121 req->l,
1122 req->analysers);
1123
1124 if (s->flags & SN_ASSIGNED)
1125 goto no_cookie;
1126
1127 memset(&expr, 0, sizeof(expr));
1128 memset(&test, 0, sizeof(test));
1129
1130 expr.arg.str = s->be->rdp_cookie_name;
1131 expr.arg_len = s->be->rdp_cookie_len;
1132
1133 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
Willy Tarreau664092c2011-12-16 19:11:42 +01001134 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || temp_pattern.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001135 goto no_cookie;
1136
1137 memset(&addr, 0, sizeof(addr));
1138 addr.sin_family = AF_INET;
1139
Willy Tarreau664092c2011-12-16 19:11:42 +01001140 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
1141 addr.sin_addr.s_addr = strtoul(temp_pattern.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001142 if (*p != '.')
1143 goto no_cookie;
1144 p++;
1145 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1146 if (*p != '.')
1147 goto no_cookie;
1148
Willy Tarreau9e000c62011-03-10 14:03:36 +01001149 clear_target(&s->target);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001150 while (srv) {
1151 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1152 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1153 /* we found the server and it is usable */
1154 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001155 set_target_server(&s->target, srv);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001156 break;
1157 }
1158 }
1159 srv = srv->next;
1160 }
1161
1162no_cookie:
1163 req->analysers &= ~an_bit;
1164 req->analyse_exp = TICK_ETERNITY;
1165 return 1;
1166}
1167
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001168int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001169 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001170 return px->down_time;
1171
1172 return now.tv_sec - px->last_change + px->down_time;
1173}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001174
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001175/*
1176 * This function returns a string containing the balancing
1177 * mode of the proxy in a format suitable for stats.
1178 */
1179
1180const char *backend_lb_algo_str(int algo) {
1181
1182 if (algo == BE_LB_ALGO_RR)
1183 return "roundrobin";
1184 else if (algo == BE_LB_ALGO_SRR)
1185 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001186 else if (algo == BE_LB_ALGO_FAS)
1187 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001188 else if (algo == BE_LB_ALGO_LC)
1189 return "leastconn";
1190 else if (algo == BE_LB_ALGO_SH)
1191 return "source";
1192 else if (algo == BE_LB_ALGO_UH)
1193 return "uri";
1194 else if (algo == BE_LB_ALGO_PH)
1195 return "url_param";
1196 else if (algo == BE_LB_ALGO_HH)
1197 return "hdr";
1198 else if (algo == BE_LB_ALGO_RCH)
1199 return "rdp-cookie";
1200 else
1201 return NULL;
1202}
1203
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001204/* This function parses a "balance" statement in a backend section describing
1205 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1206 * returns -1, it may write an error message into ther <err> buffer, for at
1207 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1208 * written. The function must be called with <args> pointing to the first word
1209 * after "balance".
1210 */
1211int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1212{
1213 if (!*(args[0])) {
1214 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001215 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1216 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001217 return 0;
1218 }
1219
1220 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001221 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1222 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001223 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001224 else if (!strcmp(args[0], "static-rr")) {
1225 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1226 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1227 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001228 else if (!strcmp(args[0], "first")) {
1229 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1230 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1231 }
Willy Tarreau51406232008-03-10 22:04:20 +01001232 else if (!strcmp(args[0], "leastconn")) {
1233 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1234 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1235 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001236 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001237 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1238 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001239 }
1240 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001241 int arg = 1;
1242
Willy Tarreau31682232007-11-29 15:38:04 +01001243 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1244 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001245
1246 while (*args[arg]) {
1247 if (!strcmp(args[arg], "len")) {
1248 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1249 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1250 return -1;
1251 }
1252 curproxy->uri_len_limit = atoi(args[arg+1]);
1253 arg += 2;
1254 }
1255 else if (!strcmp(args[arg], "depth")) {
1256 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1257 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1258 return -1;
1259 }
1260 /* hint: we store the position of the ending '/' (depth+1) so
1261 * that we avoid a comparison while computing the hash.
1262 */
1263 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1264 arg += 2;
1265 }
1266 else {
1267 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1268 return -1;
1269 }
1270 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001271 }
Willy Tarreau01732802007-11-01 22:48:15 +01001272 else if (!strcmp(args[0], "url_param")) {
1273 if (!*args[1]) {
1274 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1275 return -1;
1276 }
Willy Tarreau31682232007-11-29 15:38:04 +01001277 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1278 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001279
1280 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001281 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001282 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001283 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001284 if (strcmp(args[2], "check_post")) {
1285 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1286 return -1;
1287 }
1288 if (*args[3]) {
1289 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1290 curproxy->url_param_post_limit = str2ui(args[3]);
1291 }
1292 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1293 if (!curproxy->url_param_post_limit)
1294 curproxy->url_param_post_limit = 48;
1295 else if ( curproxy->url_param_post_limit < 3 )
1296 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1297 }
Benoitaffb4812009-03-25 13:02:10 +01001298 }
1299 else if (!strncmp(args[0], "hdr(", 4)) {
1300 const char *beg, *end;
1301
1302 beg = args[0] + 4;
1303 end = strchr(beg, ')');
1304
1305 if (!end || end == beg) {
1306 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1307 return -1;
1308 }
1309
1310 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1311 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1312
1313 free(curproxy->hh_name);
1314 curproxy->hh_len = end - beg;
1315 curproxy->hh_name = my_strndup(beg, end - beg);
1316 curproxy->hh_match_domain = 0;
1317
1318 if (*args[1]) {
1319 if (strcmp(args[1], "use_domain_only")) {
1320 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1321 return -1;
1322 }
1323 curproxy->hh_match_domain = 1;
1324 }
1325
Emeric Brun736aa232009-06-30 17:56:00 +02001326 }
1327 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1328 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1329 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1330
1331 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1332 const char *beg, *end;
1333
1334 beg = args[0] + 11;
1335 end = strchr(beg, ')');
1336
1337 if (!end || end == beg) {
1338 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1339 return -1;
1340 }
1341
1342 free(curproxy->hh_name);
1343 curproxy->hh_name = my_strndup(beg, end - beg);
1344 curproxy->hh_len = end - beg;
1345 }
1346 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1347 free(curproxy->hh_name);
1348 curproxy->hh_name = strdup("mstshash");
1349 curproxy->hh_len = strlen(curproxy->hh_name);
1350 }
1351 else { /* syntax */
1352 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1353 return -1;
1354 }
Willy Tarreau01732802007-11-01 22:48:15 +01001355 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001356 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001357 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 +01001358 return -1;
1359 }
1360 return 0;
1361}
1362
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001363
1364/************************************************************************/
1365/* All supported keywords must be declared here. */
1366/************************************************************************/
1367
Willy Tarreaua5e37562011-12-16 17:06:15 +01001368/* set temp integer to the number of enabled servers on the proxy */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001369static int
1370acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
1371 struct acl_expr *expr, struct acl_test *test)
1372{
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 if (px->srv_act)
Willy Tarreaua5e37562011-12-16 17:06:15 +01001384 temp_pattern.data.integer = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001385 else if (px->lbprm.fbck)
Willy Tarreaua5e37562011-12-16 17:06:15 +01001386 temp_pattern.data.integer = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001387 else
Willy Tarreaua5e37562011-12-16 17:06:15 +01001388 temp_pattern.data.integer = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001389
1390 return 1;
1391}
1392
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001393/* report in test->flags a success or failure depending on the designated
1394 * server's state. There is no match function involved since there's no pattern.
1395 */
1396static int
1397acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, int dir,
1398 struct acl_expr *expr, struct acl_test *test)
1399{
1400 struct server *srv = expr->arg.srv;
1401
1402 test->flags = ACL_TEST_F_VOL_TEST;
1403 if (!(srv->state & SRV_MAINTAIN) &&
1404 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
1405 test->flags |= ACL_TEST_F_SET_RES_PASS;
1406 else
1407 test->flags |= ACL_TEST_F_SET_RES_FAIL;
1408 return 1;
1409}
1410
Willy Tarreaua5e37562011-12-16 17:06:15 +01001411/* set temp integer to the number of enabled servers on the proxy */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001412static int
1413acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
1414 struct acl_expr *expr, struct acl_test *test)
1415{
1416 struct server *iterator;
1417 test->flags = ACL_TEST_F_VOL_TEST;
1418 if (expr->arg_len) {
1419 /* another proxy was designated, we must look for it */
1420 for (px = proxy; px; px = px->next)
1421 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1422 break;
1423 }
1424 if (!px)
1425 return 0;
1426
Willy Tarreaua5e37562011-12-16 17:06:15 +01001427 temp_pattern.data.integer = 0;
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001428 iterator = px->srv;
1429 while (iterator) {
Willy Tarreaua36af912009-10-10 12:02:45 +02001430 if ((iterator->state & SRV_RUNNING) == 0) {
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001431 iterator = iterator->next;
1432 continue;
1433 }
1434 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001435 /* configuration is stupid */
1436 temp_pattern.data.integer = -1;
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001437 return 1;
1438 }
1439
Willy Tarreaua5e37562011-12-16 17:06:15 +01001440 temp_pattern.data.integer += (iterator->maxconn - iterator->cur_sess)
1441 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001442 iterator = iterator->next;
1443 }
1444
1445 return 1;
1446}
1447
Willy Tarreaua5e37562011-12-16 17:06:15 +01001448/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001449static int
1450acl_fetch_be_id(struct proxy *px, struct session *l4, void *l7, int dir,
1451 struct acl_expr *expr, struct acl_test *test) {
1452
1453 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreaua5e37562011-12-16 17:06:15 +01001454 temp_pattern.data.integer = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001455
1456 return 1;
1457}
1458
Willy Tarreaua5e37562011-12-16 17:06:15 +01001459/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001460static int
1461acl_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, int dir,
1462 struct acl_expr *expr, struct acl_test *test) {
1463
Willy Tarreau827aee92011-03-10 16:55:02 +01001464 if (!target_srv(&l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001465 return 0;
1466
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001467 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreaua5e37562011-12-16 17:06:15 +01001468 temp_pattern.data.integer = target_srv(&l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001469
1470 return 1;
1471}
1472
Willy Tarreaua5e37562011-12-16 17:06:15 +01001473/* set temp integer to the number of connections per second reaching the backend */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001474static int
1475acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
1476 struct acl_expr *expr, struct acl_test *test)
1477{
1478 test->flags = ACL_TEST_F_VOL_TEST;
1479 if (expr->arg_len) {
1480 /* another proxy was designated, we must look for it */
1481 for (px = proxy; px; px = px->next)
1482 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1483 break;
1484 }
1485 if (!px)
1486 return 0;
1487
Willy Tarreaua5e37562011-12-16 17:06:15 +01001488 temp_pattern.data.integer = read_freq_ctr(&px->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001489 return 1;
1490}
1491
Willy Tarreaua5e37562011-12-16 17:06:15 +01001492/* set temp integer to the number of concurrent connections on the backend */
Willy Tarreaua36af912009-10-10 12:02:45 +02001493static int
1494acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1495 struct acl_expr *expr, struct acl_test *test)
1496{
1497 test->flags = ACL_TEST_F_VOL_TEST;
1498 if (expr->arg_len) {
1499 /* another proxy was designated, we must look for it */
1500 for (px = proxy; px; px = px->next)
1501 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1502 break;
1503 }
1504 if (!px)
1505 return 0;
1506
Willy Tarreaua5e37562011-12-16 17:06:15 +01001507 temp_pattern.data.integer = px->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001508 return 1;
1509}
1510
Willy Tarreaua5e37562011-12-16 17:06:15 +01001511/* set temp integer to the total number of queued connections on the backend */
Willy Tarreaua36af912009-10-10 12:02:45 +02001512static int
1513acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1514 struct acl_expr *expr, struct acl_test *test)
1515{
1516 test->flags = ACL_TEST_F_VOL_TEST;
1517 if (expr->arg_len) {
1518 /* another proxy was designated, we must look for it */
1519 for (px = proxy; px; px = px->next)
1520 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1521 break;
1522 }
1523 if (!px)
1524 return 0;
1525
Willy Tarreaua5e37562011-12-16 17:06:15 +01001526 temp_pattern.data.integer = px->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001527 return 1;
1528}
1529
Willy Tarreaua5e37562011-12-16 17:06:15 +01001530/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001531 * by the number of running servers and rounded up. If there is no running
1532 * server, we return twice the total, just as if we had half a running server.
1533 * This is more or less correct anyway, since we expect the last server to come
1534 * back soon.
1535 */
1536static int
1537acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
1538 struct acl_expr *expr, struct acl_test *test)
1539{
1540 int nbsrv;
1541
1542 test->flags = ACL_TEST_F_VOL_TEST;
1543 if (expr->arg_len) {
1544 /* another proxy was designated, we must look for it */
1545 for (px = proxy; px; px = px->next)
1546 if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str))
1547 break;
1548 }
1549 if (!px)
1550 return 0;
1551
1552 if (px->srv_act)
1553 nbsrv = px->srv_act;
1554 else if (px->lbprm.fbck)
1555 nbsrv = 1;
1556 else
1557 nbsrv = px->srv_bck;
1558
1559 if (nbsrv > 0)
Willy Tarreaua5e37562011-12-16 17:06:15 +01001560 temp_pattern.data.integer = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001561 else
Willy Tarreaua5e37562011-12-16 17:06:15 +01001562 temp_pattern.data.integer = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001563
1564 return 1;
1565}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001566
Willy Tarreaua5e37562011-12-16 17:06:15 +01001567/* set temp integer to the number of concurrent connections on the server in the backend */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001568static int
1569acl_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, int dir,
1570 struct acl_expr *expr, struct acl_test *test)
1571{
1572 struct server *srv = expr->arg.srv;
1573
Willy Tarreaua5e37562011-12-16 17:06:15 +01001574 temp_pattern.data.integer = srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001575 return 1;
1576}
1577
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001578/* Note: must not be declared <const> as its list will be overwritten */
1579static struct acl_kw_list acl_kws = {{ },{
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001580 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING },
1581 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING },
1582 { "be_id", acl_parse_int, acl_fetch_be_id, acl_match_int, ACL_USE_NOTHING },
1583 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING },
1584 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING },
1585 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING },
1586 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING },
1587 { "srv_is_up", acl_parse_nothing, acl_fetch_srv_is_up, acl_match_nothing, ACL_USE_NOTHING },
Willy Tarreau4a0d8282011-02-23 15:24:16 +01001588 { "srv_id", acl_parse_int, acl_fetch_srv_id, acl_match_int, ACL_USE_RTR_INTERNAL },
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001589 { "srv_conn", acl_parse_int, acl_fetch_srv_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001590 { NULL, NULL, NULL, NULL },
1591}};
1592
1593
1594__attribute__((constructor))
1595static void __backend_init(void)
1596{
1597 acl_register_keywords(&acl_kws);
1598}
1599
Willy Tarreaubaaee002006-06-26 02:48:02 +02001600/*
1601 * Local variables:
1602 * c-indent-level: 8
1603 * c-basic-offset: 8
1604 * End:
1605 */