blob: 9a5b1968f0c737bfd7de0f0c9a786648ac705880 [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 Tarreau34db1082012-04-19 17:16:54 +020031#include <proto/arg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <proto/backend.h>
Willy Tarreaud1de8af2012-05-18 22:12:14 +020033#include <proto/buffers.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020034#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020035#include <proto/lb_chash.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010036#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020037#include <proto/lb_fwlc.h>
38#include <proto/lb_fwrr.h>
39#include <proto/lb_map.h>
Willy Tarreau26d8c592012-05-07 18:12:14 +020040#include <proto/protocols.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020042#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010044#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020045#include <proto/session.h>
Willy Tarreauc63190d2012-05-11 14:23:52 +020046#include <proto/sock_raw.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010047#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/task.h>
49
Willy Tarreaubaaee002006-06-26 02:48:02 +020050/*
51 * This function recounts the number of usable active and backup servers for
52 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010053 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010054 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010055 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020056 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020057void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020058{
59 struct server *srv;
60
Willy Tarreau20697042007-11-15 23:26:18 +010061 px->srv_act = px->srv_bck = 0;
62 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010063 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020064 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010065 if (!srv_is_usable(srv->state, srv->eweight))
66 continue;
67
68 if (srv->state & SRV_BACKUP) {
69 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010070 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010071 px->lbprm.fbck = srv;
72 px->srv_bck++;
73 px->lbprm.tot_wbck += srv->eweight;
74 } else {
75 px->srv_act++;
76 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020077 }
78 }
Willy Tarreaub625a082007-11-26 01:15:43 +010079}
Willy Tarreau20697042007-11-15 23:26:18 +010080
Willy Tarreaub625a082007-11-26 01:15:43 +010081/* This function simply updates the backend's tot_weight and tot_used values
82 * after servers weights have been updated. It is designed to be used after
83 * recount_servers() or equivalent.
84 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020085void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010086{
Willy Tarreau20697042007-11-15 23:26:18 +010087 if (px->srv_act) {
88 px->lbprm.tot_weight = px->lbprm.tot_wact;
89 px->lbprm.tot_used = px->srv_act;
90 }
Willy Tarreaub625a082007-11-26 01:15:43 +010091 else if (px->lbprm.fbck) {
92 /* use only the first backup server */
93 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
94 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010095 }
96 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010097 px->lbprm.tot_weight = px->lbprm.tot_wbck;
98 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +010099 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100100}
101
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200102/*
103 * This function tries to find a running server for the proxy <px> following
104 * the source hash method. Depending on the number of active/backup servers,
105 * it will either look for active servers, or for backup servers.
106 * If any server is found, it will be returned. If no valid server is found,
107 * NULL is returned.
108 */
109struct server *get_server_sh(struct proxy *px, const char *addr, int len)
110{
111 unsigned int h, l;
112
113 if (px->lbprm.tot_weight == 0)
114 return NULL;
115
116 l = h = 0;
117
118 /* note: we won't hash if there's only one server left */
119 if (px->lbprm.tot_used == 1)
120 goto hash_done;
121
122 while ((l + sizeof (int)) <= len) {
123 h ^= ntohl(*(unsigned int *)(&addr[l]));
124 l += sizeof (int);
125 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100126 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
127 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200128 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200129 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
130 return chash_get_server_hash(px, h);
131 else
132 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200133}
134
135/*
136 * This function tries to find a running server for the proxy <px> following
137 * the URI hash method. In order to optimize cache hits, the hash computation
138 * ends at the question mark. Depending on the number of active/backup servers,
139 * it will either look for active servers, or for backup servers.
140 * If any server is found, it will be returned. If no valid server is found,
141 * NULL is returned.
142 *
143 * This code was contributed by Guillaume Dallaire, who also selected this hash
144 * algorithm out of a tens because it gave him the best results.
145 *
146 */
147struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
148{
149 unsigned long hash = 0;
150 int c;
151 int slashes = 0;
152
153 if (px->lbprm.tot_weight == 0)
154 return NULL;
155
156 /* note: we won't hash if there's only one server left */
157 if (px->lbprm.tot_used == 1)
158 goto hash_done;
159
160 if (px->uri_len_limit)
161 uri_len = MIN(uri_len, px->uri_len_limit);
162
163 while (uri_len--) {
164 c = *uri++;
165 if (c == '/') {
166 slashes++;
167 if (slashes == px->uri_dirs_depth1) /* depth+1 */
168 break;
169 }
170 else if (c == '?')
171 break;
172
173 hash = c + (hash << 6) + (hash << 16) - hash;
174 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100175 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
176 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200177 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200178 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
179 return chash_get_server_hash(px, hash);
180 else
181 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200182}
183
Willy Tarreau01732802007-11-01 22:48:15 +0100184/*
185 * This function tries to find a running server for the proxy <px> following
186 * the URL parameter hash method. It looks for a specific parameter in the
187 * URL and hashes it to compute the server ID. This is useful to optimize
188 * performance by avoiding bounces between servers in contexts where sessions
189 * are shared but cookies are not usable. If the parameter is not found, NULL
190 * is returned. If any server is found, it will be returned. If no valid server
191 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100192 */
193struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
194{
195 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200196 const char *p;
197 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100198 int plen;
199
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200200 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100201 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100202 return NULL;
203
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200204 if ((p = memchr(uri, '?', uri_len)) == NULL)
205 return NULL;
206
Willy Tarreau01732802007-11-01 22:48:15 +0100207 p++;
208
209 uri_len -= (p - uri);
210 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200211 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100212
213 while (uri_len > plen) {
214 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200215 if (params[plen] == '=') {
216 if (memcmp(params, px->url_param_name, plen) == 0) {
217 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100218 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200219 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100220 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200221 p += plen + 1;
222 uri_len -= plen + 1;
223
Willy Tarreau01732802007-11-01 22:48:15 +0100224 while (uri_len && *p != '&') {
225 hash = *p + (hash << 6) + (hash << 16) - hash;
226 uri_len--;
227 p++;
228 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100229 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
230 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200231 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
232 return chash_get_server_hash(px, hash);
233 else
234 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100235 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200236 }
237 /* skip to next parameter */
238 p = memchr(params, '&', uri_len);
239 if (!p)
240 return NULL;
241 p++;
242 uri_len -= (p - params);
243 params = p;
244 }
245 return NULL;
246}
247
248/*
249 * this does the same as the previous server_ph, but check the body contents
250 */
251struct server *get_server_ph_post(struct session *s)
252{
253 unsigned long hash = 0;
254 struct http_txn *txn = &s->txn;
255 struct buffer *req = s->req;
256 struct http_msg *msg = &txn->req;
257 struct proxy *px = s->be;
258 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100259 unsigned long len = msg->body_len;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200260 const char *params = b_ptr(req, (int)(msg->sov - req->o));
Willy Tarreau157dd632009-12-06 19:18:09 +0100261 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200262
Willy Tarreau06a000f2012-05-18 23:04:32 +0200263 if (len > buffer_len(req) - msg->sov)
264 len = buffer_len(req) - msg->sov;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200265
Willy Tarreau157dd632009-12-06 19:18:09 +0100266 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200267 return NULL;
268
Willy Tarreau157dd632009-12-06 19:18:09 +0100269 if (px->lbprm.tot_weight == 0)
270 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200271
272 while (len > plen) {
273 /* Look for the parameter name followed by an equal symbol */
274 if (params[plen] == '=') {
275 if (memcmp(params, px->url_param_name, plen) == 0) {
276 /* OK, we have the parameter here at <params>, and
277 * the value after the equal sign, at <p>
278 * skip the equal symbol
279 */
280 p += plen + 1;
281 len -= plen + 1;
282
283 while (len && *p != '&') {
284 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100285 /* if in a POST, body must be URI encoded or it's not a URI.
286 * Do not interprete any possible binary data as a parameter.
287 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200288 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
289 break;
290 return NULL; /* oh, no; this is not uri-encoded.
291 * This body does not contain parameters.
292 */
293 }
294 hash = *p + (hash << 6) + (hash << 16) - hash;
295 len--;
296 p++;
297 /* should we break if vlen exceeds limit? */
298 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100299 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
300 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200301 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
302 return chash_get_server_hash(px, hash);
303 else
304 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200305 }
306 }
Willy Tarreau01732802007-11-01 22:48:15 +0100307 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200308 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100309 if (!p)
310 return NULL;
311 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200312 len -= (p - params);
313 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100314 }
315 return NULL;
316}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200317
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200318
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319/*
Benoitaffb4812009-03-25 13:02:10 +0100320 * This function tries to find a running server for the proxy <px> following
321 * the Header parameter hash method. It looks for a specific parameter in the
322 * URL and hashes it to compute the server ID. This is useful to optimize
323 * performance by avoiding bounces between servers in contexts where sessions
324 * are shared but cookies are not usable. If the parameter is not found, NULL
325 * is returned. If any server is found, it will be returned. If no valid server
326 * is found, NULL is returned.
327 */
328struct server *get_server_hh(struct session *s)
329{
330 unsigned long hash = 0;
331 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100332 struct proxy *px = s->be;
333 unsigned int plen = px->hh_len;
334 unsigned long len;
335 struct hdr_ctx ctx;
336 const char *p;
337
338 /* tot_weight appears to mean srv_count */
339 if (px->lbprm.tot_weight == 0)
340 return NULL;
341
Benoitaffb4812009-03-25 13:02:10 +0100342 ctx.idx = 0;
343
344 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau09d1e252012-05-18 22:36:34 +0200345 http_find_header2(px->hh_name, plen, b_ptr(s->req, -s->req->o), &txn->hdr_idx, &ctx);
Benoitaffb4812009-03-25 13:02:10 +0100346
347 /* if the header is not found or empty, let's fallback to round robin */
348 if (!ctx.idx || !ctx.vlen)
349 return NULL;
350
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200351 /* note: we won't hash if there's only one server left */
352 if (px->lbprm.tot_used == 1)
353 goto hash_done;
354
Benoitaffb4812009-03-25 13:02:10 +0100355 /* Found a the hh_name in the headers.
356 * we will compute the hash based on this value ctx.val.
357 */
358 len = ctx.vlen;
359 p = (char *)ctx.line + ctx.val;
360 if (!px->hh_match_domain) {
361 while (len) {
362 hash = *p + (hash << 6) + (hash << 16) - hash;
363 len--;
364 p++;
365 }
366 } else {
367 int dohash = 0;
368 p += len - 1;
369 /* special computation, use only main domain name, not tld/host
370 * going back from the end of string, start hashing at first
371 * dot stop at next.
372 * This is designed to work with the 'Host' header, and requires
373 * a special option to activate this.
374 */
375 while (len) {
376 if (*p == '.') {
377 if (!dohash)
378 dohash = 1;
379 else
380 break;
381 } else {
382 if (dohash)
383 hash = *p + (hash << 6) + (hash << 16) - hash;
384 }
385 len--;
386 p--;
387 }
388 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100389 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
390 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200391 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200392 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
393 return chash_get_server_hash(px, hash);
394 else
395 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100396}
397
Willy Tarreau34db1082012-04-19 17:16:54 +0200398/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200399struct server *get_server_rch(struct session *s)
400{
401 unsigned long hash = 0;
402 struct proxy *px = s->be;
403 unsigned long len;
404 const char *p;
405 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200406 struct sample smp;
Willy Tarreau34db1082012-04-19 17:16:54 +0200407 struct arg args[2];
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200408 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200409
410 /* tot_weight appears to mean srv_count */
411 if (px->lbprm.tot_weight == 0)
412 return NULL;
413
Willy Tarreau37406352012-04-23 16:16:37 +0200414 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200415
Willy Tarreau34db1082012-04-19 17:16:54 +0200416 args[0].type = ARGT_STR;
417 args[0].data.str.str = px->hh_name;
418 args[0].data.str.len = px->hh_len;
419 args[1].type = ARGT_STOP;
420
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200421 b_rew(s->req, rewind = s->req->o);
422
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200423 ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +0200424 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100425
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200426 b_adv(s->req, rewind);
427
Willy Tarreau37406352012-04-23 16:16:37 +0200428 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200429 return NULL;
430
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200431 /* note: we won't hash if there's only one server left */
432 if (px->lbprm.tot_used == 1)
433 goto hash_done;
434
Emeric Brun736aa232009-06-30 17:56:00 +0200435 /* Found a the hh_name in the headers.
436 * we will compute the hash based on this value ctx.val.
437 */
Willy Tarreauf853c462012-04-23 18:53:56 +0200438 p = smp.data.str.str;
Emeric Brun736aa232009-06-30 17:56:00 +0200439 while (len) {
440 hash = *p + (hash << 6) + (hash << 16) - hash;
441 len--;
442 p++;
443 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100444 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
445 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200446 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200447 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
448 return chash_get_server_hash(px, hash);
449 else
450 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200451}
Benoitaffb4812009-03-25 13:02:10 +0100452
453/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200454 * This function applies the load-balancing algorithm to the session, as
455 * defined by the backend it is assigned to. The session is then marked as
456 * 'assigned'.
457 *
458 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
459 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100460 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200461 * The function tries to keep the original connection slot if it reconnects to
462 * the same server, otherwise it releases it and tries to offer it.
463 *
464 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465 *
466 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100467 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200468 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
469 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200470 * SRV_STATUS_INTERNAL for other unrecoverable errors.
471 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200472 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100473 * it does not need to be called anymore. This means that target_srv(&s->target)
474 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200475 *
476 */
477
478int assign_server(struct session *s)
479{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100480
Willy Tarreau7c669d72008-06-20 15:04:11 +0200481 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100482 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200483 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100484
Simon Horman8effd3d2011-08-13 08:03:52 +0900485 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200486
Willy Tarreau7c669d72008-06-20 15:04:11 +0200487 err = SRV_STATUS_INTERNAL;
488 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
489 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100490
Willy Tarreau827aee92011-03-10 16:55:02 +0100491 prev_srv = target_srv(&s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200492 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200493
Willy Tarreau7c669d72008-06-20 15:04:11 +0200494 /* We have to release any connection slot before applying any LB algo,
495 * otherwise we may erroneously end up with no available slot.
496 */
497 if (conn_slot)
498 sess_change_server(s, NULL);
499
Willy Tarreau827aee92011-03-10 16:55:02 +0100500 /* We will now try to find the good server and store it into <target_srv(&s->target)>.
501 * Note that <target_srv(&s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200502 * as well as if no server is available (check error code).
503 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100504
Willy Tarreau827aee92011-03-10 16:55:02 +0100505 srv = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100506 clear_target(&s->target);
Willy Tarreau664beb82011-03-10 11:38:29 +0100507
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200508 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200509 /* we must check if we have at least one server available */
510 if (!s->be->lbprm.tot_weight) {
511 err = SRV_STATUS_NOSRV;
512 goto out;
513 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200514
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200515 /* First check whether we need to fetch some data or simply call
516 * the LB lookup function. Only the hashing functions will need
517 * some input data in fact, and will support multiple algorithms.
518 */
519 switch (s->be->lbprm.algo & BE_LB_LKUP) {
520 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100521 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200522 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200523
Willy Tarreauf09c6602012-02-13 17:12:08 +0100524 case BE_LB_LKUP_FSTREE:
525 srv = fas_get_next_server(s->be, prev_srv);
526 break;
527
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200528 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100529 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200530 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200531
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200532 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200533 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200534 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200535 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100536 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200537 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100538 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200539 break;
540 }
541 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200542 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200543 err = SRV_STATUS_INTERNAL;
544 goto out;
545 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200546
547 switch (s->be->lbprm.algo & BE_LB_PARM) {
548 case BE_LB_HASH_SRC:
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200549 if (s->req->prod->addr.from.ss_family == AF_INET) {
550 srv = get_server_sh(s->be,
551 (void *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr,
552 4);
553 }
554 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
555 srv = get_server_sh(s->be,
556 (void *)&((struct sockaddr_in6 *)&s->req->prod->addr.from)->sin6_addr,
557 16);
558 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200559 else {
560 /* unknown IP family */
561 err = SRV_STATUS_INTERNAL;
562 goto out;
563 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200564 break;
565
566 case BE_LB_HASH_URI:
567 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100568 if (s->txn.req.msg_state < HTTP_MSG_BODY)
569 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100570 srv = get_server_uh(s->be,
Willy Tarreau09d1e252012-05-18 22:36:34 +0200571 b_ptr(s->req, (int)(s->txn.req.sl.rq.u - s->req->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100572 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200573 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200574
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200575 case BE_LB_HASH_PRM:
576 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100577 if (s->txn.req.msg_state < HTTP_MSG_BODY)
578 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100579
Willy Tarreau827aee92011-03-10 16:55:02 +0100580 srv = get_server_ph(s->be,
Willy Tarreau09d1e252012-05-18 22:36:34 +0200581 b_ptr(s->req, (int)(s->txn.req.sl.rq.u - s->req->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100582 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100583
Willy Tarreau827aee92011-03-10 16:55:02 +0100584 if (!srv && s->txn.meth == HTTP_METH_POST)
585 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200586 break;
Benoitaffb4812009-03-25 13:02:10 +0100587
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200588 case BE_LB_HASH_HDR:
589 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100590 if (s->txn.req.msg_state < HTTP_MSG_BODY)
591 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100592 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200593 break;
594
595 case BE_LB_HASH_RDP:
596 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100597 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200598 break;
599
600 default:
601 /* unknown balancing algorithm */
602 err = SRV_STATUS_INTERNAL;
603 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100604 }
Emeric Brun736aa232009-06-30 17:56:00 +0200605
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200606 /* If the hashing parameter was not found, let's fall
607 * back to round robin on the map.
608 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100609 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200610 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100611 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200612 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100613 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200614 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200615
616 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200617 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200618
Willy Tarreau7c669d72008-06-20 15:04:11 +0200619 default:
620 /* unknown balancing algorithm */
621 err = SRV_STATUS_INTERNAL;
622 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200623 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200624
Willy Tarreau827aee92011-03-10 16:55:02 +0100625 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200626 err = SRV_STATUS_FULL;
627 goto out;
628 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100629 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100630 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100631 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100632 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100633 set_target_server(&s->target, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200634 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200635 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau9e000c62011-03-10 14:03:36 +0100636 set_target_proxy(&s->target, s->be);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100638 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreau6471afb2011-09-23 10:54:59 +0200639 is_addr(&s->req->cons->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100640 /* in proxy mode, we need a valid destination address */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100641 set_target_proxy(&s->target, s->be);
Willy Tarreau664beb82011-03-10 11:38:29 +0100642 }
643 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200644 err = SRV_STATUS_NOSRV;
645 goto out;
646 }
647
648 s->flags |= SN_ASSIGNED;
649 err = SRV_STATUS_OK;
650 out:
651
652 /* Either we take back our connection slot, or we offer it to someone
653 * else if we don't need it anymore.
654 */
655 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100656 if (conn_slot == srv) {
657 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200658 } else {
659 if (may_dequeue_tasks(conn_slot, s->be))
660 process_srv_queue(conn_slot);
661 }
662 }
663
664 out_err:
665 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200666}
667
668
669/*
670 * This function assigns a server address to a session, and sets SN_ADDR_SET.
671 * The address is taken from the currently assigned server, or from the
672 * dispatch or transparent address.
673 *
674 * It may return :
675 * SRV_STATUS_OK if everything is OK.
676 * SRV_STATUS_INTERNAL for other unrecoverable errors.
677 *
678 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
679 * not cleared, so it's to the caller to clear it if required.
680 *
681 */
682int assign_server_address(struct session *s)
683{
684#ifdef DEBUG_FULL
685 fprintf(stderr,"assign_server_address : s=%p\n",s);
686#endif
687
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200688 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200689 /* A server is necessarily known for this session */
690 if (!(s->flags & SN_ASSIGNED))
691 return SRV_STATUS_INTERNAL;
692
Willy Tarreau6471afb2011-09-23 10:54:59 +0200693 s->req->cons->addr.to = target_srv(&s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200694
Willy Tarreau6471afb2011-09-23 10:54:59 +0200695 if (!is_addr(&s->req->cons->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200696 /* if the server has no address, we use the same address
697 * the client asked, which is handy for remapping ports
698 * locally on multiple addresses at once.
699 */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200700 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreau59b94792012-05-11 16:16:40 +0200701 si_get_to_addr(s->req->prod);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200702
Willy Tarreau6471afb2011-09-23 10:54:59 +0200703 if (s->req->prod->addr.to.ss_family == AF_INET) {
704 ((struct sockaddr_in *)&s->req->cons->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
705 } else if (s->req->prod->addr.to.ss_family == AF_INET6) {
706 ((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 +0200707 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200708 }
709
Willy Tarreaubaaee002006-06-26 02:48:02 +0200710 /* if this server remaps proxied ports, we'll use
711 * the port the client connected to with an offset. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100712 if (target_srv(&s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100713 int base_port;
714
Willy Tarreau9b061e32012-04-07 18:03:52 +0200715 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreau59b94792012-05-11 16:16:40 +0200716 si_get_to_addr(s->req->prod);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100717
718 /* First, retrieve the port from the incoming connection */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200719 base_port = get_host_port(&s->req->prod->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100720
721 /* Second, assign the outgoing connection's port */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200722 base_port += get_host_port(&s->req->cons->addr.to);
723 set_host_port(&s->req->cons->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724 }
725 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200726 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 /* connect to the defined dispatch addr */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200728 s->req->cons->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100730 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreau59b94792012-05-11 16:16:40 +0200732 si_get_to_addr(s->req->prod);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733
Willy Tarreau6471afb2011-09-23 10:54:59 +0200734 if (s->req->prod->addr.to.ss_family == AF_INET || s->req->prod->addr.to.ss_family == AF_INET6) {
735 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 +0200736 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100737 /* when we support IPv6 on the backend, we may add other tests */
738 //qfprintf(stderr, "Cannot get original server address.\n");
739 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200740 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100741 else if (s->be->options & PR_O_HTTP_PROXY) {
742 /* If HTTP PROXY option is set, then server is already assigned
743 * during incoming client request parsing. */
744 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100745 else {
746 /* no server and no LB algorithm ! */
747 return SRV_STATUS_INTERNAL;
748 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200749
750 s->flags |= SN_ADDR_SET;
751 return SRV_STATUS_OK;
752}
753
754
755/* This function assigns a server to session <s> if required, and can add the
756 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200757 * If ->srv_conn is set, the session is first released from the server.
758 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
759 * be called before any connection and after any retry or redispatch occurs.
760 *
761 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762 *
763 * Returns :
764 *
765 * SRV_STATUS_OK if everything is OK.
Willy Tarreau827aee92011-03-10 16:55:02 +0100766 * SRV_STATUS_NOSRV if no server is available. target_srv(&s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200767 * SRV_STATUS_QUEUED if the connection has been queued.
768 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100769 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200770 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200771 * SRV_STATUS_INTERNAL for other unrecoverable errors.
772 *
773 */
774int assign_server_and_queue(struct session *s)
775{
776 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100777 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200778 int err;
779
780 if (s->pend_pos)
781 return SRV_STATUS_INTERNAL;
782
Willy Tarreau7c669d72008-06-20 15:04:11 +0200783 err = SRV_STATUS_OK;
784 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100785 struct server *prev_srv = target_srv(&s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100786
Willy Tarreau7c669d72008-06-20 15:04:11 +0200787 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100788 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200789 /* This session was previously assigned to a server. We have to
790 * update the session's and the server's stats :
791 * - if the server changed :
792 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
793 * - set SN_REDISP if it was successfully redispatched
794 * - increment srv->redispatches and be->redispatches
795 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100796 */
797
Willy Tarreau827aee92011-03-10 16:55:02 +0100798 if (prev_srv != target_srv(&s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200799 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
800 s->txn.flags &= ~TX_CK_MASK;
801 s->txn.flags |= TX_CK_DOWN;
802 }
803 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100804 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100805 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200806 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100807 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100808 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100809 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100810 }
811 }
812
Willy Tarreaubaaee002006-06-26 02:48:02 +0200813 switch (err) {
814 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200815 /* we have SN_ASSIGNED set */
Willy Tarreau827aee92011-03-10 16:55:02 +0100816 srv = target_srv(&s->target);
817 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200818 return SRV_STATUS_OK; /* dispatch or proxy mode */
819
820 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100821 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200822 return SRV_STATUS_OK;
823
824 /* OK, this session already has an assigned server, but no
825 * connection slot yet. Either it is a redispatch, or it was
826 * assigned from persistence information (direct mode).
827 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100828 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200829 /* server scheduled for redirection, and already assigned. We
830 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100831 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100832 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100833 return SRV_STATUS_OK;
834 }
835
Willy Tarreau7c669d72008-06-20 15:04:11 +0200836 /* We might have to queue this session if the assigned server is full.
837 * We know we have to queue it into the server's queue, so if a maxqueue
838 * is set on the server, we must also check that the server's queue is
839 * not full, in which case we have to return FULL.
840 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100841 if (srv->maxconn &&
842 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200843
Willy Tarreau827aee92011-03-10 16:55:02 +0100844 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200845 return SRV_STATUS_FULL;
846
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847 p = pendconn_add(s);
848 if (p)
849 return SRV_STATUS_QUEUED;
850 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200851 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200852 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200853
854 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100855 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200856 return SRV_STATUS_OK;
857
858 case SRV_STATUS_FULL:
859 /* queue this session into the proxy's queue */
860 p = pendconn_add(s);
861 if (p)
862 return SRV_STATUS_QUEUED;
863 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200864 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200865
866 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200867 return err;
868
Willy Tarreaubaaee002006-06-26 02:48:02 +0200869 case SRV_STATUS_INTERNAL:
870 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200871
Willy Tarreaubaaee002006-06-26 02:48:02 +0200872 default:
873 return SRV_STATUS_INTERNAL;
874 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100875}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200876
Willy Tarreaub1d67742010-03-29 19:36:59 +0200877/* If an explicit source binding is specified on the server and/or backend, and
878 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200879 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200880 */
881static void assign_tproxy_address(struct session *s)
882{
883#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau827aee92011-03-10 16:55:02 +0100884 struct server *srv = target_srv(&s->target);
885
886 if (srv && srv->state & SRV_BIND_SRC) {
887 switch (srv->state & SRV_TPROXY_MASK) {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200888 case SRV_TPROXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200889 s->req->cons->addr.from = srv->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200890 break;
891 case SRV_TPROXY_CLI:
892 case SRV_TPROXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200893 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200894 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200895 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200896 case SRV_TPROXY_DYN:
Willy Tarreau827aee92011-03-10 16:55:02 +0100897 if (srv->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100898 char *vptr;
899 int vlen;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200900 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100901
Willy Tarreaubce70882009-09-07 11:51:47 +0200902 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100903 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200904 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100905 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
906
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200907 b_rew(s->req, rewind = s->req->o);
Willy Tarreau294c4732011-12-16 21:35:50 +0100908 if (http_get_hdr(&s->txn.req, srv->bind_hdr_name, srv->bind_hdr_len,
909 &s->txn.hdr_idx, srv->bind_hdr_occ, NULL, &vptr, &vlen)) {
910 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
911 htonl(inetaddr_host_lim(vptr, vptr + vlen));
912 }
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200913 b_adv(s->req, rewind);
Willy Tarreaubce70882009-09-07 11:51:47 +0200914 }
915 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200916 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200917 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200918 }
919 }
920 else if (s->be->options & PR_O_BIND_SRC) {
921 switch (s->be->options & PR_O_TPXY_MASK) {
922 case PR_O_TPXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200923 s->req->cons->addr.from = s->be->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200924 break;
925 case PR_O_TPXY_CLI:
926 case PR_O_TPXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200927 /* FIXME: what can we do if the client connects in IPv6 or socket unix? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200928 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200929 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200930 case PR_O_TPXY_DYN:
931 if (s->be->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100932 char *vptr;
933 int vlen;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200934 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100935
Willy Tarreaubce70882009-09-07 11:51:47 +0200936 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100937 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200938 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100939 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
940
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200941 b_rew(s->req, rewind = s->req->o);
Willy Tarreau294c4732011-12-16 21:35:50 +0100942 if (http_get_hdr(&s->txn.req, s->be->bind_hdr_name, s->be->bind_hdr_len,
943 &s->txn.hdr_idx, s->be->bind_hdr_occ, NULL, &vptr, &vlen)) {
944 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
945 htonl(inetaddr_host_lim(vptr, vptr + vlen));
946 }
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200947 b_adv(s->req, rewind);
Willy Tarreaubce70882009-09-07 11:51:47 +0200948 }
949 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200950 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200951 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200952 }
953 }
954#endif
955}
956
957
Willy Tarreaubaaee002006-06-26 02:48:02 +0200958/*
959 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200960 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100961 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200962 * It can return one of :
963 * - SN_ERR_NONE if everything's OK
964 * - SN_ERR_SRVTO if there are no more servers
965 * - SN_ERR_SRVCL if the connection was refused by the server
966 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
967 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
968 * - SN_ERR_INTERNAL for any other purely internal errors
969 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
970 */
971int connect_server(struct session *s)
972{
Willy Tarreau827aee92011-03-10 16:55:02 +0100973 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200974 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200975
976 if (!(s->flags & SN_ADDR_SET)) {
977 err = assign_server_address(s);
978 if (err != SRV_STATUS_OK)
979 return SN_ERR_INTERNAL;
980 }
981
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200982 /* the target was only on the session, assign it to the SI now */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100983 copy_target(&s->req->cons->target, &s->target);
Willy Tarreaud88edf22009-06-14 15:48:17 +0200984
Willy Tarreau26d8c592012-05-07 18:12:14 +0200985 /* set the correct protocol on the output stream interface */
Willy Tarreaud02394b2012-05-11 18:32:18 +0200986 if (s->target.type == TARG_TYPE_SERVER) {
Willy Tarreau26d8c592012-05-07 18:12:14 +0200987 s->req->cons->proto = target_srv(&s->target)->proto;
Willy Tarreaud02394b2012-05-11 18:32:18 +0200988 stream_interface_prepare(s->req->cons, target_srv(&s->target)->sock);
989 }
Willy Tarreau26d8c592012-05-07 18:12:14 +0200990 else if (s->target.type == TARG_TYPE_PROXY) {
Willy Tarreaud02394b2012-05-11 18:32:18 +0200991 /* proxies exclusively run on sock_raw right now */
Willy Tarreau26d8c592012-05-07 18:12:14 +0200992 s->req->cons->proto = protocol_by_family(s->req->cons->addr.to.ss_family);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200993 stream_interface_prepare(s->req->cons, &sock_raw);
Willy Tarreau26d8c592012-05-07 18:12:14 +0200994 if (!s->req->cons->proto)
995 return SN_ERR_INTERNAL;
996 }
Willy Tarreaud02394b2012-05-11 18:32:18 +0200997 else
998 return SN_ERR_INTERNAL; /* how did we get there ? */
999
1000 /* process the case where the server requires the PROXY protocol to be sent */
1001 s->req->cons->send_proxy_ofs = 0;
1002 if (s->target.type == TARG_TYPE_SERVER && (s->target.ptr.s->state & SRV_SEND_PROXY)) {
1003 s->req->cons->send_proxy_ofs = 1; /* must compute size */
1004 si_get_to_addr(s->req->prod);
1005 }
Willy Tarreau26d8c592012-05-07 18:12:14 +02001006
Willy Tarreaub1d67742010-03-29 19:36:59 +02001007 assign_tproxy_address(s);
1008
William Lallemandb7ff6a32012-03-02 14:35:21 +01001009 /* flag for logging source ip/port */
1010 if (s->fe->options2 & PR_O2_SRC_ADDR)
1011 s->req->cons->flags |= SI_FL_SRC_ADDR;
1012
Willy Tarreau26d8c592012-05-07 18:12:14 +02001013 err = s->req->cons->proto->connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001014
Willy Tarreau9650f372009-08-16 14:02:45 +02001015 if (err != SN_ERR_NONE)
1016 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001017
Willy Tarreau827aee92011-03-10 16:55:02 +01001018 srv = target_srv(&s->target);
1019 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001020 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001021 srv->cur_sess++;
1022 if (srv->cur_sess > srv->counters.cur_sess_max)
1023 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001024 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001025 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001026 }
1027
Willy Tarreaubaaee002006-06-26 02:48:02 +02001028 return SN_ERR_NONE; /* connection is OK */
1029}
1030
1031
Willy Tarreaubaaee002006-06-26 02:48:02 +02001032/* This function performs the "redispatch" part of a connection attempt. It
1033 * will assign a server if required, queue the connection if required, and
1034 * handle errors that might arise at this level. It can change the server
1035 * state. It will return 1 if it encounters an error, switches the server
1036 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1037 * that the connection is ready to use.
1038 */
1039
1040int srv_redispatch_connect(struct session *t)
1041{
Willy Tarreau827aee92011-03-10 16:55:02 +01001042 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001043 int conn_err;
1044
1045 /* We know that we don't have any connection pending, so we will
1046 * try to get a new one, and wait in this state if it's queued
1047 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001048 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001049 conn_err = assign_server_and_queue(t);
Willy Tarreau827aee92011-03-10 16:55:02 +01001050 srv = target_srv(&t->target);
1051
Willy Tarreaubaaee002006-06-26 02:48:02 +02001052 switch (conn_err) {
1053 case SRV_STATUS_OK:
1054 break;
1055
Willy Tarreau7c669d72008-06-20 15:04:11 +02001056 case SRV_STATUS_FULL:
1057 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1058 * and we can redispatch to another server, or it is not and we return
1059 * 503. This only makes sense in DIRECT mode however, because normal LB
1060 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001061 * would bring us on the same server again. Note that t->target is set
1062 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001063 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001064 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1065 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001066 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001067 goto redispatch;
1068 }
1069
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001070 if (!t->req->cons->err_type) {
1071 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001072 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001073 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001074
Willy Tarreau827aee92011-03-10 16:55:02 +01001075 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001076 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001077 return 1;
1078
Willy Tarreaubaaee002006-06-26 02:48:02 +02001079 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001080 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001081 if (!t->req->cons->err_type) {
1082 t->req->cons->err_type = SI_ET_CONN_ERR;
1083 t->req->cons->err_loc = NULL;
1084 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001085
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001086 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001087 return 1;
1088
1089 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001090 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001091 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001092 /* do nothing else and do not wake any other session up */
1093 return 1;
1094
Willy Tarreaubaaee002006-06-26 02:48:02 +02001095 case SRV_STATUS_INTERNAL:
1096 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001097 if (!t->req->cons->err_type) {
1098 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001099 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001100 }
1101
Willy Tarreau827aee92011-03-10 16:55:02 +01001102 if (srv)
1103 srv_inc_sess_ctr(srv);
1104 if (srv)
1105 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001106 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001107
1108 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001109 if (may_dequeue_tasks(srv, t->be))
1110 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001111 return 1;
1112 }
1113 /* if we get here, it's because we got SRV_STATUS_OK, which also
1114 * means that the connection has not been queued.
1115 */
1116 return 0;
1117}
1118
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001119/* Apply RDP cookie persistence to the current session. For this, the function
1120 * tries to extract an RDP cookie from the request buffer, and look for the
1121 * matching server in the list. If the server is found, it is assigned to the
1122 * session. This always returns 1, and the analyser removes itself from the
1123 * list. Nothing is performed if a server was already assigned.
1124 */
1125int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1126{
1127 struct proxy *px = s->be;
1128 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001129 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001130 struct server *srv = px->srv;
1131 struct sockaddr_in addr;
1132 char *p;
Willy Tarreau34db1082012-04-19 17:16:54 +02001133 struct arg args[2];
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001134
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001135 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001136 now_ms, __FUNCTION__,
1137 s,
1138 req,
1139 req->rex, req->wex,
1140 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001141 req->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001142 req->analysers);
1143
1144 if (s->flags & SN_ASSIGNED)
1145 goto no_cookie;
1146
Willy Tarreau37406352012-04-23 16:16:37 +02001147 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001148
Willy Tarreau34db1082012-04-19 17:16:54 +02001149 args[0].type = ARGT_STR;
1150 args[0].data.str.str = s->be->rdp_cookie_name;
1151 args[0].data.str.len = s->be->rdp_cookie_len;
1152 args[1].type = ARGT_STOP;
1153
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001154 ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +02001155 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001156 goto no_cookie;
1157
1158 memset(&addr, 0, sizeof(addr));
1159 addr.sin_family = AF_INET;
1160
Willy Tarreau664092c2011-12-16 19:11:42 +01001161 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001162 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001163 if (*p != '.')
1164 goto no_cookie;
1165 p++;
1166 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1167 if (*p != '.')
1168 goto no_cookie;
1169
Willy Tarreau9e000c62011-03-10 14:03:36 +01001170 clear_target(&s->target);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001171 while (srv) {
1172 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1173 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1174 /* we found the server and it is usable */
1175 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001176 set_target_server(&s->target, srv);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001177 break;
1178 }
1179 }
1180 srv = srv->next;
1181 }
1182
1183no_cookie:
1184 req->analysers &= ~an_bit;
1185 req->analyse_exp = TICK_ETERNITY;
1186 return 1;
1187}
1188
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001189int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001190 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001191 return px->down_time;
1192
1193 return now.tv_sec - px->last_change + px->down_time;
1194}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001195
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001196/*
1197 * This function returns a string containing the balancing
1198 * mode of the proxy in a format suitable for stats.
1199 */
1200
1201const char *backend_lb_algo_str(int algo) {
1202
1203 if (algo == BE_LB_ALGO_RR)
1204 return "roundrobin";
1205 else if (algo == BE_LB_ALGO_SRR)
1206 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001207 else if (algo == BE_LB_ALGO_FAS)
1208 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001209 else if (algo == BE_LB_ALGO_LC)
1210 return "leastconn";
1211 else if (algo == BE_LB_ALGO_SH)
1212 return "source";
1213 else if (algo == BE_LB_ALGO_UH)
1214 return "uri";
1215 else if (algo == BE_LB_ALGO_PH)
1216 return "url_param";
1217 else if (algo == BE_LB_ALGO_HH)
1218 return "hdr";
1219 else if (algo == BE_LB_ALGO_RCH)
1220 return "rdp-cookie";
1221 else
1222 return NULL;
1223}
1224
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001225/* This function parses a "balance" statement in a backend section describing
1226 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001227 * returns -1, it will write an error message into the <err> buffer which will
1228 * automatically be allocated and must be passed as NULL. The trailing '\n'
1229 * will not be written. The function must be called with <args> pointing to the
1230 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001231 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001232int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001233{
1234 if (!*(args[0])) {
1235 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001236 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1237 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001238 return 0;
1239 }
1240
1241 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001242 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1243 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001244 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001245 else if (!strcmp(args[0], "static-rr")) {
1246 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1247 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1248 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001249 else if (!strcmp(args[0], "first")) {
1250 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1251 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1252 }
Willy Tarreau51406232008-03-10 22:04:20 +01001253 else if (!strcmp(args[0], "leastconn")) {
1254 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1255 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1256 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001257 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001258 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1259 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001260 }
1261 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001262 int arg = 1;
1263
Willy Tarreau31682232007-11-29 15:38:04 +01001264 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1265 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001266
1267 while (*args[arg]) {
1268 if (!strcmp(args[arg], "len")) {
1269 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001270 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001271 return -1;
1272 }
1273 curproxy->uri_len_limit = atoi(args[arg+1]);
1274 arg += 2;
1275 }
1276 else if (!strcmp(args[arg], "depth")) {
1277 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001278 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001279 return -1;
1280 }
1281 /* hint: we store the position of the ending '/' (depth+1) so
1282 * that we avoid a comparison while computing the hash.
1283 */
1284 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1285 arg += 2;
1286 }
1287 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001288 memprintf(err, "%s only accepts parameters 'len' and 'depth' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001289 return -1;
1290 }
1291 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001292 }
Willy Tarreau01732802007-11-01 22:48:15 +01001293 else if (!strcmp(args[0], "url_param")) {
1294 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001295 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001296 return -1;
1297 }
Willy Tarreau31682232007-11-29 15:38:04 +01001298 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1299 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001300
1301 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001302 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001303 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001304 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001305 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001306 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001307 return -1;
1308 }
1309 if (*args[3]) {
1310 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1311 curproxy->url_param_post_limit = str2ui(args[3]);
1312 }
1313 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1314 if (!curproxy->url_param_post_limit)
1315 curproxy->url_param_post_limit = 48;
1316 else if ( curproxy->url_param_post_limit < 3 )
1317 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1318 }
Benoitaffb4812009-03-25 13:02:10 +01001319 }
1320 else if (!strncmp(args[0], "hdr(", 4)) {
1321 const char *beg, *end;
1322
1323 beg = args[0] + 4;
1324 end = strchr(beg, ')');
1325
1326 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001327 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001328 return -1;
1329 }
1330
1331 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1332 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1333
1334 free(curproxy->hh_name);
1335 curproxy->hh_len = end - beg;
1336 curproxy->hh_name = my_strndup(beg, end - beg);
1337 curproxy->hh_match_domain = 0;
1338
1339 if (*args[1]) {
1340 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001341 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001342 return -1;
1343 }
1344 curproxy->hh_match_domain = 1;
1345 }
1346
Emeric Brun736aa232009-06-30 17:56:00 +02001347 }
1348 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1349 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1350 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1351
1352 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1353 const char *beg, *end;
1354
1355 beg = args[0] + 11;
1356 end = strchr(beg, ')');
1357
1358 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001359 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001360 return -1;
1361 }
1362
1363 free(curproxy->hh_name);
1364 curproxy->hh_name = my_strndup(beg, end - beg);
1365 curproxy->hh_len = end - beg;
1366 }
1367 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1368 free(curproxy->hh_name);
1369 curproxy->hh_name = strdup("mstshash");
1370 curproxy->hh_len = strlen(curproxy->hh_name);
1371 }
1372 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001373 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001374 return -1;
1375 }
Willy Tarreau01732802007-11-01 22:48:15 +01001376 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001377 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001378 memprintf(err, "only supports 'roundrobin', 'static-rr', 'leastconn', 'source', 'uri', 'url_param', 'hdr(name)' and 'rdp-cookie(name)' options.");
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001379 return -1;
1380 }
1381 return 0;
1382}
1383
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001384
1385/************************************************************************/
1386/* All supported keywords must be declared here. */
1387/************************************************************************/
1388
Willy Tarreau34db1082012-04-19 17:16:54 +02001389/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001390 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001391 * undefined behaviour.
1392 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001393static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001394acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001395 const struct arg *args, struct sample *smp)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001396{
Willy Tarreau37406352012-04-23 16:16:37 +02001397 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001398 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001399 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001400
1401 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001402 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001403 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001404 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001405 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001406 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001407
1408 return 1;
1409}
1410
Willy Tarreau37406352012-04-23 16:16:37 +02001411/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001412 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001413 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1414 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001415 */
1416static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001417acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001418 const struct arg *args, struct sample *smp)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001419{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001420 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001421
Willy Tarreau37406352012-04-23 16:16:37 +02001422 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001423 smp->type = SMP_T_BOOL;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001424 if (!(srv->state & SRV_MAINTAIN) &&
1425 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001426 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001427 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001428 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001429 return 1;
1430}
1431
Willy Tarreau34db1082012-04-19 17:16:54 +02001432/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001433 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001434 * undefined behaviour.
1435 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001436static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001437acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001438 const struct arg *args, struct sample *smp)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001439{
1440 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001441
Willy Tarreau37406352012-04-23 16:16:37 +02001442 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001443 smp->type = SMP_T_UINT;
1444 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001445
Willy Tarreau24e32d82012-04-23 23:55:44 +02001446 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001447 if ((iterator->state & SRV_RUNNING) == 0)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001448 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001449
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001450 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001451 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001452 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001453 return 1;
1454 }
1455
Willy Tarreauf853c462012-04-23 18:53:56 +02001456 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001457 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001458 }
1459
1460 return 1;
1461}
1462
Willy Tarreaua5e37562011-12-16 17:06:15 +01001463/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001464static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001465acl_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001466 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +02001467{
Willy Tarreauf853c462012-04-23 18:53:56 +02001468 smp->flags = SMP_F_VOL_TXN;
1469 smp->type = SMP_T_UINT;
1470 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001471 return 1;
1472}
1473
Willy Tarreaua5e37562011-12-16 17:06:15 +01001474/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001475static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001476acl_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001477 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +02001478{
Willy Tarreau827aee92011-03-10 16:55:02 +01001479 if (!target_srv(&l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001480 return 0;
1481
Willy Tarreauf853c462012-04-23 18:53:56 +02001482 smp->type = SMP_T_UINT;
1483 smp->data.uint = target_srv(&l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001484
1485 return 1;
1486}
1487
Willy Tarreau34db1082012-04-19 17:16:54 +02001488/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001489 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001490 * undefined behaviour.
1491 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001492static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001493acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001494 const struct arg *args, struct sample *smp)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001495{
Willy Tarreau37406352012-04-23 16:16:37 +02001496 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001497 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001498 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001499 return 1;
1500}
1501
Willy Tarreau34db1082012-04-19 17:16:54 +02001502/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001503 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001504 * undefined behaviour.
1505 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001506static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001507acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001508 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001509{
Willy Tarreau37406352012-04-23 16:16:37 +02001510 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001511 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001512 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001513 return 1;
1514}
1515
Willy Tarreau34db1082012-04-19 17:16:54 +02001516/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001517 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001518 * undefined behaviour.
1519 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001520static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001521acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001522 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001523{
Willy Tarreau37406352012-04-23 16:16:37 +02001524 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001525 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001526 smp->data.uint = args->data.prx->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.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001535 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001536 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001537 */
1538static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001539acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001540 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001541{
1542 int nbsrv;
1543
Willy Tarreau37406352012-04-23 16:16:37 +02001544 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001545 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001546 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001547
1548 if (px->srv_act)
1549 nbsrv = px->srv_act;
1550 else if (px->lbprm.fbck)
1551 nbsrv = 1;
1552 else
1553 nbsrv = px->srv_bck;
1554
1555 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001556 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001557 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001558 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001559
1560 return 1;
1561}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001562
Willy Tarreau34db1082012-04-19 17:16:54 +02001563/* set temp integer to the number of concurrent connections on the server in the backend.
1564 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1565 * undefined behaviour.
1566 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001567static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001568acl_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001569 const struct arg *args, struct sample *smp)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001570{
Willy Tarreauf853c462012-04-23 18:53:56 +02001571 smp->flags = SMP_F_VOL_TEST;
1572 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001573 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001574 return 1;
1575}
1576
Willy Tarreau61612d42012-04-19 18:42:05 +02001577/* Note: must not be declared <const> as its list will be overwritten.
1578 * Please take care of keeping this list alphabetically sorted.
1579 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001580static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001581 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1582 { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
Willy Tarreau61612d42012-04-19 18:42:05 +02001583 { "be_id", acl_parse_int, acl_fetch_be_id, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001584 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1585 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1586 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1587 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
Willy Tarreau61612d42012-04-19 18:42:05 +02001588 { "srv_conn", acl_parse_int, acl_fetch_srv_conn, acl_match_int, ACL_USE_NOTHING, ARG1(1,SRV) },
1589 { "srv_id", acl_parse_int, acl_fetch_srv_id, acl_match_int, ACL_USE_RTR_INTERNAL, 0 },
1590 { "srv_is_up", acl_parse_nothing, acl_fetch_srv_is_up, acl_match_nothing, ACL_USE_NOTHING, ARG1(1,SRV) },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001591 { NULL, NULL, NULL, NULL },
1592}};
1593
1594
1595__attribute__((constructor))
1596static void __backend_init(void)
1597{
1598 acl_register_keywords(&acl_kws);
1599}
1600
Willy Tarreaubaaee002006-06-26 02:48:02 +02001601/*
1602 * Local variables:
1603 * c-indent-level: 8
1604 * c-basic-offset: 8
1605 * End:
1606 */