blob: 3f30bea0485f1dc48c81b24230bb7f08aae15c92 [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 Tarreau03fa5df2010-05-24 21:02:37 +020033#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020034#include <proto/lb_chash.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010035#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020036#include <proto/lb_fwlc.h>
37#include <proto/lb_fwrr.h>
38#include <proto/lb_map.h>
Willy Tarreau26d8c592012-05-07 18:12:14 +020039#include <proto/protocols.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020041#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010043#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020044#include <proto/session.h>
Willy Tarreauc63190d2012-05-11 14:23:52 +020045#include <proto/sock_raw.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010046#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/task.h>
48
Willy Tarreaubaaee002006-06-26 02:48:02 +020049/*
50 * This function recounts the number of usable active and backup servers for
51 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010052 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010053 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010054 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020055 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020056void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020057{
58 struct server *srv;
59
Willy Tarreau20697042007-11-15 23:26:18 +010060 px->srv_act = px->srv_bck = 0;
61 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010062 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020063 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010064 if (!srv_is_usable(srv->state, srv->eweight))
65 continue;
66
67 if (srv->state & SRV_BACKUP) {
68 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010069 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010070 px->lbprm.fbck = srv;
71 px->srv_bck++;
72 px->lbprm.tot_wbck += srv->eweight;
73 } else {
74 px->srv_act++;
75 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020076 }
77 }
Willy Tarreaub625a082007-11-26 01:15:43 +010078}
Willy Tarreau20697042007-11-15 23:26:18 +010079
Willy Tarreaub625a082007-11-26 01:15:43 +010080/* This function simply updates the backend's tot_weight and tot_used values
81 * after servers weights have been updated. It is designed to be used after
82 * recount_servers() or equivalent.
83 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020084void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010085{
Willy Tarreau20697042007-11-15 23:26:18 +010086 if (px->srv_act) {
87 px->lbprm.tot_weight = px->lbprm.tot_wact;
88 px->lbprm.tot_used = px->srv_act;
89 }
Willy Tarreaub625a082007-11-26 01:15:43 +010090 else if (px->lbprm.fbck) {
91 /* use only the first backup server */
92 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
93 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010094 }
95 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010096 px->lbprm.tot_weight = px->lbprm.tot_wbck;
97 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +010098 }
Willy Tarreaub625a082007-11-26 01:15:43 +010099}
100
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200101/*
102 * This function tries to find a running server for the proxy <px> following
103 * the source hash method. Depending on the number of active/backup servers,
104 * it will either look for active servers, or for backup servers.
105 * If any server is found, it will be returned. If no valid server is found,
106 * NULL is returned.
107 */
108struct server *get_server_sh(struct proxy *px, const char *addr, int len)
109{
110 unsigned int h, l;
111
112 if (px->lbprm.tot_weight == 0)
113 return NULL;
114
115 l = h = 0;
116
117 /* note: we won't hash if there's only one server left */
118 if (px->lbprm.tot_used == 1)
119 goto hash_done;
120
121 while ((l + sizeof (int)) <= len) {
122 h ^= ntohl(*(unsigned int *)(&addr[l]));
123 l += sizeof (int);
124 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100125 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
126 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200127 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200128 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
129 return chash_get_server_hash(px, h);
130 else
131 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200132}
133
134/*
135 * This function tries to find a running server for the proxy <px> following
136 * the URI hash method. In order to optimize cache hits, the hash computation
137 * ends at the question mark. Depending on the number of active/backup servers,
138 * it will either look for active servers, or for backup servers.
139 * If any server is found, it will be returned. If no valid server is found,
140 * NULL is returned.
141 *
142 * This code was contributed by Guillaume Dallaire, who also selected this hash
143 * algorithm out of a tens because it gave him the best results.
144 *
145 */
146struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
147{
148 unsigned long hash = 0;
149 int c;
150 int slashes = 0;
151
152 if (px->lbprm.tot_weight == 0)
153 return NULL;
154
155 /* note: we won't hash if there's only one server left */
156 if (px->lbprm.tot_used == 1)
157 goto hash_done;
158
159 if (px->uri_len_limit)
160 uri_len = MIN(uri_len, px->uri_len_limit);
161
162 while (uri_len--) {
163 c = *uri++;
164 if (c == '/') {
165 slashes++;
166 if (slashes == px->uri_dirs_depth1) /* depth+1 */
167 break;
168 }
169 else if (c == '?')
170 break;
171
172 hash = c + (hash << 6) + (hash << 16) - hash;
173 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100174 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
175 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200176 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200177 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
178 return chash_get_server_hash(px, hash);
179 else
180 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200181}
182
Willy Tarreau01732802007-11-01 22:48:15 +0100183/*
184 * This function tries to find a running server for the proxy <px> following
185 * the URL parameter hash method. It looks for a specific parameter in the
186 * URL and hashes it to compute the server ID. This is useful to optimize
187 * performance by avoiding bounces between servers in contexts where sessions
188 * are shared but cookies are not usable. If the parameter is not found, NULL
189 * is returned. If any server is found, it will be returned. If no valid server
190 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100191 */
192struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
193{
194 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200195 const char *p;
196 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100197 int plen;
198
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200199 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100200 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100201 return NULL;
202
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200203 if ((p = memchr(uri, '?', uri_len)) == NULL)
204 return NULL;
205
Willy Tarreau01732802007-11-01 22:48:15 +0100206 p++;
207
208 uri_len -= (p - uri);
209 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200210 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100211
212 while (uri_len > plen) {
213 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200214 if (params[plen] == '=') {
215 if (memcmp(params, px->url_param_name, plen) == 0) {
216 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100217 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200218 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100219 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200220 p += plen + 1;
221 uri_len -= plen + 1;
222
Willy Tarreau01732802007-11-01 22:48:15 +0100223 while (uri_len && *p != '&') {
224 hash = *p + (hash << 6) + (hash << 16) - hash;
225 uri_len--;
226 p++;
227 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100228 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
229 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200230 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
231 return chash_get_server_hash(px, hash);
232 else
233 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100234 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200235 }
236 /* skip to next parameter */
237 p = memchr(params, '&', uri_len);
238 if (!p)
239 return NULL;
240 p++;
241 uri_len -= (p - params);
242 params = p;
243 }
244 return NULL;
245}
246
247/*
248 * this does the same as the previous server_ph, but check the body contents
249 */
250struct server *get_server_ph_post(struct session *s)
251{
252 unsigned long hash = 0;
253 struct http_txn *txn = &s->txn;
254 struct buffer *req = s->req;
255 struct http_msg *msg = &txn->req;
256 struct proxy *px = s->be;
257 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100258 unsigned long len = msg->body_len;
Willy Tarreauea1175a2012-03-05 15:52:30 +0100259 const char *params = req->p + msg->sov;
Willy Tarreau157dd632009-12-06 19:18:09 +0100260 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200261
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100262 if (len > req->i - (msg->sov - msg->som))
263 len = req->i - (msg->sov - msg->som);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200264
Willy Tarreau157dd632009-12-06 19:18:09 +0100265 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200266 return NULL;
267
Willy Tarreau157dd632009-12-06 19:18:09 +0100268 if (px->lbprm.tot_weight == 0)
269 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200270
271 while (len > plen) {
272 /* Look for the parameter name followed by an equal symbol */
273 if (params[plen] == '=') {
274 if (memcmp(params, px->url_param_name, plen) == 0) {
275 /* OK, we have the parameter here at <params>, and
276 * the value after the equal sign, at <p>
277 * skip the equal symbol
278 */
279 p += plen + 1;
280 len -= plen + 1;
281
282 while (len && *p != '&') {
283 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100284 /* if in a POST, body must be URI encoded or it's not a URI.
285 * Do not interprete any possible binary data as a parameter.
286 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200287 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
288 break;
289 return NULL; /* oh, no; this is not uri-encoded.
290 * This body does not contain parameters.
291 */
292 }
293 hash = *p + (hash << 6) + (hash << 16) - hash;
294 len--;
295 p++;
296 /* should we break if vlen exceeds limit? */
297 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100298 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
299 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200300 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
301 return chash_get_server_hash(px, hash);
302 else
303 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200304 }
305 }
Willy Tarreau01732802007-11-01 22:48:15 +0100306 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200307 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100308 if (!p)
309 return NULL;
310 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200311 len -= (p - params);
312 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100313 }
314 return NULL;
315}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200317
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318/*
Benoitaffb4812009-03-25 13:02:10 +0100319 * This function tries to find a running server for the proxy <px> following
320 * the Header parameter hash method. It looks for a specific parameter in the
321 * URL and hashes it to compute the server ID. This is useful to optimize
322 * performance by avoiding bounces between servers in contexts where sessions
323 * are shared but cookies are not usable. If the parameter is not found, NULL
324 * is returned. If any server is found, it will be returned. If no valid server
325 * is found, NULL is returned.
326 */
327struct server *get_server_hh(struct session *s)
328{
329 unsigned long hash = 0;
330 struct http_txn *txn = &s->txn;
331 struct http_msg *msg = &txn->req;
332 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 Tarreau3a215be2012-03-09 21:39:51 +0100345 http_find_header2(px->hh_name, plen, s->req->p + msg->sol, &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];
Emeric Brun736aa232009-06-30 17:56:00 +0200408
409 /* tot_weight appears to mean srv_count */
410 if (px->lbprm.tot_weight == 0)
411 return NULL;
412
Willy Tarreau37406352012-04-23 16:16:37 +0200413 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200414
Willy Tarreau34db1082012-04-19 17:16:54 +0200415 args[0].type = ARGT_STR;
416 args[0].data.str.str = px->hh_name;
417 args[0].data.str.len = px->hh_len;
418 args[1].type = ARGT_STOP;
419
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200420 ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +0200421 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100422
Willy Tarreau37406352012-04-23 16:16:37 +0200423 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200424 return NULL;
425
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200426 /* note: we won't hash if there's only one server left */
427 if (px->lbprm.tot_used == 1)
428 goto hash_done;
429
Emeric Brun736aa232009-06-30 17:56:00 +0200430 /* Found a the hh_name in the headers.
431 * we will compute the hash based on this value ctx.val.
432 */
Willy Tarreauf853c462012-04-23 18:53:56 +0200433 p = smp.data.str.str;
Emeric Brun736aa232009-06-30 17:56:00 +0200434 while (len) {
435 hash = *p + (hash << 6) + (hash << 16) - hash;
436 len--;
437 p++;
438 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100439 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
440 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200441 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200442 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
443 return chash_get_server_hash(px, hash);
444 else
445 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200446}
Benoitaffb4812009-03-25 13:02:10 +0100447
448/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200449 * This function applies the load-balancing algorithm to the session, as
450 * defined by the backend it is assigned to. The session is then marked as
451 * 'assigned'.
452 *
453 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
454 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100455 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200456 * The function tries to keep the original connection slot if it reconnects to
457 * the same server, otherwise it releases it and tries to offer it.
458 *
459 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460 *
461 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100462 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200463 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
464 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465 * SRV_STATUS_INTERNAL for other unrecoverable errors.
466 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200467 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100468 * it does not need to be called anymore. This means that target_srv(&s->target)
469 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200470 *
471 */
472
473int assign_server(struct session *s)
474{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100475
Willy Tarreau7c669d72008-06-20 15:04:11 +0200476 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100477 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200478 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100479
Simon Horman8effd3d2011-08-13 08:03:52 +0900480 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200481
Willy Tarreau7c669d72008-06-20 15:04:11 +0200482 err = SRV_STATUS_INTERNAL;
483 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
484 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100485
Willy Tarreau827aee92011-03-10 16:55:02 +0100486 prev_srv = target_srv(&s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200487 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200488
Willy Tarreau7c669d72008-06-20 15:04:11 +0200489 /* We have to release any connection slot before applying any LB algo,
490 * otherwise we may erroneously end up with no available slot.
491 */
492 if (conn_slot)
493 sess_change_server(s, NULL);
494
Willy Tarreau827aee92011-03-10 16:55:02 +0100495 /* We will now try to find the good server and store it into <target_srv(&s->target)>.
496 * Note that <target_srv(&s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200497 * as well as if no server is available (check error code).
498 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100499
Willy Tarreau827aee92011-03-10 16:55:02 +0100500 srv = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100501 clear_target(&s->target);
Willy Tarreau664beb82011-03-10 11:38:29 +0100502
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200503 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200504 /* we must check if we have at least one server available */
505 if (!s->be->lbprm.tot_weight) {
506 err = SRV_STATUS_NOSRV;
507 goto out;
508 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200509
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200510 /* First check whether we need to fetch some data or simply call
511 * the LB lookup function. Only the hashing functions will need
512 * some input data in fact, and will support multiple algorithms.
513 */
514 switch (s->be->lbprm.algo & BE_LB_LKUP) {
515 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100516 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200517 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200518
Willy Tarreauf09c6602012-02-13 17:12:08 +0100519 case BE_LB_LKUP_FSTREE:
520 srv = fas_get_next_server(s->be, prev_srv);
521 break;
522
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200523 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100524 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200525 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200526
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200527 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200528 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200529 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200530 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100531 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200532 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100533 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200534 break;
535 }
536 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200537 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200538 err = SRV_STATUS_INTERNAL;
539 goto out;
540 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200541
542 switch (s->be->lbprm.algo & BE_LB_PARM) {
543 case BE_LB_HASH_SRC:
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200544 if (s->req->prod->addr.from.ss_family == AF_INET) {
545 srv = get_server_sh(s->be,
546 (void *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr,
547 4);
548 }
549 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
550 srv = get_server_sh(s->be,
551 (void *)&((struct sockaddr_in6 *)&s->req->prod->addr.from)->sin6_addr,
552 16);
553 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200554 else {
555 /* unknown IP family */
556 err = SRV_STATUS_INTERNAL;
557 goto out;
558 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200559 break;
560
561 case BE_LB_HASH_URI:
562 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100563 if (s->txn.req.msg_state < HTTP_MSG_BODY)
564 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100565 srv = get_server_uh(s->be,
Willy Tarreau3a215be2012-03-09 21:39:51 +0100566 s->req->p + s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau827aee92011-03-10 16:55:02 +0100567 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200568 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200569
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200570 case BE_LB_HASH_PRM:
571 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100572 if (s->txn.req.msg_state < HTTP_MSG_BODY)
573 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100574
Willy Tarreau827aee92011-03-10 16:55:02 +0100575 srv = get_server_ph(s->be,
Willy Tarreau3a215be2012-03-09 21:39:51 +0100576 s->req->p + s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau827aee92011-03-10 16:55:02 +0100577 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100578
Willy Tarreau827aee92011-03-10 16:55:02 +0100579 if (!srv && s->txn.meth == HTTP_METH_POST)
580 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200581 break;
Benoitaffb4812009-03-25 13:02:10 +0100582
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200583 case BE_LB_HASH_HDR:
584 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100585 if (s->txn.req.msg_state < HTTP_MSG_BODY)
586 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100587 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200588 break;
589
590 case BE_LB_HASH_RDP:
591 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100592 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200593 break;
594
595 default:
596 /* unknown balancing algorithm */
597 err = SRV_STATUS_INTERNAL;
598 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100599 }
Emeric Brun736aa232009-06-30 17:56:00 +0200600
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200601 /* If the hashing parameter was not found, let's fall
602 * back to round robin on the map.
603 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100604 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200605 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100606 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200607 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100608 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200609 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200610
611 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200612 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200613
Willy Tarreau7c669d72008-06-20 15:04:11 +0200614 default:
615 /* unknown balancing algorithm */
616 err = SRV_STATUS_INTERNAL;
617 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200619
Willy Tarreau827aee92011-03-10 16:55:02 +0100620 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200621 err = SRV_STATUS_FULL;
622 goto out;
623 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100624 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100625 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100626 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100627 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100628 set_target_server(&s->target, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200629 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200630 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau9e000c62011-03-10 14:03:36 +0100631 set_target_proxy(&s->target, s->be);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100633 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreau6471afb2011-09-23 10:54:59 +0200634 is_addr(&s->req->cons->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100635 /* in proxy mode, we need a valid destination address */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100636 set_target_proxy(&s->target, s->be);
Willy Tarreau664beb82011-03-10 11:38:29 +0100637 }
638 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200639 err = SRV_STATUS_NOSRV;
640 goto out;
641 }
642
643 s->flags |= SN_ASSIGNED;
644 err = SRV_STATUS_OK;
645 out:
646
647 /* Either we take back our connection slot, or we offer it to someone
648 * else if we don't need it anymore.
649 */
650 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100651 if (conn_slot == srv) {
652 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200653 } else {
654 if (may_dequeue_tasks(conn_slot, s->be))
655 process_srv_queue(conn_slot);
656 }
657 }
658
659 out_err:
660 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200661}
662
663
664/*
665 * This function assigns a server address to a session, and sets SN_ADDR_SET.
666 * The address is taken from the currently assigned server, or from the
667 * dispatch or transparent address.
668 *
669 * It may return :
670 * SRV_STATUS_OK if everything is OK.
671 * SRV_STATUS_INTERNAL for other unrecoverable errors.
672 *
673 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
674 * not cleared, so it's to the caller to clear it if required.
675 *
676 */
677int assign_server_address(struct session *s)
678{
679#ifdef DEBUG_FULL
680 fprintf(stderr,"assign_server_address : s=%p\n",s);
681#endif
682
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200683 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684 /* A server is necessarily known for this session */
685 if (!(s->flags & SN_ASSIGNED))
686 return SRV_STATUS_INTERNAL;
687
Willy Tarreau6471afb2011-09-23 10:54:59 +0200688 s->req->cons->addr.to = target_srv(&s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200689
Willy Tarreau6471afb2011-09-23 10:54:59 +0200690 if (!is_addr(&s->req->cons->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200691 /* if the server has no address, we use the same address
692 * the client asked, which is handy for remapping ports
693 * locally on multiple addresses at once.
694 */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200695 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreau59b94792012-05-11 16:16:40 +0200696 si_get_to_addr(s->req->prod);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200697
Willy Tarreau6471afb2011-09-23 10:54:59 +0200698 if (s->req->prod->addr.to.ss_family == AF_INET) {
699 ((struct sockaddr_in *)&s->req->cons->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
700 } else if (s->req->prod->addr.to.ss_family == AF_INET6) {
701 ((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 +0200702 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200703 }
704
Willy Tarreaubaaee002006-06-26 02:48:02 +0200705 /* if this server remaps proxied ports, we'll use
706 * the port the client connected to with an offset. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100707 if (target_srv(&s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100708 int base_port;
709
Willy Tarreau9b061e32012-04-07 18:03:52 +0200710 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreau59b94792012-05-11 16:16:40 +0200711 si_get_to_addr(s->req->prod);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100712
713 /* First, retrieve the port from the incoming connection */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200714 base_port = get_host_port(&s->req->prod->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100715
716 /* Second, assign the outgoing connection's port */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200717 base_port += get_host_port(&s->req->cons->addr.to);
718 set_host_port(&s->req->cons->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719 }
720 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200721 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200722 /* connect to the defined dispatch addr */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200723 s->req->cons->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100725 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreau59b94792012-05-11 16:16:40 +0200727 si_get_to_addr(s->req->prod);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200728
Willy Tarreau6471afb2011-09-23 10:54:59 +0200729 if (s->req->prod->addr.to.ss_family == AF_INET || s->req->prod->addr.to.ss_family == AF_INET6) {
730 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 +0200731 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100732 /* when we support IPv6 on the backend, we may add other tests */
733 //qfprintf(stderr, "Cannot get original server address.\n");
734 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100736 else if (s->be->options & PR_O_HTTP_PROXY) {
737 /* If HTTP PROXY option is set, then server is already assigned
738 * during incoming client request parsing. */
739 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100740 else {
741 /* no server and no LB algorithm ! */
742 return SRV_STATUS_INTERNAL;
743 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200744
745 s->flags |= SN_ADDR_SET;
746 return SRV_STATUS_OK;
747}
748
749
750/* This function assigns a server to session <s> if required, and can add the
751 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200752 * If ->srv_conn is set, the session is first released from the server.
753 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
754 * be called before any connection and after any retry or redispatch occurs.
755 *
756 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757 *
758 * Returns :
759 *
760 * SRV_STATUS_OK if everything is OK.
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 * SRV_STATUS_NOSRV if no server is available. target_srv(&s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762 * SRV_STATUS_QUEUED if the connection has been queued.
763 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100764 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200765 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766 * SRV_STATUS_INTERNAL for other unrecoverable errors.
767 *
768 */
769int assign_server_and_queue(struct session *s)
770{
771 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100772 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773 int err;
774
775 if (s->pend_pos)
776 return SRV_STATUS_INTERNAL;
777
Willy Tarreau7c669d72008-06-20 15:04:11 +0200778 err = SRV_STATUS_OK;
779 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100780 struct server *prev_srv = target_srv(&s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100781
Willy Tarreau7c669d72008-06-20 15:04:11 +0200782 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100783 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200784 /* This session was previously assigned to a server. We have to
785 * update the session's and the server's stats :
786 * - if the server changed :
787 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
788 * - set SN_REDISP if it was successfully redispatched
789 * - increment srv->redispatches and be->redispatches
790 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100791 */
792
Willy Tarreau827aee92011-03-10 16:55:02 +0100793 if (prev_srv != target_srv(&s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200794 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
795 s->txn.flags &= ~TX_CK_MASK;
796 s->txn.flags |= TX_CK_DOWN;
797 }
798 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100799 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100800 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200801 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100802 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100803 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100804 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100805 }
806 }
807
Willy Tarreaubaaee002006-06-26 02:48:02 +0200808 switch (err) {
809 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200810 /* we have SN_ASSIGNED set */
Willy Tarreau827aee92011-03-10 16:55:02 +0100811 srv = target_srv(&s->target);
812 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200813 return SRV_STATUS_OK; /* dispatch or proxy mode */
814
815 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100816 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200817 return SRV_STATUS_OK;
818
819 /* OK, this session already has an assigned server, but no
820 * connection slot yet. Either it is a redispatch, or it was
821 * assigned from persistence information (direct mode).
822 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100823 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200824 /* server scheduled for redirection, and already assigned. We
825 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100826 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100827 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100828 return SRV_STATUS_OK;
829 }
830
Willy Tarreau7c669d72008-06-20 15:04:11 +0200831 /* We might have to queue this session if the assigned server is full.
832 * We know we have to queue it into the server's queue, so if a maxqueue
833 * is set on the server, we must also check that the server's queue is
834 * not full, in which case we have to return FULL.
835 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100836 if (srv->maxconn &&
837 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200838
Willy Tarreau827aee92011-03-10 16:55:02 +0100839 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200840 return SRV_STATUS_FULL;
841
Willy Tarreaubaaee002006-06-26 02:48:02 +0200842 p = pendconn_add(s);
843 if (p)
844 return SRV_STATUS_QUEUED;
845 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200846 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200848
849 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100850 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851 return SRV_STATUS_OK;
852
853 case SRV_STATUS_FULL:
854 /* queue this session into the proxy's queue */
855 p = pendconn_add(s);
856 if (p)
857 return SRV_STATUS_QUEUED;
858 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200859 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200860
861 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200862 return err;
863
Willy Tarreaubaaee002006-06-26 02:48:02 +0200864 case SRV_STATUS_INTERNAL:
865 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200866
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867 default:
868 return SRV_STATUS_INTERNAL;
869 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100870}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200871
Willy Tarreaub1d67742010-03-29 19:36:59 +0200872/* If an explicit source binding is specified on the server and/or backend, and
873 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200874 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200875 */
876static void assign_tproxy_address(struct session *s)
877{
878#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau827aee92011-03-10 16:55:02 +0100879 struct server *srv = target_srv(&s->target);
880
881 if (srv && srv->state & SRV_BIND_SRC) {
882 switch (srv->state & SRV_TPROXY_MASK) {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200883 case SRV_TPROXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200884 s->req->cons->addr.from = srv->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200885 break;
886 case SRV_TPROXY_CLI:
887 case SRV_TPROXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200888 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200889 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200890 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200891 case SRV_TPROXY_DYN:
Willy Tarreau827aee92011-03-10 16:55:02 +0100892 if (srv->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100893 char *vptr;
894 int vlen;
895
Willy Tarreaubce70882009-09-07 11:51:47 +0200896 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100897 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200898 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100899 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
900
901 if (http_get_hdr(&s->txn.req, srv->bind_hdr_name, srv->bind_hdr_len,
902 &s->txn.hdr_idx, srv->bind_hdr_occ, NULL, &vptr, &vlen)) {
903 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
904 htonl(inetaddr_host_lim(vptr, vptr + vlen));
905 }
Willy Tarreaubce70882009-09-07 11:51:47 +0200906 }
907 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200908 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200909 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200910 }
911 }
912 else if (s->be->options & PR_O_BIND_SRC) {
913 switch (s->be->options & PR_O_TPXY_MASK) {
914 case PR_O_TPXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200915 s->req->cons->addr.from = s->be->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200916 break;
917 case PR_O_TPXY_CLI:
918 case PR_O_TPXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200919 /* FIXME: what can we do if the client connects in IPv6 or socket unix? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200920 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200921 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200922 case PR_O_TPXY_DYN:
923 if (s->be->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100924 char *vptr;
925 int vlen;
926
Willy Tarreaubce70882009-09-07 11:51:47 +0200927 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100928 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200929 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100930 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
931
932 if (http_get_hdr(&s->txn.req, s->be->bind_hdr_name, s->be->bind_hdr_len,
933 &s->txn.hdr_idx, s->be->bind_hdr_occ, NULL, &vptr, &vlen)) {
934 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
935 htonl(inetaddr_host_lim(vptr, vptr + vlen));
936 }
Willy Tarreaubce70882009-09-07 11:51:47 +0200937 }
938 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200939 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200940 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200941 }
942 }
943#endif
944}
945
946
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947/*
948 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200949 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100950 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951 * It can return one of :
952 * - SN_ERR_NONE if everything's OK
953 * - SN_ERR_SRVTO if there are no more servers
954 * - SN_ERR_SRVCL if the connection was refused by the server
955 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
956 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
957 * - SN_ERR_INTERNAL for any other purely internal errors
958 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
959 */
960int connect_server(struct session *s)
961{
Willy Tarreau827aee92011-03-10 16:55:02 +0100962 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200963 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200964
965 if (!(s->flags & SN_ADDR_SET)) {
966 err = assign_server_address(s);
967 if (err != SRV_STATUS_OK)
968 return SN_ERR_INTERNAL;
969 }
970
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200971 /* the target was only on the session, assign it to the SI now */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100972 copy_target(&s->req->cons->target, &s->target);
Willy Tarreaud88edf22009-06-14 15:48:17 +0200973
Willy Tarreau26d8c592012-05-07 18:12:14 +0200974 /* set the correct protocol on the output stream interface */
Willy Tarreaud02394b2012-05-11 18:32:18 +0200975 if (s->target.type == TARG_TYPE_SERVER) {
Willy Tarreau26d8c592012-05-07 18:12:14 +0200976 s->req->cons->proto = target_srv(&s->target)->proto;
Willy Tarreaud02394b2012-05-11 18:32:18 +0200977 stream_interface_prepare(s->req->cons, target_srv(&s->target)->sock);
978 }
Willy Tarreau26d8c592012-05-07 18:12:14 +0200979 else if (s->target.type == TARG_TYPE_PROXY) {
Willy Tarreaud02394b2012-05-11 18:32:18 +0200980 /* proxies exclusively run on sock_raw right now */
Willy Tarreau26d8c592012-05-07 18:12:14 +0200981 s->req->cons->proto = protocol_by_family(s->req->cons->addr.to.ss_family);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200982 stream_interface_prepare(s->req->cons, &sock_raw);
Willy Tarreau26d8c592012-05-07 18:12:14 +0200983 if (!s->req->cons->proto)
984 return SN_ERR_INTERNAL;
985 }
Willy Tarreaud02394b2012-05-11 18:32:18 +0200986 else
987 return SN_ERR_INTERNAL; /* how did we get there ? */
988
989 /* process the case where the server requires the PROXY protocol to be sent */
990 s->req->cons->send_proxy_ofs = 0;
991 if (s->target.type == TARG_TYPE_SERVER && (s->target.ptr.s->state & SRV_SEND_PROXY)) {
992 s->req->cons->send_proxy_ofs = 1; /* must compute size */
993 si_get_to_addr(s->req->prod);
994 }
Willy Tarreau26d8c592012-05-07 18:12:14 +0200995
Willy Tarreaub1d67742010-03-29 19:36:59 +0200996 assign_tproxy_address(s);
997
William Lallemandb7ff6a32012-03-02 14:35:21 +0100998 /* flag for logging source ip/port */
999 if (s->fe->options2 & PR_O2_SRC_ADDR)
1000 s->req->cons->flags |= SI_FL_SRC_ADDR;
1001
Willy Tarreau26d8c592012-05-07 18:12:14 +02001002 err = s->req->cons->proto->connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001003
Willy Tarreau9650f372009-08-16 14:02:45 +02001004 if (err != SN_ERR_NONE)
1005 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001006
Willy Tarreau827aee92011-03-10 16:55:02 +01001007 srv = target_srv(&s->target);
1008 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001009 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001010 srv->cur_sess++;
1011 if (srv->cur_sess > srv->counters.cur_sess_max)
1012 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001013 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001014 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001015 }
1016
Willy Tarreaubaaee002006-06-26 02:48:02 +02001017 return SN_ERR_NONE; /* connection is OK */
1018}
1019
1020
Willy Tarreaubaaee002006-06-26 02:48:02 +02001021/* This function performs the "redispatch" part of a connection attempt. It
1022 * will assign a server if required, queue the connection if required, and
1023 * handle errors that might arise at this level. It can change the server
1024 * state. It will return 1 if it encounters an error, switches the server
1025 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1026 * that the connection is ready to use.
1027 */
1028
1029int srv_redispatch_connect(struct session *t)
1030{
Willy Tarreau827aee92011-03-10 16:55:02 +01001031 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001032 int conn_err;
1033
1034 /* We know that we don't have any connection pending, so we will
1035 * try to get a new one, and wait in this state if it's queued
1036 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001037 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001038 conn_err = assign_server_and_queue(t);
Willy Tarreau827aee92011-03-10 16:55:02 +01001039 srv = target_srv(&t->target);
1040
Willy Tarreaubaaee002006-06-26 02:48:02 +02001041 switch (conn_err) {
1042 case SRV_STATUS_OK:
1043 break;
1044
Willy Tarreau7c669d72008-06-20 15:04:11 +02001045 case SRV_STATUS_FULL:
1046 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1047 * and we can redispatch to another server, or it is not and we return
1048 * 503. This only makes sense in DIRECT mode however, because normal LB
1049 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001050 * would bring us on the same server again. Note that t->target is set
1051 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001052 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001053 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1054 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001055 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001056 goto redispatch;
1057 }
1058
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001059 if (!t->req->cons->err_type) {
1060 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001061 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001062 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001063
Willy Tarreau827aee92011-03-10 16:55:02 +01001064 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001065 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001066 return 1;
1067
Willy Tarreaubaaee002006-06-26 02:48:02 +02001068 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001069 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001070 if (!t->req->cons->err_type) {
1071 t->req->cons->err_type = SI_ET_CONN_ERR;
1072 t->req->cons->err_loc = NULL;
1073 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001074
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001075 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001076 return 1;
1077
1078 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001079 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001080 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001081 /* do nothing else and do not wake any other session up */
1082 return 1;
1083
Willy Tarreaubaaee002006-06-26 02:48:02 +02001084 case SRV_STATUS_INTERNAL:
1085 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001086 if (!t->req->cons->err_type) {
1087 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001088 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001089 }
1090
Willy Tarreau827aee92011-03-10 16:55:02 +01001091 if (srv)
1092 srv_inc_sess_ctr(srv);
1093 if (srv)
1094 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001095 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001096
1097 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001098 if (may_dequeue_tasks(srv, t->be))
1099 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100 return 1;
1101 }
1102 /* if we get here, it's because we got SRV_STATUS_OK, which also
1103 * means that the connection has not been queued.
1104 */
1105 return 0;
1106}
1107
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001108/* Apply RDP cookie persistence to the current session. For this, the function
1109 * tries to extract an RDP cookie from the request buffer, and look for the
1110 * matching server in the list. If the server is found, it is assigned to the
1111 * session. This always returns 1, and the analyser removes itself from the
1112 * list. Nothing is performed if a server was already assigned.
1113 */
1114int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1115{
1116 struct proxy *px = s->be;
1117 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001118 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001119 struct server *srv = px->srv;
1120 struct sockaddr_in addr;
1121 char *p;
Willy Tarreau34db1082012-04-19 17:16:54 +02001122 struct arg args[2];
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001123
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001124 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 +02001125 now_ms, __FUNCTION__,
1126 s,
1127 req,
1128 req->rex, req->wex,
1129 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001130 req->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001131 req->analysers);
1132
1133 if (s->flags & SN_ASSIGNED)
1134 goto no_cookie;
1135
Willy Tarreau37406352012-04-23 16:16:37 +02001136 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001137
Willy Tarreau34db1082012-04-19 17:16:54 +02001138 args[0].type = ARGT_STR;
1139 args[0].data.str.str = s->be->rdp_cookie_name;
1140 args[0].data.str.len = s->be->rdp_cookie_len;
1141 args[1].type = ARGT_STOP;
1142
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001143 ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +02001144 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001145 goto no_cookie;
1146
1147 memset(&addr, 0, sizeof(addr));
1148 addr.sin_family = AF_INET;
1149
Willy Tarreau664092c2011-12-16 19:11:42 +01001150 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001151 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001152 if (*p != '.')
1153 goto no_cookie;
1154 p++;
1155 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1156 if (*p != '.')
1157 goto no_cookie;
1158
Willy Tarreau9e000c62011-03-10 14:03:36 +01001159 clear_target(&s->target);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001160 while (srv) {
1161 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1162 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1163 /* we found the server and it is usable */
1164 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001165 set_target_server(&s->target, srv);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001166 break;
1167 }
1168 }
1169 srv = srv->next;
1170 }
1171
1172no_cookie:
1173 req->analysers &= ~an_bit;
1174 req->analyse_exp = TICK_ETERNITY;
1175 return 1;
1176}
1177
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001178int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001179 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001180 return px->down_time;
1181
1182 return now.tv_sec - px->last_change + px->down_time;
1183}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001184
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001185/*
1186 * This function returns a string containing the balancing
1187 * mode of the proxy in a format suitable for stats.
1188 */
1189
1190const char *backend_lb_algo_str(int algo) {
1191
1192 if (algo == BE_LB_ALGO_RR)
1193 return "roundrobin";
1194 else if (algo == BE_LB_ALGO_SRR)
1195 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001196 else if (algo == BE_LB_ALGO_FAS)
1197 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001198 else if (algo == BE_LB_ALGO_LC)
1199 return "leastconn";
1200 else if (algo == BE_LB_ALGO_SH)
1201 return "source";
1202 else if (algo == BE_LB_ALGO_UH)
1203 return "uri";
1204 else if (algo == BE_LB_ALGO_PH)
1205 return "url_param";
1206 else if (algo == BE_LB_ALGO_HH)
1207 return "hdr";
1208 else if (algo == BE_LB_ALGO_RCH)
1209 return "rdp-cookie";
1210 else
1211 return NULL;
1212}
1213
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001214/* This function parses a "balance" statement in a backend section describing
1215 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001216 * returns -1, it will write an error message into the <err> buffer which will
1217 * automatically be allocated and must be passed as NULL. The trailing '\n'
1218 * will not be written. The function must be called with <args> pointing to the
1219 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001220 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001221int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001222{
1223 if (!*(args[0])) {
1224 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001225 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1226 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001227 return 0;
1228 }
1229
1230 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001231 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1232 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001233 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001234 else if (!strcmp(args[0], "static-rr")) {
1235 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1236 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1237 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001238 else if (!strcmp(args[0], "first")) {
1239 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1240 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1241 }
Willy Tarreau51406232008-03-10 22:04:20 +01001242 else if (!strcmp(args[0], "leastconn")) {
1243 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1244 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1245 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001246 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001247 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1248 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001249 }
1250 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001251 int arg = 1;
1252
Willy Tarreau31682232007-11-29 15:38:04 +01001253 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1254 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001255
1256 while (*args[arg]) {
1257 if (!strcmp(args[arg], "len")) {
1258 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001259 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001260 return -1;
1261 }
1262 curproxy->uri_len_limit = atoi(args[arg+1]);
1263 arg += 2;
1264 }
1265 else if (!strcmp(args[arg], "depth")) {
1266 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001267 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001268 return -1;
1269 }
1270 /* hint: we store the position of the ending '/' (depth+1) so
1271 * that we avoid a comparison while computing the hash.
1272 */
1273 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1274 arg += 2;
1275 }
1276 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001277 memprintf(err, "%s only accepts parameters 'len' and 'depth' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001278 return -1;
1279 }
1280 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001281 }
Willy Tarreau01732802007-11-01 22:48:15 +01001282 else if (!strcmp(args[0], "url_param")) {
1283 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001284 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001285 return -1;
1286 }
Willy Tarreau31682232007-11-29 15:38:04 +01001287 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1288 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001289
1290 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001291 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001292 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001293 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001294 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001295 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001296 return -1;
1297 }
1298 if (*args[3]) {
1299 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1300 curproxy->url_param_post_limit = str2ui(args[3]);
1301 }
1302 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1303 if (!curproxy->url_param_post_limit)
1304 curproxy->url_param_post_limit = 48;
1305 else if ( curproxy->url_param_post_limit < 3 )
1306 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1307 }
Benoitaffb4812009-03-25 13:02:10 +01001308 }
1309 else if (!strncmp(args[0], "hdr(", 4)) {
1310 const char *beg, *end;
1311
1312 beg = args[0] + 4;
1313 end = strchr(beg, ')');
1314
1315 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001316 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001317 return -1;
1318 }
1319
1320 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1321 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1322
1323 free(curproxy->hh_name);
1324 curproxy->hh_len = end - beg;
1325 curproxy->hh_name = my_strndup(beg, end - beg);
1326 curproxy->hh_match_domain = 0;
1327
1328 if (*args[1]) {
1329 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001330 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001331 return -1;
1332 }
1333 curproxy->hh_match_domain = 1;
1334 }
1335
Emeric Brun736aa232009-06-30 17:56:00 +02001336 }
1337 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1338 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1339 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1340
1341 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1342 const char *beg, *end;
1343
1344 beg = args[0] + 11;
1345 end = strchr(beg, ')');
1346
1347 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001348 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001349 return -1;
1350 }
1351
1352 free(curproxy->hh_name);
1353 curproxy->hh_name = my_strndup(beg, end - beg);
1354 curproxy->hh_len = end - beg;
1355 }
1356 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1357 free(curproxy->hh_name);
1358 curproxy->hh_name = strdup("mstshash");
1359 curproxy->hh_len = strlen(curproxy->hh_name);
1360 }
1361 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001362 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001363 return -1;
1364 }
Willy Tarreau01732802007-11-01 22:48:15 +01001365 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001366 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001367 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 +01001368 return -1;
1369 }
1370 return 0;
1371}
1372
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001373
1374/************************************************************************/
1375/* All supported keywords must be declared here. */
1376/************************************************************************/
1377
Willy Tarreau34db1082012-04-19 17:16:54 +02001378/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001379 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001380 * undefined behaviour.
1381 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001382static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001383acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001384 const struct arg *args, struct sample *smp)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001385{
Willy Tarreau37406352012-04-23 16:16:37 +02001386 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001387 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001388 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001389
1390 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001391 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001392 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001393 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001394 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001395 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001396
1397 return 1;
1398}
1399
Willy Tarreau37406352012-04-23 16:16:37 +02001400/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001401 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001402 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1403 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001404 */
1405static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001406acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001407 const struct arg *args, struct sample *smp)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001408{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001409 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001410
Willy Tarreau37406352012-04-23 16:16:37 +02001411 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001412 smp->type = SMP_T_BOOL;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001413 if (!(srv->state & SRV_MAINTAIN) &&
1414 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001415 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001416 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001417 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001418 return 1;
1419}
1420
Willy Tarreau34db1082012-04-19 17:16:54 +02001421/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001422 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001423 * undefined behaviour.
1424 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001425static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001426acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001427 const struct arg *args, struct sample *smp)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001428{
1429 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001430
Willy Tarreau37406352012-04-23 16:16:37 +02001431 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001432 smp->type = SMP_T_UINT;
1433 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001434
Willy Tarreau24e32d82012-04-23 23:55:44 +02001435 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001436 if ((iterator->state & SRV_RUNNING) == 0)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001437 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001438
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001439 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001440 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001441 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001442 return 1;
1443 }
1444
Willy Tarreauf853c462012-04-23 18:53:56 +02001445 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001446 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001447 }
1448
1449 return 1;
1450}
1451
Willy Tarreaua5e37562011-12-16 17:06:15 +01001452/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001453static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001454acl_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001455 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +02001456{
Willy Tarreauf853c462012-04-23 18:53:56 +02001457 smp->flags = SMP_F_VOL_TXN;
1458 smp->type = SMP_T_UINT;
1459 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001460 return 1;
1461}
1462
Willy Tarreaua5e37562011-12-16 17:06:15 +01001463/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001464static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001465acl_fetch_srv_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 Tarreau827aee92011-03-10 16:55:02 +01001468 if (!target_srv(&l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001469 return 0;
1470
Willy Tarreauf853c462012-04-23 18:53:56 +02001471 smp->type = SMP_T_UINT;
1472 smp->data.uint = target_srv(&l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001473
1474 return 1;
1475}
1476
Willy Tarreau34db1082012-04-19 17:16:54 +02001477/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001478 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001479 * undefined behaviour.
1480 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001481static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001482acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001483 const struct arg *args, struct sample *smp)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001484{
Willy Tarreau37406352012-04-23 16:16:37 +02001485 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001486 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001487 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001488 return 1;
1489}
1490
Willy Tarreau34db1082012-04-19 17:16:54 +02001491/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001492 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001493 * undefined behaviour.
1494 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001495static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001496acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001497 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001498{
Willy Tarreau37406352012-04-23 16:16:37 +02001499 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001500 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001501 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001502 return 1;
1503}
1504
Willy Tarreau34db1082012-04-19 17:16:54 +02001505/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001506 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001507 * undefined behaviour.
1508 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001509static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001510acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001511 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001512{
Willy Tarreau37406352012-04-23 16:16:37 +02001513 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001514 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001515 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001516 return 1;
1517}
1518
Willy Tarreaua5e37562011-12-16 17:06:15 +01001519/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001520 * by the number of running servers and rounded up. If there is no running
1521 * server, we return twice the total, just as if we had half a running server.
1522 * This is more or less correct anyway, since we expect the last server to come
1523 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001524 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001525 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001526 */
1527static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001528acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001529 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001530{
1531 int nbsrv;
1532
Willy Tarreau37406352012-04-23 16:16:37 +02001533 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001534 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001535 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001536
1537 if (px->srv_act)
1538 nbsrv = px->srv_act;
1539 else if (px->lbprm.fbck)
1540 nbsrv = 1;
1541 else
1542 nbsrv = px->srv_bck;
1543
1544 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001545 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001546 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001547 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001548
1549 return 1;
1550}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001551
Willy Tarreau34db1082012-04-19 17:16:54 +02001552/* set temp integer to the number of concurrent connections on the server in the backend.
1553 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1554 * undefined behaviour.
1555 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001556static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001557acl_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001558 const struct arg *args, struct sample *smp)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001559{
Willy Tarreauf853c462012-04-23 18:53:56 +02001560 smp->flags = SMP_F_VOL_TEST;
1561 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001562 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001563 return 1;
1564}
1565
Willy Tarreau61612d42012-04-19 18:42:05 +02001566/* Note: must not be declared <const> as its list will be overwritten.
1567 * Please take care of keeping this list alphabetically sorted.
1568 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001569static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001570 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1571 { "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 +02001572 { "be_id", acl_parse_int, acl_fetch_be_id, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001573 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1574 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1575 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1576 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
Willy Tarreau61612d42012-04-19 18:42:05 +02001577 { "srv_conn", acl_parse_int, acl_fetch_srv_conn, acl_match_int, ACL_USE_NOTHING, ARG1(1,SRV) },
1578 { "srv_id", acl_parse_int, acl_fetch_srv_id, acl_match_int, ACL_USE_RTR_INTERNAL, 0 },
1579 { "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 +01001580 { NULL, NULL, NULL, NULL },
1581}};
1582
1583
1584__attribute__((constructor))
1585static void __backend_init(void)
1586{
1587 acl_register_keywords(&acl_kws);
1588}
1589
Willy Tarreaubaaee002006-06-26 02:48:02 +02001590/*
1591 * Local variables:
1592 * c-indent-level: 8
1593 * c-basic-offset: 8
1594 * End:
1595 */